bpo-36379: __ipow__ must be a ternaryfunc, not a binaryfunc by ZackerySpytz · Pull Request #13546 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_capi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crashes when attempting to use the *modulo* parameter when ``__ipow__``
is implemented in C.
26 changes: 26 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5486,6 +5486,27 @@ static PyTypeObject matmulType = {
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
Expand Down Expand Up @@ -5811,6 +5832,11 @@ PyInit__testcapi(void)
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;
Expand Down
2 changes: 1 addition & 1 deletion Objects/typeobject.c