Skip to content

Commit 1ed017a

Browse files
Issue python#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.
1 parent 726fc13 commit 1ed017a

File tree

9 files changed

+23
-71
lines changed

9 files changed

+23
-71
lines changed

Modules/_collectionsmodule.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,6 @@ deque_del_item(dequeobject *deque, Py_ssize_t i)
12221222
static int
12231223
deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
12241224
{
1225-
PyObject *old_value;
12261225
block *b;
12271226
Py_ssize_t n, len=Py_SIZE(deque), halflen=(len+1)>>1, index=i;
12281227

@@ -1249,9 +1248,7 @@ deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
12491248
b = b->leftlink;
12501249
}
12511250
Py_INCREF(v);
1252-
old_value = b->data[i];
1253-
b->data[i] = v;
1254-
Py_DECREF(old_value);
1251+
Py_SETREF(b->data[i], v);
12551252
return 0;
12561253
}
12571254

Modules/_ctypes/_ctypes.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5123,23 +5123,22 @@ int
51235123
comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
51245124
{
51255125
PyObject *hresult, *text, *details;
5126-
PyBaseExceptionObject *bself;
51275126
PyObject *a;
51285127
int status;
51295128

51305129
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
5131-
return -1;
5130+
return -1;
51325131

51335132
if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details))
51345133
return -1;
51355134

51365135
a = PySequence_GetSlice(args, 1, PySequence_Size(args));
51375136
if (!a)
5138-
return -1;
5137+
return -1;
51395138
status = PyObject_SetAttrString(self, "args", a);
51405139
Py_DECREF(a);
51415140
if (status < 0)
5142-
return -1;
5141+
return -1;
51435142

51445143
if (PyObject_SetAttrString(self, "hresult", hresult) < 0)
51455144
return -1;
@@ -5150,9 +5149,8 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
51505149
if (PyObject_SetAttrString(self, "details", details) < 0)
51515150
return -1;
51525151

5153-
bself = (PyBaseExceptionObject *)self;
51545152
Py_INCREF(args);
5155-
Py_SETREF(bself->args, args);
5153+
Py_SETREF((PyBaseExceptionObject *)self->args, args);
51565154

51575155
return 0;
51585156
}

Modules/_elementtree.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,13 +2338,9 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self,
23382338
PyObject *element_factory)
23392339
/*[clinic end generated code: output=91cfa7558970ee96 input=1b424eeefc35249c]*/
23402340
{
2341-
PyObject *tmp;
2342-
23432341
if (element_factory) {
23442342
Py_INCREF(element_factory);
2345-
tmp = self->element_factory;
2346-
self->element_factory = element_factory;
2347-
Py_XDECREF(tmp);
2343+
Py_SETREF(self->element_factory, element_factory);
23482344
}
23492345

23502346
return 0;

Modules/_io/bytesio.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ scan_eol(bytesio *self, Py_ssize_t len)
8787
static int
8888
unshare_buffer(bytesio *self, size_t size)
8989
{
90-
PyObject *new_buf, *old_buf;
90+
PyObject *new_buf;
9191
assert(SHARED_BUF(self));
9292
assert(self->exports == 0);
9393
assert(size >= (size_t)self->string_size);
@@ -96,9 +96,7 @@ unshare_buffer(bytesio *self, size_t size)
9696
return -1;
9797
memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf),
9898
self->string_size);
99-
old_buf = self->buf;
100-
self->buf = new_buf;
101-
Py_DECREF(old_buf);
99+
Py_SETREF(self->buf, new_buf);
102100
return 0;
103101
}
104102

Modules/itertoolsmodule.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg)
7777
static PyObject *
7878
groupby_next(groupbyobject *gbo)
7979
{
80-
PyObject *newvalue, *newkey, *r, *grouper, *tmp;
80+
PyObject *newvalue, *newkey, *r, *grouper;
8181

8282
/* skip to next iteration group */
8383
for (;;) {
@@ -110,19 +110,12 @@ groupby_next(groupbyobject *gbo)
110110
}
111111
}
112112

113-
tmp = gbo->currkey;
114-
gbo->currkey = newkey;
115-
Py_XDECREF(tmp);
116-
117-
tmp = gbo->currvalue;
118-
gbo->currvalue = newvalue;
119-
Py_XDECREF(tmp);
113+
Py_SETREF(gbo->currkey, newkey);
114+
Py_SETREF(gbo->currvalue, newvalue);
120115
}
121116

122117
Py_INCREF(gbo->currkey);
123-
tmp = gbo->tgtkey;
124-
gbo->tgtkey = gbo->currkey;
125-
Py_XDECREF(tmp);
118+
Py_SETREF(gbo->tgtkey, gbo->currkey);
126119

127120
grouper = _grouper_create(gbo, gbo->tgtkey);
128121
if (grouper == NULL)
@@ -3445,7 +3438,7 @@ accumulate_traverse(accumulateobject *lz, visitproc visit, void *arg)
34453438
static PyObject *
34463439
accumulate_next(accumulateobject *lz)
34473440
{
3448-
PyObject *val, *oldtotal, *newtotal;
3441+
PyObject *val, *newtotal;
34493442

34503443
val = (*Py_TYPE(lz->it)->tp_iternext)(lz->it);
34513444
if (val == NULL)
@@ -3465,11 +3458,8 @@ accumulate_next(accumulateobject *lz)
34653458
if (newtotal == NULL)
34663459
return NULL;
34673460

3468-
oldtotal = lz->total;
3469-
lz->total = newtotal;
3470-
Py_DECREF(oldtotal);
3471-
34723461
Py_INCREF(newtotal);
3462+
Py_SETREF(lz->total, newtotal);
34733463
return newtotal;
34743464
}
34753465

