File tree Expand file tree Collapse file tree 3 files changed +19
-0
lines changed Expand file tree Collapse file tree 3 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -757,6 +757,14 @@ typedef double mp_float_t;
757
757
#define MICROPY_CAN_OVERRIDE_BUILTINS (0)
758
758
#endif
759
759
760
+ // Allow ports to provide a lazy load function for undefined globals
761
+ // By enabling this, MicroPython will call mp_lazy_load_global() whenever an undefined
762
+ // global is accessed. Ports can return a non-MP_OBJ_NULL value which will be returned instead
763
+ // of raising NameError. It is also cached for faster future access.
764
+ #ifndef MICROPY_LAZY_LOAD_GLOBAL
765
+ #define MICROPY_LAZY_LOAD_GLOBAL (0)
766
+ #endif
767
+
760
768
// Whether to check that the "self" argument of a builtin method has the
761
769
// correct type. Such an explicit check is only needed if a builtin
762
770
// method escapes to Python land without a first argument, eg
Original file line number Diff line number Diff line change @@ -184,6 +184,14 @@ mp_obj_t mp_load_global(qstr qst) {
184
184
#endif
185
185
elem = mp_map_lookup ((mp_map_t * )& mp_module_builtins_globals .map , MP_OBJ_NEW_QSTR (qst ), MP_MAP_LOOKUP );
186
186
if (elem == NULL ) {
187
+ #if MICROPY_LAZY_LOAD_GLOBAL
188
+ mp_obj_t obj = mp_lazy_load_global (qst );
189
+ if (obj != MP_OBJ_NULL ) {
190
+ // cache it for faster access next time
191
+ mp_store_global (qst , obj );
192
+ return obj ;
193
+ }
194
+ #endif
187
195
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE ) {
188
196
mp_raise_msg (& mp_type_NameError , "name not defined" );
189
197
} else {
Original file line number Diff line number Diff line change @@ -93,6 +93,9 @@ static inline void mp_globals_set(mp_obj_dict_t *d) { MP_STATE_THREAD(dict_globa
93
93
94
94
mp_obj_t mp_load_name (qstr qst );
95
95
mp_obj_t mp_load_global (qstr qst );
96
+ #ifdef MICROPY_LAZY_LOAD_GLOBAL
97
+ mp_obj_t mp_lazy_load_global (qstr qst );
98
+ #endif
96
99
mp_obj_t mp_load_build_class (void );
97
100
void mp_store_name (qstr qst , mp_obj_t obj );
98
101
void mp_store_global (qstr qst , mp_obj_t obj );
You can’t perform that action at this time.
0 commit comments