Skip to content

Commit ec5b05c

Browse files
committed
simplify usages of PythonObject.getAttribute
1 parent 68aa438 commit ec5b05c

File tree

7 files changed

+21
-91
lines changed

7 files changed

+21
-91
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ImpModuleBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
import com.oracle.truffle.api.dsl.ImportStatic;
8787
import com.oracle.truffle.api.dsl.NodeFactory;
8888
import com.oracle.truffle.api.dsl.Specialization;
89-
import com.oracle.truffle.api.frame.VirtualFrame;
9089
import com.oracle.truffle.api.interop.ArityException;
9190
import com.oracle.truffle.api.interop.ForeignAccess;
9291
import com.oracle.truffle.api.interop.Message;
@@ -332,7 +331,8 @@ public int run(@SuppressWarnings("unused") Object noName) {
332331
public abstract static class CreateBuiltin extends PythonBuiltinNode {
333332
@SuppressWarnings("unused")
334333
@Specialization
335-
public Object run(VirtualFrame frame, PythonObject moduleSpec) {
334+
@TruffleBoundary
335+
public Object run(PythonObject moduleSpec) {
336336
Object origin = moduleSpec.getAttribute("origin");
337337
Object name = moduleSpec.getAttribute("name");
338338
if ("built-in".equals(origin)) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/PythonModule.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ public String getModuleName() {
5252
return name;
5353
}
5454

55-
@Override
56-
public PythonObject getValidStorageFullLookup(String attributeId) {
57-
if (isOwnAttribute(attributeId)) {
58-
return this;
59-
} else {
60-
return null;
61-
}
62-
}
63-
6455
@Override
6556
public String toString() {
6657
return "<module '" + this.getAttribute(__NAME__) + "'>";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/PythonObject.java

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.object;
2727

28-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.AttributeError;
29-
3028
import java.util.ArrayList;
3129
import java.util.HashMap;
3230
import java.util.List;
3331
import java.util.Map;
3432

35-
import com.oracle.graal.python.PythonLanguage;
3633
import com.oracle.graal.python.builtins.objects.PNone;
3734
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
3835
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
@@ -97,27 +94,9 @@ public final Location getOwnValidLocation(String attributeId) {
9794
}
9895
}
9996

100-
public PythonObject getValidStorageFullLookup(String attributeId) {
101-
PythonObject s = null;
102-
if (isOwnAttribute(attributeId)) {
103-
s = this;
104-
} else if (pythonClass != null) {
105-
s = pythonClass.getValidStorageFullLookup(attributeId);
106-
}
107-
return s;
108-
}
109-
11097
@TruffleBoundary
111-
public Object getAttribute(String name) {
112-
// Find the storage location
113-
Location storageLocation = getOwnValidLocation(name);
114-
115-
// Continue the look up in PythonType.
116-
if (storageLocation == null) {
117-
return pythonClass == null ? PNone.NO_VALUE : pythonClass.getAttribute(name);
118-
}
119-
120-
return storageLocation.get(getStorage());
98+
public final Object getAttribute(String name) {
99+
return getStorage().get(name, PNone.NO_VALUE);
121100
}
122101

123102
@TruffleBoundary
@@ -126,16 +105,6 @@ public void setAttribute(Object name, Object value) {
126105
getStorage().define(name, value);
127106
}
128107

129-
@TruffleBoundary
130-
public void deleteAttribute(String name) {
131-
// Find the storage location
132-
if (!getStorage().containsKey(name)) {
133-
throw PythonLanguage.getCore().raise(AttributeError, "%s object has no attribute %s", this, name);
134-
} else {
135-
getStorage().delete(name);
136-
}
137-
}
138-
139108
@TruffleBoundary
140109
public List<String> getAttributeNames() {
141110
ArrayList<String> keyList = new ArrayList<>();
@@ -194,7 +163,7 @@ public int compareTo(Object o) {
194163
*/
195164
@Override
196165
public String toString() {
197-
return "<" + pythonClass.getName() + " object at 0x" + Integer.toHexString(hashCode()) + ">";
166+
return "<" + getPythonClass().getName() + " object at 0x" + Integer.toHexString(hashCode()) + ">";
198167
}
199168

200169
/**

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonClass.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import com.oracle.graal.python.builtins.objects.PNone;
4141
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassNativeWrapper;
4242
import com.oracle.graal.python.builtins.objects.function.PFunction;
43-
import com.oracle.graal.python.builtins.objects.function.PythonCallable;
4443
import com.oracle.graal.python.builtins.objects.object.PythonObject;
4544
import com.oracle.truffle.api.Assumption;
4645
import com.oracle.truffle.api.CompilerAsserts;
@@ -258,30 +257,6 @@ PythonClass[] mergeMROs(MROMergeState[] toMerge, List<PythonClass> mro) {
258257
return mro.toArray(new PythonClass[mro.size()]);
259258
}
260259

261-
@Override
262-
public PythonObject getValidStorageFullLookup(String attributeId) {
263-
PythonObject store = null;
264-
265-
if (isOwnAttribute(attributeId)) {
266-
store = this;
267-
} else if (getBaseClasses().length > 0) {
268-
store = getBaseClasses()[0].getValidStorageFullLookup(attributeId);
269-
}
270-
271-
return store;
272-
}
273-
274-
public PythonCallable lookUpMethod(String methodName) {
275-
Object attr = getAttribute(methodName);
276-
assert attr != null;
277-
278-
if (attr instanceof PythonCallable) {
279-
return (PythonCallable) attr;
280-
}
281-
282-
return null;
283-
}
284-
285260
public void addMethod(PFunction method) {
286261
setAttribute(method.getName(), method);
287262
}
@@ -295,24 +270,6 @@ public void setAttribute(Object key, Object value) {
295270
super.setAttribute(key, value);
296271
}
297272

298-
@Override
299-
@TruffleBoundary
300-
public void deleteAttribute(String key) {
301-
invalidateAttributeInMROFinalAssumptions(key);
302-
super.deleteAttribute(key);
303-
}
304-
305-
@Override
306-
@TruffleBoundary
307-
public Object getAttribute(String name) {
308-
for (PythonClass o : methodResolutionOrder) {
309-
if (o.getStorage().containsKey(name)) {
310-
return o.getStorage().get(name);
311-
}
312-
}
313-
return PNone.NO_VALUE;
314-
}
315-
316273
/**
317274
* This method supports initialization and solves boot-order problems and should not normally be
318275
* used.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/LookupAttributeInMRONode.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,16 @@ protected static Object lookupSlow(PythonClass klass, Object key, ReadAttributeF
185185
}
186186
return PNone.NO_VALUE;
187187
}
188+
189+
public static Object lookupSlow(PythonClass klass, String key) {
190+
PythonClass[] mro = klass.getMethodResolutionOrder();
191+
for (int i = 0; i < mro.length; i++) {
192+
PythonClass kls = mro[i];
193+
Object value = kls.getAttribute(key);
194+
if (value != PNone.NO_VALUE) {
195+
return value;
196+
}
197+
}
198+
return PNone.NO_VALUE;
199+
}
188200
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupAndCallBinaryNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static LookupAndCallBinaryNode create(String name, String rname, Supplier
119119
}
120120

121121
protected Object getMethod(Object receiver, String methodName) {
122-
return GetClassNode.getItSlowPath(receiver).getAttribute(methodName);
122+
return LookupAttributeInMRONode.lookupSlow(GetClassNode.getItSlowPath(receiver), methodName);
123123
}
124124

125125
protected boolean isReversible() {
@@ -154,7 +154,7 @@ private UnexpectedResultException handleLeftURE(Object left, Object right, Unexp
154154

155155
protected PythonBinaryBuiltinNode getBuiltin(Object receiver) {
156156
assert receiver instanceof Boolean || receiver instanceof Integer || receiver instanceof Long || receiver instanceof Double || receiver instanceof String;
157-
Object attribute = GetClassNode.getItSlowPath(receiver).getAttribute(name);
157+
Object attribute = LookupAttributeInMRONode.lookupSlow(GetClassNode.getItSlowPath(receiver), name);
158158
if (attribute instanceof PBuiltinFunction) {
159159
PBuiltinFunction builtinFunction = (PBuiltinFunction) attribute;
160160
if (PythonBinaryBuiltinNode.class.isAssignableFrom(builtinFunction.getBuiltinNodeFactory().getNodeClass())) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupAndCallUnaryNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.graal.python.builtins.objects.PNone;
4646
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
4747
import com.oracle.graal.python.nodes.PNodeWithContext;
48+
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
4849
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
4950
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
5051
import com.oracle.graal.python.nodes.object.GetClassNode;
@@ -103,7 +104,7 @@ public static LookupAndCallUnaryNode create(String name, Supplier<NoAttributeHan
103104

104105
protected PythonUnaryBuiltinNode getBuiltin(Object receiver) {
105106
assert receiver instanceof Boolean || receiver instanceof Integer || receiver instanceof Long || receiver instanceof Double || receiver instanceof String || receiver instanceof PNone;
106-
Object attribute = GetClassNode.getItSlowPath(receiver).getAttribute(name);
107+
Object attribute = LookupAttributeInMRONode.lookupSlow(GetClassNode.getItSlowPath(receiver), name);
107108
if (attribute instanceof PBuiltinFunction) {
108109
PBuiltinFunction builtinFunction = (PBuiltinFunction) attribute;
109110
if (PythonUnaryBuiltinNode.class.isAssignableFrom(builtinFunction.getBuiltinNodeFactory().getNodeClass())) {

0 commit comments

Comments
 (0)