Skip to content

Commit c9ebebf

Browse files
committed
Refactoring of object.__ne__.
1 parent edd3ff5 commit c9ebebf

File tree

5 files changed

+93
-16
lines changed

5 files changed

+93
-16
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77

88
import sys
99

10+
class Empty:
11+
def __repr__(self):
12+
return '<Empty>'
13+
14+
class Cmp:
15+
def __init__(self,arg):
16+
self.arg = arg
17+
18+
def __repr__(self):
19+
return '<Cmp %s>' % self.arg
20+
21+
def __eq__(self, other):
22+
return self.arg == other
23+
1024
class First():
1125

1226
def __init__(self, value):
@@ -17,6 +31,28 @@ def __eq__(self, other):
1731

1832
class BasicComparisonTest(unittest.TestCase):
1933

34+
set1 = [2, 2.0, 2, 2+0j, Cmp(2.0)]
35+
set2 = [[1], (3,), None, Empty()]
36+
candidates = set1 + set2
37+
38+
def test_comparisons(self):
39+
for a in self.candidates:
40+
for b in self.candidates:
41+
if ((a in self.set1) and (b in self.set1)) or a is b:
42+
self.assertEqual(a, b)
43+
else:
44+
self.assertNotEqual(a, b)
45+
46+
def test_id_comparisons(self):
47+
# Ensure default comparison compares id() of args
48+
L = []
49+
for i in range(10):
50+
L.insert(len(L)//2, Empty())
51+
for a in L:
52+
for b in L:
53+
self.assertEqual(a == b, id(a) == id(b),
54+
'a=%r, b=%r' % (a, b))
55+
2056
def test_ne(self):
2157
x = First(1)
2258
y = First(1)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,10 @@ private TruffleObject getNativeFunction() {
879879
return isFunc;
880880
}
881881

882+
public static IsNode create() {
883+
return new CExtNodes.IsNode();
884+
}
885+
882886
}
883887

884888
public abstract static class AllToJavaNode extends PNodeWithContext {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/complex/ComplexBuiltins.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,41 @@ PNotImplemented doComplex(Object left, Object right) {
367367

368368
@GenerateNodeFactory
369369
@Builtin(name = __EQ__, fixedNumOfPositionalArgs = 2)
370+
@TypeSystemReference(PythonArithmeticTypes.class)
370371
static abstract class EqNode extends PythonBinaryBuiltinNode {
371372
@Specialization
372373
boolean doComplex(PComplex left, PComplex right) {
373374
return left.equals(right);
374375
}
375376

377+
@Specialization
378+
boolean doComplexInt(PComplex left, long right) {
379+
if (left.getImag() == 0) {
380+
return left.getReal() == right;
381+
}
382+
return false;
383+
}
384+
385+
@Specialization
386+
boolean doComplexInt(PComplex left, PInt right) {
387+
if (left.getImag() == 0) {
388+
try {
389+
return left.getReal() == right.getValue().longValueExact();
390+
} catch (ArithmeticException e) {
391+
// do nothing -> return false;
392+
}
393+
}
394+
return false;
395+
}
396+
397+
@Specialization
398+
boolean doComplexInt(PComplex left, double right) {
399+
if (left.getImag() == 0) {
400+
return left.getReal() == right;
401+
}
402+
return false;
403+
}
404+
376405
@SuppressWarnings("unused")
377406
@Fallback
378407
PNotImplemented doGeneric(Object left, Object right) {

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454
import com.oracle.graal.python.builtins.CoreFunctions;
5555
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5656
import com.oracle.graal.python.builtins.PythonBuiltins;
57-
import com.oracle.graal.python.builtins.modules.BuiltinConstructors;
5857
import com.oracle.graal.python.builtins.objects.PNone;
5958
import com.oracle.graal.python.builtins.objects.PNotImplemented;
59+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
6060
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
6161
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
6262
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
@@ -186,12 +186,14 @@ public int hash(Object self) {
186186
@GenerateNodeFactory
187187
public abstract static class EqNode extends PythonBinaryBuiltinNode {
188188
@Specialization
189-
public boolean eq(PythonNativeObject self, PythonNativeObject other) {
190-
return self.object.equals(other.object);
189+
@TruffleBoundary
190+
public boolean eq(PythonNativeObject self, PythonNativeObject other,
191+
@Cached("create()") CExtNodes.IsNode nativeIsNode) {
192+
return nativeIsNode.execute(self, other);
191193
}
192194

193-
@Specialization
194-
public boolean eq(Object self, Object other) {
195+
@Fallback
196+
public Object eq(Object self, Object other) {
195197
return self == other;
196198
}
197199
}
@@ -200,20 +202,30 @@ public boolean eq(Object self, Object other) {
200202
@GenerateNodeFactory
201203
public abstract static class NeNode extends PythonBinaryBuiltinNode {
202204

205+
@Child private LookupAndCallBinaryNode eqNode;
206+
@Child private CastToBooleanNode ifFalseNode;
207+
203208
@Specialization
204209
@TruffleBoundary
205-
public boolean ne(PythonNativeObject self, PythonNativeObject other) {
206-
return !self.object.equals(other.object);
210+
public boolean ne(PythonNativeObject self, PythonNativeObject other,
211+
@Cached("create()") CExtNodes.IsNode nativeIsNode) {
212+
return !nativeIsNode.execute(self, other);
207213
}
208214

209-
@Specialization
210-
public Object ne(Object self, Object other,
211-
@Cached("create(__EQ__)") LookupAndCallBinaryNode eqNode,
212-
@Cached("createIfFalseNode()") CastToBooleanNode ifFalseNode) {
215+
@Fallback
216+
public Object ne(Object self, Object other) {
217+
if (eqNode == null) {
218+
CompilerDirectives.transferToInterpreterAndInvalidate();
219+
eqNode = insert(LookupAndCallBinaryNode.create(__EQ__));
220+
}
213221
Object result = eqNode.executeObject(self, other);
214222
if (result == PNotImplemented.NOT_IMPLEMENTED) {
215223
return result;
216224
}
225+
if (ifFalseNode == null) {
226+
CompilerDirectives.transferToInterpreterAndInvalidate();
227+
ifFalseNode = insert(CastToBooleanNode.createIfFalseNode());
228+
}
217229
return ifFalseNode.executeWith(result);
218230
}
219231
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,9 @@ boolean doDD(double left, double right) {
163163
return left == right;
164164
}
165165

166-
protected static CExtNodes.IsNode getNativeIsNode() {
167-
return new CExtNodes.IsNode();
168-
}
169-
170166
@Specialization
171167
boolean doNative(PythonNativeObject left, PythonNativeObject right,
172-
@Cached("getNativeIsNode()") CExtNodes.IsNode isNode) {
168+
@Cached("create()") CExtNodes.IsNode isNode) {
173169
return isNode.execute(left, right);
174170
}
175171

0 commit comments

Comments
 (0)