Skip to content

Commit bc02b23

Browse files
committed
module_exec
1 parent 32d2406 commit bc02b23

38 files changed

+250
-334
lines changed

Lib/test/test_pickle.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -136,46 +136,6 @@ def test_c_methods(self): # TODO(RUSTPYTHON): Remove this test when it passes
136136
def test_buffers_error(self): # TODO(RUSTPYTHON): Remove this test when it passes
137137
return super().test_buffers_error()
138138

139-
# TODO: RUSTPYTHON
140-
@unittest.expectedFailure
141-
def test_builtin_functions(self): # TODO(RUSTPYTHON): Remove this test when it passes
142-
return super().test_builtin_functions()
143-
144-
# TODO: RUSTPYTHON
145-
@unittest.expectedFailure
146-
def test_bytearray_memoization(self): # TODO(RUSTPYTHON): Remove this test when it passes
147-
return super().test_bytearray_memoization()
148-
149-
# TODO: RUSTPYTHON
150-
@unittest.expectedFailure
151-
def test_bytes_memoization(self): # TODO(RUSTPYTHON): Remove this test when it passes
152-
return super().test_bytes_memoization()
153-
154-
# TODO: RUSTPYTHON
155-
@unittest.expectedFailure
156-
def test_in_band_buffers(self): # TODO(RUSTPYTHON): Remove this test when it passes
157-
return super().test_in_band_buffers()
158-
159-
# TODO: RUSTPYTHON
160-
@unittest.expectedFailure
161-
def test_oob_buffers(self): # TODO(RUSTPYTHON): Remove this test when it passes
162-
return super().test_oob_buffers()
163-
164-
# TODO: RUSTPYTHON
165-
@unittest.expectedFailure
166-
def test_oob_buffers_writable_to_readonly(self): # TODO(RUSTPYTHON): Remove this test when it passes
167-
return super().test_oob_buffers_writable_to_readonly()
168-
169-
# TODO: RUSTPYTHON
170-
@unittest.expectedFailure
171-
def test_buffers_error(self): # TODO(RUSTPYTHON): Remove this test when it passes
172-
return super().test_buffers_error()
173-
174-
# TODO: RUSTPYTHON
175-
@unittest.expectedFailure
176-
def test_builtin_functions(self): # TODO(RUSTPYTHON): Remove this test when it passes
177-
return super().test_builtin_functions()
178-
179139
# TODO: RUSTPYTHON
180140
@unittest.expectedFailure
181141
def test_bytearray_memoization(self): # TODO(RUSTPYTHON): Remove this test when it passes
@@ -201,7 +161,6 @@ def test_oob_buffers(self): # TODO(RUSTPYTHON): Remove this test when it passes
201161
def test_oob_buffers_writable_to_readonly(self): # TODO(RUSTPYTHON): Remove this test when it passes
202162
return super().test_oob_buffers_writable_to_readonly()
203163

204-
205164
class InMemoryPickleTests(AbstractPickleTests, AbstractUnpickleTests,
206165
BigmemPickleTests, unittest.TestCase):
207166

@@ -250,11 +209,6 @@ def test_oob_buffers_writable_to_readonly(self): # TODO(RUSTPYTHON): Remove this
250209
def test_buffers_error(self): # TODO(RUSTPYTHON): Remove this test when it passes
251210
return super().test_buffers_error()
252211

253-
# TODO: RUSTPYTHON
254-
@unittest.expectedFailure
255-
def test_builtin_functions(self): # TODO(RUSTPYTHON): Remove this test when it passes
256-
return super().test_builtin_functions()
257-
258212
# TODO: RUSTPYTHON
259213
@unittest.expectedFailure
260214
def test_bytearray_memoization(self): # TODO(RUSTPYTHON): Remove this test when it passes

Lib/test/test_pickletools.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ def test_optimize_binput_and_memoize(self):
6767
def test_buffers_error(self): # TODO(RUSTPYTHON): Remove this test when it passes
6868
return super().test_buffers_error()
6969

70-
# TODO: RUSTPYTHON
71-
@unittest.expectedFailure
72-
def test_builtin_functions(self): # TODO(RUSTPYTHON): Remove this test when it passes
73-
return super().test_builtin_functions()
74-
7570
# TODO: RUSTPYTHON
7671
@unittest.expectedFailure
7772
def test_bytearray_memoization(self): # TODO(RUSTPYTHON): Remove this test when it passes

