8000 gh-99537: Use Py_SETREF(var, NULL) in C code (#99687) · python/cpython@81f7359 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81f7359

Browse files
authored
gh-99537: Use Py_SETREF(var, NULL) in C code (#99687)
Replace "Py_DECREF(var); var = NULL;" with "Py_SETREF(var, NULL);".
1 parent 5d9183c commit 81f7359

22 files changed

+44
-87
lines changed

Modules/_abc.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
624624

625625
switch (PyObject_IsTrue(result)) {
626626
case -1:
627-
Py_DECREF(result);
628-
result = NULL;
627+
Py_SETREF(result, NULL);
629628
break;
630629
case 0:
631630
Py_DECREF(result);

Modules/_datetimemodule.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,8 +1328,7 @@ call_tzname(PyObject *tzinfo, PyObject *tzinfoarg)
13281328
PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must "
13291329
"return None or a string, not '%s'",
13301330
Py_TYPE(result)->tp_name);
1331-
Py_DECREF(result);
1332-
result = NULL;
1331+
Py_SETREF(result, NULL);
13331332
}
13341333

13351334
return result;
@@ -1849,8 +1848,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
18491848
x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */
18501849
if (x2 == NULL)
18511850
goto Done;
1852-
Py_DECREF(x1);
1853-
x1 = NULL;
1851+
Py_SETREF(x1, NULL);
18541852

18551853
/* x2 has days in seconds */
18561854
x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */
@@ -1867,8 +1865,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
18671865
x1 = PyNumber_Multiply(x3, us_per_second); /* us */
18681866
if (x1 == NULL)
18691867
goto Done;
1870-
Py_DECREF(x3);
1871-
x3 = NULL;
1868+
Py_SETREF(x3, NULL);
18721869

18731870
/* x1 has days+seconds in us */
18741871
x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self));
@@ -2038,8 +2035,7 @@ multiply_truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *floatobj,
20382035
goto error;
20392036
}
20402037
temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, op));
2041-
Py_DECREF(pyus_in);
2042-
pyus_in = NULL;
2038+
Py_SETREF(pyus_in, NULL);
20432039
if (temp == NULL)
20442040
goto error;
20452041
pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, !op));

Modules/_elementtree.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,7 @@ get_attrib_from_keywords(PyObject *kwds)
345345
}
346346
attrib = PyDict_Copy(attrib);
347347
if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
348-
Py_DECREF(attrib);
349-
attrib = NULL;
348+
Py_SETREF(attrib, NULL);
350349
}
351350
}
352351
else if (!PyErr_Occurred()) {

Modules/_io/_iomodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
334334
goto error;
335335
result = raw;
336336

337-
Py_DECREF(path_or_fd);
338-
path_or_fd = NULL;
337+
Py_SETREF(path_or_fd, NULL);
339338

340339
modeobj = PyUnicode_FromString(mode);
341340
if (modeobj == NULL)

Modules/_pickle.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4344,8 +4344,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
43444344
if (reduce_value != Py_NotImplemented) {
43454345
goto reduce;
43464346
}
4347-
Py_DECREF(reduce_value);
4348-
reduce_value = NULL;
4347+
Py_SETREF(reduce_value, NULL);
43494348
}
43504349