Modules/pyexpat.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,12 +1226,8 @@ xmlparse_dealloc(xmlparseobject *self)
12261226
self->itself = NULL;
12271227

12281228
if (self->handlers != NULL) {
1229-
PyObject *temp;
1230-
for (i = 0; handler_info[i].name != NULL; i++) {
1231-
temp = self->handlers[i];
1232-
self->handlers[i] = NULL;
1233-
Py_XDECREF(temp);
1234-
}
1229+
for (i = 0; handler_info[i].name != NULL; i++)
1230+
Py_CLEAR(self->handlers[i]);
12351231
PyMem_Free(self->handlers);
12361232
self->handlers = NULL;
12371233
}
@@ -1345,7 +1341,6 @@ sethandler(xmlparseobject *self, PyObject *name, PyObject* v)
13451341
int handlernum = handlername2int(name);
13461342
if (handlernum >= 0) {
13471343
xmlhandler c_handler = NULL;
1348-
PyObject *temp = self->handlers[handlernum];
13491344

13501345
if (v == Py_None) {
13511346
/* If this is the character data handler, and a character
@@ -1367,8 +1362,7 @@ sethandler(xmlparseobject *self, PyObject *name, PyObject* v)
13671362
Py_INCREF(v);
13681363
c_handler = handler_info[handlernum].handler;
13691364
}
1370-
self->handlers[handlernum] = v;
1371-
Py_XDECREF(temp);
1365+
Py_SETREF(self->handlers[handlernum], v);
13721366
handler_info[handlernum].setter(self->itself, c_handler);
13731367
return 1;
13741368
}
@@ -1898,15 +1892,12 @@ static void
18981892
clear_handlers(xmlparseobject *self, int initial)
18991893
{
19001894
int i = 0;
1901-
PyObject *temp;
19021895

19031896
for (; handler_info[i].name != NULL; i++) {
19041897
if (initial)
19051898
self->handlers[i] = NULL;
19061899
else {
1907-
temp = self->handlers[i];
1908-
self->handlers[i] = NULL;
1909-
Py_XDECREF(temp);
1900+
Py_CLEAR(self->handlers[i]);
19101901
handler_info[i].setter(self->itself, NULL);
19111902
}
19121903
}

Objects/listobject.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ int
216216
PyList_SetItem(PyObject *op, Py_ssize_t i,
217217
PyObject *newitem)
218218
{
219-
PyObject *olditem;
220219
PyObject **p;
221220
if (!PyList_Check(op)) {
222221
Py_XDECREF(newitem);
@@ -230,9 +229,7 @@ PyList_SetItem(PyObject *op, Py_ssize_t i,
230229
return -1;
231230
}
232231
p = ((PyListObject *)op) -> ob_item + i;
233-
olditem = *p;
234-
*p = newitem;
235-
Py_XDECREF(olditem);
232+
Py_SETREF(*p, newitem);
236233
return 0;
237234
}
238235

@@ -730,7 +727,6 @@ list_inplace_repeat(PyListObject *self, Py_ssize_t n)
730727
static int
731728
list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
732729
{
733-
PyObject *old_value;
734730
if (i < 0 || i >= Py_SIZE(a)) {
735731
PyErr_SetString(PyExc_IndexError,
736732
"list assignment index out of range");
@@ -739,9 +735,7 @@ list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
739735
if (v == NULL)
740736
return list_ass_slice(a, i, i+1, v);
741737
Py_INCREF(v);
742-
old_value = a->ob_item[i];
743-
a->ob_item[i] = v;
744-
Py_DECREF(old_value);
738+
Py_SETREF(a->ob_item[i], v);
745739
return 0;
746740
}
747741

Objects/tupleobject.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ PyTuple_GetItem(PyObject *op, Py_ssize_t i)
149149
int
150150
PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
151151
{
152-
PyObject *olditem;
153152
PyObject **p;
154153
if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
155154
Py_XDECREF(newitem);
@@ -163,9 +162,7 @@ PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
163162
return -1;
164163
}
165164
p = ((PyTupleObject *)op) -> ob_item + i;
166-
olditem = *p;
167-
*p = newitem;
168-
Py_XDECREF(olditem);
165+
Py_SETREF(*p, newitem);
169166
return 0;
170167
}
171168

Objects/typeobject.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ type_qualname(PyTypeObject *type, void *context)
401401
static int
402402
type_set_name(PyTypeObject *type, PyObject *value, void *context)
403403
{
404-
PyHeapTypeObject* et;
405404
char *tp_name;
406405
PyObject *tmp;
407406

@@ -430,17 +429,9 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
430429
if (tp_name == NULL)
431430
return -1;
432431

433-
et = (PyHeapTypeObject*)type;
434-
435-
Py_INCREF(value);
436-
437-
/* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name
438-
value. (Bug #16447.) */
439-
tmp = et->ht_name;
440-
et->ht_name = value;
441-
442432
type->tp_name = tp_name;
443-
Py_DECREF(tmp);
433+
Py_INCREF(value);
434+
Py_SETREF(((PyHeapTypeObject*)type)->ht_name, value);
444435

445436
return 0;
446437
}

0 commit comments

Comments
 (0)