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.
diff --git a/Objects/listobject.c b/Objects/listobject.c
index eee7c68..fcc21cb 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -216,7 +216,6 @@
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 @@
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 @@
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 @@
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 7efa1a6..8e1b00b 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -149,7 +149,6 @@
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 @@
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 4fac9e8..78ef6ac 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -401,7 +401,6 @@
static int
type_set_name(PyTypeObject *type, PyObject *value, void *context)
{
- PyHeapTypeObject* et;
char *tp_name;
PyObject *tmp;
@@ -430,17 +429,9 @@
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;
}