43514350
if (type == &PyType_Type) {

Modules/_scproxy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ get_proxy_settings(PyObject* Py_UNUSED(mod), PyObject *Py_UNUSED(ignored))
8484
if (v == NULL) goto error;
8585

8686
r = PyDict_SetItemString(result, "exclude_simple", v);
87-
Py_DECREF(v); v = NULL;
87+
Py_SETREF(v, NULL);
8888
if (r == -1) goto error;
8989

9090
anArray = CFDictionaryGetValue(proxyDict,

Modules/_struct.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,8 +2164,7 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
21642164
_structmodulestate *state = get_struct_state(module);
21652165

21662166
if (fmt == NULL) {
2167-
Py_DECREF(*ptr);
2168-
*ptr = NULL;
2167+
Py_SETREF(*ptr, NULL);
21692168
return 1;
21702169
}
21712170

Modules/_tkinter.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,8 +2163,7 @@ _tkinter_tkapp_splitlist(TkappObject *self, PyObject *arg)
21632163
for (i = 0; i < argc; i++) {
21642164
PyObject *s = unicodeFromTclString(argv[i]);
21652165
if (!s) {
2166-
Py_DECREF(v);
2167-
v = NULL;
2166+
Py_SETREF(v, NULL);
21682167
goto finally;
21692168
}
21702169
PyTuple_SET_ITEM(v, i, s);

Modules/_xxsubinterpretersmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,8 +2320,7 @@ channel_list_all(PyObject *self, PyObject *Py_UNUSED(ignored))
23202320
PyObject *id = (PyObject *)newchannelid(&ChannelIDtype, *cur, 0,
23212321
&_globals.channels, 0, 0);
23222322
if (id == NULL) {
2323-
Py_DECREF(ids);
2324-
ids = NULL;
2323+
Py_SETREF(ids, NULL);
23252324
break;
23262325
}
23272326
PyList_SET_ITEM(ids, (Py_ssize_t)i, id);

Modules/_zoneinfo.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ zoneinfo_new_instance(PyTypeObject *type, PyObject *key)
220220
}
221221

222222
PyObject *rv = PyObject_CallMethod(file_obj, "close", NULL);
223-
Py_DECREF(file_obj);
224-
file_obj = NULL;
223+
Py_SETREF(file_obj, NULL);
225224
if (rv == NULL) {
226225
goto error;
227226
}

Modules/cjkcodecs/multibytecodec.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,8 +1463,7 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
14631463
goto errorexit;
14641464
}
14651465

1466-
Py_DECREF(cres);
1467-
cres = NULL;
1466+
Py_SETREF(cres, NULL);
14681467

14691468
if (sizehint < 0 || buf.writer.pos != 0 || rsize == 0)
14701469
break;

Modules/mathmodule.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,8 +3152,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
31523152
long i_result = PyLong_AsLongAndOverflow(result, &overflow);
31533153
/* If this already overflowed, don't even enter the loop. */
31543154
if (overflow == 0) {
3155-
Py_DECREF(result);
3156-
result = NULL;
3155+
Py_SETREF(result, NULL);
31573156
}
31583157
/* Loop over all the items in the iterable until we finish, we overflow
31593158
* or we found a non integer element */
@@ -3200,8 +3199,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
32003199
*/
32013200
if (PyFloat_CheckExact(result)) {
32023201
double f_result = PyFloat_AS_DOUBLE(result);
3203-
Py_DECREF(result);
3204-
result = NULL;
3202+
Py_SETREF(result, NULL);
32053203
while(result == NULL) {
32063204
item = PyIter_Next(iter);
32073205
if (item == NULL) {
@@ -3250,8 +3248,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
32503248
if (item == NULL) {
32513249
/* error, or end-of-sequence */
32523250
if (PyErr_Occurred()) {
3253-
Py_DECREF(result);
3254-
result = NULL;
3251+
Py_SETREF(result, NULL);
32553252
}
32563253
break;
32573254
}

Modules/nismodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,7 @@ nis_maps (PyObject *module, PyObject *args, PyObject *kwdict)
458458
if (!str || PyList_Append(list, str) < 0)
459459
{
460460
Py_XDECREF(str);
461-
Py_DECREF(list);
462-
list = NULL;
461+
Py_SETREF(list, NULL);
463462
break;
464463
}
465464
Py_DECREF(str);

Modules/posixmodule.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4036,14 +4036,12 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
40364036
Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
40374037
}
40384038
if (v == NULL) {
4039-
Py_DECREF(list);
4040-
list = NULL;
4039+
Py_SETREF(list, NULL);
40414040
break;
40424041
}
40434042
if (PyList_Append(list, v) != 0) {
40444043
Py_DECREF(v);
4045-
Py_DECREF(list);
4046-
list = NULL;
4044+
Py_SETREF(list, NULL);
40474045
break;
40484046
}
40494047
Py_DECREF(v);
@@ -13131,15 +13129,13 @@ os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
1313113129
PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
1313213130
trace - start);
1313313131
if (!attribute) {
13134-
Py_DECREF(result);
13135-
result = NULL;
13132+
Py_SETREF(result, NULL);
1313613133
goto exit;
1313713134
}
1313813135
error = PyList_Append(result, attribute);
1313913136
Py_DECREF(attribute);
1314013137
if (error) {
13141-
Py_DECREF(result);
13142-
result = NULL;
13138+
Py_SETREF(result, NULL);
1314313139
goto exit;
1314413140
}
1314513141
start = trace + 1;

Objects/abstract.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
806806
PyErr_Format(PyExc_TypeError,
807807
"__format__ must return a str, not %.200s",
808808
Py_TYPE(result)->tp_name);
809-
Py_DECREF(result);
810-
result = NULL;
809+
Py_SETREF(result, NULL);
811810
goto done;
812811
}
813812

@@ -2791,8 +2790,7 @@ PyObject_GetIter(PyObject *o)
27912790
"iter() returned non-iterator "
27922791
"of type '%.100s'",
27932792
Py_TYPE(res)->tp_name);
2794-
Py_DECREF(res);
2795-
res = NULL;
2793+
Py_SETREF(res, NULL);
27962794
}
27972795
return res;
27982796
}
@@ -2812,8 +2810,7 @@ PyObject_GetAIter(PyObject *o) {
28122810
PyErr_Format(PyExc_TypeError,
28132811
"aiter() returned not an async iterator of type '%.100s'",
28142812
Py_TYPE(it)->tp_name);
2815-
Py_DECREF(it);
2816-
it = NULL;
2813+
Py_SETREF(it, NULL);
28172814
}
28182815
return it;
28192816
}

