[Python-checkins] gh-99537: Use Py_CLEAR() function in C code (#99686)

vstinner webhook-mailer at python.org
Tue Nov 22 09:23:03 EST 2022


https://github.com/python/cpython/commit/9a91182d4a87e4511dad20ad101e3eab0e1c5088
commit: 9a91182d4a87e4511dad20ad101e3eab0e1c5088
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-11-22T15:22:55+01:00
summary:

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.

files:
M Modules/_xxsubinterpretersmodule.c
M Modules/_zoneinfo.c

diff --git a/Modules/_xxsubinterpretersmodule.c b/Modules/_xxsubinterpretersmodule.c
index e65137e58fb5..244ae3517e1d 100644
--- a/Modules/_xxsubinterpretersmodule.c
+++ b/Modules/_xxsubinterpretersmodule.c
@@ -2383,8 +2383,7 @@ channel_list_interpreters(PyObject *self, PyObject *args, PyObject *kwds)
     goto finally;
 
 except:
-    Py_XDECREF(ids);
-    ids = NULL;
+    Py_CLEAR(ids);
 
 finally:
     return ids;
diff --git a/Modules/_zoneinfo.c b/Modules/_zoneinfo.c
index c0fef13ba17d..cb7d4c943845 100644
--- a/Modules/_zoneinfo.c
+++ b/Modules/_zoneinfo.c
@@ -231,8 +231,7 @@ zoneinfo_new_instance(PyTypeObject *type, PyObject *key)
 
     goto cleanup;
 error:
-    Py_XDECREF(self);
-    self = NULL;
+    Py_CLEAR(self);
 cleanup:
     if (file_obj != NULL) {
         PyObject *exc, *val, *tb;
@@ -2606,14 +2605,9 @@ static PyMethodDef module_methods[] = {{NULL, NULL}};
 static void
 module_free(void *m)
 {
-    Py_XDECREF(_tzpath_find_tzfile);
-    _tzpath_find_tzfile = NULL;
-
-    Py_XDECREF(_common_mod);
-    _common_mod = NULL;
-
-    Py_XDECREF(io_open);
-    io_open = NULL;
+    Py_CLEAR(_tzpath_find_tzfile);
+    Py_CLEAR(_common_mod);
+    Py_CLEAR(io_open);
 
     xdecref_ttinfo(&NO_TTINFO);
 



More information about the Python-checkins mailing list