summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2018-10-25 23:48:03 +0300
committerDamien George <damien.p.george@gmail.com>2018-11-01 13:33:16 +1100
commit5c18730f28c4f70496f52a59293ee4a575ab3fa5 (patch)
treea2f78ecef32a28b419e400b2d8c28f4d9da93a6b
parent30ed2b3cabd4fe3cc8e0b865846a05d957937a50 (diff)
py/runtime: Fix qstr assumptions when handling "import *".
There was an assumption that all names in a module dict are qstr's. However, they can be dynamically generated (by assigning to globals()), and in case of a long name, it won't be a qstr. Handle this situation properly, including taking care of not creating superfluous qstr's for names starting with "_" (which aren't imported by "import *").
-rw-r--r--py/runtime.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/py/runtime.c b/py/runtime.c
index 8f020f5d5..33c4c1822 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -1379,9 +1379,13 @@ void mp_import_all(mp_obj_t module) {
mp_map_t *map = &mp_obj_module_get_globals(module)->map;
for (size_t i = 0; i < map->alloc; i++) {
if (MP_MAP_SLOT_IS_FILLED(map, i)) {
- qstr name = MP_OBJ_QSTR_VALUE(map->table[i].key);
- if (*qstr_str(name) != '_') {
- mp_store_name(name, map->table[i].value);
+ // Entry in module global scope may be generated programmatically
+ // (and thus be not a qstr for longer names). Avoid turning it in
+ // qstr if it has '_' and was used exactly to save memory.
+ const char *name = mp_obj_str_get_str(map->table[i].key);
+ if (*name != '_') {
+ qstr qname = mp_obj_str_get_qstr(map->table[i].key);
+ mp_store_name(qname, map->table[i].value);
}
}
}