Objects/classobject.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ method_repr(PyMethodObject *a)
283283
}
284284

285285
if (funcname != NULL && !PyUnicode_Check(funcname)) {
286-
Py_DECREF(funcname);
287-
funcname = NULL;
286+
Py_SETREF(funcname, NULL);
288287
}
289288

290289
/* XXX Shouldn't use repr()/%R here! */
@@ -484,8 +483,7 @@ instancemethod_repr(PyObject *self)
484483
return NULL;
485484
}
486485
if (funcname != NULL && !PyUnicode_Check(funcname)) {
487-
Py_DECREF(funcname);
488-
funcname = NULL;
486+
Py_SETREF(funcname, NULL);
489487
}
490488

491489
result = PyUnicode_FromFormat("<instancemethod %V at %p>",

Objects/descrobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,7 @@ descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
906906
descr->d_type = (PyTypeObject*)Py_XNewRef(type);
907907
descr->d_name = PyUnicode_InternFromString(name);
908908
if (descr->d_name == NULL) {
909-
Py_DECREF(descr);
910-
descr = NULL;
909+
Py_SETREF(descr, NULL);
911910
}
912911
else {
913912
descr->d_qualname = NULL;

Objects/fileobject.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ PyFile_GetLine(PyObject *f, int n)
6767
}
6868
if (result != NULL && !PyBytes_Check(result) &&
6969
!PyUnicode_Check(result)) {
70-
Py_DECREF(result);
71-
result = NULL;
70+
Py_SETREF(result, NULL);
7271
PyErr_SetString(PyExc_TypeError,
7372
"object.readline() returned non-string");
7473
}
@@ -77,8 +76,7 @@ PyFile_GetLine(PyObject *f, int n)
7776
const char *s = PyBytes_AS_STRING(result);
7877
Py_ssize_t len = PyBytes_GET_SIZE(result);
7978
if (len == 0) {
80-
Py_DECREF(result);
81-
result = NULL;
79+
Py_SETREF(result, NULL);
8280
PyErr_SetString(PyExc_EOFError,
8381
"EOF when reading a line");
8482
}
@@ -95,8 +93,7 @@ PyFile_GetLine(PyObject *f, int n)
9593
if (n < 0 && result != NULL && PyUnicode_Check(result)) {
9694
Py_ssize_t len = PyUnicode_GET_LENGTH(result);
9795
if (len == 0) {
98-
Py_DECREF(result);
99-
result = NULL;
96+
Py_SETREF(result, NULL);
10097
PyErr_SetString(PyExc_EOFError,
10198
"EOF when reading a line");
10299
}

Objects/typeobject.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,8 +1213,7 @@ type_repr(PyTypeObject *type)
12131213
if (mod == NULL)
12141214
PyErr_Clear();
12151215
else if (!PyUnicode_Check(mod)) {
1216-
Py_DECREF(mod);
1217-
mod = NULL;
1216+
Py_SETREF(mod, NULL);
12181217
}
12191218
name = type_qualname(type, NULL);
12201219
if (name == NULL) {
@@ -1288,8 +1287,7 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
12881287
int res = type->tp_init(obj, args, kwds);
12891288
if (res < 0) {
12901289
assert(_PyErr_Occurred(tstate));
1291-
Py_DECREF(obj);
1292-
obj = NULL;
1290+
Py_SETREF(obj, NULL);
12931291
}
12941292
else {
12951293
assert(!_PyErr_Occurred(tstate));
@@ -5007,8 +5005,7 @@ object_repr(PyObject *self)
50075005
if (mod == NULL)
50085006
PyErr_Clear();
50095007
else if (!PyUnicode_Check(mod)) {
5010-
Py_DECREF(mod);
5011-
mod = NULL;
5008+
Py_SETREF(mod, NULL);
50125009
}
50135010
name = type_qualname(type, NULL);
50145011
if (name == NULL) {
@@ -8107,8 +8104,7 @@ slot_tp_hash(PyObject *self)
81078104
func = lookup_maybe_method(self, &_Py_ID(__hash__), &unbound);
81088105

81098106
if (func == Py_None) {
8110-
Py_DECREF(func);
8111-
func = NULL;
8107+
Py_SETREF(func, NULL);
81128108
}
81138109

81148110
if (func == NULL) {

Python/bltinmodule.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
218218
"__class__ set to %.200R defining %.200R as %.200R";
219219
PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
220220
}
221-
Py_DECREF(cls);
222-
cls = NULL;
221+
Py_SETREF(cls, NULL);
223222
goto error;
224223
}
225224
}
@@ -2483,8 +2482,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
24832482
long i_result = PyLong_AsLongAndOverflow(result, &overflow);
24842483
/* If this already overflowed, don't even enter the loop. */
24852484
if (overflow == 0) {
2486-
Py_DECREF(result);
2487-
result = NULL;
2485+
Py_SETREF(result, NULL);
24882486
}
24892487
while(result == NULL) {
24902488
item = PyIter_Next(iter);
@@ -2534,8 +2532,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
25342532

25352533
if (PyFloat_CheckExact(result)) {
25362534
double f_result = PyFloat_AS_DOUBLE(result);
2537-
Py_DECREF(result);
2538-
result = NULL;
2535+
Py_SETREF(result, NULL);
25392536
while(result == NULL) {
25402537
item = PyIter_Next(iter);
25412538
if (item == NULL) {
@@ -2582,8 +2579,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
25822579
if (item == NULL) {
25832580
/* error, or end-of-sequence */
25842581
if (PyErr_Occurred()) {
2585-
Py_DECREF(result);
2586-
result = NULL;
2582+
Py_SETREF(result, NULL);
25872583
}
25882584
break;
25892585
}

Python/errors.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ _PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value,
3737
if (traceback != NULL && !PyTraceBack_Check(traceback)) {
3838
/* XXX Should never happen -- fatal error instead? */
3939
/* Well, it could be None. */
40-
Py_DECREF(traceback);
41-
traceback = NULL;
40+
Py_SETREF(traceback, NULL);
4241
}
4342

4443
/* Save these in locals to safeguard against recursive

0 commit comments

Comments
 (0)
0