Skip to content

Commit 74d52ee

Browse files
authored
Merge pull request #1011 from palaviv/_imp2
More _imp
2 parents 0c9d3c8 + 106f5f7 commit 74d52ee

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

tests/snippets/imp.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
import _imp
2+
import time as import_time
23

34
assert _imp.is_builtin("time") == True
45
assert _imp.is_builtin("os") == False
56
assert _imp.is_builtin("not existing module") == False
7+
8+
assert _imp.is_frozen("__hello__") == True
9+
assert _imp.is_frozen("os") == False
10+
11+
class FakeSpec:
12+
def __init__(self, name):
13+
self.name = name
14+
15+
A = FakeSpec("time")
16+
17+
imp_time = _imp.create_builtin(A)
18+
assert imp_time.sleep == import_time.sleep
19+
20+
B = FakeSpec("not existing module")
21+
assert _imp.create_builtin(B) == None
22+
23+
_imp.exec_builtin(imp_time) == 0
24+
25+
_imp.get_frozen_object("__hello__")
26+
27+
hello = _imp.init_frozen("__hello__")
28+
assert hello.initialized == True

vm/src/stdlib/imp.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
use crate::compile;
2+
use crate::import::import_file;
3+
use crate::obj::objcode::PyCodeRef;
4+
use crate::obj::objmodule::PyModuleRef;
5+
use crate::obj::objstr;
16
use crate::obj::objstr::PyStringRef;
2-
use crate::pyobject::{PyObjectRef, PyResult};
7+
use crate::pyobject::{ItemProtocol, PyObjectRef, PyResult};
38
use crate::vm::VirtualMachine;
49

510
fn imp_extension_suffixes(vm: &VirtualMachine) -> PyResult {
@@ -25,6 +30,53 @@ fn imp_is_builtin(name: PyStringRef, vm: &VirtualMachine) -> bool {
2530
vm.stdlib_inits.borrow().contains_key(name.as_str())
2631
}
2732

33+
fn imp_is_frozen(name: PyStringRef, vm: &VirtualMachine) -> bool {
34+
vm.frozen.borrow().contains_key(name.as_str())
35+
}
36+
37+
fn imp_create_builtin(spec: PyObjectRef, vm: &VirtualMachine) -> PyResult {
38+
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap();
39+
40+
let name = &objstr::get_value(&vm.get_attribute(spec.clone(), "name")?);
41+
42+
if let Ok(module) = sys_modules.get_item(name, vm) {
43+
Ok(module)
44+
} else {
45+
if let Some(make_module_func) = vm.stdlib_inits.borrow().get(name) {
46+
Ok(make_module_func(vm))
47+
} else {
48+
Ok(vm.get_none())
49+
}
50+
}
51+
}
52+
53+
fn imp_exec_builtin(_mod: PyModuleRef, _vm: &VirtualMachine) -> i32 {
54+
// TOOD: Should we do something here?
55+
0
56+
}
57+
58+
fn imp_get_frozen_object(name: PyStringRef, vm: &VirtualMachine) -> PyResult<PyCodeRef> {
59+
if let Some(frozen) = vm.frozen.borrow().get(name.as_str()) {
60+
compile::compile(vm, frozen, &compile::Mode::Exec, "frozen".to_string())
61+
.map_err(|err| vm.new_syntax_error(&err))
62+
} else {
63+
Err(vm.new_import_error(format!("No such frozen object named {}", name.as_str())))
64+
}
65+
}
66+
67+
fn imp_init_frozen(name: PyStringRef, vm: &VirtualMachine) -> PyResult {
68+
if let Some(frozen) = vm.frozen.borrow().get(name.as_str()) {
69+
import_file(vm, name.as_str(), "frozen".to_string(), frozen.to_string())
70+
} else {
71+
Err(vm.new_import_error(format!("No such frozen object named {}", name.as_str())))
72+
}
73+
}
74+
75+
fn imp_is_frozen_package(_name: PyStringRef, _vm: &VirtualMachine) -> bool {
76+
// TODO: Support frozen package.
77+
false
78+
}
79+
2880
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
2981
let ctx = &vm.ctx;
3082
let module = py_module!(vm, "_imp", {
@@ -33,6 +85,12 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
3385
"release_lock" => ctx.new_rustfunc(imp_release_lock),
3486
"lock_held" => ctx.new_rustfunc(imp_lock_held),
3587
"is_builtin" => ctx.new_rustfunc(imp_is_builtin),
88+
"is_frozen" => ctx.new_rustfunc(imp_is_frozen),
89+
"create_builtin" => ctx.new_rustfunc(imp_create_builtin),
90+
"exec_builtin" => ctx.new_rustfunc(imp_exec_builtin),
91+
"get_frozen_object" => ctx.new_rustfunc(imp_get_frozen_object),
92+
"init_frozen" => ctx.new_rustfunc(imp_init_frozen),
93+
"is_frozen_package" => ctx.new_rustfunc(imp_is_frozen_package),
3694
});
3795

3896
module

vm/src/vm.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ impl VirtualMachine {
255255
syntax_error
256256
}
257257

258+
pub fn new_import_error(&self, msg: String) -> PyObjectRef {
259+
let import_error = self.ctx.exceptions.import_error.clone();
260+
self.new_exception(import_error, msg)
261+
}
262+
258263
pub fn new_scope_with_builtins(&self) -> Scope {
259264
Scope::with_builtins(None, self.ctx.new_dict(), self)
260265
}

0 commit comments

Comments
 (0)