From da1ce93da8aaa2ae050610ec51fdb3e72988a589 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 14 Feb 2014 00:22:06 +0200 Subject: [PATCH 1/3] Implement "from module import *" construct. --- py/runtime.c | 11 +++++++++++ py/runtime.h | 1 + py/showbc.c | 4 ++++ py/vm.c | 4 ++++ 4 files changed, 20 insertions(+) diff --git a/py/runtime.c b/py/runtime.c index f12b3e6125..d15a38e089 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -1016,6 +1016,17 @@ mp_obj_t rt_import_from(mp_obj_t module, qstr name) { return x; } +void rt_import_all(mp_obj_t module) { + DEBUG_printf("import all %p\n", module); + + mp_map_t *map = mp_obj_module_get_globals(module); + for (uint i = 0; i < map->alloc; i++) { + if (map->table[i].key != MP_OBJ_NULL) { + rt_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value); + } + } +} + mp_map_t *rt_locals_get(void) { return map_locals; } diff --git a/py/runtime.h b/py/runtime.h index 20595c6a58..f5a9f2abc7 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -39,6 +39,7 @@ mp_obj_t rt_getiter(mp_obj_t o); mp_obj_t rt_iternext(mp_obj_t o); mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level); mp_obj_t rt_import_from(mp_obj_t module, qstr name); +void rt_import_all(mp_obj_t module); struct _mp_map_t; struct _mp_map_t *rt_locals_get(void); diff --git a/py/showbc.c b/py/showbc.c index 9dfbc88721..e3387dbe27 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -390,6 +390,10 @@ void mp_byte_code_print(const byte *ip, int len) { printf("IMPORT_FROM %s", qstr_str(qstr)); break; + case MP_BC_IMPORT_STAR: + printf("IMPORT_STAR"); + break; + default: printf("code %p, byte code 0x%02x not implemented\n", ip, op); assert(0); diff --git a/py/vm.c b/py/vm.c index 1d47076dfb..10db35e25f 100644 --- a/py/vm.c +++ b/py/vm.c @@ -595,6 +595,10 @@ unwind_return: PUSH(obj1); break; + case MP_BC_IMPORT_STAR: + rt_import_all(TOP()); + break; + default: printf("code %p, byte code 0x%02x not implemented\n", ip, op); assert(0); From b2e8c52298b2745ef2734ca721ec70628c93f018 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 14 Feb 2014 00:24:12 +0200 Subject: [PATCH 2/3] Add testcase for "from module import sym". --- tests/basics/import2a.py | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 tests/basics/import2a.py diff --git a/tests/basics/import2a.py b/tests/basics/import2a.py new file mode 100644 index 0000000000..ce32b10b1b --- /dev/null +++ b/tests/basics/import2a.py @@ -0,0 +1,2 @@ +from import1b import var +print(var) From e7299b5296d53168ec897511b1427056cb6e888a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 14 Feb 2014 00:24:44 +0200 Subject: [PATCH 3/3] Add testcase for "from module import *". --- tests/basics/import3a.py | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 tests/basics/import3a.py diff --git a/tests/basics/import3a.py b/tests/basics/import3a.py new file mode 100644 index 0000000000..2e9d41f71d --- /dev/null +++ b/tests/basics/import3a.py @@ -0,0 +1,2 @@ +from import1b import * +print(var)