o @= m1
self.assertEqual(o, ("matmul", 42, m1))
+ def test_c_type_with_ipow(self):
+ # When the __ipow__ method of a type was implemented in C, using the
+ # modulo param would cause segfaults.
+ o = _testcapi.ipowType()
+ self.assertEqual(o.__ipow__(1), (1, None))
+ self.assertEqual(o.__ipow__(2, 2), (2, 2))
+
def test_return_null_without_error(self):
# Issue #23571: A function must not return NULL without setting an
# error
--- /dev/null
+Fix crashes when attempting to use the *modulo* parameter when ``__ipow__``
+is implemented in C.
PyObject_Del, /* tp_free */
};
+typedef struct {
+ PyObject_HEAD
+} ipowObject;
+
+static PyObject *
+ipowType_ipow(PyObject *self, PyObject *other, PyObject *mod)
+{
+ return Py_BuildValue("OO", other, mod);
+}
+
+static PyNumberMethods ipowType_as_number = {
+ .nb_inplace_power = ipowType_ipow
+};
+
+static PyTypeObject ipowType = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "ipowType",
+ .tp_basicsize = sizeof(ipowObject),
+ .tp_as_number = &ipowType_as_number,
+ .tp_new = PyType_GenericNew
+};
typedef struct {
PyObject_HEAD
return NULL;
Py_INCREF(&matmulType);
PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType);
+ if (PyType_Ready(&ipowType) < 0) {
+ return NULL;
+ }
+ Py_INCREF(&ipowType);
+ PyModule_AddObject(m, "ipowType", (PyObject *)&ipowType);
if (PyType_Ready(&awaitType) < 0)
return NULL;
IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
wrap_binaryfunc, "%="),
IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
- wrap_binaryfunc, "**="),
+ wrap_ternaryfunc, "**="),
IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
wrap_binaryfunc, "<<="),
IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,