Skip to content

Commit 59bbea2

Browse files
committed
during recursive import from we need to check sys.modules, too
1 parent 5ca2534 commit 59bbea2

File tree

1 file changed

+37
-1
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement

1 file changed

+37
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/ImportFromNode.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@
2525
*/
2626
package com.oracle.graal.python.nodes.statement;
2727

28+
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
2829
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
2930
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ImportError;
3031

3132
import com.oracle.graal.python.builtins.objects.function.PArguments;
33+
import com.oracle.graal.python.builtins.objects.str.PString;
34+
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
35+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
3236
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
3337
import com.oracle.graal.python.nodes.frame.WriteNode;
38+
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
39+
import com.oracle.graal.python.nodes.subscript.GetItemNode;
3440
import com.oracle.graal.python.runtime.exception.PException;
41+
import com.oracle.truffle.api.CompilerDirectives;
3542
import com.oracle.truffle.api.frame.VirtualFrame;
3643
import com.oracle.truffle.api.nodes.ExplodeLoop;
3744

@@ -40,7 +47,11 @@ public class ImportFromNode extends AbstractImportNode {
4047
private final int level;
4148
private final String[] fromlist;
4249
@Children private final WriteNode[] aslist;
50+
@Child private GetAttributeNode getName;
51+
@Child private GetItemNode getItem;
52+
@Child private ReadAttributeFromObjectNode readModules;
4353
@Child private LookupAndCallBinaryNode readNode = LookupAndCallBinaryNode.create(__GETATTRIBUTE__);
54+
private final IsBuiltinClassProfile attrErrorProfile = IsBuiltinClassProfile.create();
4455

4556
public static ImportFromNode create(String importee, String[] fromlist, WriteNode[] readNodes, int level) {
4657
return new ImportFromNode(importee, fromlist, readNodes, level);
@@ -68,7 +79,32 @@ public void executeVoid(VirtualFrame frame) {
6879
try {
6980
writeNode.doWrite(frame, readNode.executeObject(importedModule, attr));
7081
} catch (PException e) {
71-
throw raise(ImportError, "cannot import name '%s'", attr);
82+
e.expectAttributeError(attrErrorProfile);
83+
if (getName == null) {
84+
CompilerDirectives.transferToInterpreterAndInvalidate();
85+
getName = insert(GetAttributeNode.create(__NAME__, null));
86+
}
87+
try {
88+
String pkgname;
89+
Object pkgname_o = getName.executeObject(importedModule);
90+
if (pkgname_o instanceof PString) {
91+
pkgname = ((PString) pkgname_o).getValue();
92+
} else if (pkgname_o instanceof String) {
93+
pkgname = (String) pkgname_o;
94+
} else {
95+
throw e;
96+
}
97+
String fullname = pkgname + "." + attr;
98+
if (getItem == null) {
99+
CompilerDirectives.transferToInterpreterAndInvalidate();
100+
getItem = insert(GetItemNode.create());
101+
readModules = insert(ReadAttributeFromObjectNode.create());
102+
}
103+
Object sysModules = readModules.execute(getCore().lookupBuiltinModule("sys"), "modules");
104+
writeNode.doWrite(frame, getItem.execute(sysModules, fullname));
105+
} catch (PException e2) {
106+
throw raise(ImportError, "cannot import name '%s'", attr);
107+
}
72108
}
73109
}
74110
}

0 commit comments

Comments
 (0)