Skip to content

Commit 3048937

Browse files
committed
Fix incorrect generic case in IsNode
1 parent 0dbe780 commit 3048937

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

graalpython/com.oracle.graal.python.test/src/tests/seq_tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ def test_contains(self):
215215

216216
self.assertRaises(TypeError, u.__contains__)
217217

218+
# define function such that we use the same code in multiple calls
219+
def isin(l, item):
220+
return item in l
221+
# activate generic case (compares built-in type to object)
222+
assert not isin([object()], list)
223+
# use generic case to compare built-in types
224+
assert isin([type([])], list)
225+
218226
# TODO These test fails
219227
# def test_contains_fake(self):
220228
# class AllEq:

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/IsExpressionNode.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,15 @@ static boolean doPNoneObject(PNone left, Object right,
306306
}
307307

308308
// pstring (may be interned)
309-
@Specialization
309+
@Specialization(limit = "1")
310310
static boolean doPString(PString left, PString right,
311-
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
311+
@CachedLibrary("left") PythonObjectLibrary lib) {
312312
return lib.isSame(left, right);
313313
}
314314

315315
// everything else
316-
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
317-
static boolean doGeneric(Object left, Object right,
316+
@Specialization(guards = "isFallbackCase(left, right)", limit = "getCallSiteInlineCacheMaxDepth()")
317+
static boolean doOther(Object left, Object right,
318318
@CachedLibrary("left") PythonObjectLibrary lib) {
319319
if (left == right) {
320320
return true;
@@ -330,6 +330,42 @@ static boolean doGeneric(Object left, Object right,
330330
return false;
331331
}
332332

333+
private static boolean isPrimitive(Object object) {
334+
return object instanceof Boolean || object instanceof Integer || object instanceof Long || object instanceof Double;
335+
}
336+
337+
private static boolean isBuiltinClassCase(Object left, Object right) {
338+
return left instanceof PythonBuiltinClassType && right instanceof PythonBuiltinClass;
339+
}
340+
341+
static boolean isFallbackCase(Object left, Object right) {
342+
if (isPrimitive(left) && isPrimitive(right)) {
343+
return false;
344+
}
345+
if (left instanceof Boolean && right instanceof PInt || left instanceof PInt && right instanceof Boolean) {
346+
return false;
347+
}
348+
if (left instanceof Integer && right instanceof PInt || left instanceof PInt && right instanceof Integer) {
349+
return false;
350+
}
351+
if (left instanceof PythonAbstractNativeObject && right instanceof PythonAbstractNativeObject) {
352+
return false;
353+
}
354+
if (left instanceof PString && right instanceof PString) {
355+
return false;
356+
}
357+
if (isBuiltinClassCase(left, right) || isBuiltinClassCase(right, left)) {
358+
return false;
359+
}
360+
if (left instanceof PCode && right instanceof PCode) {
361+
return false;
362+
}
363+
if (left instanceof PNone || right instanceof PNone) {
364+
return false;
365+
}
366+
return true;
367+
}
368+
333369
public static IsNode create() {
334370
return IsNodeGen.create();
335371
}

0 commit comments

Comments
 (0)