@@ -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:
199199let code_obj =if objtype:: isinstance ( source, & vm. ctx . code_type ( ) ) {
@@ -213,8 +213,6 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
213213return 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:
235233let code_obj =if objtype:: isinstance ( source, & vm. ctx . str_type ( ) ) {
@@ -249,8 +247,6 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
249247return 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(
259255vm : & mut VirtualMachine ,
260256globals : Option < & PyObjectRef > ,
261257locals : 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+
263281let current_scope = vm. current_scope ( ) ;
264282let parent =match globals{
265283Some ( dict) =>Some ( Scope :: new ( dict. clone ( ) , Some ( vm. get_builtin_scope ( ) ) ) ) ,
@@ -270,7 +288,7 @@ fn make_scope(
270288None => current_scope. locals . clone ( ) ,
271289} ;
272290
273- Scope :: new ( locals, parent)
291+ Ok ( Scope :: new ( locals, parent) )
274292}
275293
276294fn builtin_format ( vm : & mut VirtualMachine , args : PyFuncArgs ) ->PyResult {