Skip to content

Commit c33abe9

Browse files
committed
Use py_module macro at more places.
1 parent 2fc53a9 commit c33abe9

File tree

7 files changed

+38
-54
lines changed

7 files changed

+38
-54
lines changed

vm/src/stdlib/json.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,17 @@ fn json_loads(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
225225
}
226226

227227
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
228-
let json_mod = ctx.new_module("json", ctx.new_scope(None));
229-
230-
ctx.set_attr(&json_mod, "dumps", ctx.new_rustfunc(json_dumps));
231-
ctx.set_attr(&json_mod, "loads", ctx.new_rustfunc(json_loads));
232-
233228
// TODO: Make this a proper type with a constructor
234229
let json_decode_error = create_type(
235230
"JSONDecodeError",
236231
&ctx.type_type,
237232
&ctx.exceptions.exception_type,
238233
&ctx.dict_type,
239234
);
240-
ctx.set_attr(&json_mod, "JSONDecodeError", json_decode_error);
241235

242-
json_mod
236+
py_module!(ctx, "json", {
237+
"dumps" => ctx.new_rustfunc(json_dumps),
238+
"loads" => ctx.new_rustfunc(json_loads),
239+
"JSONDecodeError" => json_decode_error
240+
})
243241
}

vm/src/stdlib/keyword.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@ fn keyword_iskeyword(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1818
}
1919

2020
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
21-
let py_mod = ctx.new_module("keyword", ctx.new_scope(None));
22-
23-
ctx.set_attr(&py_mod, "iskeyword", ctx.new_rustfunc(keyword_iskeyword));
24-
2521
let keyword_kwlist = ctx.new_list(
2622
lexer::get_keywords()
2723
.keys()
2824
.map(|k| ctx.new_str(k.to_string()))
2925
.collect(),
3026
);
31-
ctx.set_attr(&py_mod, "kwlist", keyword_kwlist);
3227

33-
py_mod
28+
py_module!(ctx, "keyword", {
29+
"iskeyword" => ctx.new_rustfunc(keyword_iskeyword),
30+
"kwlist" => keyword_kwlist
31+
})
3432
}

vm/src/stdlib/pystruct.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,8 @@ fn struct_unpack(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
341341
}
342342

343343
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
344-
let py_mod = ctx.new_module(&"struct".to_string(), ctx.new_scope(None));
345-
346-
ctx.set_attr(&py_mod, "pack", ctx.new_rustfunc(struct_pack));
347-
ctx.set_attr(&py_mod, "unpack", ctx.new_rustfunc(struct_unpack));
348-
349-
py_mod
344+
py_module!(ctx, "struct", {
345+
"pack" => ctx.new_rustfunc(struct_pack),
346+
"unpack" => ctx.new_rustfunc(struct_unpack)
347+
})
350348
}

vm/src/stdlib/string.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use crate::pyobject::{PyContext, PyObjectRef};
77

88
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
9-
let py_mod = ctx.new_module(&"string".to_string(), ctx.new_scope(None));
10-
119
let ascii_lowercase = "abcdefghijklmnopqrstuvwxyz".to_string();
1210
let ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".to_string();
1311
let ascii_letters = format!("{}{}", ascii_lowercase, ascii_uppercase);
@@ -21,15 +19,15 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
2119
*/
2220

2321
// Constants:
24-
ctx.set_attr(&py_mod, "ascii_letters", ctx.new_str(ascii_letters));
25-
ctx.set_attr(&py_mod, "ascii_lowercase", ctx.new_str(ascii_lowercase));
26-
ctx.set_attr(&py_mod, "ascii_uppercase", ctx.new_str(ascii_uppercase));
27-
ctx.set_attr(&py_mod, "digits", ctx.new_str(digits));
28-
ctx.set_attr(&py_mod, "hexdigits", ctx.new_str(hexdigits));
29-
ctx.set_attr(&py_mod, "octdigits", ctx.new_str(octdigits));
30-
// ctx.set_attr(&py_mod, "printable", ctx.new_str(printable));
31-
ctx.set_attr(&py_mod, "punctuation", ctx.new_str(punctuation));
32-
// ctx.set_attr(&py_mod, "whitespace", ctx.new_str(whitespace));
33-
34-
py_mod
22+
py_module!(ctx, "string", {
23+
"ascii_letters" => ctx.new_str(ascii_letters),
24+
"ascii_lowercase" => ctx.new_str(ascii_lowercase),
25+
"ascii_uppercase" => ctx.new_str(ascii_uppercase),
26+
"digits" => ctx.new_str(digits),
27+
"hexdigits" => ctx.new_str(hexdigits),
28+
"octdigits" => ctx.new_str(octdigits),
29+
// "printable", ctx.new_str(printable)
30+
"punctuation" => ctx.new_str(punctuation)
31+
// "whitespace", ctx.new_str(whitespace)
32+
})
3533
}

vm/src/stdlib/time_module.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ fn time_time(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3131
}
3232

3333
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
34-
let py_mod = ctx.new_module("time", ctx.new_scope(None));
35-
36-
ctx.set_attr(&py_mod, "sleep", ctx.new_rustfunc(time_sleep));
37-
ctx.set_attr(&py_mod, "time", ctx.new_rustfunc(time_time));
38-
39-
py_mod
34+
py_module!(ctx, "time", {
35+
"sleep" => ctx.new_rustfunc(time_sleep),
36+
"time" => ctx.new_rustfunc(time_time)
37+
})
4038
}

vm/src/stdlib/tokenize.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ fn tokenize_tokenize(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2626
// TODO: create main function when called with -m
2727

2828
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
29-
let py_mod = ctx.new_module("tokenize", ctx.new_scope(None));
30-
31-
// Number theory functions:
32-
ctx.set_attr(&py_mod, "tokenize", ctx.new_rustfunc(tokenize_tokenize));
33-
34-
py_mod
29+
py_module!(ctx, "tokenize", {
30+
"tokenize" => ctx.new_rustfunc(tokenize_tokenize)
31+
})
3532
}

vm/src/stdlib/types.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@ fn types_new_class(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3131
}
3232

3333
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
34-
let py_mod = ctx.new_module("types", ctx.new_scope(None));
35-
36-
// Number theory functions:
37-
ctx.set_attr(&py_mod, "new_class", ctx.new_rustfunc(types_new_class));
38-
ctx.set_attr(&py_mod, "FunctionType", ctx.function_type());
39-
ctx.set_attr(&py_mod, "LambdaType", ctx.function_type());
40-
ctx.set_attr(&py_mod, "CodeType", ctx.code_type());
41-
ctx.set_attr(&py_mod, "FrameType", ctx.frame_type());
42-
43-
py_mod
34+
py_module!(ctx, "types", {
35+
"new_class" => ctx.new_rustfunc(types_new_class),
36+
"FunctionType" => ctx.function_type(),
37+
"LambdaType" => ctx.function_type(),
38+
"CodeType" => ctx.code_type(),
39+
"FrameType" => ctx.frame_type()
40+
})
4441
}

0 commit comments

Comments
 (0)