Commit b00a768e authored by Kevin Modzelewski's avatar Kevin Modzelewski

Fix unexpected-AttributeError-when-importing

The repro is:
- import a.b, and in a.b do
- import a.b as ab

This will cause an attribute error, since a.b (the name) is successfully importable,
but a.b (the attribute) doesn't exist yet.
parent 62ac9c86
......@@ -1700,8 +1700,10 @@ public:
l = r + 1;
continue;
}
pushAssign(tmpname, new AST_Attribute(makeLoad(tmpname, node, true), AST_TYPE::Load,
internString(a->name.s().substr(l, r - l))));
auto attr = new AST_Attribute(makeLoad(tmpname, node, true), AST_TYPE::Load,
internString(a->name.s().substr(l, r - l)));
attr->lineno = import->lineno;
pushAssign(tmpname, attr);
l = r + 1;
} while (l < a->name.s().size());
pushAssign(a->asname, makeLoad(tmpname, node, true));
......
print "running test_package.import_target"
# Since we are currently importing test_package.import_target, this
# import will succeed (return directly from sys.modules), even though
# test_package will not have the 'import_target' attribute yet
import test_package.import_target
try:
print test_package.import_target
assert 0
except AttributeError:
pass
try:
print getattr(test_package, 'import_target')
assert 0
except AttributeError:
pass
# You can do 'import test_package.import_target', but adding an asname will cause an exception:
try:
import test_package.import_target as i
assert 0
i
except AttributeError:
pass
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