Skip to content

Commit 8f09174

Browse files
committed
make sure native objects that also implement __index__ but are not int subclasses also convert to asIndex
1 parent 9d94d81 commit 8f09174

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PythonAbstractNativeObject.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@
5858
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5959
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
6060
import com.oracle.graal.python.nodes.PRaiseNode;
61+
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
62+
import com.oracle.graal.python.nodes.call.CallNode;
6163
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
64+
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
6265
import com.oracle.truffle.api.Assumption;
6366
import com.oracle.truffle.api.CompilerAsserts;
6467
import com.oracle.truffle.api.CompilerDirectives;
@@ -77,6 +80,7 @@
7780
import com.oracle.truffle.api.library.ExportLibrary;
7881
import com.oracle.truffle.api.library.ExportMessage;
7982
import com.oracle.truffle.api.object.Shape;
83+
import com.oracle.truffle.api.profiles.ConditionProfile;
8084
import com.oracle.truffle.api.profiles.ValueProfile;
8185
import com.oracle.truffle.llvm.spi.ReferenceLibrary;
8286

@@ -160,18 +164,22 @@ public void setDict(PHashingCollection value) throws UnsupportedMessageException
160164
}
161165

162166
@ExportMessage
163-
public boolean canBeIndex(@Exclusive @Cached IsSubtypeNode isSubtypeNode) {
164-
return isSubtypeNode.execute(this, PythonBuiltinClassType.PInt);
165-
}
166-
167-
@ExportMessage
168-
public Object asIndexWithState(@SuppressWarnings("unused") ThreadState threadState,
167+
public Object asIndexWithState(ThreadState threadState,
168+
@Exclusive @Cached GetLazyClassNode getClass,
169169
@Exclusive @Cached IsSubtypeNode isSubtypeNode,
170-
@Exclusive @Cached PRaiseNode raise) {
171-
if (canBeIndex(isSubtypeNode)) {
172-
return this;
170+
// arguments for super-implementation call
171+
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
172+
@Exclusive @Cached PRaiseNode raise,
173+
@Exclusive @Cached CallNode callNode,
174+
@Exclusive @Cached IsSubtypeNode isSubtype,
175+
@Exclusive @Cached LookupInheritedAttributeNode.Dynamic lookupIndex,
176+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile noIndex,
177+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile resultProfile,
178+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile gotState) {
179+
if (isSubtypeNode.execute(getClass.execute(this), PythonBuiltinClassType.PInt)) {
180+
return this; // subclasses of 'int' should do early return
173181
} else {
174-
throw raise.raiseIntegerInterpretationError(this);
182+
return asIndexWithState(threadState, lib, raise, callNode, isSubtype, lookupIndex, noIndex, resultProfile, gotState);
175183
}
176184
}
177185

0 commit comments

Comments
 (0)