diff --git a/Include/object.h b/Include/object.h index 4179651985ea2c..50d9747cf95fdf 100644 --- a/Include/object.h +++ b/Include/object.h @@ -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 --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 1b804ae1b58172..7e20301ec1b649 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -391,7 +391,7 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt 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); @@ -960,7 +960,7 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 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, PyObject *args, PyObject *kwds) /* 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(PyTypeObject *type, PyObject *args, PyObject 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, PyObject *args, PyObject *kwds) 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, PyObject *args, PyObject *kwds) 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); @@ -5152,7 +5152,7 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds) bself = (PyBaseExceptionObject *)self; Py_INCREF(args); - Py_XSETREF(bself->args, args); + Py_SETREF(bself->args, args); return 0; } diff --git a/Modules/_curses_panel.c b/Modules/_curses_panel.c index 75bbb173d0c22d..759b73109d25c1 100644 --- a/Modules/_curses_panel.c +++ b/Modules/_curses_panel.c @@ -313,7 +313,7 @@ PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args) return NULL; } Py_INCREF(temp); - Py_XSETREF(po->wo, temp); + Py_SETREF(po->wo, temp); Py_INCREF(Py_None); return Py_None; } diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 6619048e278962..0f1d6a1faa40c8 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1943,7 +1943,7 @@ element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value) if (strcmp(name, "tag") == 0) { Py_INCREF(value); - Py_XSETREF(self->tag, value); + Py_SETREF(self->tag, value); } else if (strcmp(name, "text") == 0) { Py_DECREF(JOIN_OBJ(self->text)); self->text = value; @@ -1958,7 +1958,7 @@ element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value) return -1; } Py_INCREF(value); - Py_XSETREF(self->extra->attrib, value); + Py_SETREF(self->extra->attrib, value); } else { PyErr_SetString(PyExc_AttributeError, "Can't set arbitrary attributes on Element"); @@ -2551,9 +2551,9 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, 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 --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 42e41582a4aabf..7570624e04e704 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -204,7 +204,7 @@ void pysqlite_flush_statement_cache(pysqlite_Connection* self) 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_statement_references(pysqlite_Connection* self } } - 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_references(pysqlite_Connection* self) } } - 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 --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 0931c84a45a58c..300da2878e4822 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -510,7 +510,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* /* 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(pysqlite_Cursor* self, int multiple, PyObject* } 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(pysqlite_Cursor* self, int multiple, PyObject* 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 --git a/Modules/_sre.c b/Modules/_sre.c index 6b58560e8c3e6b..150229dfbb6c8f 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -756,7 +756,7 @@ deepcopy(PyObject** object, PyObject* memo) if (!copy) return 0; - Py_XSETREF(*object, copy); + Py_SETREF(*object, copy); return 1; /* success */ } diff --git a/Modules/_ssl.c b/Modules/_ssl.c index 8b5b8f7df93cb4..d2b65001b9b9f0 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -1589,7 +1589,7 @@ static int PySSL_set_context(PySSLSocket *self, PyObject *value, 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 { diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index a7f1111777ca1a..781fbcdf1a5f61 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -631,7 +631,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); diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 3edf1587149263..da454de8d9ae42 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -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 --git a/Modules/zipimport.c b/Modules/zipimport.c index 3abb9f06a9073c..e840271bfd67c6 100644 --- a/Modules/zipimport.c +++ b/Modules/zipimport.c @@ -155,7 +155,7 @@ zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds) 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 --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index 802e7dbe748b69..eb627287826092 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -668,7 +668,7 @@ save_unconsumed_input(compobject *self, int err) 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; } } @@ -680,7 +680,7 @@ save_unconsumed_input(compobject *self, int err) (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 --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 11a4408662461a..495c3ebc23696d 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -3520,7 +3520,7 @@ PyBytes_Concat(PyObject **pv, PyObject *w) /* Multiple references, need to create new object */ PyObject *v; v = bytes_concat(*pv, w); - Py_XSETREF(*pv, v); + Py_SETREF(*pv, v); } } diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 4252e930bd5163..e6c327d22f85df 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -527,7 +527,7 @@ func_new(PyTypeObject* type, PyObject* args, PyObject* kw) 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 --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 7a114e0e7b854f..2be32e01b83239 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -1064,7 +1064,7 @@ longrangeiter_next(longrangeiterobject *r) 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 --git a/Objects/typeobject.c b/Objects/typeobject.c index 7628ce7413b482..f88a5fbe5166c7 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -424,7 +424,7 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context) 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, PyObject *value, void *context) et = (PyHeapTypeObject*)type; Py_INCREF(value); - Py_XSETREF(et->ht_qualname, value); + Py_SETREF(et->ht_qualname, value); return 0; } @@ -2897,7 +2897,7 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name) else method_cache_misses++; #endif - Py_XSETREF(method_cache[h].name, name); + Py_SETREF(method_cache[h].name, name); } return res; } diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index e2813fd444d362..230125b62ba265 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1667,7 +1667,7 @@ unicode_resize(PyObject **p_unicode, Py_ssize_t length) _Py_INCREF_UNICODE_EMPTY(); if (!unicode_empty) return -1; - Py_XSETREF(*p_unicode, unicode_empty); + Py_SETREF(*p_unicode, unicode_empty); return 0; } @@ -1675,7 +1675,7 @@ unicode_resize(PyObject **p_unicode, Py_ssize_t length) PyObject *copy = resize_copy(unicode, length); if (copy == NULL) return -1; - Py_XSETREF(*p_unicode, copy); + Py_SETREF(*p_unicode, copy); return 0; } @@ -13326,7 +13326,7 @@ _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, 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; @@ -15012,7 +15012,7 @@ PyUnicode_InternInPlace(PyObject **p) if (t) { Py_INCREF(t); - Py_XSETREF(*p, t); + Py_SETREF(*p, t); return; } diff --git a/Python/_warnings.c b/Python/_warnings.c index 78f532ecb49469..978bad135c146b 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -680,7 +680,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, 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 --git a/Python/ceval.c b/Python/ceval.c index 18112109316907..ee79c219555a06 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -3229,7 +3229,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) Py_INCREF(self); func = PyMethod_GET_FUNCTION(func); Py_INCREF(func); - Py_XSETREF(*pfunc, self); + Py_SETREF(*pfunc, self); na++; /* n++; */ } else @@ -4682,7 +4682,7 @@ call_function(PyObject ***pp_stack, int oparg Py_INCREF(self); func = PyMethod_GET_FUNCTION(func); Py_INCREF(func); - Py_XSETREF(*pfunc, self); + Py_SETREF(*pfunc, self); na++; n++; } else diff --git a/Python/errors.c b/Python/errors.c index 1d6e432d6b2aaa..47d7c4b9924164 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -311,9 +311,9 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) --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; }