split vm.import and vm.import_from (#5212) · sthagen/RustPython-RustPython@6eed8f4 · GitHub
Skip to content

Commit 6eed8f4

Browse files
authored
split vm.import and vm.import_from (RustPython#5212)
1 parent ae72316 commit 6eed8f4

19 files changed

Lines changed: 56 additions & 40 deletions

File tree

examples/call_between_rust_and_python.rs

Lines changed: 1 addition & 1 deletion

examples/package_embed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn py_main(interp: &Interpreter) -> vm::PyResult<PyStrRef> {
77
// Add local library path
88
vm.insert_sys_path(vm.new_pyobj("examples"))
99
.expect("add examples to sys.path failed");
10-
let module = vm.import("package_embed", None, 0)?;
10+
let module = vm.import("package_embed", 0)?;
1111
let name_func = module.get_attr("context", vm)?;
1212
let result = name_func.call((), vm)?;
1313
let result: PyStrRef = result.get_attr("name", vm)?.try_into_value(vm)?;

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode, quiet: bool) -> PyResu
175175
)?;
176176
}
177177

178-
let site_result = vm.import("site", None, 0);
178+
let site_result = vm.import("site", 0);
179179
if site_result.is_err() {
180180
warn!(
181181
"Failed to import site, consider adding the Lib directory to your RUSTPYTHONPATH \

stdlib/src/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
1010
.expect("Expect array has array type.");
1111

1212
let collections_abc = vm
13-
.import("collections.abc", None, 0)
13+
.import("collections.abc", 0)
1414
.expect("Expect collections exist.");
1515
let abc = collections_abc
1616
.get_attr("abc", vm)
@@ -1165,7 +1165,7 @@ mod array {
11651165
let bytes = vm.ctx.new_bytes(array.get_bytes().to_vec());
11661166
let code = MachineFormatCode::from_typecode(array.typecode()).unwrap();
11671167
let code = PyInt::from(u8::from(code)).into_pyobject(vm);
1168-
let module = vm.import("array", None, 0)?;
1168+
let module = vm.import("array", 0)?;
11691169
let func = module.get_attr("_array_reconstructor", vm)?;
11701170
Ok((
11711171
func,

stdlib/src/sqlite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ mod _sqlite {
13011301

13021302
#[pymethod]
13031303
fn iterdump(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
1304-
let module = vm.import("sqlite3.dump", None, 0)?;
1304+
let module = vm.import("sqlite3.dump", 0)?;
13051305
let func = module.get_attr("_iterdump", vm)?;
13061306
func.call((zelf,), vm)
13071307
}

vm/src/builtins/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl GetAttr for PyModule {
202202
impl Representable for PyModule {
203203
#[inline]
204204
fn repr(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
205-
let importlib = vm.import("_frozen_importlib", None, 0)?;
205+
let importlib = vm.import("_frozen_importlib", 0)?;
206206
let module_repr = importlib.get_attr("_module_repr", vm)?;
207207
let repr = module_repr.call((zelf.to_owned(),), vm)?;
208208
repr.downcast()

vm/src/builtins/object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,11 @@ pub fn init(ctx: &Context) {
366366

367367
fn common_reduce(obj: PyObjectRef, proto: usize, vm: &VirtualMachine) -> PyResult {
368368
if proto >= 2 {
369-
let reducelib = vm.import("__reducelib", None, 0)?;
369+
let reducelib = vm.import("__reducelib", 0)?;
370370
let reduce_2 = reducelib.get_attr("reduce_2", vm)?;
371371
reduce_2.call((obj,), vm)
372372
} else {
373-
let copyreg = vm.import("copyreg", None, 0)?;
373+
let copyreg = vm.import("copyreg", 0)?;
374374
let reduce_ex = copyreg.get_attr("_reduce_ex", vm)?;
375375
reduce_ex.call((obj, proto), vm)
376376
}

vm/src/builtins/tuple.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,13 @@ impl<T: TransmuteFromObject> AsRef<[T]> for PyTupleTyped<T> {
510510
}
511511

512512
impl<T: TransmuteFromObject> PyTupleTyped<T> {
513+
pub fn empty(vm: &VirtualMachine) -> Self {
514+
Self {
515+
tuple: vm.ctx.empty_tuple.clone(),
516+
_marker: PhantomData,
517+
}
518+
}
519+
513520
#[inline]
514521
pub fn as_slice(&self) -> &[T] {
515522
unsafe { &*(self.tuple.as_slice() as *const [PyObjectRef] as *const [T]) }

vm/src/frame.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,12 +1136,13 @@ impl ExecutingFrame<'_> {
11361136
}
11371137

11381138
#[cfg_attr(feature = "flame-it", flame("Frame"))]
1139-
fn import(&mut self, vm: &VirtualMachine, module: Option<&Py<PyStr>>) -> PyResult<()> {
1140-
let module = module.unwrap_or(vm.ctx.empty_str);
1141-
let from_list = <Option<PyTupleTyped<PyStrRef>>>::try_from_object(vm, self.pop_value())?;
1139+
fn import(&mut self, vm: &VirtualMachine, module_name: Option<&Py<PyStr>>) -> PyResult<()> {
1140+
let module_name = module_name.unwrap_or(vm.ctx.empty_str);
1141+
let from_list = <Option<PyTupleTyped<PyStrRef>>>::try_from_object(vm, self.pop_value())?
1142+
.unwrap_or_else(|| PyTupleTyped::empty(vm));
11421143
let level = usize::try_from_object(vm, self.pop_value())?;
11431144

1144-
let module = vm.import(module, from_list, level)?;
1145+
let module = vm.import_from(module_name, from_list, level)?;
11451146

11461147
self.push_value(module);
11471148
Ok(())

vm/src/import.rs

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)