Skip to content

Commit e44b1a1

Browse files
committed
if the number is already a PInt, we shouldn't call __index__
1 parent 592fd2d commit e44b1a1

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -710,22 +710,28 @@ public final boolean canBeIndex(@Exclusive @Cached HasInheritedAttributeNode.Dyn
710710

711711
@ExportMessage
712712
public int asIndexWithState(ThreadState state,
713+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile wasPInt,
713714
@Exclusive @Cached("createBinaryProfile()") ConditionProfile gotState,
714715
@Exclusive @Cached("createBinaryProfile()") ConditionProfile noIndex,
715716
@Exclusive @Cached LookupInheritedAttributeNode.Dynamic lookupIndex,
716717
@Exclusive @Cached BranchProfile overflow,
717718
@Exclusive @Cached PRaiseNode raise,
718719
@Exclusive @Cached CallNode callNode,
719720
@Exclusive @Cached CastToJavaLongNode castToLong) {
720-
Object indexAttr = lookupIndex.execute(this, __INDEX__);
721-
if (noIndex.profile(indexAttr == PNone.NO_VALUE)) {
722-
throw raise.raise(PythonBuiltinClassType.TypeError, "'%p' object cannot be interpreted as an integer", this);
723-
}
724721
Object result;
725-
if (gotState.profile(state == null)) {
726-
result = callNode.execute(indexAttr, this);
722+
if (wasPInt.profile(this instanceof PInt)) {
723+
// c.f. PyNumber_Index
724+
result = this;
727725
} else {
728-
result = callNode.execute(PArguments.frameForCall(state), indexAttr, this);
726+
Object indexAttr = lookupIndex.execute(this, __INDEX__);
727+
if (noIndex.profile(indexAttr == PNone.NO_VALUE)) {
728+
throw raise.raise(PythonBuiltinClassType.TypeError, "'%p' object cannot be interpreted as an integer", this);
729+
}
730+
if (gotState.profile(state == null)) {
731+
result = callNode.execute(indexAttr, this);
732+
} else {
733+
result = callNode.execute(PArguments.frameForCall(state), indexAttr, this);
734+
}
729735
}
730736
try {
731737
return PInt.intValueExact(castToLong.execute(result));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public boolean canBeIndex(Object receiver) {
233233
* Coerces the receiver into an index-sized integer, using the same
234234
* mechanism as {@code PyNumber_AsSsize_t}:
235235
* <ol>
236-
* <li> Call <code>__index__</code> (resp. <code>PyNumber_Index</code>) </li>
236+
* <li> Call <code>__index__</code> if the object is not already a Python int (resp. <code>PyNumber_Index</code>) </li>
237237
* <li> Do a hard cast to long as per <code>PyLong_AsSsize_t</code> </li>
238238
* </ol>
239239
* @return <code>-1</code> if the cast fails or overflows the <code>int</code> range

0 commit comments

Comments
 (0)