|
39 | 39 | import com.oracle.graal.python.nodes.frame.WriteNode;
|
40 | 40 | import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
|
41 | 41 | 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; |
42 | 44 | import com.oracle.graal.python.runtime.exception.PException;
|
43 | 45 | import com.oracle.truffle.api.CompilerDirectives;
|
44 | 46 | import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
|
|
49 | 51 | public class ImportFromNode extends AbstractImportNode {
|
50 | 52 | @Children private final WriteNode[] aslist;
|
51 | 53 | @Child private GetItemNode getItem;
|
| 54 | + @Child private CastToJavaStringNode castToJavaStringNode; |
52 | 55 |
|
53 | 56 | private final String importee;
|
54 | 57 | private final int level;
|
@@ -98,25 +101,23 @@ public void executeVoid(VirtualFrame frame) {
|
98 | 101 | try {
|
99 | 102 | moduleName = pol.lookupAttributeStrict(importedModule, frame, __NAME__);
|
100 | 103 | 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){ |
106 | 107 | throw pe;
|
107 | 108 | }
|
108 | 109 | String fullname = PString.cat(pkgname, ".", attr);
|
109 | 110 | PythonModule sys = getContext().getCore().lookupBuiltinModule("sys");
|
110 | 111 | Object sysModules = pol.lookupAttribute(sys, frame, "modules");
|
111 | 112 | assert sysModules != PNone.NO_VALUE : "ImportFromNode: sys.modules was not found!";
|
112 | 113 | writeNode.doWrite(frame, ensureGetItemNode().execute(frame, sysModules, fullname));
|
113 |
| - } catch (PException e2) { |
| 114 | + } catch (PException pe2) { |
114 | 115 | Object modulePath = "unknown location";
|
115 |
| - if (!getAttrErrorProfile.profileException(e2, PythonBuiltinClassType.AttributeError)) { |
| 116 | + if (!getAttrErrorProfile.profileException(pe2, PythonBuiltinClassType.AttributeError)) { |
116 | 117 | try {
|
117 | 118 | modulePath = pol.lookupAttributeStrict(importedModule, frame, __FILE__);
|
118 |
| - } catch (PException e3) { |
119 |
| - e3.expectAttributeError(getFileErrorProfile); |
| 119 | + } catch (PException pe3) { |
| 120 | + pe3.expectAttributeError(getFileErrorProfile); |
120 | 121 | }
|
121 | 122 | }
|
122 | 123 |
|
@@ -146,4 +147,12 @@ private GetItemNode ensureGetItemNode() {
|
146 | 147 | }
|
147 | 148 | return getItem;
|
148 | 149 | }
|
| 150 | + |
| 151 | + private CastToJavaStringNode ensureCastToStringNode() { |
| 152 | + if (castToJavaStringNode == null) { |
| 153 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 154 | + castToJavaStringNode = insert(CastToJavaStringNode.create()); |
| 155 | + } |
| 156 | + return castToJavaStringNode; |
| 157 | + } |
149 | 158 | }
|
0 commit comments