Skip to content

Commit 6ba5b09

Browse files
committed
Use py_module at more places.
1 parent c1ddcbb commit 6ba5b09

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
lines changed

vm/src/stdlib/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,8 @@ fn ast_parse(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
624624
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
625625
py_module!(ctx, "ast", {
626626
"parse" => ctx.new_rustfunc(ast_parse),
627-
"Module" => ctx.new_class("_ast.Module", ctx.object()),
628-
"FunctionDef" =>ctx.new_class("_ast.FunctionDef", ctx.object()),
629-
"Call" => ctx.new_class("_ast.Call", ctx.object())
627+
"Module" => py_class!(ctx, "_ast.Module", ctx.object(), {}),
628+
"FunctionDef" => py_class!(ctx, "_ast.FunctionDef", ctx.object(), {}),
629+
"Call" => py_class!(ctx, "_ast.Call", ctx.object(), {})
630630
})
631631
}

vm/src/stdlib/io.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
347347

348348
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
349349
//IOBase the abstract base class of the IO Module
350-
let io_base = ctx.new_class("IOBase", ctx.object());
350+
let io_base = py_class!(ctx, "IOBase", ctx.object(), {});
351351

352352
// IOBase Subclasses
353-
let raw_io_base = ctx.new_class("RawIOBase", ctx.object());
353+
let raw_io_base = py_class!(ctx, "RawIOBase", ctx.object(), {});
354354

355355
let buffered_io_base = py_class!(ctx, "BufferedIOBase", io_base.clone(), {
356356
"__init__" => ctx.new_rustfunc(buffered_io_base_init)

vm/src/stdlib/os.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,17 @@ fn os_error(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
119119
}
120120

121121
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
122-
let py_mod = ctx.new_module(&"os".to_string(), ctx.new_scope(None));
123-
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(os_open));
124-
ctx.set_attr(&py_mod, "close", ctx.new_rustfunc(os_close));
125-
ctx.set_attr(&py_mod, "error", ctx.new_rustfunc(os_error));
126-
127-
ctx.set_attr(&py_mod, "O_RDONLY", ctx.new_int(0));
128-
ctx.set_attr(&py_mod, "O_WRONLY", ctx.new_int(1));
129-
ctx.set_attr(&py_mod, "O_RDWR", ctx.new_int(2));
130-
ctx.set_attr(&py_mod, "O_NONBLOCK", ctx.new_int(4));
131-
ctx.set_attr(&py_mod, "O_APPEND", ctx.new_int(8));
132-
ctx.set_attr(&py_mod, "O_CREAT", ctx.new_int(512));
122+
let py_mod = py_module!(ctx, "os", {
123+
"open" => ctx.new_rustfunc(os_open),
124+
"close" => ctx.new_rustfunc(os_close),
125+
"error" => ctx.new_rustfunc(os_error),
126+
"O_RDONLY" => ctx.new_int(0),
127+
"O_WRONLY" => ctx.new_int(1),
128+
"O_RDWR" => ctx.new_int(2),
129+
"O_NONBLOCK" => ctx.new_int(4),
130+
"O_APPEND" => ctx.new_int(8),
131+
"O_CREAT" => ctx.new_int(512)
132+
});
133133

134134
if cfg!(windows) {
135135
ctx.set_attr(&py_mod, "name", ctx.new_str("nt".to_string()));

vm/src/stdlib/weakref.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ use crate::VirtualMachine;
1313
use std::rc::Rc;
1414

1515
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
16-
let py_mod = ctx.new_module("_weakref", ctx.new_scope(None));
16+
let py_ref_class = py_class!(ctx, "ref", ctx.object(), {
17+
"__new__" => ctx.new_rustfunc(ref_new),
18+
"__call__" => ctx.new_rustfunc(ref_call)
19+
});
1720

18-
let py_ref_class = ctx.new_class("ref", ctx.object());
19-
ctx.set_attr(&py_ref_class, "__new__", ctx.new_rustfunc(ref_new));
20-
ctx.set_attr(&py_ref_class, "__call__", ctx.new_rustfunc(ref_call));
21-
ctx.set_attr(&py_mod, "ref", py_ref_class);
22-
py_mod
21+
py_module!(ctx, "_weakref", {
22+
"ref" => py_ref_class
23+
})
2324
}
2425

2526
fn ref_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

wasm/lib/src/browser_module.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,10 @@ fn browser_prompt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
313313
const BROWSER_NAME: &str = "browser";
314314

315315
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
316-
let promise = {
317-
let promise = ctx.new_class("Promise", ctx.object());
318-
ctx.set_attr(&promise, "then", ctx.new_rustfunc(promise_then));
319-
ctx.set_attr(&promise, "catch", ctx.new_rustfunc(promise_catch));
320-
promise
321-
};
316+
let promise = py_class!(ctx, "Promise", ctx.object(), {
317+
"then" => ctx.new_rustfunc(promise_then),
318+
"catch" => ctx.new_rustfunc(promise_catch)
319+
});
322320

323321
py_module!(ctx, BROWSER_NAME, {
324322
"fetch" => ctx.new_rustfunc(browser_fetch),

0 commit comments

Comments
 (0)