From: Serhiy Storchaka Date: Sun, 10 Apr 2016 15:05:12 +0000 (+0300) Subject: Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=763a61ca954a1c5a1b56411dd5e57a7d58afff75;p=python Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF in places where Py_DECREF was used. --- diff --git a/Include/object.h b/Include/object.h index 6481a5b65f..9fc05831f9 100644 --- a/Include/object.h +++ b/Include/object.h @@ -828,18 +828,28 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force); * * 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/_bsddb.c b/Modules/_bsddb.c index 5fd52d99fa..a319333b8e 100644 --- a/Modules/_bsddb.c +++ b/Modules/_bsddb.c @@ -2498,7 +2498,7 @@ DB_set_private(DBObject* self, PyObject* private_obj) { /* We can set the private field even if db is closed */ Py_INCREF(private_obj); - Py_XSETREF(self->private_obj, private_obj); + Py_SETREF(self->private_obj, private_obj); RETURN_NONE(); } @@ -6997,7 +6997,7 @@ DBEnv_set_private(DBEnvObject* self, PyObject* private_obj) { /* We can set the private field even if dbenv is closed */ Py_INCREF(private_obj); - Py_XSETREF(self->private_obj, private_obj); + Py_SETREF(self->private_obj, private_obj); RETURN_NONE(); } @@ -7410,7 +7410,7 @@ DBEnv_rep_set_transport(DBEnvObject* self, PyObject* args) RETURN_IF_ERR(); Py_INCREF(rep_transport); - Py_XSETREF(self->rep_transport, rep_transport); + Py_SETREF(self->rep_transport, rep_transport); RETURN_NONE(); } diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 7f66d6a85f..65c55cb3f8 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -424,7 +424,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); @@ -992,7 +992,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; } @@ -1457,7 +1457,7 @@ PyCArrayType_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); /* Special case for character arrays. A permanent annoyance: char arrays are also strings! @@ -1880,7 +1880,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; } @@ -2388,7 +2388,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); diff --git a/Modules/_curses_panel.c b/Modules/_curses_panel.c index dbc0f5332a..d61281ed06 100644 --- a/Modules/_curses_panel.c +++ b/Modules/_curses_panel.c @@ -284,7 +284,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 793d4c73c6..0d16a697a0 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1574,7 +1574,7 @@ element_setattr(ElementObject* self, const char* name, 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; @@ -1587,7 +1587,7 @@ element_setattr(ElementObject* self, const char* name, PyObject* value) if (!self->extra) element_new_extra(self, NULL); Py_INCREF(value); - Py_XSETREF(self->extra->attrib, value); + Py_SETREF(self->extra->attrib, value); } else { PyErr_SetString(PyExc_AttributeError, name); return -1; @@ -1799,10 +1799,10 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, self->index++; Py_INCREF(node); - Py_XSETREF(self->this, (ElementObject*) node); + Py_SETREF(self->this, (ElementObject*) node); Py_INCREF(node); - Py_XSETREF(self->last, (ElementObject*) node); + Py_SETREF(self->last, (ElementObject*) node); if (treebuilder_append_event(self, self->start_event_obj, node) < 0) goto error; diff --git a/Modules/_json.c b/Modules/_json.c index 9c8f66ce11..fede6b14a1 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1717,7 +1717,7 @@ scanner_init(PyObject *self, PyObject *args, PyObject *kwds) } else if (PyUnicode_Check(s->encoding)) { PyObject *tmp = PyUnicode_AsEncodedString(s->encoding, NULL, NULL); - Py_XSETREF(s->encoding, tmp); + Py_SETREF(s->encoding, tmp); } if (s->encoding == NULL) goto bail; diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 9ced405aca..6b0e3c628b 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -228,7 +228,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; @@ -815,7 +815,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) @@ -846,7 +846,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 b3ec8f253c..f2b48ffa3a 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -544,7 +544,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); @@ -569,7 +569,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; @@ -681,7 +681,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 485020e127..829fb6bdcc 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -2054,7 +2054,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 a44414b188..23d4d5ceab 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -1467,7 +1467,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/bz2module.c b/Modules/bz2module.c index c33e03ca22..fe417c5ffa 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -1953,7 +1953,7 @@ BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args) self->running = 0; input_left += bzs->avail_in; if (input_left != 0) { - Py_XSETREF(self->unused_data, + Py_SETREF(self->unused_data, PyString_FromStringAndSize(bzs->next_in, input_left)); if (self->unused_data == NULL) goto error; diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 24933211f9..68285b82f6 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -492,7 +492,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 81674c80f5..c0e17f3a2e 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -621,7 +621,7 @@ initsignal(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/zlibmodule.c b/Modules/zlibmodule.c index f3282b2eeb..799e7dfeb0 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -491,7 +491,7 @@ save_unconsumed_input(compobject *self, int err) PyString_AS_STRING(self->unused_data), old_size); Py_MEMCPY(PyString_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; } } @@ -503,7 +503,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/exceptions.c b/Objects/exceptions.c index 8906f1a786..3fc0e13786 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -59,7 +59,7 @@ BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds) return -1; Py_INCREF(args); - Py_XSETREF(self->args, args); + Py_SETREF(self->args, args); if (PyTuple_GET_SIZE(self->args) == 1) { Py_INCREF(PyTuple_GET_ITEM(self->args, 0)); @@ -622,7 +622,7 @@ EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args, if (!subslice) return -1; - Py_XSETREF(self->args, subslice); + Py_SETREF(self->args, subslice); } return 0; } diff --git a/Objects/fileobject.c b/Objects/fileobject.c index e4f7fc404d..9ae068cfc5 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -574,8 +574,8 @@ PyFile_SetEncodingAndErrors(PyObject *f, const char *enc, char* errors) oerrors = Py_None; Py_INCREF(Py_None); } - Py_XSETREF(file->f_encoding, str); - Py_XSETREF(file->f_errors, oerrors); + Py_SETREF(file->f_encoding, str); + Py_SETREF(file->f_errors, oerrors); return 1; } diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 26369626f9..0e76a446af 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -428,7 +428,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/stringobject.c b/Objects/stringobject.c index faaa11125c..f585ffc728 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -3865,7 +3865,7 @@ PyString_Concat(register PyObject **pv, register PyObject *w) return; } v = string_concat((PyStringObject *) *pv, w); - Py_XSETREF(*pv, v); + Py_SETREF(*pv, v); } void @@ -4750,7 +4750,7 @@ PyString_InternInPlace(PyObject **p) t = PyDict_GetItem(interned, (PyObject *)s); if (t) { Py_INCREF(t); - Py_XSETREF(*p, t); + Py_SETREF(*p, t); return; } diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index d06ce2cb97..0b81a3cbf6 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -436,7 +436,7 @@ int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) return -1; Py_UNICODE_COPY(w->str, v->str, length < v->length ? length : v->length); - Py_XSETREF(*unicode, w); + Py_SETREF(*unicode, w); return 0; } diff --git a/Python/_warnings.c b/Python/_warnings.c index 932f2f8d13..8e8c0cceb3 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -528,7 +528,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, goto handle_error; } else if (!is_true) { - Py_XSETREF(*filename, PyString_FromString("__main__")); + Py_SETREF(*filename, PyString_FromString("__main__")); if (*filename == NULL) goto handle_error; } diff --git a/Python/ceval.c b/Python/ceval.c index e56e957490..6e5e27280e 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -4360,7 +4360,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 466a2534dd..d823e1397c 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -229,9 +229,9 @@ finally: --tstate->recursion_depth; /* throw away the old exception and use the recursion error instead */ Py_INCREF(PyExc_RuntimeError); - Py_XSETREF(*exc, PyExc_RuntimeError); + Py_SETREF(*exc, PyExc_RuntimeError); Py_INCREF(PyExc_RecursionErrorInst); - Py_XSETREF(*val, PyExc_RecursionErrorInst); + Py_SETREF(*val, PyExc_RecursionErrorInst); /* just keeping the old traceback */ return; }