Skip to content

Commit 6548c36

Browse files
committed
Use check_but_allow_none for exec/eval.
1 parent 4dd0304 commit 6548c36

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

tests/snippets/test_exec.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@
1010
exec("assert 4 == x", {'x': 2}, {'x': 4})
1111

1212
exec("assert max(1, 2) == 2", {}, {})
13+
14+
exec("max(1, 5, square(5)) == 25", None)
15+
16+
try:
17+
exec("", 1)
18+
except TypeError:
19+
pass
20+
else:
21+
raise TypeError("exec should fail unless globals is a dict or None")

vm/src/builtins.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,11 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
190190
vm,
191191
args,
192192
required = [(source, None)],
193-
optional = [
194-
(globals, Some(vm.ctx.dict_type())),
195-
(locals, Some(vm.ctx.dict_type()))
196-
]
193+
optional = [(globals, None), (locals, Some(vm.ctx.dict_type()))]
197194
);
198195

196+
check_but_allow_none!(vm, globals, vm.ctx.dict_type());
197+
199198
// Determine code object:
200199
let code_obj = if objtype::isinstance(source, &vm.ctx.code_type()) {
201200
source.clone()
@@ -227,12 +226,11 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
227226
vm,
228227
args,
229228
required = [(source, None)],
230-
optional = [
231-
(globals, Some(vm.ctx.dict_type())),
232-
(locals, Some(vm.ctx.dict_type()))
233-
]
229+
optional = [(globals, None), (locals, Some(vm.ctx.dict_type()))]
234230
);
235231

232+
check_but_allow_none!(vm, globals, vm.ctx.dict_type());
233+
236234
// Determine code object:
237235
let code_obj = if objtype::isinstance(source, &vm.ctx.str_type()) {
238236
let mode = compile::Mode::Exec;

0 commit comments

Comments
 (0)