Skip to content

Commit 914968f

Browse files
committed
always call both directions for comparison operations
1 parent 70adc8a commit 914968f

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_binop.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*graalpython.lib-python.3.test.test_binop.FallbackBlockingTests.test_fallback_ne_blocking
2+
*graalpython.lib-python.3.test.test_binop.OperationOrderTests.test_comparison_orders
23
*graalpython.lib-python.3.test.test_binop.RatTestCase.test_add
34
*graalpython.lib-python.3.test.test_binop.RatTestCase.test_constructor
45
*graalpython.lib-python.3.test.test_binop.RatTestCase.test_div

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupAndCallBinaryNode.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public abstract static class NotImplementedHandler extends PNodeWithContext {
7474
protected final String name;
7575
protected final String rname;
7676
protected final Supplier<NotImplementedHandler> handlerFactory;
77+
private final boolean alwaysCheckReverse;
7778

7879
@Child private CallBinaryMethodNode dispatchNode;
7980
@Child private CallBinaryMethodNode reverseDispatchNode;
@@ -103,27 +104,32 @@ public abstract static class NotImplementedHandler extends PNodeWithContext {
103104

104105
public abstract Object executeObject(VirtualFrame frame, Object arg, Object arg2);
105106

106-
LookupAndCallBinaryNode(String name, String rname, Supplier<NotImplementedHandler> handlerFactory) {
107+
LookupAndCallBinaryNode(String name, String rname, Supplier<NotImplementedHandler> handlerFactory, boolean alwaysCheckReverse) {
107108
this.name = name;
108109
this.rname = rname;
109110
this.handlerFactory = handlerFactory;
111+
this.alwaysCheckReverse = alwaysCheckReverse;
110112
}
111113

112114
public static LookupAndCallBinaryNode create(String name) {
113-
return LookupAndCallBinaryNodeGen.create(name, null, null);
115+
return LookupAndCallBinaryNodeGen.create(name, null, null, false);
114116
}
115117

116118
public static LookupAndCallBinaryNode createReversible(String name, String reverseName, Supplier<NotImplementedHandler> handlerFactory) {
117119
assert name.startsWith("__") && reverseName.startsWith("__r");
118-
return LookupAndCallBinaryNodeGen.create(name, reverseName, handlerFactory);
120+
return LookupAndCallBinaryNodeGen.create(name, reverseName, handlerFactory, false);
119121
}
120122

121123
public static LookupAndCallBinaryNode create(String name, String rname) {
122-
return LookupAndCallBinaryNodeGen.create(name, rname, null);
124+
return LookupAndCallBinaryNodeGen.create(name, rname, null, false);
125+
}
126+
127+
public static LookupAndCallBinaryNode create(String name, String rname, boolean alwaysCheckReverse) {
128+
return LookupAndCallBinaryNodeGen.create(name, rname, null, alwaysCheckReverse);
123129
}
124130

125131
public static LookupAndCallBinaryNode create(String name, String rname, Supplier<NotImplementedHandler> handlerFactory) {
126-
return LookupAndCallBinaryNodeGen.create(name, rname, handlerFactory);
132+
return LookupAndCallBinaryNodeGen.create(name, rname, handlerFactory, false);
127133
}
128134

129135
protected Object getMethod(Object receiver, String methodName) {
@@ -316,7 +322,7 @@ Object callObject(VirtualFrame frame, Object left, Object right,
316322
Object leftCallable = getattr.execute(leftClass);
317323
Object rightClass = libRight.getLazyPythonClass(right);
318324
Object rightCallable = getattrR.execute(rightClass);
319-
if (leftCallable == rightCallable) {
325+
if (!alwaysCheckReverse && leftCallable == rightCallable) {
320326
rightCallable = PNone.NO_VALUE;
321327
}
322328
if (leftCallable != PNone.NO_VALUE) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public abstract class BinaryComparisonNode extends BinaryOpNode {
5656
this.magicMethod = magicMethod;
5757
this.magicReverseMethod = magicReverseMethod;
5858
this.operation = operation;
59-
this.callNode = LookupAndCallBinaryNode.create(magicMethod, magicReverseMethod);
59+
// see cpython://Objects/object.c#do_richcompare - CPython always calls the reverse method
60+
this.callNode = LookupAndCallBinaryNode.create(magicMethod, magicReverseMethod, true);
6061
}
6162

6263
public static BinaryComparisonNode create(String magicMethod, String magicReverseMethod, String operation, ExpressionNode left, ExpressionNode right) {

0 commit comments

Comments
 (0)