Skip to content

Commit 0302498

Browse files
committed
[GR-50783] CallTernaryMethodNode should reverse the arguments for reverse builtins.
PullRequest: graalpython/3098
2 parents 7279f9c + 8d54bb4 commit 0302498

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,26 @@ def __pow__(self, power):
326326
from pow_tests import test_pow
327327
test_pow()
328328

329+
# Needs to run multiple times to invoke the cached node too
330+
def call_rpow(x):
331+
return x.__rpow__(2, 10)
332+
333+
assert call_rpow(3) == 8
334+
assert call_rpow(3) == 8
335+
336+
class MyRPow:
337+
def __pow__(self, other, modulus=None):
338+
return (2 ** other) % modulus if modulus else 2 ** other
339+
def __rpow__(self, other, modulus=None):
340+
return (3 ** other) % modulus if modulus else 3 ** other
341+
342+
assert MyRPow() ** 2 == 4
343+
assert pow(MyRPow(), 2, 3) == 1
344+
345+
assert 2 ** MyRPow() == 9
346+
# This works on GraalPy, but not on CPython: feature or a bug?
347+
# assert_exception(lambda: pow(2, MyRPow(), 5), TypeError)
348+
329349
def test_slot1binfull():
330350
class A:
331351
def __add__(self, other):

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/PDecoratedMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
/**
5353
* Storage for classmethods, staticmethods and instancemethods
5454
*/
55-
public class PDecoratedMethod extends PythonBuiltinObject implements BoundBuiltinCallable<Object> {
55+
public final class PDecoratedMethod extends PythonBuiltinObject implements BoundBuiltinCallable<Object> {
5656
private Object callable;
5757

5858
public PDecoratedMethod(Object cls, Shape instanceShape) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static Object doBuiltinFunctionCachedReverse(VirtualFrame frame, @SuppressWarnin
111111
@SuppressWarnings("unused") @Cached("func") PBuiltinFunction cachedFunc,
112112
@SuppressWarnings("unused") @Cached("isForReverseBinaryOperation(func.getCallTarget())") boolean isReverse,
113113
@Cached("getBuiltin(frame, func, 3)") PythonBuiltinBaseNode builtinNode) {
114-
return callTernaryBuiltin(frame, builtinNode, arg1, arg2, arg3);
114+
return callTernaryBuiltin(frame, builtinNode, arg2, arg1, arg3);
115115
}
116116

117117
@Specialization(guards = {"func.getCallTarget() == ct", "builtinNode != null", "!isReverse"}, //
@@ -129,7 +129,7 @@ static Object doBuiltinFunctionCtCachedReverse(VirtualFrame frame, @SuppressWarn
129129
@SuppressWarnings("unused") @Cached("func.getCallTarget()") RootCallTarget ct,
130130
@SuppressWarnings("unused") @Cached("isForReverseBinaryOperation(func.getCallTarget())") boolean isReverse,
131131
@Cached("getBuiltin(frame, func, 3)") PythonBuiltinBaseNode builtinNode) {
132-
return callTernaryBuiltin(frame, builtinNode, arg1, arg2, arg3);
132+
return callTernaryBuiltin(frame, builtinNode, arg2, arg1, arg3);
133133
}
134134

135135
@Specialization(guards = {"isSingleContext()", "func == cachedFunc", "builtinNode != null", "!takesSelfArg"}, limit = "getCallSiteInlineCacheMaxDepth()")

0 commit comments

Comments
 (0)