Commit 0e2a4c25 authored by Marius Wachtler's avatar Marius Wachtler

Add support for import * if globals is not a module

parent 12abc42b
......@@ -618,9 +618,7 @@ Value ASTInterpreter::visit_langPrimitive(AST_LangPrimitive* node) {
Value module = visit_expr(node->args[0]);
RELEASE_ASSERT(globals == source_info->parent_module,
"'import *' currently not supported with overridden globals");
v = importStar(module.o, source_info->parent_module);
v = importStar(module.o, globals);
} else if (node->opcode == AST_LangPrimitive::NONE) {
v = None;
} else if (node->opcode == AST_LangPrimitive::LANDINGPAD) {
......
......@@ -4863,13 +4863,9 @@ extern "C" Box* importFrom(Box* _m, const std::string* name) {
raiseExcHelper(ImportError, "cannot import name %s", name->c_str());
}
extern "C" Box* importStar(Box* _from_module, BoxedModule* to_module) {
extern "C" Box* importStar(Box* _from_module, Box* to_globals) {
STAT_TIMER(t0, "us_timer_importStar");
// TODO(kmod): it doesn't seem too bad to update this to take custom globals;
// it looks like mostly a matter of changing the getattr calls to getitem.
RELEASE_ASSERT(getGlobals() == to_module, "importStar doesn't support custom globals yet");
ASSERT(isSubclass(_from_module->cls, module_cls), "%s", _from_module->cls->tp_name);
BoxedModule* from_module = static_cast<BoxedModule*>(_from_module);
......@@ -4902,8 +4898,7 @@ extern "C" Box* importStar(Box* _from_module, BoxedModule* to_module) {
if (!attr_value)
raiseExcHelper(AttributeError, "'module' object has no attribute '%s'", casted_attr_name->data());
to_module->setattr(casted_attr_name->s, attr_value, NULL);
setGlobal(to_globals, casted_attr_name->s, attr_value);
}
return None;
}
......@@ -4913,7 +4908,7 @@ extern "C" Box* importStar(Box* _from_module, BoxedModule* to_module) {
if (p.first()[0] == '_')
continue;
to_module->setattr(p.first(), module_attrs->attr_list->attrs[p.second], NULL);
setGlobal(to_globals, p.first(), module_attrs->attr_list->attrs[p.second]);
}
return None;
......
......@@ -79,7 +79,7 @@ extern "C" void delitem(Box* target, Box* slice);
extern "C" Box* getclsattr(Box* obj, const char* attr);
extern "C" Box* unaryop(Box* operand, int op_type);
extern "C" Box* importFrom(Box* obj, const std::string* attr);
extern "C" Box* importStar(Box* from_module, BoxedModule* to_module);
extern "C" Box* importStar(Box* from_module, Box* to_globals);
extern "C" Box** unpackIntoArray(Box* obj, int64_t expected_size);
extern "C" void assertNameDefined(bool b, const char* name, BoxedClass* exc_cls, bool local_var_msg);
extern "C" void assertFailDerefNameDefined(const char* name);
......
......@@ -156,3 +156,8 @@ l = types.ModuleType("TestMod2")
exec ("global a; a=1; print a; b=2", g.__dict__, l.__dict__)
print g.a
print l.b
s = "from sys import *"
g = dict()
exec s in g
print "version" in g
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment