Merge pull request #881 from youknowone/globals-builtins

Add __builtins__ to globals()
This commit is contained in:
Windel Bouwman authored and GitHub committed 2019-05-12 11:40:07 +02:00
commit 937704fbd9
10 files changed
+65 -14

No files matched your search

+2 -2
View File
@@ -99,7 +99,7 @@ fn bench_rustpy_nbody(b: &mut test::Bencher) {
};
b.iter(|| {
let scope = vm.ctx.new_scope();
let scope = vm.new_scope_with_builtins();
let res: PyResult = vm.run_code_obj(code.clone(), scope);
assert!(res.is_ok());
})
@@ -118,7 +118,7 @@ fn bench_rustpy_mandelbrot(b: &mut test::Bencher) {
};
b.iter(|| {
let scope = vm.ctx.new_scope();
let scope = vm.new_scope_with_builtins();
let res: PyResult = vm.run_code_obj(code.clone(), scope);
assert!(res.is_ok());
})
+2 -2
View File
@@ -77,7 +77,7 @@ fn _run_string(vm: &VirtualMachine, source: &str, source_path: String) -> PyResu
// trace!("Code object: {:?}", code_obj.borrow());
let attrs = vm.ctx.new_dict();
attrs.set_item("__file__", vm.new_str(source_path), vm)?;
vm.run_code_obj(code_obj, Scope::new(None, attrs))
vm.run_code_obj(code_obj, Scope::with_builtins(None, attrs, vm))
}
fn handle_exception(vm: &VirtualMachine, result: PyResult) {
@@ -217,7 +217,7 @@ fn run_shell(vm: &VirtualMachine) -> PyResult {
"Welcome to the magnificent Rust Python {} interpreter \u{1f631} \u{1f596}",
crate_version!()
);
let vars = vm.ctx.new_scope();
let vars = vm.new_scope_with_builtins();
// Read a single line:
let mut input = String::new();
+28
View File
@@ -0,0 +1,28 @@
from testutils import assertRaises
assert '__builtins__' in globals()
# assert type(__builtins__).__name__ == 'module'
with assertRaises(AttributeError):
__builtins__.__builtins__
__builtins__.x = 'new'
assert x == 'new'
exec('assert "__builtins__" in globals()', dict())
exec('assert __builtins__ == 7', {'__builtins__': 7})
exec('assert not isinstance(__builtins__, dict)')
exec('assert isinstance(__builtins__, dict)', {})
namespace = {}
exec('', namespace)
assert namespace['__builtins__'] == __builtins__.__dict__
# with assertRaises(NameError):
# exec('print(__builtins__)', {'__builtins__': {}})
# __builtins__ is deletable but names are alive
del __builtins__
with assertRaises(NameError):
__builtins__
assert print
+12 -3
View File
@@ -218,18 +218,27 @@ fn make_scope(
}
None => None,
};
let current_scope = vm.current_scope();
let globals = match globals {
Some(dict) => dict.clone().downcast().unwrap(),
Some(dict) => {
let dict: PyDictRef = dict.clone().downcast().unwrap();
if !dict.contains_key("__builtins__", vm) {
let builtins_dict = vm.builtins.dict.as_ref().unwrap().as_object();
dict.set_item("__builtins__", builtins_dict.clone(), vm)
.unwrap();
}
dict
}
None => current_scope.globals.clone(),
};
let locals = match locals {
Some(dict) => dict.clone().downcast().ok(),
None => current_scope.get_only_locals(),
};
Ok(Scope::new(locals, globals))
let scope = Scope::with_builtins(locals, globals, vm);
Ok(scope)
}
fn builtin_format(
+1 -1
View File
@@ -24,7 +24,7 @@ mod tests {
fn test_print_42() {
let source = String::from("print('Hello world')\n");
let mut vm = VirtualMachine::new();
let vars = vm.ctx.new_scope();
let vars = vm.new_scope_with_builtins();
let _result = eval(&mut vm, &source, vars, "<unittest>");
// TODO: check result?
+14
View File
@@ -98,6 +98,20 @@ impl Scope {
Scope { locals, globals }
}
pub fn with_builtins(
locals: Option<PyDictRef>,
globals: PyDictRef,
vm: &VirtualMachine,
) -> Scope {
if !globals.contains_key("__builtins__", vm) {
globals
.clone()
.set_item("__builtins__", vm.builtins.clone(), vm)
.unwrap();
}
Scope::new(locals, globals)
}
pub fn get_locals(&self) -> PyDictRef {
match self.locals.iter().next() {
Some(dict) => dict.clone(),
+1 -1
View File
@@ -61,7 +61,7 @@ pub fn import_file(
sys_modules.set_item(module_name, module.clone(), vm)?;
// Execute main code in module:
vm.run_code_obj(code_obj, Scope::new(None, attrs))?;
vm.run_code_obj(code_obj, Scope::with_builtins(None, attrs, vm))?;
Ok(module)
}
-4
View File
@@ -648,10 +648,6 @@ impl PyContext {
objtype::new(self.type_type(), name, vec![base], PyAttributes::new()).unwrap()
}
pub fn new_scope(&self) -> Scope {
Scope::new(None, self.new_dict())
}
pub fn new_module(&self, name: &str, dict: PyDictRef) -> PyObjectRef {
PyObject::new(
PyModule {
+4
View File
@@ -250,6 +250,10 @@ impl VirtualMachine {
syntax_error
}
pub fn new_scope_with_builtins(&self) -> Scope {
Scope::with_builtins(None, self.ctx.new_dict(), self)
}
pub fn get_none(&self) -> PyObjectRef {
self.ctx.none()
}
+1 -1
View File
@@ -26,7 +26,7 @@ pub(crate) struct StoredVirtualMachine {
impl StoredVirtualMachine {
fn new(id: String, inject_browser_module: bool) -> StoredVirtualMachine {
let mut vm = VirtualMachine::new();
let scope = vm.ctx.new_scope();
let scope = vm.new_scope_with_builtins();
if inject_browser_module {
setup_browser_module(&vm);
}