Skip to content

Commit e88d6ac

Browse files
committed
Add init_importlib
1 parent 7ff59b2 commit e88d6ac

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ fn main() {
5454
// Construct vm:
5555
let vm = VirtualMachine::new();
5656

57+
let res = import::init_importlib(&vm);
58+
handle_exception(&vm, res);
59+
5760
// Figure out if a -c option was given:
5861
let result = if let Some(command) = matches.value_of("c") {
5962
run_command(&vm, command.to_string())

vm/src/import.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,43 @@ use crate::pyobject::{ItemProtocol, PyResult};
1111
use crate::util;
1212
use crate::vm::VirtualMachine;
1313

14+
pub fn init_importlib(vm: &VirtualMachine) -> PyResult {
15+
let importlib = import_frozen(vm, "_frozen_importlib")?;
16+
let impmod = import_builtin(vm, "_imp")?;
17+
let install = vm.get_attribute(importlib, "_install")?;
18+
vm.invoke(install, vec![vm.sys_module.clone(), impmod])
19+
}
20+
21+
fn import_frozen(vm: &VirtualMachine, module_name: &str) -> PyResult {
22+
if let Some(frozen) = vm.frozen.borrow().get(module_name) {
23+
import_file(vm, module_name, "frozen".to_string(), frozen.to_string())
24+
} else {
25+
Err(vm.new_import_error(format!("Cannot import frozen module {}", module_name)))
26+
}
27+
}
28+
29+
fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult {
30+
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap();
31+
if let Some(make_module_func) = vm.stdlib_inits.borrow().get(module_name) {
32+
let module = make_module_func(vm);
33+
sys_modules.set_item(module_name, module.clone(), vm)?;
34+
Ok(module)
35+
} else {
36+
Err(vm.new_import_error(format!("Cannot import bultin module {}", module_name)))
37+
}
38+
}
39+
1440
pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &str) -> PyResult {
1541
// Cached modules:
1642
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap();
1743

1844
// First, see if we already loaded the module:
1945
if let Ok(module) = sys_modules.get_item(module_name.to_string(), vm) {
2046
Ok(module)
21-
} else if let Some(frozen) = vm.frozen.borrow().get(module_name) {
22-
import_file(vm, module_name, "frozen".to_string(), frozen.to_string())
23-
} else if let Some(make_module_func) = vm.stdlib_inits.borrow().get(module_name) {
24-
let module = make_module_func(vm);
25-
sys_modules.set_item(module_name, module.clone(), vm)?;
26-
Ok(module)
47+
} else if vm.frozen.borrow().contains_key(module_name) {
48+
import_frozen(vm, module_name)
49+
} else if vm.stdlib_inits.borrow().contains_key(module_name) {
50+
import_builtin(vm, module_name)
2751
} else {
2852
let notfound_error = vm.context().exceptions.module_not_found_error.clone();
2953
let import_error = vm.context().exceptions.import_error.clone();

0 commit comments

Comments
 (0)