Skip to content

Commit cef6707

Browse files
committed
Initial structure for io module
1 parent ce91a25 commit cef6707

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

vm/src/stdlib/io.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* I/O core tools.
3+
*/
4+
5+
// use super::super::obj::{objstr, objtype};
6+
use super::super::pyobject::{
7+
AttributeProtocol, DictProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult,
8+
};
9+
use super::super::VirtualMachine;
10+
11+
fn string_io_init(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
12+
// arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
13+
// TODO
14+
Ok(vm.get_none())
15+
}
16+
17+
fn string_io_getvalue(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
18+
arg_check!(vm, args);
19+
// TODO
20+
Ok(vm.get_none())
21+
}
22+
23+
fn bytes_io_init(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
24+
// TODO
25+
Ok(vm.get_none())
26+
}
27+
28+
fn bytes_io_getvalue(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
29+
arg_check!(vm, args);
30+
// TODO
31+
Ok(vm.get_none())
32+
}
33+
34+
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
35+
let py_mod = ctx.new_module(&"io".to_string(), ctx.new_scope(None));
36+
37+
let io_base = ctx.new_class("IOBase", ctx.object());
38+
py_mod.set_item("IOBase", io_base.clone());
39+
40+
let string_io = ctx.new_class("StringIO", io_base.clone());
41+
string_io.set_attr("__init__", ctx.new_rustfunc(string_io_init));
42+
string_io.set_attr("getvalue", ctx.new_rustfunc(string_io_getvalue));
43+
py_mod.set_item("StringIO", string_io);
44+
45+
let bytes_io = ctx.new_class("BytesIO", io_base.clone());
46+
bytes_io.set_attr("__init__", ctx.new_rustfunc(bytes_io_init));
47+
bytes_io.set_attr("getvalue", ctx.new_rustfunc(bytes_io_getvalue));
48+
py_mod.set_item("BytesIO", bytes_io);
49+
50+
py_mod
51+
}

vm/src/stdlib/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod ast;
2+
mod io;
23
mod json;
34
mod keyword;
45
mod math;
@@ -10,6 +11,7 @@ pub type StdlibInitFunc = fn(&PyContext) -> PyObjectRef;
1011

1112
pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
1213
let mut modules = HashMap::new();
14+
modules.insert("io".to_string(), io::mk_module as StdlibInitFunc);
1315
modules.insert("json".to_string(), json::mk_module as StdlibInitFunc);
1416
modules.insert("ast".to_string(), ast::mk_module as StdlibInitFunc);
1517
modules.insert("keyword".to_string(), keyword::mk_module as StdlibInitFunc);

0 commit comments

Comments
 (0)