Merge pull request #282 from pfalcon/from-star

Implement "from mod import *"
This commit is contained in:
Damien George 2014-02-14 23:00:09 +00:00
commit 25735ba6d3
6 changed files with 24 additions and 0 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

2
tests/basics/import2a.py Normal file
View File

@ -0,0 +1,2 @@
from import1b import var
print(var)

2
tests/basics/import3a.py Normal file
View File

@ -0,0 +1,2 @@
from import1b import *
print(var)