Add _imp.get_frozen_object

This commit is contained in:
Aviv Palivoda
2019-06-01 15:03:45 +03:00
parent 9a168b10d1
commit 4faaf2d6ca
3 changed files with 19 additions and 0 deletions

View File

@@ -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__")

View File

@@ -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<PyCodeRef> {
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

View File

@@ -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)
}