diff --git a/tests/snippets/imp.py b/tests/snippets/imp.py index fee4701e3..0f60c8082 100644 --- a/tests/snippets/imp.py +++ b/tests/snippets/imp.py @@ -21,3 +21,5 @@ B = FakeSpec("not existing module") assert _imp.create_builtin(B) == None _imp.exec_builtin(imp_time) == 0 + +_imp.get_frozen_object("__hello__") diff --git a/vm/src/stdlib/imp.rs b/vm/src/stdlib/imp.rs index f285f9d60..6aa233b8d 100644 --- a/vm/src/stdlib/imp.rs +++ b/vm/src/stdlib/imp.rs @@ -1,3 +1,5 @@ +use crate::compile; +use crate::obj::objcode::PyCodeRef; use crate::obj::objmodule::PyModuleRef; use crate::obj::objstr; use crate::obj::objstr::PyStringRef; @@ -52,6 +54,15 @@ fn imp_exec_builtin(_mod: PyModuleRef, _vm: &VirtualMachine) -> i32 { 0 } +fn imp_get_frozen_object(name: PyStringRef, vm: &VirtualMachine) -> PyResult { + if let Some(frozen) = vm.frozen.borrow().get(name.as_str()) { + compile::compile(vm, frozen, &compile::Mode::Exec, "frozen".to_string()) + .map_err(|err| vm.new_syntax_error(&err)) + } else { + Err(vm.new_import_error(format!("No such frozen object named {}", name.as_str()))) + } +} + pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; let module = py_module!(vm, "_imp", { @@ -63,6 +74,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "is_frozen" => ctx.new_rustfunc(imp_is_frozen), "create_builtin" => ctx.new_rustfunc(imp_create_builtin), "exec_builtin" => ctx.new_rustfunc(imp_exec_builtin), + "get_frozen_object" => ctx.new_rustfunc(imp_get_frozen_object), }); module diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 17d1caffb..f256b2279 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -256,6 +256,11 @@ impl VirtualMachine { syntax_error } + pub fn new_import_error(&self, msg: String) -> PyObjectRef { + let import_error = self.ctx.exceptions.import_error.clone(); + self.new_exception(import_error, msg) + } + pub fn new_scope_with_builtins(&self) -> Scope { Scope::with_builtins(None, self.ctx.new_dict(), self) }