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
}

0 commit comments

Comments
 (0)