@@ -680,12 +680,6 @@ is not considered to be a reference to the type object, to save
680680complications in the deallocation function. (This is actually a
681681decision that's up to the implementer of each new type so if you want,
682682you can count such references to the type object.)
683-
684- *** WARNING*** The Py_DECREF macro must have a side-effect-free argument
685- since it may evaluate its argument multiple times. (The alternative
686- would be to mace it a proper function or assign it to a global temporary
687- variable first, both of which are slower; and in a multi-threaded
688- environment the global variable trick is not safe.)
689683*/
690684
691685/* First define a pile of simple helper macros, one set per special
@@ -764,15 +758,16 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
764758
765759#define Py_INCREF (op ) ( \
766760 _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
767- ((PyObject*)(op))->ob_refcnt++)
761+ ((PyObject *)(op))->ob_refcnt++)
768762
769763#define Py_DECREF (op ) \
770764 do { \
765+ PyObject *_py_decref_tmp = (PyObject *)(op); \
771766 if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \
772- --((PyObject*)(op)) ->ob_refcnt != 0) \
773- _Py_CHECK_REFCNT(op) \
767+ --(_py_decref_tmp) ->ob_refcnt != 0) \
768+ _Py_CHECK_REFCNT(_py_decref_tmp) \
774769 else \
775- _Py_Dealloc((PyObject *)(op)); \
770+ _Py_Dealloc(_py_decref_tmp); \
776771 } while (0)
777772
778773/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
@@ -811,16 +806,27 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
811806 */
812807#define Py_CLEAR (op ) \
813808 do { \
814- if (op) { \
815- PyObject *_py_tmp = (PyObject *)(op); \
809+ PyObject *_py_tmp = (PyObject *)(op); \
810+ if (_py_tmp != NULL) { \
816811 (op) = NULL; \
817812 Py_DECREF(_py_tmp); \
818813 } \
819814 } while (0)
820815
821816/* Macros to use in case the object pointer may be NULL: */
822- #define Py_XINCREF (op ) do { if ((op) == NULL) ; else Py_INCREF(op); } while (0)
823- #define Py_XDECREF (op ) do { if ((op) == NULL) ; else Py_DECREF(op); } while (0)
817+ #define Py_XINCREF (op ) \
818+ do { \
819+ PyObject *_py_xincref_tmp = (PyObject *)(op); \
820+ if (_py_xincref_tmp != NULL) \
821+ Py_INCREF(_py_xincref_tmp); \
822+ } while (0)
823+
824+ #define Py_XDECREF (op ) \
825+ do { \
826+ PyObject *_py_xdecref_tmp = (PyObject *)(op); \
827+ if (_py_xdecref_tmp != NULL) \
828+ Py_DECREF(_py_xdecref_tmp); \
829+ } while (0)
824830
825831/*
826832These are provided as conveniences to Python runtime embedders, so that
0 commit comments