Skip to content

Commit e9bfea8

Browse files
committed
Format module building in a more readable way
1 parent d627230 commit e9bfea8

File tree

8 files changed

+46
-10
lines changed

8 files changed

+46
-10
lines changed

vm/src/stdlib/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,20 +610,23 @@ fn ast_parse(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
610610
}
611611

612612
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
613+
// TODO: maybe we can use some clever macro to generate this?
613614
let ast_mod = ctx.new_module("ast", ctx.new_scope(None));
615+
614616
ctx.set_attr(&ast_mod, "parse", ctx.new_rustfunc(ast_parse));
617+
615618
ctx.set_attr(
616619
&ast_mod,
617620
"Module",
618621
ctx.new_class("_ast.Module", ctx.object()),
619622
);
620623

621-
// TODO: maybe we can use some clever macro to generate this?
622624
ctx.set_attr(
623625
&ast_mod,
624626
"FunctionDef",
625627
ctx.new_class("_ast.FunctionDef", ctx.object()),
626628
);
627629
ctx.set_attr(&ast_mod, "Call", ctx.new_class("_ast.Call", ctx.object()));
630+
628631
ast_mod
629632
}

vm/src/stdlib/io.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,20 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
3535
let io_base = ctx.new_class("IOBase", ctx.object());
3636
ctx.set_attr(&py_mod, "IOBase", io_base.clone());
3737

38-
let string_io = ctx.new_class("StringIO", io_base.clone());
39-
ctx.set_attr(&string_io, "__init__", ctx.new_rustfunc(string_io_init));
40-
ctx.set_attr(&string_io, "getvalue", ctx.new_rustfunc(string_io_getvalue));
38+
let string_io = {
39+
let string_io = ctx.new_class("StringIO", io_base.clone());
40+
ctx.set_attr(&string_io, "__init__", ctx.new_rustfunc(string_io_init));
41+
ctx.set_attr(&string_io, "getvalue", ctx.new_rustfunc(string_io_getvalue));
42+
string_io
43+
};
4144
ctx.set_attr(&py_mod, "StringIO", string_io);
4245

43-
let bytes_io = ctx.new_class("BytesIO", io_base.clone());
44-
ctx.set_attr(&bytes_io, "__init__", ctx.new_rustfunc(bytes_io_init));
45-
ctx.set_attr(&bytes_io, "getvalue", ctx.new_rustfunc(bytes_io_getvalue));
46+
let bytes_io = {
47+
let bytes_io = ctx.new_class("BytesIO", io_base.clone());
48+
ctx.set_attr(&bytes_io, "__init__", ctx.new_rustfunc(bytes_io_init));
49+
ctx.set_attr(&bytes_io, "getvalue", ctx.new_rustfunc(bytes_io_getvalue));
50+
bytes_io
51+
};
4652
ctx.set_attr(&py_mod, "BytesIO", bytes_io);
4753

4854
py_mod

vm/src/stdlib/json.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,10 @@ fn loads(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
233233

234234
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
235235
let json_mod = ctx.new_module("json", ctx.new_scope(None));
236+
236237
ctx.set_attr(&json_mod, "dumps", ctx.new_rustfunc(dumps));
237238
ctx.set_attr(&json_mod, "loads", ctx.new_rustfunc(loads));
239+
238240
// TODO: Make this a proper type with a constructor
239241
let json_decode_error = create_type(
240242
"JSONDecodeError",
@@ -243,5 +245,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
243245
&ctx.dict_type,
244246
);
245247
ctx.set_attr(&json_mod, "JSONDecodeError", json_decode_error);
248+
246249
json_mod
247250
}

vm/src/stdlib/keyword.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ fn keyword_iskeyword(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1919

2020
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
2121
let py_mod = ctx.new_module("keyword", ctx.new_scope(None));
22+
2223
ctx.set_attr(&py_mod, "iskeyword", ctx.new_rustfunc(keyword_iskeyword));
24+
2325
let keyword_kwlist = ctx.new_list(
2426
lexer::get_keywords()
2527
.keys()
2628
.map(|k| ctx.new_str(k.to_string()))
2729
.collect(),
2830
);
2931
ctx.set_attr(&py_mod, "kwlist", keyword_kwlist);
32+
3033
py_mod
3134
}

vm/src/stdlib/pystruct.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,9 @@ fn struct_unpack(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
342342

343343
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
344344
let py_mod = ctx.new_module(&"struct".to_string(), ctx.new_scope(None));
345+
345346
ctx.set_attr(&py_mod, "pack", ctx.new_rustfunc(struct_pack));
346347
ctx.set_attr(&py_mod, "unpack", ctx.new_rustfunc(struct_unpack));
348+
347349
py_mod
348350
}

vm/src/stdlib/re.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ fn re_search(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5151

5252
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
5353
let py_mod = ctx.new_module("re", ctx.new_scope(None));
54+
5455
let match_type = ctx.new_class("Match", ctx.object());
5556
ctx.set_attr(&py_mod, "Match", match_type);
57+
5658
ctx.set_attr(&py_mod, "match", ctx.new_rustfunc(re_match));
5759
ctx.set_attr(&py_mod, "search", ctx.new_rustfunc(re_search));
60+
5861
py_mod
5962
}

vm/src/stdlib/time_module.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ fn time_time(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3232

3333
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
3434
let py_mod = ctx.new_module("time", ctx.new_scope(None));
35+
3536
ctx.set_attr(&py_mod, "sleep", ctx.new_rustfunc(time_sleep));
3637
ctx.set_attr(&py_mod, "time", ctx.new_rustfunc(time_time));
38+
3739
py_mod
3840
}

vm/src/sysmodule.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,38 @@ fn sys_getsizeof(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3939
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
4040
let path_list = match env::var_os("PYTHONPATH") {
4141
Some(paths) => env::split_paths(&paths)
42-
.map(|path| ctx.new_str(path.to_str().unwrap().to_string()))
42+
.map(|path| {
43+
ctx.new_str(
44+
path.to_str()
45+
.expect("PYTHONPATH isn't valid unicode")
46+
.to_string(),
47+
)
48+
})
4349
.collect(),
4450
None => vec![],
4551
};
4652
let path = ctx.new_list(path_list);
53+
4754
let modules = ctx.new_dict();
55+
4856
let sys_name = "sys";
4957
let sys_mod = ctx.new_module(&sys_name, ctx.new_scope(None));
58+
5059
ctx.set_item(&modules, sys_name, sys_mod.clone());
60+
5161
ctx.set_item(&sys_mod, "modules", modules);
5262
ctx.set_item(&sys_mod, "argv", argv(ctx));
5363
ctx.set_item(&sys_mod, "getrefcount", ctx.new_rustfunc(sys_getrefcount));
5464
ctx.set_item(&sys_mod, "getsizeof", ctx.new_rustfunc(sys_getsizeof));
55-
let maxsize = ctx.new_int(std::usize::MAX.to_bigint().unwrap());
56-
ctx.set_item(&sys_mod, "maxsize", maxsize);
65+
ctx.set_item(
66+
&sys_mod,
67+
"maxsize",
68+
ctx.new_int(std::usize::MAX.to_bigint().unwrap()),
69+
);
5770
ctx.set_item(&sys_mod, "path", path);
5871
ctx.set_item(&sys_mod, "ps1", ctx.new_str(">>>>> ".to_string()));
5972
ctx.set_item(&sys_mod, "ps2", ctx.new_str("..... ".to_string()));
6073
ctx.set_item(&sys_mod, "_getframe", ctx.new_rustfunc(getframe));
74+
6175
sys_mod
6276
}

0 commit comments

Comments
 (0)