Skip to content

Commit da88a59

Browse files
committed
Use _bootstrap.py __import__
1 parent 6d34ddc commit da88a59

3 files changed

Lines changed: 9 additions & 23 deletions

File tree

vm/src/builtins.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -732,27 +732,7 @@ fn builtin_sum(iterable: PyIterable, start: OptionalArg, vm: &VirtualMachine) ->
732732

733733
// Should be renamed to builtin___import__?
734734
fn builtin_import(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
735-
arg_check!(
736-
vm,
737-
args,
738-
required = [(name, Some(vm.ctx.str_type()))],
739-
optional = [
740-
(_globals, Some(vm.ctx.dict_type())),
741-
(_locals, Some(vm.ctx.dict_type()))
742-
]
743-
);
744-
let current_path = {
745-
match vm.current_frame() {
746-
Some(frame) => {
747-
let mut source_pathbuf = PathBuf::from(&frame.code.source_path);
748-
source_pathbuf.pop();
749-
source_pathbuf
750-
}
751-
None => PathBuf::new(),
752-
}
753-
};
754-
755-
import_module(vm, current_path, &objstr::get_value(name))
735+
vm.invoke(vm.import_func.borrow().clone(), args)
756736
}
757737

758738
// builtin_vars

vm/src/import.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ pub fn init_importlib(vm: &VirtualMachine) -> PyResult {
1616
let impmod = import_builtin(vm, "_imp")?;
1717
let install = vm.get_attribute(importlib.clone(), "_install")?;
1818
vm.invoke(install, vec![vm.sys_module.clone(), impmod])?;
19-
let install_external = vm.get_attribute(importlib, "_install_external_importers")?;
20-
vm.invoke(install_external, vec![])
19+
vm.import_func
20+
.replace(vm.get_attribute(importlib.clone(), "__import__")?);
21+
let install_external = vm.get_attribute(importlib.clone(), "_install_external_importers")?;
22+
vm.invoke(install_external, vec![])?;
23+
Ok(vm.get_none())
2124
}
2225

2326
fn import_frozen(vm: &VirtualMachine, module_name: &str) -> PyResult {

vm/src/vm.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub struct VirtualMachine {
5555
pub wasm_id: Option<String>,
5656
pub exceptions: RefCell<Vec<PyObjectRef>>,
5757
pub frozen: RefCell<HashMap<String, &'static str>>,
58+
pub import_func: RefCell<PyObjectRef>,
5859
}
5960

6061
impl VirtualMachine {
@@ -68,6 +69,7 @@ impl VirtualMachine {
6869

6970
let stdlib_inits = RefCell::new(stdlib::get_module_inits());
7071
let frozen = RefCell::new(frozen::get_module_inits());
72+
let import_func = RefCell::new(ctx.none());
7173
let vm = VirtualMachine {
7274
builtins: builtins.clone(),
7375
sys_module: sysmod.clone(),
@@ -77,6 +79,7 @@ impl VirtualMachine {
7779
wasm_id: None,
7880
exceptions: RefCell::new(vec![]),
7981
frozen,
82+
import_func,
8083
};
8184

8285
builtins::make_module(&vm, builtins.clone());

0 commit comments

Comments
 (0)