Skip to content

Commit 15aaa6d

Browse files
committed
Add builtin_globals.
1 parent 372f09a commit 15aaa6d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

tests/snippets/test_exec.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,12 @@ def f():
3535
pass
3636
else:
3737
raise TypeError("exec should fail unless globals is a dict or None")
38+
39+
g = globals()
40+
g['x'] = 2
41+
exec('x += 2')
42+
assert x == 4
43+
assert g['x'] == x
44+
45+
exec("del x")
46+
assert 'x' not in g

vm/src/builtins.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ fn builtin_getattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
303303
vm.get_attribute(obj.clone(), attr.clone())
304304
}
305305

306-
// builtin_globals
306+
fn builtin_globals(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
307+
Ok(vm.current_scope().globals.clone())
308+
}
307309

308310
fn builtin_hasattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
309311
arg_check!(
@@ -743,6 +745,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
743745
"filter" => ctx.filter_type(),
744746
"format" => ctx.new_rustfunc(builtin_format),
745747
"getattr" => ctx.new_rustfunc(builtin_getattr),
748+
"globals" => ctx.new_rustfunc(builtin_globals),
746749
"hasattr" => ctx.new_rustfunc(builtin_hasattr),
747750
"hash" => ctx.new_rustfunc(builtin_hash),
748751
"hex" => ctx.new_rustfunc(builtin_hex),

0 commit comments

Comments
 (0)