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 #959.
This commit is contained in:
Damien George 2014-12-09 16:19:48 +00:00
parent e6e8ad8ab2
commit 78d702c300
14 changed files with 283 additions and 307 deletions

View File

@ -73,6 +73,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_op_setitem_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_op_delitem_obj);
extern const mp_obj_module_t mp_module___main__;
extern const mp_obj_module_t mp_module_builtins;
extern const mp_obj_module_t mp_module_array;
extern const mp_obj_module_t mp_module_collections;
extern const mp_obj_module_t mp_module_io;
@ -83,6 +84,9 @@ extern const mp_obj_module_t mp_module_struct;
extern const mp_obj_module_t mp_module_sys;
extern const mp_obj_module_t mp_module_gc;
extern const mp_obj_dict_t mp_module_builtins_globals;
extern mp_obj_dict_t *mp_module_builtins_override_dict;
struct _dummy_t;
extern struct _dummy_t mp_sys_stdin_obj;
extern struct _dummy_t mp_sys_stdout_obj;

View File

@ -44,7 +44,6 @@
#include "runtime0.h"
#include "runtime.h"
#include "builtin.h"
#include "builtintables.h"
#if 0 // print debugging info
#define DEBUG_PRINT (1)
@ -56,6 +55,14 @@
#define PATH_SEP_CHAR '/'
#if MICROPY_MODULE_WEAK_LINKS
STATIC const mp_map_elem_t mp_builtin_module_weak_links_table[] = {
MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS
};
STATIC MP_DEFINE_CONST_MAP(mp_builtin_module_weak_links_map, mp_builtin_module_weak_links_table);
#endif
bool mp_obj_is_package(mp_obj_t module) {
mp_obj_t dest[2];
mp_load_method_maybe(module, MP_QSTR___path__, dest);