Skip to content

Commit 2e3e3a7

Browse files
committed
[GR-31098] [GR-31128] [GR-31129] Various small fixes for Pandas.
PullRequest: graalpython/1779
2 parents a0a6d68 + 3048937 commit 2e3e3a7

File tree

6 files changed

+139
-60
lines changed

6 files changed

+139
-60
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_long.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ def compile_module(self, name):
175175
(0,),
176176
(-1,),
177177
(-2,),
178+
(True,),
179+
(False,),
178180
(0x7fffffff,),
179181
(0xffffffff,),
180182
# we could use larger values on 64-bit systems but how should we know?

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/builtins/modules/PythonCextBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ long doPrimitiveNativeWrapperToLong(VirtualFrame frame, Object object, int mode,
11771177
try {
11781178
if (resolvedPointer instanceof PrimitiveNativeWrapper) {
11791179
PrimitiveNativeWrapper wrapper = (PrimitiveNativeWrapper) resolvedPointer;
1180-
if (requiredPInt(mode) && !wrapper.isIntLike()) {
1180+
if (requiredPInt(mode) && !wrapper.isSubtypeOfInt()) {
11811181
throw raise(TypeError, ErrorMessages.INTEGER_REQUIRED);
11821182
}
11831183
return ensureConvertPIntToPrimitiveNode().executeLong(wrapper, signed(mode), PInt.intValueExact(targetTypeSize), exact(mode));
@@ -1198,7 +1198,7 @@ Object doGeneric(VirtualFrame frame, Object objectPtr, int mode, long targetType
11981198
try {
11991199
if (resolvedPointer instanceof PrimitiveNativeWrapper) {
12001200
PrimitiveNativeWrapper wrapper = (PrimitiveNativeWrapper) resolvedPointer;
1201-
if (requiredPInt(mode) && !wrapper.isIntLike()) {
1201+
if (requiredPInt(mode) && !wrapper.isSubtypeOfInt()) {
12021202
throw raise(TypeError, ErrorMessages.INTEGER_REQUIRED);
12031203
}
12041204
return ensureConvertPIntToPrimitiveNode().execute(wrapper, signed(mode), PInt.intValueExact(targetTypeSize), exact(mode));

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,10 @@ public boolean isIntLike() {
17151715
return (state & (PRIMITIVE_STATE_BYTE | PRIMITIVE_STATE_INT | PRIMITIVE_STATE_LONG)) != 0;
17161716
}
17171717

1718+
public boolean isSubtypeOfInt() {
1719+
return !isDouble();
1720+
}
1721+
17181722
// this method exists just for readability
17191723
public Object getMaterializedObject(PythonNativeWrapperLibrary lib) {
17201724
return lib.getDelegate(this);

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

Lines changed: 83 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -278,71 +278,95 @@ public static long hash(double receiver) {
278278
return DefaultPythonDoubleExports.hash(receiver);
279279
}
280280

281-
private static class DefaultNodes extends Node {
282-
private static final byte REVERSE_COMP = 0b001;
283-
private static final byte LEFT_COMPARE = 0b010;
284-
private static final byte SUBT_COMPARE = 0b100;
285-
286-
@Child private IsSubtypeNode isSubtype;
287-
@Child private IsSameTypeNode isSameType;
288-
@Child private GetClassNode getClassNode;
289-
@Child private PRaiseNode raiseNode;
290-
@CompilationFinal byte state = 0;
291-
292-
protected IsSubtypeNode getIsSubtypeNode() {
293-
if (isSubtype == null) {
294-
CompilerDirectives.transferToInterpreterAndInvalidate();
295-
reportPolymorphicSpecialize();
296-
isSubtype = insert(IsSubtypeNode.create());
281+
private abstract static class DefaultNodes extends Node {
282+
283+
protected abstract IsSubtypeNode getIsSubtypeNode();
284+
285+
protected abstract IsSameTypeNode getIsSameTypeNode();
286+
287+
protected abstract GetClassNode getGetClassNode();
288+
289+
protected abstract PRaiseNode getRaiseNode();
290+
291+
protected abstract void enterReverseCompare();
292+
293+
protected abstract void enterLeftCompare();
294+
295+
protected abstract void enterSubtypeCompare();
296+
297+
private static final class CachedDefaultNodes extends DefaultNodes {
298+
private static final byte REVERSE_COMP = 0b001;
299+
private static final byte LEFT_COMPARE = 0b010;
300+
private static final byte SUBT_COMPARE = 0b100;
301+
302+
@Child private IsSubtypeNode isSubtype;
303+
@Child private IsSameTypeNode isSameType;
304+
@Child private GetClassNode getClassNode;
305+
@Child private PRaiseNode raiseNode;
306+
@CompilationFinal byte state = 0;
307+
308+
@Override
309+
protected IsSubtypeNode getIsSubtypeNode() {
310+
if (isSubtype == null) {
311+
CompilerDirectives.transferToInterpreterAndInvalidate();
312+
reportPolymorphicSpecialize();
313+
isSubtype = insert(IsSubtypeNode.create());
314+
}
315+
return isSubtype;
297316
}
298-
return isSubtype;
299-
}
300317

301-
protected IsSameTypeNode getIsSameTypeNode() {
302-
if (isSameType == null) {
303-
CompilerDirectives.transferToInterpreterAndInvalidate();
304-
isSameType = insert(IsSameTypeNodeGen.create());
318+
@Override
319+
protected IsSameTypeNode getIsSameTypeNode() {
320+
if (isSameType == null) {
321+
CompilerDirectives.transferToInterpreterAndInvalidate();
322+
isSameType = insert(IsSameTypeNodeGen.create());
323+
}
324+
return isSameType;
305325
}
306-
return isSameType;
307-
}
308326

309-
private GetClassNode getGetClassNode() {
310-
if (getClassNode == null) {
311-
CompilerDirectives.transferToInterpreterAndInvalidate();
312-
getClassNode = insert(GetClassNode.create());
327+
@Override
328+
protected GetClassNode getGetClassNode() {
329+
if (getClassNode == null) {
330+
CompilerDirectives.transferToInterpreterAndInvalidate();
331+
getClassNode = insert(GetClassNode.create());
332+
}
333+
return getClassNode;
313334
}
314-
return getClassNode;
315-
}
316335

317-
protected PRaiseNode getRaiseNode() {
318-
if (raiseNode == null) {
319-
CompilerDirectives.transferToInterpreterAndInvalidate();
320-
raiseNode = insert(PRaiseNode.create());
336+
@Override
337+
protected PRaiseNode getRaiseNode() {
338+
if (raiseNode == null) {
339+
CompilerDirectives.transferToInterpreterAndInvalidate();
340+
raiseNode = insert(PRaiseNode.create());
341+
}
342+
return raiseNode;
321343
}
322-
return raiseNode;
323-
}
324344

325-
protected void enterReverseCompare() {
326-
if ((state & REVERSE_COMP) == 0) {
327-
CompilerDirectives.transferToInterpreterAndInvalidate();
328-
reportPolymorphicSpecialize();
329-
state |= REVERSE_COMP;
345+
@Override
346+
protected void enterReverseCompare() {
347+
if ((state & REVERSE_COMP) == 0) {
348+
CompilerDirectives.transferToInterpreterAndInvalidate();
349+
reportPolymorphicSpecialize();
350+
state |= REVERSE_COMP;
351+
}
330352
}
331-
}
332353

333-
protected void enterLeftCompare() {
334-
if ((state & LEFT_COMPARE) == 0) {
335-
CompilerDirectives.transferToInterpreterAndInvalidate();
336-
reportPolymorphicSpecialize();
337-
state |= LEFT_COMPARE;
354+
@Override
355+
protected void enterLeftCompare() {
356+
if ((state & LEFT_COMPARE) == 0) {
357+
CompilerDirectives.transferToInterpreterAndInvalidate();
358+
reportPolymorphicSpecialize();
359+
state |= LEFT_COMPARE;
360+
}
338361
}
339-
}
340362

341-
protected void enterSubtypeCompare() {
342-
if ((state & SUBT_COMPARE) == 0) {
343-
CompilerDirectives.transferToInterpreterAndInvalidate();
344-
reportPolymorphicSpecialize();
345-
state |= SUBT_COMPARE;
363+
@Override
364+
protected void enterSubtypeCompare() {
365+
if ((state & SUBT_COMPARE) == 0) {
366+
CompilerDirectives.transferToInterpreterAndInvalidate();
367+
reportPolymorphicSpecialize();
368+
state |= SUBT_COMPARE;
369+
}
346370
}
347371
}
348372

@@ -359,6 +383,11 @@ protected IsSameTypeNode getIsSameTypeNode() {
359383
return IsSameTypeNodeGen.getUncached();
360384
}
361385

386+
@Override
387+
protected GetClassNode getGetClassNode() {
388+
return GetClassNode.getUncached();
389+
}
390+
362391
@Override
363392
protected PRaiseNode getRaiseNode() {
364393
return PRaiseNode.getUncached();
@@ -378,7 +407,7 @@ protected void enterSubtypeCompare() {
378407
}
379408

380409
private static DefaultNodes create() {
381-
return new DefaultNodes();
410+
return new CachedDefaultNodes();
382411
}
383412

384413
private static DefaultNodes getUncached() {

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)