diff -r 719c11b6b6ff Include/object.h --- a/Include/object.h Wed Apr 06 09:50:03 2016 +0300 +++ b/Include/object.h Wed Apr 06 10:42:47 2016 +0300 @@ -851,18 +851,28 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *) * * 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); \ diff -r 719c11b6b6ff Modules/_ctypes/_ctypes.c --- a/Modules/_ctypes/_ctypes.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/_ctypes/_ctypes.c Wed Apr 06 10:42:47 2016 +0300 @@ -391,7 +391,7 @@ StructUnionType_new(PyTypeObject *type, Py_DECREF((PyObject *)dict); return NULL; } - Py_XSETREF(result->tp_dict, (PyObject *)dict); + Py_SETREF(result->tp_dict, (PyObject *)dict); dict->format = _ctypes_alloc_format_string(NULL, "B"); if (dict->format == NULL) { Py_DECREF(result); @@ -870,7 +870,7 @@ PyCPointerType_SetProto(StgDictObject *s return -1; } Py_INCREF(proto); - Py_XSETREF(stgdict->proto, proto); + Py_SETREF(stgdict->proto, proto); return 0; } @@ -960,7 +960,7 @@ PyCPointerType_new(PyTypeObject *type, P Py_DECREF((PyObject *)stgdict); return NULL; } - Py_XSETREF(result->tp_dict, (PyObject *)stgdict); + Py_SETREF(result->tp_dict, (PyObject *)stgdict); return (PyObject *)result; } @@ -1403,7 +1403,7 @@ PyCArrayType_new(PyTypeObject *type, PyO /* replace the class dict by our updated spam dict */ if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) goto error; - Py_XSETREF(result->tp_dict, (PyObject *)stgdict); /* steal the reference */ + Py_SETREF(result->tp_dict, (PyObject *)stgdict); /* steal the reference */ stgdict = NULL; /* Special case for character arrays. @@ -1816,7 +1816,7 @@ static PyObject *CreateSwappedType(PyTyp Py_DECREF((PyObject *)stgdict); return NULL; } - Py_XSETREF(result->tp_dict, (PyObject *)stgdict); + Py_SETREF(result->tp_dict, (PyObject *)stgdict); return (PyObject *)result; } @@ -1944,7 +1944,7 @@ PyCSimpleType_new(PyTypeObject *type, Py Py_DECREF((PyObject *)stgdict); return NULL; } - Py_XSETREF(result->tp_dict, (PyObject *)stgdict); + Py_SETREF(result->tp_dict, (PyObject *)stgdict); /* Install from_param class methods in ctypes base classes. Overrides the PyCSimpleType_from_param generic method. @@ -2307,7 +2307,7 @@ PyCFuncPtrType_new(PyTypeObject *type, P Py_DECREF((PyObject *)stgdict); return NULL; } - Py_XSETREF(result->tp_dict, (PyObject *)stgdict); + Py_SETREF(result->tp_dict, (PyObject *)stgdict); if (-1 == make_funcptrtype_dict(stgdict)) { Py_DECREF(result); @@ -2451,7 +2451,7 @@ KeepRef(CDataObject *target, Py_ssize_t return -1; } if (ob->b_objects == NULL || !PyDict_CheckExact(ob->b_objects)) { - Py_XSETREF(ob->b_objects, keep); /* refcount consumed */ + Py_SETREF(ob->b_objects, keep); /* refcount consumed */ return 0; } key = unique_key(target, index); @@ -5150,7 +5150,7 @@ comerror_init(PyObject *self, PyObject * return -1; Py_INCREF(args); - Py_XSETREF(((PyBaseExceptionObject *)self)->args, args); + Py_SETREF(((PyBaseExceptionObject *)self)->args, args); return 0; } diff -r 719c11b6b6ff Modules/_curses_panel.c --- a/Modules/_curses_panel.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/_curses_panel.c Wed Apr 06 10:42:47 2016 +0300 @@ -313,7 +313,7 @@ PyCursesPanel_replace_panel(PyCursesPane return NULL; } Py_INCREF(temp); - Py_XSETREF(po->wo, temp); + Py_SETREF(po->wo, temp); Py_INCREF(Py_None); return Py_None; } diff -r 719c11b6b6ff Modules/_datetimemodule.c --- a/Modules/_datetimemodule.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/_datetimemodule.c Wed Apr 06 10:42:47 2016 +0300 @@ -1058,7 +1058,7 @@ format_utcoffset(char *buf, size_t bufle /* Offset is normalized, so it is negative if days < 0 */ if (GET_TD_DAYS(offset) < 0) { sign = '-'; - Py_XSETREF(offset, delta_negative((PyDateTime_Delta *)offset)); + Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset)); if (offset == NULL) return -1; } @@ -3045,7 +3045,7 @@ tzinfo_fromutc(PyDateTime_TZInfo *self, if (dst == Py_None) goto Inconsistent; if (delta_bool((PyDateTime_Delta *)dst) != 0) { - Py_XSETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, + Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, (PyDateTime_Delta *)dst, 1)); if (result == NULL) goto Fail; @@ -4445,7 +4445,7 @@ datetime_subtract(PyObject *left, PyObje return NULL; if (offdiff != NULL) { - Py_XSETREF(result, delta_subtract(result, offdiff)); + Py_SETREF(result, delta_subtract(result, offdiff)); Py_DECREF(offdiff); } } diff -r 719c11b6b6ff Modules/_elementtree.c --- a/Modules/_elementtree.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/_elementtree.c Wed Apr 06 10:42:47 2016 +0300 @@ -1957,7 +1957,7 @@ element_tag_setter(ElementObject *self, { _VALIDATE_ATTR_VALUE(value); Py_INCREF(value); - Py_XSETREF(self->tag, value); + Py_SETREF(self->tag, value); return 0; } @@ -1990,7 +1990,7 @@ element_attrib_setter(ElementObject *sel return -1; } Py_INCREF(value); - Py_XSETREF(self->extra->attrib, value); + Py_SETREF(self->extra->attrib, value); return 0; } @@ -2524,9 +2524,9 @@ treebuilder_handle_start(TreeBuilderObje self->index++; Py_INCREF(node); - Py_XSETREF(self->this, node); + Py_SETREF(self->this, node); Py_INCREF(node); - Py_XSETREF(self->last, node); + Py_SETREF(self->last, node); if (treebuilder_append_event(self, self->start_event_obj, node) < 0) goto error; diff -r 719c11b6b6ff Modules/_io/bytesio.c --- a/Modules/_io/bytesio.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/_io/bytesio.c Wed Apr 06 10:42:47 2016 +0300 @@ -96,7 +96,7 @@ unshare_buffer(bytesio *self, size_t siz return -1; memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf), self->string_size); - Py_XSETREF(self->buf, new_buf); + Py_SETREF(self->buf, new_buf); return 0; } diff -r 719c11b6b6ff Modules/_sqlite/connection.c --- a/Modules/_sqlite/connection.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/_sqlite/connection.c Wed Apr 06 10:42:47 2016 +0300 @@ -204,7 +204,7 @@ void pysqlite_flush_statement_cache(pysq node = node->next; } - Py_XSETREF(self->statement_cache, + Py_SETREF(self->statement_cache, (pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self)); Py_DECREF(self); self->statement_cache->decref_factory = 0; @@ -794,7 +794,7 @@ static void _pysqlite_drop_unused_statem } } - Py_XSETREF(self->statements, new_list); + Py_SETREF(self->statements, new_list); } static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self) @@ -825,7 +825,7 @@ static void _pysqlite_drop_unused_cursor } } - Py_XSETREF(self->cursors, new_list); + Py_SETREF(self->cursors, new_list); } PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) diff -r 719c11b6b6ff Modules/_sqlite/cursor.c --- a/Modules/_sqlite/cursor.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/_sqlite/cursor.c Wed Apr 06 10:42:47 2016 +0300 @@ -510,7 +510,7 @@ PyObject* _pysqlite_query_execute(pysqli /* reset description and rowcount */ Py_INCREF(Py_None); - Py_XSETREF(self->description, Py_None); + Py_SETREF(self->description, Py_None); self->rowcount = -1L; func_args = PyTuple_New(1); @@ -535,7 +535,7 @@ PyObject* _pysqlite_query_execute(pysqli } if (self->statement->in_use) { - Py_XSETREF(self->statement, + Py_SETREF(self->statement, PyObject_New(pysqlite_Statement, &pysqlite_StatementType)); if (!self->statement) { goto error; @@ -652,7 +652,7 @@ PyObject* _pysqlite_query_execute(pysqli numcols = sqlite3_column_count(self->statement->st); Py_END_ALLOW_THREADS - Py_XSETREF(self->description, PyTuple_New(numcols)); + Py_SETREF(self->description, PyTuple_New(numcols)); if (!self->description) { goto error; } diff -r 719c11b6b6ff Modules/_sre.c --- a/Modules/_sre.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/_sre.c Wed Apr 06 10:42:47 2016 +0300 @@ -753,7 +753,7 @@ deepcopy(PyObject** object, PyObject* me if (!copy) return 0; - Py_XSETREF(*object, copy); + Py_SETREF(*object, copy); return 1; /* success */ } diff -r 719c11b6b6ff Modules/_ssl.c --- a/Modules/_ssl.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/_ssl.c Wed Apr 06 10:42:47 2016 +0300 @@ -1589,7 +1589,7 @@ static int PySSL_set_context(PySSLSocket return -1; #else Py_INCREF(value); - Py_XSETREF(self->ctx, (PySSLContext *)value); + Py_SETREF(self->ctx, (PySSLContext *)value); SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); #endif } else { @@ -1646,7 +1646,7 @@ PySSL_get_owner(PySSLSocket *self, void static int PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) { - Py_XSETREF(self->owner, PyWeakref_NewRef(value, NULL)); + Py_SETREF(self->owner, PyWeakref_NewRef(value, NULL)); if (self->owner == NULL) return -1; return 0; diff -r 719c11b6b6ff Modules/itertoolsmodule.c --- a/Modules/itertoolsmodule.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/itertoolsmodule.c Wed Apr 06 10:42:47 2016 +0300 @@ -622,7 +622,7 @@ tee_next(teeobject *to) link = teedataobject_jumplink(to->dataobj); if (link == NULL) return NULL; - Py_XSETREF(to->dataobj, (teedataobject *)link); + Py_SETREF(to->dataobj, (teedataobject *)link); to->index = 0; } value = teedataobject_getitem(to->dataobj, to->index); @@ -3457,7 +3457,7 @@ accumulate_next(accumulateobject *lz) return NULL; Py_INCREF(newtotal); - Py_XSETREF(lz->total, newtotal); + Py_SETREF(lz->total, newtotal); return newtotal; } diff -r 719c11b6b6ff Modules/signalmodule.c --- a/Modules/signalmodule.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/signalmodule.c Wed Apr 06 10:42:47 2016 +0300 @@ -1266,7 +1266,7 @@ PyInit__signal(void) if (Handlers[SIGINT].func == DefaultHandler) { /* Install default int handler */ Py_INCREF(IntHandler); - Py_XSETREF(Handlers[SIGINT].func, IntHandler); + Py_SETREF(Handlers[SIGINT].func, IntHandler); old_siginthandler = PyOS_setsig(SIGINT, signal_handler); } diff -r 719c11b6b6ff Modules/zipimport.c --- a/Modules/zipimport.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/zipimport.c Wed Apr 06 10:42:47 2016 +0300 @@ -155,7 +155,7 @@ zipimporter_init(ZipImporter *self, PyOb tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP); if (tmp == NULL) goto error; - Py_XSETREF(self->prefix, tmp); + Py_SETREF(self->prefix, tmp); } } else diff -r 719c11b6b6ff Modules/zlibmodule.c --- a/Modules/zlibmodule.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Modules/zlibmodule.c Wed Apr 06 10:42:47 2016 +0300 @@ -667,7 +667,7 @@ save_unconsumed_input(compobject *self, PyBytes_AS_STRING(self->unused_data), old_size); Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size, self->zst.next_in, self->zst.avail_in); - Py_XSETREF(self->unused_data, new_data); + Py_SETREF(self->unused_data, new_data); self->zst.avail_in = 0; } } @@ -679,7 +679,7 @@ save_unconsumed_input(compobject *self, (char *)self->zst.next_in, self->zst.avail_in); if (new_data == NULL) return -1; - Py_XSETREF(self->unconsumed_tail, new_data); + Py_SETREF(self->unconsumed_tail, new_data); } return 0; } diff -r 719c11b6b6ff Objects/bytesobject.c --- a/Objects/bytesobject.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Objects/bytesobject.c Wed Apr 06 10:42:47 2016 +0300 @@ -3698,7 +3698,7 @@ PyBytes_Concat(PyObject **pv, PyObject * /* Multiple references, need to create new object */ PyObject *v; v = bytes_concat(*pv, w); - Py_XSETREF(*pv, v); + Py_SETREF(*pv, v); } } diff -r 719c11b6b6ff Objects/funcobject.c --- a/Objects/funcobject.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Objects/funcobject.c Wed Apr 06 10:42:47 2016 +0300 @@ -504,7 +504,7 @@ func_new(PyTypeObject* type, PyObject* a if (name != Py_None) { Py_INCREF(name); - Py_XSETREF(newfunc->func_name, name); + Py_SETREF(newfunc->func_name, name); } if (defaults != Py_None) { Py_INCREF(defaults); diff -r 719c11b6b6ff Objects/listobject.c --- a/Objects/listobject.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Objects/listobject.c Wed Apr 06 10:42:47 2016 +0300 @@ -735,7 +735,7 @@ list_ass_item(PyListObject *a, Py_ssize_ if (v == NULL) return list_ass_slice(a, i, i+1, v); Py_INCREF(v); - Py_XSETREF(a->ob_item[i], v); + Py_SETREF(a->ob_item[i], v); return 0; } diff -r 719c11b6b6ff Objects/rangeobject.c --- a/Objects/rangeobject.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Objects/rangeobject.c Wed Apr 06 10:42:47 2016 +0300 @@ -1064,7 +1064,7 @@ longrangeiter_next(longrangeiterobject * result = PyNumber_Add(r->start, product); Py_DECREF(product); if (result) { - Py_XSETREF(r->index, new_index); + Py_SETREF(r->index, new_index); } else { Py_DECREF(new_index); diff -r 719c11b6b6ff Objects/typeobject.c --- a/Objects/typeobject.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Objects/typeobject.c Wed Apr 06 10:42:47 2016 +0300 @@ -424,7 +424,7 @@ type_set_name(PyTypeObject *type, PyObje type->tp_name = tp_name; Py_INCREF(value); - Py_XSETREF(((PyHeapTypeObject*)type)->ht_name, value); + Py_SETREF(((PyHeapTypeObject*)type)->ht_name, value); return 0; } @@ -445,7 +445,7 @@ type_set_qualname(PyTypeObject *type, Py et = (PyHeapTypeObject*)type; Py_INCREF(value); - Py_XSETREF(et->ht_qualname, value); + Py_SETREF(et->ht_qualname, value); return 0; } @@ -2903,7 +2903,7 @@ PyObject * else method_cache_misses++; #endif - Py_XSETREF(method_cache[h].name, name); + Py_SETREF(method_cache[h].name, name); } return res; } diff -r 719c11b6b6ff Objects/unicodeobject.c --- a/Objects/unicodeobject.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Objects/unicodeobject.c Wed Apr 06 10:42:47 2016 +0300 @@ -1830,7 +1830,7 @@ unicode_resize(PyObject **p_unicode, Py_ _Py_INCREF_UNICODE_EMPTY(); if (!unicode_empty) return -1; - Py_XSETREF(*p_unicode, unicode_empty); + Py_SETREF(*p_unicode, unicode_empty); return 0; } @@ -1838,7 +1838,7 @@ unicode_resize(PyObject **p_unicode, Py_ PyObject *copy = resize_copy(unicode, length); if (copy == NULL) return -1; - Py_XSETREF(*p_unicode, copy); + Py_SETREF(*p_unicode, copy); return 0; } @@ -13547,7 +13547,7 @@ int return -1; _PyUnicode_FastCopyCharacters(newbuffer, 0, writer->buffer, 0, writer->pos); - Py_XSETREF(writer->buffer, newbuffer); + Py_SETREF(writer->buffer, newbuffer); } _PyUnicodeWriter_Update(writer); return 0; @@ -15264,7 +15264,7 @@ PyUnicode_InternInPlace(PyObject **p) if (t) { Py_INCREF(t); - Py_XSETREF(*p, t); + Py_SETREF(*p, t); return; } diff -r 719c11b6b6ff Python/_warnings.c --- a/Python/_warnings.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Python/_warnings.c Wed Apr 06 10:42:47 2016 +0300 @@ -730,7 +730,7 @@ setup_context(Py_ssize_t stack_level, Py goto handle_error; } else if (!is_true) { - Py_XSETREF(*filename, PyUnicode_FromString("__main__")); + Py_SETREF(*filename, PyUnicode_FromString("__main__")); if (*filename == NULL) goto handle_error; } diff -r 719c11b6b6ff Python/ceval.c --- a/Python/ceval.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Python/ceval.c Wed Apr 06 10:42:47 2016 +0300 @@ -3236,7 +3236,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int Py_INCREF(self); func = PyMethod_GET_FUNCTION(func); Py_INCREF(func); - Py_XSETREF(*pfunc, self); + Py_SETREF(*pfunc, self); na++; /* n++; */ } else @@ -4747,7 +4747,7 @@ call_function(PyObject ***pp_stack, int Py_INCREF(self); func = PyMethod_GET_FUNCTION(func); Py_INCREF(func); - Py_XSETREF(*pfunc, self); + Py_SETREF(*pfunc, self); na++; n++; } else diff -r 719c11b6b6ff Python/errors.c --- a/Python/errors.c Wed Apr 06 09:50:03 2016 +0300 +++ b/Python/errors.c Wed Apr 06 10:42:47 2016 +0300 @@ -311,9 +311,9 @@ finally: --tstate->recursion_depth; /* throw away the old exception and use the recursion error instead */ Py_INCREF(PyExc_RecursionError); - Py_XSETREF(*exc, PyExc_RecursionError); + Py_SETREF(*exc, PyExc_RecursionError); Py_INCREF(PyExc_RecursionErrorInst); - Py_XSETREF(*val, PyExc_RecursionErrorInst); + Py_SETREF(*val, PyExc_RecursionErrorInst); /* just keeping the old traceback */ return; }