gh-111178: Fix function signatures to fix undefined behavior (#131191) · python/cpython@a577663
@@ -590,8 +590,9 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
590590591591592592static PyObject *
593-gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs)
593+gen_throw(PyObject *op, PyObject *const *args, Py_ssize_t nargs)
594594{
595+PyGenObject *gen = _PyGen_CAST(op);
595596PyObject *typ;
596597PyObject *tb = NULL;
597598PyObject *val = NULL;
@@ -821,8 +822,9 @@ static PyMemberDef gen_memberlist[] = {
821822};
822823823824static PyObject *
824-gen_sizeof(PyGenObject *gen, PyObject *Py_UNUSED(ignored))
825+gen_sizeof(PyObject *op, PyObject *Py_UNUSED(ignored))
825826{
827+PyGenObject *gen = _PyGen_CAST(op);
826828Py_ssize_t res;
827829res = offsetof(PyGenObject, gi_iframe) + offsetof(_PyInterpreterFrame, localsplus);
828830PyCodeObject *code = _PyGen_GetCode(gen);
@@ -837,7 +839,7 @@ static PyMethodDef gen_methods[] = {
837839 {"send", gen_send, METH_O, send_doc},
838840 {"throw", _PyCFunction_CAST(gen_throw), METH_FASTCALL, throw_doc},
839841 {"close", gen_close, METH_NOARGS, close_doc},
840- {"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
842+ {"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
841843 {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
842844 {NULL, NULL} /* Sentinel */
843845};
@@ -1197,7 +1199,7 @@ static PyMethodDef coro_methods[] = {
11971199 {"send", gen_send, METH_O, coro_send_doc},
11981200 {"throw",_PyCFunction_CAST(gen_throw), METH_FASTCALL, coro_throw_doc},
11991201 {"close", gen_close, METH_NOARGS, coro_close_doc},
1200- {"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
1202+ {"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
12011203 {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
12021204 {NULL, NULL} /* Sentinel */
12031205};
@@ -1288,7 +1290,7 @@ static PyObject *
12881290coro_wrapper_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
12891291{
12901292PyCoroWrapper *cw = _PyCoroWrapper_CAST(self);
1291-return gen_throw((PyGenObject *)cw->cw_coroutine, args, nargs);
1293+return gen_throw((PyObject*)cw->cw_coroutine, args, nargs);
12921294}
1293129512941296static PyObject *
@@ -1625,7 +1627,7 @@ static PyMethodDef async_gen_methods[] = {
16251627 {"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc},
16261628 {"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc},
16271629 {"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc},
1628- {"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
1630+ {"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
16291631 {"__class_getitem__", Py_GenericAlias,
16301632METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
16311633 {NULL, NULL} /* Sentinel */
@@ -1842,7 +1844,7 @@ async_gen_asend_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
18421844o->ags_gen->ag_running_async = 1;
18431845 }
184418461845-PyObject *result = gen_throw((PyGenObject*)o->ags_gen, args, nargs);
1847+PyObject *result = gen_throw((PyObject*)o->ags_gen, args, nargs);
18461848result = async_gen_unwrap_value(o->ags_gen, result);
1847184918481850if (result == NULL) {
@@ -2249,7 +2251,7 @@ async_gen_athrow_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
22492251o->agt_gen->ag_running_async = 1;
22502252 }
225122532252-PyObject *retval = gen_throw((PyGenObject*)o->agt_gen, args, nargs);
2254+PyObject *retval = gen_throw((PyObject*)o->agt_gen, args, nargs);
22532255if (o->agt_args) {
22542256retval = async_gen_unwrap_value(o->agt_gen, retval);
22552257if (retval == NULL) {