summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-04 20:08:21 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-05 12:29:15 +0300
commitc6813d92db57c0973d0c93e1053deb4181076277 (patch)
treef5bb4218d17052e7e071ac6937df17266c9ba752
parenta0d32991ed57ebbbb8c9207dab3223b12ca4aa44 (diff)
downloadmicropython-c6813d92db57c0973d0c93e1053deb4181076277.tar.gz
micropython-c6813d92db57c0973d0c93e1053deb4181076277.zip
py: Put default namespace into module __main__.
That's how CPython has it, in particular, "import __main__" should work.
-rw-r--r--py/builtin.h1
-rw-r--r--py/builtintables.c1
-rw-r--r--py/runtime.c23
3 files changed, 18 insertions, 7 deletions
diff --git a/py/builtin.h b/py/builtin.h
index 3120e5baf6..60b7c85b81 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -33,6 +33,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_namedtuple_obj);
+extern const mp_obj_module_t mp_module___main__;
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;
diff --git a/py/builtintables.c b/py/builtintables.c
index 03fb92e569..1edeefa441 100644
--- a/py/builtintables.c
+++ b/py/builtintables.c
@@ -118,6 +118,7 @@ STATIC const mp_builtin_elem_t builtin_object_table[] = {
};
STATIC const mp_builtin_elem_t builtin_module_table[] = {
+ { MP_QSTR___main__, (mp_obj_t)&mp_module___main__ },
{ MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython },
{ MP_QSTR_array, (mp_obj_t)&mp_module_array },
diff --git a/py/runtime.c b/py/runtime.c
index 238c3a56e2..773f998d34 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -33,6 +33,14 @@ STATIC mp_map_t *map_locals;
STATIC mp_map_t *map_globals;
STATIC mp_map_t map_builtins;
+STATIC mp_map_t map_main;
+
+const mp_obj_module_t mp_module___main__ = {
+ .base = { &mp_type_module },
+ .name = MP_QSTR___main__,
+ .globals = (mp_map_t*)&map_main,
+};
+
// a good optimising compiler will inline this if necessary
STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
@@ -41,17 +49,18 @@ STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
void mp_init(void) {
mp_emit_glue_init();
- // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
- map_locals = map_globals = mp_map_new(1);
-
- // init built-in hash table
- mp_map_init(&map_builtins, 3);
-
// init global module stuff
mp_module_init();
+ mp_map_init(&map_main, 1);
// add some builtins that can't be done in ROM
- mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
+ mp_map_add_qstr(&map_main, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
+
+ // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
+ map_locals = map_globals = &map_main;
+
+ // init built-in hash table
+ mp_map_init(&map_builtins, 3);
#if MICROPY_CPYTHON_COMPAT
// Precreate sys module, so "import sys" didn't throw exceptions.