Skip to content

Commit c4953ee

Browse files
committed
Remove new macro and inline check into make_scope.
1 parent 0d4887a commit c4953ee

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

vm/src/builtins.rs

+26-8
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
193193
optional = [(globals, None), (locals, Some(vm.ctx.dict_type()))]
194194
);
195195

196-
check_but_allow_none!(vm, globals, vm.ctx.dict_type());
196+
let scope = make_scope(vm, globals, locals)?;
197197

198198
// Determine code object:
199199
let code_obj = if objtype::isinstance(source, &vm.ctx.code_type()) {
@@ -213,8 +213,6 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
213213
return Err(vm.new_type_error("code argument must be str or code object".to_string()));
214214
};
215215

216-
let scope = make_scope(vm, globals, locals);
217-
218216
// Run the source:
219217
vm.run_code_obj(code_obj.clone(), scope)
220218
}
@@ -229,7 +227,7 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
229227
optional = [(globals, None), (locals, Some(vm.ctx.dict_type()))]
230228
);
231229

232-
check_but_allow_none!(vm, globals, vm.ctx.dict_type());
230+
let scope = make_scope(vm, globals, locals)?;
233231

234232
// Determine code object:
235233
let code_obj = if objtype::isinstance(source, &vm.ctx.str_type()) {
@@ -249,8 +247,6 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
249247
return Err(vm.new_type_error("source argument must be str or code object".to_string()));
250248
};
251249

252-
let scope = make_scope(vm, globals, locals);
253-
254250
// Run the code:
255251
vm.run_code_obj(code_obj, scope)
256252
}
@@ -259,7 +255,29 @@ fn make_scope(
259255
vm: &mut VirtualMachine,
260256
globals: Option<&PyObjectRef>,
261257
locals: Option<&PyObjectRef>,
262-
) -> ScopeRef {
258+
) -> PyResult<ScopeRef> {
259+
let dict_type = vm.ctx.dict_type();
260+
let globals = match globals {
261+
Some(arg) => {
262+
if arg.is(&vm.get_none()) {
263+
None
264+
} else {
265+
if vm.isinstance(arg, &dict_type)? {
266+
Some(arg)
267+
} else {
268+
let arg_typ = arg.typ();
269+
let actual_type = vm.to_pystr(&arg_typ)?;
270+
let expected_type_name = vm.to_pystr(&dict_type)?;
271+
return Err(vm.new_type_error(format!(
272+
"globals must be a {}, not {}",
273+
expected_type_name, actual_type
274+
)));
275+
}
276+
}
277+
}
278+
None => None,
279+
};
280+
263281
let current_scope = vm.current_scope();
264282
let parent = match globals {
265283
Some(dict) => Some(Scope::new(dict.clone(), Some(vm.get_builtin_scope()))),
@@ -270,7 +288,7 @@ fn make_scope(
270288
None => current_scope.locals.clone(),
271289
};
272290

273-
Scope::new(locals, parent)
291+
Ok(Scope::new(locals, parent))
274292
}
275293

276294
fn builtin_format(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/macros.rs

-28
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,6 @@ macro_rules! type_check {
3535
};
3636
}
3737

38-
#[macro_export]
39-
macro_rules! check_but_allow_none {
40-
( $vm: ident, $arg: ident, $expected_type: expr ) => {
41-
let $arg = match $arg {
42-
Some(arg) => {
43-
if arg.is(&$vm.get_none()) {
44-
None
45-
} else {
46-
if $crate::obj::objtype::real_isinstance($vm, arg, &$expected_type)? {
47-
Some(arg)
48-
} else {
49-
let arg_typ = arg.typ();
50-
let actual_type = $vm.to_pystr(&arg_typ)?;
51-
let expected_type_name = $vm.to_pystr(&$expected_type)?;
52-
return Err($vm.new_type_error(format!(
53-
"{} must be a {}, not {}",
54-
stringify!($arg),
55-
expected_type_name,
56-
actual_type
57-
)));
58-
}
59-
}
60-
}
61-
None => None,
62-
};
63-
};
64-
}
65-
6638
#[macro_export]
6739
macro_rules! arg_check {
6840
( $vm: ident, $args:ident ) => {

0 commit comments

Comments
 (0)