Skip to content

Commit 4c62f30

Browse files
committed
import nodes: reuse the PythonObjectLibrary
1 parent f10664b commit 4c62f30

File tree

2 files changed

+22
-50
lines changed

2 files changed

+22
-50
lines changed

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

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@
2525
*/
2626
package com.oracle.graal.python.nodes.statement;
2727

28-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FILE__;
2928
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
29+
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__PATH__;
3030
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__SPEC__;
3131

3232
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
33+
import com.oracle.graal.python.builtins.objects.PNone;
3334
import com.oracle.graal.python.builtins.objects.function.PArguments;
3435
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3536
import com.oracle.graal.python.builtins.objects.str.PString;
3637
import com.oracle.graal.python.nodes.ErrorMessages;
3738
import com.oracle.graal.python.nodes.PRaiseImportErrorNode;
38-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
39-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetAnyAttributeNode;
40-
import com.oracle.graal.python.nodes.attributes.GetAttributeNodeGen.GetAnyAttributeNodeGen;
4139
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
4240
import com.oracle.graal.python.nodes.frame.WriteNode;
4341
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
@@ -52,13 +50,10 @@
5250

5351
public class ImportFromNode extends AbstractImportNode {
5452
@Children private final WriteNode[] aslist;
55-
@Child private GetAttributeNode getName;
56-
@Child private GetAttributeNode getPath;
53+
@Child private PythonObjectLibrary pythonLibrary;
5754
@Child private GetItemNode getItem;
5855
@Child private ReadAttributeFromObjectNode readModules;
59-
@Child private GetAnyAttributeNode getAttributeNode = GetAnyAttributeNodeGen.create();
6056
@Child private PRaiseImportErrorNode raiseNode;
61-
@Child private PythonObjectLibrary pythonLibrary;
6257

6358
private final String importee;
6459
private final int level;
@@ -95,16 +90,18 @@ protected ImportFromNode(String importee, String[] fromlist, WriteNode[] readNod
9590
public void executeVoid(VirtualFrame frame) {
9691
Object globals = PArguments.getGlobals(frame);
9792
Object importedModule = importModule(frame, importee, globals, fromlist, level);
93+
PythonObjectLibrary pol = ensurePythonLibrary();
94+
9895
for (int i = 0; i < fromlist.length; i++) {
9996
String attr = fromlist[i];
10097
WriteNode writeNode = aslist[i];
10198
try {
102-
writeNode.doWrite(frame, getAttributeNode.executeObject(frame, importedModule, attr));
99+
writeNode.doWrite(frame, pol.lookupAttributeStrict(importedModule, frame, attr));
103100
} catch (PException pe) {
104101
pe.expectAttributeError(getAttrErrorProfile);
105102
Object moduleName = "<unknown module name>";
106103
try {
107-
moduleName = ensureGetNameNode().executeObject(frame, importedModule);
104+
moduleName = pol.lookupAttributeStrict(importedModule, frame, __NAME__);
108105
String pkgname;
109106
if (moduleName instanceof PString) {
110107
pkgname = ((PString) moduleName).getValue();
@@ -120,13 +117,12 @@ public void executeVoid(VirtualFrame frame) {
120117
Object modulePath = "unknown location";
121118
if (!getAttrErrorProfile.profileException(e2, PythonBuiltinClassType.AttributeError)) {
122119
try {
123-
modulePath = ensureGetPathNode().executeObject(frame, importedModule);
120+
modulePath = pol.lookupAttributeStrict(importedModule, frame, __PATH__);
124121
} catch (PException e3) {
125122
e3.expectAttributeError(getFileErrorProfile);
126123
}
127124
}
128125

129-
PythonObjectLibrary pol = ensurePythonLibrary();
130126
if (isModuleInitialising(frame, pol, importedModule)) {
131127
throw ensureRaiseNode().raiseImportError(frame, moduleName, modulePath, ErrorMessages.CANNOT_IMPORT_NAME_CIRCULAR, attr, moduleName);
132128
} else {
@@ -139,8 +135,11 @@ public void executeVoid(VirtualFrame frame) {
139135

140136
private boolean isModuleInitialising(VirtualFrame frame, PythonObjectLibrary pol, Object importedModule) {
141137
Object spec = pol.lookupAttribute(importedModule, frame, __SPEC__);
142-
Object initializing = pol.lookupAttribute(spec, frame, "_initializing");
143-
return pol.isTrue(initializing);
138+
if (spec != PNone.NO_VALUE) {
139+
Object initializing = pol.lookupAttribute(spec, frame, "_initializing");
140+
return pol.isTrue(initializing);
141+
}
142+
return false;
144143
}
145144

146145
private PythonObjectLibrary ensurePythonLibrary() {
@@ -151,22 +150,6 @@ private PythonObjectLibrary ensurePythonLibrary() {
151150
return pythonLibrary;
152151
}
153152

154-
private GetAttributeNode ensureGetNameNode() {
155-
if (getName == null) {
156-
CompilerDirectives.transferToInterpreterAndInvalidate();
157-
getName = insert(GetAttributeNode.create(__NAME__));
158-
}
159-
return getName;
160-
}
161-
162-
private GetAttributeNode ensureGetPathNode() {
163-
if (getPath == null) {
164-
CompilerDirectives.transferToInterpreterAndInvalidate();
165-
getPath = insert(GetAttributeNode.create(__FILE__));
166-
}
167-
return getPath;
168-
}
169-
170153
private PRaiseImportErrorNode ensureRaiseNode() {
171154
if (raiseNode == null) {
172155
CompilerDirectives.transferToInterpreterAndInvalidate();

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

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import com.oracle.graal.python.builtins.objects.str.PString;
4040
import com.oracle.graal.python.nodes.ErrorMessages;
4141
import com.oracle.graal.python.nodes.PRaiseNode;
42-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
43-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetAnyAttributeNode;
4442
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
4543
import com.oracle.graal.python.nodes.control.GetNextNode;
4644
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
@@ -69,7 +67,6 @@ public class ImportStarNode extends AbstractImportNode {
6967
@Child private GetItemNode getItemNode;
7068
@Child private PythonObjectLibrary pythonLibrary;
7169
@Child private CastToJavaStringNode castToStringNode;
72-
@Child private GetAnyAttributeNode readNode;
7370
@Child private GetNextNode nextNode;
7471
@Child private PRaiseNode raiseNode;
7572

@@ -97,14 +94,6 @@ private void writeAttribute(VirtualFrame frame, PythonObject globals, String nam
9794
}
9895
}
9996

100-
private Object readAttribute(VirtualFrame frame, Object object, String name) {
101-
if (readNode == null) {
102-
CompilerDirectives.transferToInterpreterAndInvalidate();
103-
readNode = insert(GetAttributeNode.GetAnyAttributeNode.create());
104-
}
105-
return readNode.executeObject(frame, object, name);
106-
}
107-
10897
public ImportStarNode(String moduleName, int level) {
10998
this.moduleName = moduleName;
11099
this.level = level;
@@ -131,22 +120,22 @@ public void executeVoid(VirtualFrame frame) {
131120
throw new IllegalStateException(e);
132121
}
133122
} else {
123+
PythonObjectLibrary pol = ensurePythonLibrary();
134124
try {
135-
Object attrAll = readAttribute(frame, importedModule, __ALL__);
125+
Object attrAll = pol.lookupAttributeStrict(importedModule, frame, __ALL__);
136126
int n = ensurePythonLibrary().lengthWithState(attrAll, PArguments.getThreadState(frame));
137127
for (int i = 0; i < n; i++) {
138128
Object attrName = ensureGetItemNode().executeWith(frame, attrAll, i);
139-
writeAttributeToLocals(frame, (PythonModule) importedModule, locals, attrName, true);
129+
writeAttributeToLocals(frame, pol, (PythonModule) importedModule, locals, attrName, true);
140130
}
141131
} catch (PException e) {
142132
e.expectAttributeError(ensureIsAttributeErrorProfile());
143133
assert importedModule instanceof PythonModule;
144-
PythonObjectLibrary pol = ensurePythonLibrary();
145134
Object keysIterator = pol.getIterator(pol.getDict(importedModule));
146135
while (true) {
147136
try {
148137
Object key = ensureGetNextNode().execute(frame, keysIterator);
149-
writeAttributeToLocals(frame, (PythonModule) importedModule, locals, key, false);
138+
writeAttributeToLocals(frame, pol, (PythonModule) importedModule, locals, key, false);
150139
} catch (PException iterException) {
151140
iterException.expectStopIteration(ensureIsStopIterationErrorProfile());
152141
break;
@@ -156,17 +145,17 @@ public void executeVoid(VirtualFrame frame) {
156145
}
157146
}
158147

159-
private void writeAttributeToLocals(VirtualFrame frame, PythonModule importedModule, PythonObject locals, Object attrName, boolean fromAll) {
148+
private void writeAttributeToLocals(VirtualFrame frame, PythonObjectLibrary pol, PythonModule importedModule, PythonObject locals, Object attrName, boolean fromAll) {
160149
try {
161150
String name = ensureCastToStringNode().execute(attrName);
162151
// skip attributes with leading '__' if there was no '__all__' attribute (see
163152
// 'ceval.c: import_all_from')
164153
if (fromAll || !PString.startsWith(name, "__")) {
165-
Object moduleAttr = readAttribute(frame, importedModule, name);
154+
Object moduleAttr = pol.lookupAttribute(importedModule, frame, name);
166155
writeAttribute(frame, locals, name, moduleAttr);
167156
}
168157
} catch (CannotCastException cce) {
169-
throw raise(PythonBuiltinClassType.TypeError, fromAll ? ErrorMessages.ITEM_IN_S_MUST_BE_STRING : ErrorMessages.KEY_IN_S_MUST_BE_STRING,
158+
throw raiseTypeError(fromAll ? ErrorMessages.ITEM_IN_S_MUST_BE_STRING : ErrorMessages.KEY_IN_S_MUST_BE_STRING,
170159
moduleName, fromAll ? __ALL__ : __DICT__, attrName);
171160
}
172161
}
@@ -211,12 +200,12 @@ private IsBuiltinClassProfile ensureIsStopIterationErrorProfile() {
211200
return isStopIterationProfile;
212201
}
213202

214-
private PException raise(PythonBuiltinClassType errType, String format, Object... args) {
203+
private PException raiseTypeError(String format, Object... args) {
215204
if (raiseNode == null) {
216205
CompilerDirectives.transferToInterpreterAndInvalidate();
217206
raiseNode = insert(PRaiseNode.create());
218207
}
219-
throw raiseNode.raise(errType, format, args);
208+
throw raiseNode.raise(PythonBuiltinClassType.TypeError, format, args);
220209
}
221210

222211
private GetNextNode ensureGetNextNode() {

0 commit comments

Comments
 (0)