From 1ed017ae92b32b27186d5793f6e58c526f350a2b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 27 Dec 2015 15:51:32 +0200 Subject: [PATCH] Issue #20440: Cleaning up the code by using Py_SETREF and Py_CLEAR. Old code is correct, but with Py_SETREF and Py_CLEAR it can be cleaner. This patch doesn't fix bugs and hence there is no need to backport it. --- Modules/_collectionsmodule.c | 5 +---- Modules/_ctypes/_ctypes.c | 10 ++++------ Modules/_elementtree.c | 6 +----- Modules/_io/bytesio.c | 6 ++---- Modules/itertoolsmodule.c | 22 ++++++---------------- Modules/pyexpat.c | 17 ++++------------- Objects/listobject.c | 10 ++-------- Objects/tupleobject.c | 5 +---- Objects/typeobject.c | 13 ++----------- 9 files changed, 23 insertions(+), 71 deletions(-) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index e3d1910b9f8..6a5358494ff 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1222,7 +1222,6 @@ deque_del_item(dequeobject *deque, Py_ssize_t i) static int deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v) { - PyObject *old_value; block *b; Py_ssize_t n, len=Py_SIZE(deque), halflen=(len+1)>>1, index=i; @@ -1249,9 +1248,7 @@ deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v) b = b->leftlink; } Py_INCREF(v); - old_value = b->data[i]; - b->data[i] = v; - Py_DECREF(old_value); + Py_SETREF(b->data[i], v); return 0; } diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 5db9f90f2e4..dddeba043ee 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5123,23 +5123,22 @@ int comerror_init(PyObject *self, PyObject *args, PyObject *kwds) { PyObject *hresult, *text, *details; - PyBaseExceptionObject *bself; PyObject *a; int status; if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) - return -1; + return -1; if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details)) return -1; a = PySequence_GetSlice(args, 1, PySequence_Size(args)); if (!a) - return -1; + return -1; status = PyObject_SetAttrString(self, "args", a); Py_DECREF(a); if (status < 0) - return -1; + return -1; if (PyObject_SetAttrString(self, "hresult", hresult) < 0) return -1; @@ -5150,9 +5149,8 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds) if (PyObject_SetAttrString(self, "details", details) < 0) return -1; - bself = (PyBaseExceptionObject *)self; Py_INCREF(args); - Py_SETREF(bself->args, args); + Py_SETREF((PyBaseExceptionObject *)self->args, args); return 0; } diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index f16d48f829f..580c53a4178 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -2338,13 +2338,9 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self, PyObject *element_factory) /*[clinic end generated code: output=91cfa7558970ee96 input=1b424eeefc35249c]*/ { - PyObject *tmp; - if (element_factory) { Py_INCREF(element_factory); - tmp = self->element_factory; - self->element_factory = element_factory; - Py_XDECREF(tmp); + Py_SETREF(self->element_factory, element_factory); } return 0; diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c index 99e71bcc7f3..6333e46438f 100644 --- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@ -87,7 +87,7 @@ scan_eol(bytesio *self, Py_ssize_t len) static int unshare_buffer(bytesio *self, size_t size) { - PyObject *new_buf, *old_buf; + PyObject *new_buf; assert(SHARED_BUF(self)); assert(self->exports == 0); assert(size >= (size_t)self->string_size); @@ -96,9 +96,7 @@ unshare_buffer(bytesio *self, size_t size) return -1; memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf), self->string_size); - old_buf = self->buf; - self->buf = new_buf; - Py_DECREF(old_buf); + Py_SETREF(self->buf, new_buf); return 0; } diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 2bd0594f00b..a4ffa0699a4 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -77,7 +77,7 @@ groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg) static PyObject * groupby_next(groupbyobject *gbo) { - PyObject *newvalue, *newkey, *r, *grouper, *tmp; + PyObject *newvalue, *newkey, *r, *grouper; /* skip to next iteration group */ for (;;) { @@ -110,19 +110,12 @@ groupby_next(groupbyobject *gbo) } } - tmp = gbo->currkey; - gbo->currkey = newkey; - Py_XDECREF(tmp); - - tmp = gbo->currvalue; - gbo->currvalue = newvalue; - Py_XDECREF(tmp); + Py_SETREF(gbo->currkey, newkey); + Py_SETREF(gbo->currvalue, newvalue); } Py_INCREF(gbo->currkey); - tmp = gbo->tgtkey; - gbo->tgtkey = gbo->currkey; - Py_XDECREF(tmp); + Py_SETREF(gbo->tgtkey, gbo->currkey); grouper = _grouper_create(gbo, gbo->tgtkey); if (grouper == NULL) @@ -3445,7 +3438,7 @@ accumulate_traverse(accumulateobject *lz, visitproc visit, void *arg) static PyObject * accumulate_next(accumulateobject *lz) { - PyObject *val, *oldtotal, *newtotal; + PyObject *val, *newtotal; val = (*Py_TYPE(lz->it)->tp_iternext)(lz->it); if (val == NULL) @@ -3465,11 +3458,8 @@ accumulate_next(accumulateobject *lz) if (newtotal == NULL) return NULL; - oldtotal = lz->total; - lz->total = newtotal; - Py_DECREF(oldtotal); - Py_INCREF(newtotal); + Py_SETREF(lz->total, newtotal); return newtotal; } diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index 9267c69e195..c6d35150b3d 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -1226,12 +1226,8 @@ xmlparse_dealloc(xmlparseobject *self) self->itself = NULL; if (self->handlers != NULL) { - PyObject *temp; - for (i = 0; handler_info[i].name != NULL; i++) { - temp = self->handlers[i]; - self->handlers[i] = NULL; - Py_XDECREF(temp); - } + for (i = 0; handler_info[i].name != NULL; i++) + Py_CLEAR(self->handlers[i]); PyMem_Free(self->handlers); self->handlers = NULL; } @@ -1345,7 +1341,6 @@ sethandler(xmlparseobject *self, PyObject *name, PyObject* v) int handlernum = handlername2int(name); if (handlernum >= 0) { xmlhandler c_handler = NULL; - PyObject *temp = self->handlers[handlernum]; if (v == Py_None) { /* If this is the character data handler, and a character @@ -1367,8 +1362,7 @@ sethandler(xmlparseobject *self, PyObject *name, PyObject* v) Py_INCREF(v); c_handler = handler_info[handlernum].handler; } - self->handlers[handlernum] = v; - Py_XDECREF(temp); + Py_SETREF(self->handlers[handlernum], v); handler_info[handlernum].setter(self->itself, c_handler); return 1; } @@ -1898,15 +1892,12 @@ static void clear_handlers(xmlparseobject *self, int initial) { int i = 0; - PyObject *temp; for (; handler_info[i].name != NULL; i++) { if (initial) self->handlers[i] = NULL; else { - temp = self->handlers[i]; - self->handlers[i] = NULL; - Py_XDECREF(temp); + Py_CLEAR(self->handlers[i]); handler_info[i].setter(self->itself, NULL); } } diff --git a/Objects/listobject.c b/Objects/listobject.c index eee7c68e9e4..fcc21cbebe0 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -216,7 +216,6 @@ int PyList_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem) { - PyObject *olditem; PyObject **p; if (!PyList_Check(op)) { Py_XDECREF(newitem); @@ -230,9 +229,7 @@ PyList_SetItem(PyObject *op, Py_ssize_t i, return -1; } p = ((PyListObject *)op) -> ob_item + i; - olditem = *p; - *p = newitem; - Py_XDECREF(olditem); + Py_SETREF(*p, newitem); return 0; } @@ -730,7 +727,6 @@ list_inplace_repeat(PyListObject *self, Py_ssize_t n) static int list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v) { - PyObject *old_value; if (i < 0 || i >= Py_SIZE(a)) { PyErr_SetString(PyExc_IndexError, "list assignment index out of range"); @@ -739,9 +735,7 @@ list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v) if (v == NULL) return list_ass_slice(a, i, i+1, v); Py_INCREF(v); - old_value = a->ob_item[i]; - a->ob_item[i] = v; - Py_DECREF(old_value); + Py_SETREF(a->ob_item[i], v); return 0; } diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 7efa1a6776a..8e1b00b63d1 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -149,7 +149,6 @@ PyTuple_GetItem(PyObject *op, Py_ssize_t i) int PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem) { - PyObject *olditem; PyObject **p; if (!PyTuple_Check(op) || op->ob_refcnt != 1) { Py_XDECREF(newitem); @@ -163,9 +162,7 @@ PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem) return -1; } p = ((PyTupleObject *)op) -> ob_item + i; - olditem = *p; - *p = newitem; - Py_XDECREF(olditem); + Py_SETREF(*p, newitem); return 0; } diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 4fac9e889bf..78ef6acb137 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -401,7 +401,6 @@ type_qualname(PyTypeObject *type, void *context) static int type_set_name(PyTypeObject *type, PyObject *value, void *context) { - PyHeapTypeObject* et; char *tp_name; PyObject *tmp; @@ -430,17 +429,9 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context) if (tp_name == NULL) return -1; - et = (PyHeapTypeObject*)type; - - Py_INCREF(value); - - /* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name - value. (Bug #16447.) */ - tmp = et->ht_name; - et->ht_name = value; - type->tp_name = tp_name; - Py_DECREF(tmp); + Py_INCREF(value); + Py_SETREF(((PyHeapTypeObject*)type)->ht_name, value); return 0; }