Skip to content

Commit 78d702c

Browse files
committed
py: Allow builtins to be overridden.
This patch adds a configuration option (MICROPY_CAN_OVERRIDE_BUILTINS) which, when enabled, allows to override all names within the builtins module. A builtins override dict is created the first time the user assigns to a name in the builtins model, and then that dict is searched first on subsequent lookups. Note that this implementation doesn't allow deleting of names. This patch also does some refactoring of builtins code, creating the modbuiltins.c file. Addresses issue adafruit#959.
1 parent e6e8ad8 commit 78d702c

File tree

14 files changed

+283
-307
lines changed

14 files changed

+283
-307
lines changed

py/builtin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_op_setitem_obj);
7373
MP_DECLARE_CONST_FUN_OBJ(mp_op_delitem_obj);
7474

7575
extern const mp_obj_module_t mp_module___main__;
76+
extern const mp_obj_module_t mp_module_builtins;
7677
extern const mp_obj_module_t mp_module_array;
7778
extern const mp_obj_module_t mp_module_collections;
7879
extern const mp_obj_module_t mp_module_io;
@@ -83,6 +84,9 @@ extern const mp_obj_module_t mp_module_struct;
8384
extern const mp_obj_module_t mp_module_sys;
8485
extern const mp_obj_module_t mp_module_gc;
8586

87+
extern const mp_obj_dict_t mp_module_builtins_globals;
88+
extern mp_obj_dict_t *mp_module_builtins_override_dict;
89+
8690
struct _dummy_t;
8791
extern struct _dummy_t mp_sys_stdin_obj;
8892
extern struct _dummy_t mp_sys_stdout_obj;

py/builtinimport.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include "runtime0.h"
4545
#include "runtime.h"
4646
#include "builtin.h"
47-
#include "builtintables.h"
4847

4948
#if 0 // print debugging info
5049
#define DEBUG_PRINT (1)
@@ -56,6 +55,14 @@
5655

5756
#define PATH_SEP_CHAR '/'
5857

58+
#if MICROPY_MODULE_WEAK_LINKS
59+
STATIC const mp_map_elem_t mp_builtin_module_weak_links_table[] = {
60+
MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS
61+
};
62+
63+
STATIC MP_DEFINE_CONST_MAP(mp_builtin_module_weak_links_map, mp_builtin_module_weak_links_table);
64+
#endif
65+
5966
bool mp_obj_is_package(mp_obj_t module) {
6067
mp_obj_t dest[2];
6168
mp_load_method_maybe(module, MP_QSTR___path__, dest);
@@ -270,7 +277,7 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
270277
#if MICROPY_MODULE_WEAK_LINKS
271278
// check if there is a weak link to this module
272279
if (i == mod_len) {
273-
mp_map_elem_t *el = mp_map_lookup((mp_map_t*)&mp_builtin_module_weak_links_dict_obj.map, MP_OBJ_NEW_QSTR(mod_name), MP_MAP_LOOKUP);
280+
mp_map_elem_t *el = mp_map_lookup((mp_map_t*)&mp_builtin_module_weak_links_map, MP_OBJ_NEW_QSTR(mod_name), MP_MAP_LOOKUP);
274281
if (el == NULL) {
275282
goto no_exist;
276283
}

py/builtintables.c

Lines changed: 0 additions & 261 deletions
This file was deleted.

py/builtintables.h

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)