Skip to content

Commit 30165f6

Browse files
committed
open builtin
1 parent edc720e commit 30165f6

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

vm/src/builtins.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ use super::obj::objint;
1313
use super::obj::objiter;
1414
use super::obj::objstr;
1515
use super::obj::objtype;
16+
17+
use super::stdlib::io::io_open;
1618
use super::pyobject::{
1719
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
18-
PyResult, Scope, TypeProtocol,
20+
PyResult, Scope, TypeProtocol
1921
};
22+
2023
use super::vm::VirtualMachine;
2124
use num_bigint::ToBigInt;
2225
use num_traits::{Signed, ToPrimitive, Zero};
@@ -585,8 +588,6 @@ fn builtin_oct(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
585588
Ok(vm.new_str(s))
586589
}
587590

588-
// builtin_open
589-
590591
fn builtin_ord(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
591592
arg_check!(vm, args, required = [(string, Some(vm.ctx.str_type()))]);
592593
let string = objstr::get_value(string);
@@ -776,6 +777,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
776777
ctx.set_attr(&py_mod, "min", ctx.new_rustfunc(builtin_min));
777778
ctx.set_attr(&py_mod, "object", ctx.object());
778779
ctx.set_attr(&py_mod, "oct", ctx.new_rustfunc(builtin_oct));
780+
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(io_open));
779781
ctx.set_attr(&py_mod, "ord", ctx.new_rustfunc(builtin_ord));
780782
ctx.set_attr(&py_mod, "next", ctx.new_rustfunc(builtin_next));
781783
ctx.set_attr(&py_mod, "pow", ctx.new_rustfunc(builtin_pow));

vm/src/stdlib/io.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ fn buffered_writer_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
243243

244244
}
245245

246-
fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
246+
pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
247247
arg_check!(
248248
vm,
249249
args,
@@ -259,7 +259,7 @@ fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
259259
let buffered_reader_class = vm.ctx.get_attr(&module, "BufferedReader").unwrap();
260260

261261
//instantiate raw fileio
262-
let file_io = vm.invoke(file_io_class, PyFuncArgs::new(vec![file.clone()], vec![])).unwrap();
262+
let file_io = vm.invoke(file_io_class, args.clone()).unwrap();
263263

264264
//This is subsequently consumed by a Buffered_class of type depending
265265
//operation in the mode. i.e:
@@ -269,7 +269,6 @@ fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
269269
// creating || writing || appending => BufferedWriter
270270
let buffered = if rust_mode.contains("w") {
271271
// vm.new_not_implemented_error("Writes are not yet implemented".to_string());
272-
println!("writer class");
273272
vm.invoke(buffered_writer_class, PyFuncArgs::new(vec![file_io.clone()], vec![]))
274273
// reading => BufferedReader
275274
} else {

vm/src/stdlib/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod ast;
2-
mod io;
2+
pub mod io;
33
mod json;
44
mod keyword;
55
mod math;

0 commit comments

Comments
 (0)