benches/microbenchmarks.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ fn bench_rustpython_code(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenc
114114
settings.user_site_directory = false;
115115

116116
Interpreter::with_init(settings, |vm| {
117-
for (name, init) in rustpython_stdlib::get_module_inits() {
118-
vm.add_native_module(name, init);
119-
}
117+
vm.add_native_module_defs(rustpython_stdlib::get_module_defs());
120118
})
121119
.enter(|vm| {
122120
let setup_code = vm

crates/derive-impl/src/pymodule.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct ModuleContext {
5858
name: String,
5959
function_items: FunctionNursery,
6060
attribute_items: ItemNursery,
61-
has_extend_module: bool, // TODO: check if `fn extend_module` exists
61+
has_module_exec: bool,
6262
errors: Vec<syn::Error>,
6363
}
6464

@@ -82,6 +82,12 @@ pub fn impl_pymodule(attr: PunctuatedNestedMeta, module_item: Item) -> Result<To
8282

8383
// collect to context
8484
for item in items.iter_mut() {
85+
// Check if module_exec function is already defined
86+
if let Item::Fn(func) = item
87+
&& func.sig.ident == "module_exec"
88+
{
89+
context.has_module_exec = true;
90+
}
8591
if matches!(item, Item::Impl(_) | Item::Trait(_)) {
8692
// #[pyclass] implementations
8793
continue;
@@ -133,7 +139,7 @@ pub fn impl_pymodule(attr: PunctuatedNestedMeta, module_item: Item) -> Result<To
133139
methods: METHOD_DEFS,
134140
slots: Default::default(),
135141
};
136-
def.slots.exec = Some(extend_module);
142+
def.slots.exec = Some(module_exec);
137143
def
138144
})
139145
}
@@ -146,16 +152,16 @@ pub fn impl_pymodule(attr: PunctuatedNestedMeta, module_item: Item) -> Result<To
146152
use ::rustpython_vm::PyPayload;
147153
let module = ::rustpython_vm::builtins::PyModule::from_def(module_def(&vm.ctx)).into_ref(&vm.ctx);
148154
__init_dict(vm, &module);
149-
extend_module(vm, &module).unwrap();
155+
module_exec(vm, &module).unwrap();
150156
module
151157
}
152158
},
153159
]);
154160
}
155-
if !is_submodule && !context.has_extend_module {
161+
if !is_submodule && !context.has_module_exec {
156162
items.push(parse_quote! {
157-
pub(crate) fn extend_module(vm: &::rustpython_vm::VirtualMachine, module: &::rustpython_vm::Py<::rustpython_vm::builtins::PyModule>) -> ::rustpython_vm::PyResult<()> {
158-
__extend_module(vm, module);
163+
pub(crate) fn module_exec(vm: &::rustpython_vm::VirtualMachine, module: &::rustpython_vm::Py<::rustpython_vm::builtins::PyModule>) -> ::rustpython_vm::PyResult<()> {
164+
__module_exec(vm, module);
159165
Ok(())
160166
}
161167
});
@@ -192,7 +198,7 @@ pub fn impl_pymodule(attr: PunctuatedNestedMeta, module_item: Item) -> Result<To
192198
}
193199
},
194200
parse_quote! {
195-
pub(crate) fn __extend_module(
201+
pub(crate) fn __module_exec(
196202
vm: &::rustpython_vm::VirtualMachine,
197203
module: &::rustpython_vm::Py<::rustpython_vm::builtins::PyModule>,
198204
) {

crates/derive/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ pub fn pyexception(attr: TokenStream, item: TokenStream) -> TokenStream {
143143
}
144144

145145
/// This attribute must be applied to an inline module.
146-
/// It defines a Python module in the form a `make_module` function in the module;
147-
/// this has to be used in a `get_module_inits` to properly register the module.
146+
/// It defines a Python module in the form of a `module_def` function in the module;
147+
/// this has to be used in a `get_module_defs` to properly register the module.
148148
/// Additionally, this macro defines 'MODULE_NAME' and 'DOC' in the module.
149149
/// # Arguments
150150
/// - `name`: the name of the python module,

crates/stdlib/src/array.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,6 @@
11
// spell-checker:ignore typecode tofile tolist fromfile
22

3-
use rustpython_vm::{PyRef, VirtualMachine, builtins::PyModule};
4-
5-
pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
6-
let module = array::make_module(vm);
7-
8-
let array = module
9-
.get_attr("array", vm)
10-
.expect("Expect array has array type.");
11-
12-
let collections_abc = vm
13-
.import("collections.abc", 0)
14-
.expect("Expect collections exist.");
15-
let abc = collections_abc
16-
.get_attr("abc", vm)
17-
.expect("Expect collections has abc submodule.");
18-
let mutable_sequence = abc
19-
.get_attr("MutableSequence", vm)
20-
.expect("Expect collections.abc has MutableSequence type.");
21-
22-
let register = &mutable_sequence
23-
.get_attr("register", vm)
24-
.expect("Expect collections.abc.MutableSequence has register method.");
25-
register
26-
.call((array,), vm)
27-
.expect("Expect collections.abc.MutableSequence.register(array.array) not fail.");
28-
29-
module
30-
}
3+
pub(crate) use array::module_def;
314

325
#[pymodule(name = "array")]
336
mod array {
@@ -1658,4 +1631,25 @@ mod array {
16581631
};
16591632
PyArray::from(array).into_ref_with_type(vm, cls)
16601633
}
1634+
1635+
// Register array.array as collections.abc.MutableSequence
1636+
pub(crate) fn module_exec(
1637+
vm: &VirtualMachine,
1638+
module: &Py<crate::vm::builtins::PyModule>,
1639+
) -> PyResult<()> {
1640+
__module_exec(vm, module);
1641+
1642+
let array_type = module
1643+
.get_attr("array", vm)
1644+
.expect("array module has array type");
1645+
1646+
// vm.import returns the top-level module, so we need to get abc submodule
1647+
let collections_abc = vm.import("collections.abc", 0)?;
1648+
let abc = collections_abc.get_attr("abc", vm)?;
1649+
let mutable_sequence = abc.get_attr("MutableSequence", vm)?;
1650+
let register = mutable_sequence.get_attr("register", vm)?;
1651+
register.call((array_type,), vm)?;
1652+
1653+
Ok(())
1654+
}
16611655
}

crates/stdlib/src/contextvars.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
use crate::vm::{PyRef, VirtualMachine, builtins::PyModule, class::StaticType};
1+
pub(crate) use _contextvars::module_def;
2+
3+
use crate::vm::PyRef;
24
use _contextvars::PyContext;
35
use core::cell::RefCell;
46

5-
pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
6-
let module = _contextvars::make_module(vm);
7-
let token_type = module.get_attr("Token", vm).unwrap();
8-
token_type
9-
.set_attr(
10-
"MISSING",
11-
_contextvars::ContextTokenMissing::static_type().to_owned(),
12-
vm,
13-
)
14-
.unwrap();
15-
module
16-
}
17-
187
thread_local! {
198
// TODO: Vec doesn't seem to match copy behavior
209
static CONTEXTS: RefCell<Vec<PyRef<PyContext>>> = RefCell::default();
@@ -643,4 +632,17 @@ mod _contextvars {
643632
fn copy_context(vm: &VirtualMachine) -> PyContext {
644633
PyContext::current(vm).copy()
645634
}
635+
636+
// Set Token.MISSING attribute
637+
pub(crate) fn module_exec(
638+
vm: &VirtualMachine,
639+
module: &Py<crate::vm::builtins::PyModule>,
640+
) -> PyResult<()> {
641+
__module_exec(vm, module);
642+
643+
let token_type = module.get_attr("Token", vm)?;
644+
token_type.set_attr("MISSING", ContextTokenMissing::static_type().to_owned(), vm)?;
645+
646+
Ok(())
647+
}
646648
}

crates/stdlib/src/gc.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub(crate) use gc::module_def;
22

33
#[pymodule]
44
mod gc {
5-
use crate::vm::{PyObjectRef, PyResult, VirtualMachine, function::FuncArgs};
5+
use crate::vm::{PyResult, VirtualMachine, function::FuncArgs};
66

77
#[pyfunction]
88
fn collect(_args: FuncArgs, _vm: &VirtualMachine) -> i32 {
@@ -40,15 +40,13 @@ mod gc {
4040
}
4141

4242
#[pyfunction]
43-
fn get_referents(_args: FuncArgs, vm: &VirtualMachine) -> PyObjectRef {
44-
// RustPython does not track object references.
45-
vm.ctx.new_tuple(vec![]).into()
43+
fn get_referents(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
44+
Err(vm.new_not_implemented_error(""))
4645
}
4746

4847
#[pyfunction]
49-
fn get_referrers(_args: FuncArgs, vm: &VirtualMachine) -> PyObjectRef {
50-
// RustPython does not track object references.
51-
vm.ctx.new_list(vec![]).into()
48+
fn get_referrers(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
49+
Err(vm.new_not_implemented_error(""))
5250
}
5351

5452
#[pyfunction]

0 commit comments

Comments
 (0)