[Python-Dev] Py_DECREF causes spurious gcc warning

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Wed Dec 17 20:14:27 EST 2003


> From: Skip Montanaro
> 
> #define Py_DECREF(op)                                   \
>         if (_Py_DEC_REFTOTAL  _Py_REF_DEBUG_COMMA       \
>             --(op)->ob_refcnt != 0) {                   \
>                 _Py_CHECK_REFCNT(op)                    \
>         } else {                                        \
>                 _Py_Dealloc((PyObject *)(op))           \
>         }
> 
> Py_INCREF would be left alone, and the X variants would become
> 
> #define Py_XINCREF(op) if ((op) == NULL) {;} else { Py_INCREF(op) }
> #define Py_XDECREF(op) if ((op) == NULL) {;} else {Py_DECREF(op) }

Then you will probably end up with different warnings - semicolons following closing braces, etc.

As Tim Peters said, the way to deal with that is to enclose the entire bit in a do ( ... } while (0) no-op (after the compiler optimises it away) but that - as he rightly pointed out - is pretty blecherous.

#define Py_XINCREF(op) do { if ((op) != NULL) { Py_INCREF(op) } } while (0)
#define Py_XDECREF(op) do { if ((op) != NULL) { Py_DECREF(op) } } while (0)

or

#define Py_XINCREF(op) do { if ((op) != NULL) Py_INCREF(op) } while (0)
#define Py_XDECREF(op) do { if ((op) != NULL) Py_DECREF(op) } while (0)

Tim Delaney



More information about the Python-Dev mailing list