diff -r a62e9717e45c Include/object.h --- a/Include/object.h Wed Jun 11 20:37:52 2008 +0200 +++ b/Include/object.h Wed Jun 11 21:13:04 2008 +0200 @@ -708,6 +708,20 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyO #define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op) #define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op) +/* Safely change an lvalue (e.g. a variable or struct member) from a + * PyObject * to another. + * When you are sure both the old and the new value are non-NULL, + * use Py_SETREF(). Otherwise, use Py_XSETREF(). + */ + +#define Py_SETREF(lvalue, rvalue) \ + { PyObject *old = (lvalue), *new = (rvalue); Py_INCREF(new); \ + (lvalue) = (new); Py_DECREF(old); } + +#define Py_XSETREF(lvalue, rvalue) \ + { PyObject *old = (lvalue), *new = (rvalue); Py_XINCREF(new); \ + (lvalue) = (new); Py_XDECREF(old); } + /* These are provided as conveniences to Python runtime embedders, so that they can have object code that is not dependent on Python compilation flags. diff -r a62e9717e45c Modules/_lsprof.c --- a/Modules/_lsprof.c Wed Jun 11 20:37:52 2008 +0200 +++ b/Modules/_lsprof.c Wed Jun 11 21:13:04 2008 +0200 @@ -762,7 +762,6 @@ static int static int profiler_init(ProfilerObject *pObj, PyObject *args, PyObject *kw) { - PyObject *o; PyObject *timer = NULL; double timeunit = 0.0; int subcalls = 1; @@ -781,10 +780,7 @@ profiler_init(ProfilerObject *pObj, PyOb if (setSubcalls(pObj, subcalls) < 0 || setBuiltins(pObj, builtins) < 0) return -1; - o = pObj->externalTimer; - pObj->externalTimer = timer; - Py_XINCREF(timer); - Py_XDECREF(o); + Py_XSETREF(pObj->externalTimer, timer); pObj->externalTimerUnit = timeunit; return 0; } diff -r a62e9717e45c Modules/_threadmodule.c --- a/Modules/_threadmodule.c Wed Jun 11 20:37:52 2008 +0200 +++ b/Modules/_threadmodule.c Wed Jun 11 21:13:04 2008 +0200 @@ -278,9 +278,7 @@ _ldict(localobject *self) return NULL; } - Py_CLEAR(self->dict); - Py_INCREF(ldict); - self->dict = ldict; /* still borrowed */ + Py_XSETREF(self->dict, ldict); /* still borrowed */ if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init && Py_TYPE(self)->tp_init((PyObject*)self, @@ -294,9 +292,7 @@ _ldict(localobject *self) } else if (self->dict != ldict) { - Py_CLEAR(self->dict); - Py_INCREF(ldict); - self->dict = ldict; + Py_XSETREF(self->dict, ldict); } return ldict; diff -r a62e9717e45c Modules/readline.c --- a/Modules/readline.c Wed Jun 11 20:37:52 2008 +0200 +++ b/Modules/readline.c Wed Jun 11 21:13:04 2008 +0200 @@ -178,10 +178,7 @@ set_hook(const char *funcname, PyObject *hook_var = NULL; } else if (PyCallable_Check(function)) { - PyObject *tmp = *hook_var; - Py_INCREF(function); - *hook_var = function; - Py_XDECREF(tmp); + Py_XSETREF(*hook_var, function); } else { PyOS_snprintf(buf, sizeof(buf), diff -r a62e9717e45c Modules/signalmodule.c --- a/Modules/signalmodule.c Wed Jun 11 20:37:52 2008 +0200 +++ b/Modules/signalmodule.c Wed Jun 11 21:13:04 2008 +0200 @@ -580,9 +580,7 @@ PyInit_signal(void) } if (Handlers[SIGINT].func == DefaultHandler) { /* Install default int handler */ - Py_INCREF(IntHandler); - Py_DECREF(Handlers[SIGINT].func); - Handlers[SIGINT].func = IntHandler; + Py_SETREF(Handlers[SIGINT].func, IntHandler); old_siginthandler = PyOS_setsig(SIGINT, signal_handler); } diff -r a62e9717e45c Modules/zlibmodule.c --- a/Modules/zlibmodule.c Wed Jun 11 20:37:52 2008 +0200 +++ b/Modules/zlibmodule.c Wed Jun 11 21:13:05 2008 +0200 @@ -706,12 +706,8 @@ PyZlib_copy(compobject *self) goto error; } - Py_INCREF(self->unused_data); - Py_INCREF(self->unconsumed_tail); - Py_XDECREF(retval->unused_data); - Py_XDECREF(retval->unconsumed_tail); - retval->unused_data = self->unused_data; - retval->unconsumed_tail = self->unconsumed_tail; + Py_XSETREF(retval->unused_data, self->unused_data); + Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail); /* Mark it as being initialized */ retval->is_initialised = 1; @@ -757,12 +753,8 @@ PyZlib_uncopy(compobject *self) goto error; } - Py_INCREF(self->unused_data); - Py_INCREF(self->unconsumed_tail); - Py_XDECREF(retval->unused_data); - Py_XDECREF(retval->unconsumed_tail); - retval->unused_data = self->unused_data; - retval->unconsumed_tail = self->unconsumed_tail; + Py_XSETREF(retval->unused_data, self->unused_data); + Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail); /* Mark it as being initialized */ retval->is_initialised = 1;