Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF
in places where Py_DECREF was used.
diff --git a/Include/object.h b/Include/object.h
index 6481a5b..9fc0583 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -828,18 +828,28 @@
*
* As in case of Py_CLEAR "the obvious" code can be deadly:
*
- * Py_XDECREF(op);
+ * Py_DECREF(op);
* op = op2;
*
* The safe way is:
*
- * Py_XSETREF(op, op2);
+ * Py_SETREF(op, op2);
*
* That arranges to set `op` to `op2` _before_ decref'ing, so that any code
* triggered as a side-effect of `op` getting torn down no longer believes
* `op` points to a valid object.
+ *
+ * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
+ * Py_DECREF.
*/
+#define Py_SETREF(op, op2) \
+ do { \
+ PyObject *_py_tmp = (PyObject *)(op); \
+ (op) = (op2); \
+ Py_DECREF(_py_tmp); \
+ } while (0)
+
#define Py_XSETREF(op, op2) \
do { \
PyObject *_py_tmp = (PyObject *)(op); \