Skip to content

Commit 9a91182

Browse files
authored
gh-99537: Use Py_CLEAR() function in C code (#99686)
Replace "Py_XDECREF(var); var = NULL;" with "Py_CLEAR(var);". Don't replace "Py_DECREF(var); var = NULL;" with "Py_CLEAR(var);". It would add an useless "if (var)" test in code path where var cannot be NULL.
1 parent 7e3f09c commit 9a91182

File tree

2 files changed

+5
-12
lines changed

2 files changed

+5
-12
lines changed

Modules/_xxsubinterpretersmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,8 +2383,7 @@ channel_list_interpreters(PyObject *self, PyObject *args, PyObject *kwds)
23832383
goto finally;
23842384

23852385
except:
2386-
Py_XDECREF(ids);
2387-
ids = NULL;
2386+
Py_CLEAR(ids);
23882387

23892388
finally:
23902389
return ids;

Modules/_zoneinfo.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ zoneinfo_new_instance(PyTypeObject *type, PyObject *key)
231231

232232
goto cleanup;
233233
error:
234-
Py_XDECREF(self);
235-
self = NULL;
234+
Py_CLEAR(self);
236235
cleanup:
237236
if (file_obj != NULL) {
238237
PyObject *exc, *val, *tb;
@@ -2606,14 +2605,9 @@ static PyMethodDef module_methods[] = {{NULL, NULL}};
26062605
static void
26072606
module_free(void *m)
26082607
{
2609-
Py_XDECREF(_tzpath_find_tzfile);
2610-
_tzpath_find_tzfile = NULL;
2611-
2612-
Py_XDECREF(_common_mod);
2613-
_common_mod = NULL;
2614-
2615-
Py_XDECREF(io_open);
2616-
io_open = NULL;
2608+
Py_CLEAR(_tzpath_find_tzfile);
2609+
Py_CLEAR(_common_mod);
2610+
Py_CLEAR(io_open);
26172611

26182612
xdecref_ttinfo(&NO_TTINFO);
26192613

0 commit comments

Comments
 (0)