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.
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;
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;
}
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;
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;
}
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;
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);
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;
}
static PyObject *
groupby_next(groupbyobject *gbo)
{
- PyObject *newvalue, *newkey, *r, *grouper, *tmp;
+ PyObject *newvalue, *newkey, *r, *grouper;
/* skip to next iteration group */
for (;;) {
}
}
- 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)
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)
if (newtotal == NULL)
return NULL;
- oldtotal = lz->total;
- lz->total = newtotal;
- Py_DECREF(oldtotal);
-
Py_INCREF(newtotal);
+ Py_SETREF(lz->total, newtotal);
return newtotal;
}
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;
}
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
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;
}
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);
}
}
PyList_SetItem(PyObject *op, Py_ssize_t i,
PyObject *newitem)
{
- PyObject *olditem;
PyObject **p;
if (!PyList_Check(op)) {
Py_XDECREF(newitem);
return -1;
}
p = ((PyListObject *)op) -> ob_item + i;
- olditem = *p;
- *p = newitem;
- Py_XDECREF(olditem);
+ Py_SETREF(*p, newitem);
return 0;
}
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");
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;
}
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);
return -1;
}
p = ((PyTupleObject *)op) -> ob_item + i;
- olditem = *p;
- *p = newitem;
- Py_XDECREF(olditem);
+ Py_SETREF(*p, newitem);
return 0;
}
static int
type_set_name(PyTypeObject *type, PyObject *value, void *context)
{
- PyHeapTypeObject* et;
char *tp_name;
PyObject *tmp;
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;
}