From afd077e8f9bc7f5c346d6963db13ab226860612b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 9 Nov 2022 16:43:48 +0100 Subject: [PATCH] Update Py_SETREF() to fix gh-98724 https://github.com/python/cpython/issues/98724 --- docs/changelog.rst | 2 ++ pythoncapi_compat.h | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index fda2bd4..f54d609 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,8 @@ Changelog ========= +* 2022-11-09: Fix ``Py_SETREF()`` and ``Py_XSETREF()`` macros + for `gh-98724 `_. * 2022-11-04: Add ``PyFrame_GetVar()`` and ``PyFrame_GetVarString()`` functions. * 2022-08-04: Add ``PyCode_GetVarnames()``, ``PyCode_GetFreevars()`` diff --git a/pythoncapi_compat.h b/pythoncapi_compat.h index e895070..d91a22e 100644 --- a/pythoncapi_compat.h +++ b/pythoncapi_compat.h @@ -90,18 +90,20 @@ _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) // Py_SETREF() and Py_XSETREF() were added to Python 3.5.2. // It is excluded from the limited C API. #if (PY_VERSION_HEX < 0x03050200 && !defined(Py_SETREF)) && !defined(Py_LIMITED_API) -#define Py_SETREF(op, op2) \ - do { \ - PyObject *_py_tmp = _PyObject_CAST(op); \ - (op) = (op2); \ - Py_DECREF(_py_tmp); \ +#define Py_SETREF(dst, src) \ + do { \ + PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \ + PyObject *_tmp_dst = (*_tmp_dst_ptr); \ + *_tmp_dst_ptr = _PyObject_CAST(src); \ + Py_DECREF(_tmp_dst); \ } while (0) -#define Py_XSETREF(op, op2) \ - do { \ - PyObject *_py_tmp = _PyObject_CAST(op); \ - (op) = (op2); \ - Py_XDECREF(_py_tmp); \ +#define Py_XSETREF(dst, src) \ + do { \ + PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \ + PyObject *_tmp_dst = (*_tmp_dst_ptr); \ + *_tmp_dst_ptr = _PyObject_CAST(src); \ + Py_XDECREF(_tmp_dst); \ } while (0) #endif