Skip to content

Commit 09a8832

Browse files
committed
[GR-70723] Fix passing NULL modulus from 2-argument pow
PullRequest: graalpython/4050
2 parents 06d253f + c65afad commit 09a8832

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,10 @@ def test_nb_slot_calls():
10761076
}
10771077
#define proxy_nb_ternary_slot(slot) \
10781078
static PyObject* proxy_##slot(PyObject *a, PyObject *b, PyObject* c) { \
1079+
if (c == NULL) { \
1080+
PyErr_SetString(PyExc_AssertionError, "modulus in NULL"); \
1081+
return NULL; \
1082+
} \
10791083
if (Py_TYPE(a) == &NativeNbSlotProxyType) { \
10801084
PyObject* delegate = get_delegate(a); \
10811085
return Py_TYPE(delegate)->tp_as_number->slot(delegate, b, c); \
@@ -1089,6 +1093,10 @@ def test_nb_slot_calls():
10891093
}
10901094
#define proxy_nb_ternary_inplace_slot(slot) \
10911095
static PyObject* proxy_##slot(PyObject *self, PyObject *b, PyObject* c) { \
1096+
if (c == NULL) { \
1097+
PyErr_SetString(PyExc_AssertionError, "modulus in NULL"); \
1098+
return NULL; \
1099+
} \
10921100
PyObject* delegate = get_delegate(self); \
10931101
PyObject* result = Py_TYPE(delegate)->tp_as_number->slot(delegate, b, c); \
10941102
if (!result) \
@@ -1176,6 +1184,8 @@ def __rmatmul__(self, other):
11761184
assert divmod(2, obj) == (0, 2)
11771185
assert obj ** 2 == 9
11781186
assert 2 ** obj == 8
1187+
assert pow(obj, 2) == 9
1188+
assert pow(2, obj) == 8
11791189
assert pow(obj, 2, 2) == 1
11801190
if isinstance(obj.delegate, int): # pow doesn't call __rpow__
11811191
assert pow(2, obj, 2) == 0

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
238238
import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
239239
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
240+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode;
240241
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
241242
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
242243
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
@@ -2029,14 +2030,20 @@ private int getDebuggerSessionCount() {
20292030
}
20302031

20312032
@Builtin(name = J_POW, minNumOfPositionalArgs = 2, numOfPositionalOnlyArgs = 0, parameterNames = {"base", "exp", "mod"})
2033+
@ArgumentClinic(name = "mod", defaultValue = "PNone.NONE")
20322034
@GenerateNodeFactory
2033-
public abstract static class PowNode extends PythonTernaryBuiltinNode {
2035+
public abstract static class PowNode extends PythonTernaryClinicBuiltinNode {
20342036

20352037
@Specialization
20362038
Object ternary(VirtualFrame frame, Object x, Object y, Object z,
20372039
@Cached PyNumberPowerNode power) {
20382040
return power.execute(frame, x, y, z);
20392041
}
2042+
2043+
@Override
2044+
protected ArgumentClinicProvider getArgumentClinic() {
2045+
return BuiltinFunctionsClinicProviders.PowNodeClinicProviderGen.INSTANCE;
2046+
}
20402047
}
20412048

20422049
// sum(iterable[, start])

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyNumberPowerNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public static Object doIt(VirtualFrame frame, Object v, Object w, Object z,
8181
@Cached GetCachedTpSlotsNode getWSlots,
8282
@Cached CallTernaryOpNode callTernaryOpNode,
8383
@Cached PRaiseNode raiseNode) {
84+
assert z != PNone.NO_VALUE;
8485
Object classV = getVClass.execute(inliningTarget, v);
8586
Object classW = getWClass.execute(inliningTarget, w);
8687
TpSlot slotV = getVSlots.execute(inliningTarget, classV).nb_power();

0 commit comments

Comments
 (0)