kopia lustrzana https://github.com/micropython/micropython
py/builtinevex: Fix setting globals for native functions in compile().
Signed-off-by: Damien George <damien@micropython.org>pull/13710/head
rodzic
916ceecaef
commit
3db29103a4
|
@ -57,9 +57,13 @@ STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj
|
||||||
// set exception handler to restore context if an exception is raised
|
// set exception handler to restore context if an exception is raised
|
||||||
nlr_push_jump_callback(&ctx.callback, mp_globals_locals_set_from_nlr_jump_callback);
|
nlr_push_jump_callback(&ctx.callback, mp_globals_locals_set_from_nlr_jump_callback);
|
||||||
|
|
||||||
// a bit of a hack: fun_bc will re-set globals, so need to make sure it's
|
// The call to mp_parse_compile_execute() in mp_builtin_compile() below passes
|
||||||
// the correct one
|
// NULL for the globals, so repopulate that entry now with the correct globals.
|
||||||
if (mp_obj_is_type(self->module_fun, &mp_type_fun_bc)) {
|
if (mp_obj_is_type(self->module_fun, &mp_type_fun_bc)
|
||||||
|
#if MICROPY_EMIT_NATIVE
|
||||||
|
|| mp_obj_is_type(self->module_fun, &mp_type_fun_native)
|
||||||
|
#endif
|
||||||
|
) {
|
||||||
mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
|
mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
|
||||||
((mp_module_context_t *)fun_bc->context)->module.globals = globals;
|
((mp_module_context_t *)fun_bc->context)->module.globals = globals;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,9 @@ def test():
|
||||||
exec(compile("print(10 + 2)", "file", "single"))
|
exec(compile("print(10 + 2)", "file", "single"))
|
||||||
print(eval(compile("10 + 3", "file", "eval")))
|
print(eval(compile("10 + 3", "file", "eval")))
|
||||||
|
|
||||||
|
# test accessing a function's globals from within a compile
|
||||||
|
exec(compile("def func():pass\nprint('x', func.__globals__['x'])", "file", "exec"))
|
||||||
|
|
||||||
# bad mode
|
# bad mode
|
||||||
try:
|
try:
|
||||||
compile('1', 'file', '')
|
compile('1', 'file', '')
|
||||||
|
|
Ładowanie…
Reference in New Issue