Skip to content

Commit d852dbe

Browse files
committed
workaround for objects with ReflectionLibrary until we remove POL.getLazyPythonClass
1 parent cccf500 commit d852dbe

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
import com.oracle.truffle.api.dsl.Specialization;
116116
import com.oracle.truffle.api.dsl.TypeSystemReference;
117117
import com.oracle.truffle.api.frame.VirtualFrame;
118+
import com.oracle.truffle.api.interop.InteropLibrary;
118119
import com.oracle.truffle.api.interop.UnsupportedMessageException;
119120
import com.oracle.truffle.api.library.CachedLibrary;
120121
import com.oracle.truffle.api.profiles.BranchProfile;
@@ -2809,6 +2810,25 @@ static PythonNativeVoidPtr doL(PythonNativeVoidPtr self) {
28092810
@Builtin(name = SpecialMethodNames.__INDEX__, minNumOfPositionalArgs = 1)
28102811
@GenerateNodeFactory
28112812
abstract static class IndexNode extends IntNode {
2813+
// TODO: Remove again once GR-30482 is fixed
2814+
@Specialization(guards = "!isAnyPythonObject(object)", limit = "3")
2815+
static long doForeign(Object object,
2816+
@CachedLibrary("object") InteropLibrary lib) {
2817+
if (lib.isBoolean(object)) {
2818+
try {
2819+
return PInt.intValue(lib.asBoolean(object));
2820+
} catch (UnsupportedMessageException e) {
2821+
// fall through
2822+
}
2823+
} else if (lib.fitsInLong(object)) {
2824+
try {
2825+
return lib.asLong(object);
2826+
} catch (UnsupportedMessageException e) {
2827+
// fall through
2828+
}
2829+
}
2830+
throw CompilerDirectives.shouldNotReachHere("Must fix GR-30482");
2831+
}
28122832
}
28132833

28142834
@Builtin(name = SpecialMethodNames.__GETNEWARGS__, minNumOfPositionalArgs = 1)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyNumberIndexNode.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4848
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins;
4949
import com.oracle.graal.python.builtins.objects.PNone;
50+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
5051
import com.oracle.graal.python.builtins.objects.cext.PythonAbstractNativeObject;
5152
import com.oracle.graal.python.builtins.objects.ints.PInt;
5253
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
@@ -95,7 +96,7 @@ static long doLong(long object) {
9596
}
9697

9798
@Specialization(rewriteOn = UnexpectedResultException.class)
98-
int doCallIndexInt(VirtualFrame frame, Object object,
99+
int doCallIndexInt(VirtualFrame frame, PythonAbstractObject object,
99100
@Shared("callIndex") @Cached CallIndexNode callIndex,
100101
@Shared("lib") @CachedLibrary(limit = "3") PythonObjectLibrary lib,
101102
@Shared("isSubtype") @Cached IsSubtypeNode isSubtype) throws UnexpectedResultException {
@@ -117,14 +118,15 @@ int doCallIndexInt(VirtualFrame frame, Object object,
117118
}
118119
}
119120

121+
// TODO: Accept "Object" again once GR-30482 is fixed
120122
@Specialization(replaces = "doCallIndexInt")
121-
static Object doCallIndex(VirtualFrame frame, Object object,
123+
static Object doCallIndex(VirtualFrame frame, PythonAbstractObject object,
122124
@Shared("callIndex") @Cached CallIndexNode callIndex,
123125
@Shared("lib") @CachedLibrary(limit = "3") PythonObjectLibrary lib,
124126
@Shared("isSubtype") @Cached IsSubtypeNode isSubtype,
125-
@Cached IsBuiltinClassProfile isInt,
126-
@Cached PRaiseNode raiseNode,
127-
@Cached WarningsModuleBuiltins.WarnNode warnNode) {
127+
@Shared("isInt") @Cached IsBuiltinClassProfile isInt,
128+
@Shared("raiseNode") @Cached PRaiseNode raiseNode,
129+
@Shared("warnNode") @Cached WarningsModuleBuiltins.WarnNode warnNode) {
128130
if (isSubtype.execute(lib.getLazyPythonClass(object), PythonBuiltinClassType.PInt)) {
129131
return object;
130132
}
@@ -133,6 +135,20 @@ static Object doCallIndex(VirtualFrame frame, Object object,
133135
return result;
134136
}
135137

138+
// TODO: Remove again once GR-30482 is fixed
139+
@Specialization(guards = "!isAnyPythonObject(object)")
140+
static Object doCallIndexForeign(VirtualFrame frame, Object object,
141+
@Shared("callIndex") @Cached CallIndexNode callIndex,
142+
@Shared("lib") @CachedLibrary(limit = "3") PythonObjectLibrary lib,
143+
@Shared("isSubtype") @Cached IsSubtypeNode isSubtype,
144+
@Shared("isInt") @Cached IsBuiltinClassProfile isInt,
145+
@Shared("raiseNode") @Cached PRaiseNode raiseNode,
146+
@Shared("warnNode") @Cached WarningsModuleBuiltins.WarnNode warnNode) {
147+
Object result = callIndex.execute(frame, object);
148+
checkResult(frame, object, result, lib, isSubtype, isInt, raiseNode, warnNode);
149+
return result;
150+
}
151+
136152
private static void checkResult(VirtualFrame frame, Object originalObject, Object result, PythonObjectLibrary lib, IsSubtypeNode isSubtype, IsBuiltinClassProfile isInt, PRaiseNode raiseNode,
137153
WarningsModuleBuiltins.WarnNode warnNode) {
138154
if (!isInt.profileObject(result, PythonBuiltinClassType.PInt)) {

0 commit comments

Comments
 (0)