gh-111178: fix UBSan failures for `anextawaitableobject` by picnixz · Pull Request #131609 · python/cpython
#define anextawaitableobject_CAST(op) ((anextawaitableobject *)(op))
static void anextawaitable_dealloc(anextawaitableobject *obj) anextawaitable_dealloc(PyObject *op) { anextawaitableobject *obj = anextawaitableobject_CAST(op); _PyObject_GC_UNTRACK(obj); Py_XDECREF(obj->wrapped); Py_XDECREF(obj->default_value); PyObject_GC_Del(obj); }
static int anextawaitable_traverse(anextawaitableobject *obj, visitproc visit, void *arg) anextawaitable_traverse(PyObject *op, visitproc visit, void *arg) { anextawaitableobject *obj = anextawaitableobject_CAST(op); Py_VISIT(obj->wrapped); Py_VISIT(obj->default_value); return 0;
static PyObject * anextawaitable_iternext(anextawaitableobject *obj) anextawaitable_iternext(PyObject *op) { /* Consider the following class: *
static PyObject * anextawaitable_proxy(anextawaitableobject *obj, char *meth, PyObject *arg) { anextawaitable_proxy(anextawaitableobject *obj, char *meth, PyObject *arg) { PyObject *awaitable = anextawaitable_getiter(obj); if (awaitable == NULL) { return NULL; } // 'arg' may be a tuple (if coming from a METH_VARARGS method) // or a single object (if coming from a METH_O method). PyObject *ret = PyObject_CallMethod(awaitable, meth, "O", arg); Py_DECREF(awaitable); if (ret != NULL) {
static PyObject * anextawaitable_send(anextawaitableobject *obj, PyObject *arg) { anextawaitable_send(PyObject *op, PyObject *arg) { anextawaitableobject *obj = anextawaitableobject_CAST(op); return anextawaitable_proxy(obj, "send", arg); }
static PyObject * anextawaitable_throw(anextawaitableobject *obj, PyObject *arg) { return anextawaitable_proxy(obj, "throw", arg); anextawaitable_throw(PyObject *op, PyObject *args) { anextawaitableobject *obj = anextawaitableobject_CAST(op); return anextawaitable_proxy(obj, "throw", args); }
static PyObject * anextawaitable_close(anextawaitableobject *obj, PyObject *arg) { return anextawaitable_proxy(obj, "close", arg); anextawaitable_close(PyObject *op, PyObject *args) { anextawaitableobject *obj = anextawaitableobject_CAST(op); return anextawaitable_proxy(obj, "close", args); }
static PyMethodDef anextawaitable_methods[] = { {"send",(PyCFunction)anextawaitable_send, METH_O, send_doc}, {"throw",(PyCFunction)anextawaitable_throw, METH_VARARGS, throw_doc}, {"close",(PyCFunction)anextawaitable_close, METH_VARARGS, close_doc}, {"send", anextawaitable_send, METH_O, send_doc}, {"throw", anextawaitable_throw, METH_VARARGS, throw_doc}, {"close", anextawaitable_close, METH_VARARGS, close_doc}, {NULL, NULL} /* Sentinel */ };