gh-111178: fix UBSan failures for `Modules/_testmultiphase.c` (#131615) · python/cpython@4519179
@@ -28,6 +28,8 @@ typedef struct {
2828PyObject *x_attr; /* Attributes dictionary */
2929} ExampleObject;
303031+#define ExampleObject_CAST(op) ((ExampleObject *)(op))
32+3133typedef struct {
3234PyObject *integer;
3335} testmultiphase_state;
@@ -39,20 +41,22 @@ typedef struct {
3941/* Example methods */
40424143static int
42-Example_traverse(ExampleObject *self, visitproc visit, void *arg)
44+Example_traverse(PyObject *op, visitproc visit, void *arg)
4345{
46+ExampleObject *self = ExampleObject_CAST(op);
4447Py_VISIT(self->x_attr);
4548return 0;
4649}
47504851static void
49-Example_finalize(ExampleObject *self)
52+Example_finalize(PyObject *op)
5053{
54+ExampleObject *self = ExampleObject_CAST(op);
5155Py_CLEAR(self->x_attr);
5256}
53575458static PyObject *
55-Example_demo(ExampleObject *self, PyObject *args)
59+Example_demo(PyObject *op, PyObject *args)
5660{
5761PyObject *o = NULL;
5862if (!PyArg_ParseTuple(args, "|O:demo", &o))
@@ -66,14 +70,15 @@ Example_demo(ExampleObject *self, PyObject *args)
6670#include "clinic/_testmultiphase.c.h"
67716872static PyMethodDef Example_methods[] = {
69- {"demo", (PyCFunction)Example_demo, METH_VARARGS,
73+ {"demo", Example_demo, METH_VARARGS,
7074PyDoc_STR("demo() -> None")},
7175 {NULL, NULL} /* sentinel */
7276};
73777478static PyObject *
75-Example_getattro(ExampleObject *self, PyObject *name)
79+Example_getattro(PyObject *op, PyObject *name)
7680{
81+ExampleObject *self = ExampleObject_CAST(op);
7782if (self->x_attr != NULL) {
7883PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
7984if (v != NULL) {
@@ -87,8 +92,9 @@ Example_getattro(ExampleObject *self, PyObject *name)
8792}
88938994static int
90-Example_setattr(ExampleObject *self, const char *name, PyObject *v)
95+Example_setattr(PyObject *op, char *name, PyObject *v)
9196{
97+ExampleObject *self = ExampleObject_CAST(op);
9298if (self->x_attr == NULL) {
9399self->x_attr = PyDict_New();
94100if (self->x_attr == NULL)
@@ -212,7 +218,7 @@ PyDoc_STRVAR(_StateAccessType_decrement_count__doc__,
212218213219// Intentionally does not use Argument Clinic
214220static PyObject *
215-_StateAccessType_increment_count_noclinic(StateAccessTypeObject *self,
221+_StateAccessType_increment_count_noclinic(PyObject *self,
216222PyTypeObject *defining_class,
217223PyObject *const *args,
218224Py_ssize_t nargs,