Skip to content

Commit f13741c

Browse files
committed
ImportFromNode: use CastToJavaStringNode
1 parent 70e1edd commit f13741c

File tree

1 file changed

+18
-9
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement

1 file changed

+18
-9
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import com.oracle.graal.python.nodes.frame.WriteNode;
4040
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
4141
import com.oracle.graal.python.nodes.subscript.GetItemNode;
42+
import com.oracle.graal.python.nodes.util.CannotCastException;
43+
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
4244
import com.oracle.graal.python.runtime.exception.PException;
4345
import com.oracle.truffle.api.CompilerDirectives;
4446
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -49,6 +51,7 @@
4951
public class ImportFromNode extends AbstractImportNode {
5052
@Children private final WriteNode[] aslist;
5153
@Child private GetItemNode getItem;
54+
@Child private CastToJavaStringNode castToJavaStringNode;
5255

5356
private final String importee;
5457
private final int level;
@@ -98,25 +101,23 @@ public void executeVoid(VirtualFrame frame) {
98101
try {
99102
moduleName = pol.lookupAttributeStrict(importedModule, frame, __NAME__);
100103
String pkgname;
101-
if (moduleName instanceof PString) {
102-
pkgname = ((PString) moduleName).getValue();
103-
} else if (moduleName instanceof String) {
104-
pkgname = (String) moduleName;
105-
} else {
104+
try {
105+
pkgname = ensureCastToStringNode().execute(moduleName);
106+
} catch (CannotCastException castException){
106107
throw pe;
107108
}
108109
String fullname = PString.cat(pkgname, ".", attr);
109110
PythonModule sys = getContext().getCore().lookupBuiltinModule("sys");
110111
Object sysModules = pol.lookupAttribute(sys, frame, "modules");
111112
assert sysModules != PNone.NO_VALUE : "ImportFromNode: sys.modules was not found!";
112113
writeNode.doWrite(frame, ensureGetItemNode().execute(frame, sysModules, fullname));
113-
} catch (PException e2) {
114+
} catch (PException pe2) {
114115
Object modulePath = "unknown location";
115-
if (!getAttrErrorProfile.profileException(e2, PythonBuiltinClassType.AttributeError)) {
116+
if (!getAttrErrorProfile.profileException(pe2, PythonBuiltinClassType.AttributeError)) {
116117
try {
117118
modulePath = pol.lookupAttributeStrict(importedModule, frame, __FILE__);
118-
} catch (PException e3) {
119-
e3.expectAttributeError(getFileErrorProfile);
119+
} catch (PException pe3) {
120+
pe3.expectAttributeError(getFileErrorProfile);
120121
}
121122
}
122123

@@ -146,4 +147,12 @@ private GetItemNode ensureGetItemNode() {
146147
}
147148
return getItem;
148149
}
150+
151+
private CastToJavaStringNode ensureCastToStringNode() {
152+
if (castToJavaStringNode == null) {
153+
CompilerDirectives.transferToInterpreterAndInvalidate();
154+
castToJavaStringNode = insert(CastToJavaStringNode.create());
155+
}
156+
return castToJavaStringNode;
157+
}
149158
}

0 commit comments

Comments
 (0)