]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-99537: Use Py_SETREF(var, NULL) in C code (#99687)
authorVictor Stinner <vstinner@python.org>
Wed, 23 Nov 2022 13:57:50 +0000 (14:57 +0100)
committerGitHub <noreply@github.com>
Wed, 23 Nov 2022 13:57:50 +0000 (14:57 +0100)
Replace "Py_DECREF(var); var = NULL;" with "Py_SETREF(var, NULL);".

22 files changed:
Modules/_abc.c
Modules/_datetimemodule.c
Modules/_elementtree.c
Modules/_io/_iomodule.c
Modules/_pickle.c
Modules/_scproxy.c
Modules/_struct.c
Modules/_tkinter.c
Modules/_xxsubinterpretersmodule.c
Modules/_zoneinfo.c
Modules/cjkcodecs/multibytecodec.c
Modules/mathmodule.c
Modules/nismodule.c
Modules/posixmodule.c
Objects/abstract.c
Objects/classobject.c
Objects/descrobject.c
Objects/fileobject.c
Objects/typeobject.c
Python/bltinmodule.c
Python/errors.c
Python/marshal.c

index e6e72427a0481d11b025a1a0707c3b6b7e0a7221..e146d4fd0cac39eccb241790421b8e1e538935fc 100644 (file)
@@ -624,8 +624,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
 
     switch (PyObject_IsTrue(result)) {
     case -1:
-        Py_DECREF(result);
-        result = NULL;
+        Py_SETREF(result, NULL);
         break;
     case 0:
         Py_DECREF(result);
index 712abc3346faf919f575e14384262ed8fd57e41e..eda8c5610ba659e608d1f4a9988835a169e71fa5 100644 (file)
@@ -1328,8 +1328,7 @@ call_tzname(PyObject *tzinfo, PyObject *tzinfoarg)
         PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must "
                      "return None or a string, not '%s'",
                      Py_TYPE(result)->tp_name);
-        Py_DECREF(result);
-        result = NULL;
+        Py_SETREF(result, NULL);
     }
 
     return result;
@@ -1849,8 +1848,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
     x2 = PyNumber_Multiply(x1, seconds_per_day);        /* days in seconds */
     if (x2 == NULL)
         goto Done;
-    Py_DECREF(x1);
-    x1 = NULL;
+    Py_SETREF(x1, NULL);
 
     /* x2 has days in seconds */
     x1 = PyLong_FromLong(GET_TD_SECONDS(self));         /* seconds */
@@ -1867,8 +1865,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
     x1 = PyNumber_Multiply(x3, us_per_second);          /* us */
     if (x1 == NULL)
         goto Done;
-    Py_DECREF(x3);
-    x3 = NULL;
+    Py_SETREF(x3, NULL);
 
     /* x1 has days+seconds in us */
     x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self));
@@ -2038,8 +2035,7 @@ multiply_truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *floatobj,
         goto error;
     }
     temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, op));
-    Py_DECREF(pyus_in);
-    pyus_in = NULL;
+    Py_SETREF(pyus_in, NULL);
     if (temp == NULL)
         goto error;
     pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, !op));
index 2da44cf2886ea42ac49f957aa6824d35dc2b302f..0c68ede42ca61da7d095e55ae5064682588eaaf6 100644 (file)
@@ -345,8 +345,7 @@ get_attrib_from_keywords(PyObject *kwds)
         }
         attrib = PyDict_Copy(attrib);
         if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
-            Py_DECREF(attrib);
-            attrib = NULL;
+            Py_SETREF(attrib, NULL);
         }
     }
     else if (!PyErr_Occurred()) {
index ccd40ab8bca451d77a97df3d8a10569ef3aba4c8..121d9617e1883b135bcf6269c2f60da4a00a13f2 100644 (file)
@@ -334,8 +334,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
         goto error;
     result = raw;
 
-    Py_DECREF(path_or_fd);
-    path_or_fd = NULL;
+    Py_SETREF(path_or_fd, NULL);
 
     modeobj = PyUnicode_FromString(mode);
     if (modeobj == NULL)
index 1e118e9cd10bbf25697355bc02e5f9fc7f60c754..2078779663a919cae58abb55081062509dcf6abf 100644 (file)
@@ -4344,8 +4344,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
         if (reduce_value != Py_NotImplemented) {
             goto reduce;
         }
-        Py_DECREF(reduce_value);
-        reduce_value = NULL;
+        Py_SETREF(reduce_value, NULL);
     }
 
     if (type == &PyType_Type) {
index 4c1f1aa300c7172474a334af4150106b3d659bdc..344b66f9aad5226763afbf213d4d31a8f4f8fec0 100644 (file)
@@ -84,7 +84,7 @@ get_proxy_settings(PyObject* Py_UNUSED(mod), PyObject *Py_UNUSED(ignored))
     if (v == NULL) goto error;
 
     r = PyDict_SetItemString(result, "exclude_simple", v);
-    Py_DECREF(v); v = NULL;
+    Py_SETREF(v, NULL);
     if (r == -1) goto error;
 
     anArray = CFDictionaryGetValue(proxyDict,
index 2f2eb25d98423098729125dfa51828c8a4b57633..c960b81b246ece4b93e730b8c87c31730a1924e6 100644 (file)
@@ -2164,8 +2164,7 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
     _structmodulestate *state = get_struct_state(module);
 
     if (fmt == NULL) {
-        Py_DECREF(*ptr);
-        *ptr = NULL;
+        Py_SETREF(*ptr, NULL);
         return 1;
     }
 
index 6ff7d2bfced29b2efca7f88c36a51168ac78ba0d..93d4474f65d62c918cd953b49eb6795e9715eeb6 100644 (file)
@@ -2163,8 +2163,7 @@ _tkinter_tkapp_splitlist(TkappObject *self, PyObject *arg)
     for (i = 0; i < argc; i++) {
         PyObject *s = unicodeFromTclString(argv[i]);
         if (!s) {
-            Py_DECREF(v);
-            v = NULL;
+            Py_SETREF(v, NULL);
             goto finally;
         }
         PyTuple_SET_ITEM(v, i, s);
index 244ae3517e1d8124606abe8981276baca66fa768..2c9e0cda1ab048004cd952696278069cae900d8d 100644 (file)
@@ -2320,8 +2320,7 @@ channel_list_all(PyObject *self, PyObject *Py_UNUSED(ignored))
         PyObject *id = (PyObject *)newchannelid(&ChannelIDtype, *cur, 0,
                                                 &_globals.channels, 0, 0);
         if (id == NULL) {
-            Py_DECREF(ids);
-            ids = NULL;
+            Py_SETREF(ids, NULL);
             break;
         }
         PyList_SET_ITEM(ids, (Py_ssize_t)i, id);
index cb7d4c943845b1efe5ddc372f44f8b684ec97b87..9d38589ea3d1b0af74cafb367b83038fcd456419 100644 (file)
@@ -220,8 +220,7 @@ zoneinfo_new_instance(PyTypeObject *type, PyObject *key)
     }
 
     PyObject *rv = PyObject_CallMethod(file_obj, "close", NULL);
-    Py_DECREF(file_obj);
-    file_obj = NULL;
+    Py_SETREF(file_obj, NULL);
     if (rv == NULL) {
         goto error;
     }
index 8b6232695d4c13e50295ffa375fb6058aea05728..1d77fd33ac3b87a04af19e4fd47d4cc8487e5e26 100644 (file)
@@ -1463,8 +1463,7 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
                 goto errorexit;
         }
 
-        Py_DECREF(cres);
-        cres = NULL;
+        Py_SETREF(cres, NULL);
 
         if (sizehint < 0 || buf.writer.pos != 0 || rsize == 0)
             break;
index 83eb338be9b83cafc5af649cf65f20e1c6224bc6..49c0293d4f5ce3c4c04ad942c6406d24b36790fc 100644 (file)
@@ -3152,8 +3152,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
         long i_result = PyLong_AsLongAndOverflow(result, &overflow);
         /* If this already overflowed, don't even enter the loop. */
         if (overflow == 0) {
-            Py_DECREF(result);
-            result = NULL;
+            Py_SETREF(result, NULL);
         }
         /* Loop over all the items in the iterable until we finish, we overflow
          * or we found a non integer element */
@@ -3200,8 +3199,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
     */
     if (PyFloat_CheckExact(result)) {
         double f_result = PyFloat_AS_DOUBLE(result);
-        Py_DECREF(result);
-        result = NULL;
+        Py_SETREF(result, NULL);
         while(result == NULL) {
             item = PyIter_Next(iter);
             if (item == NULL) {
@@ -3250,8 +3248,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
         if (item == NULL) {
             /* error, or end-of-sequence */
             if (PyErr_Occurred()) {
-                Py_DECREF(result);
-                result = NULL;
+                Py_SETREF(result, NULL);
             }
             break;
         }
index 39b991162b2761315aecd60e21d2691ac1e65d05..ec7f6d8031e84bc0f9a97ab93fb3eb9bb0505304 100644 (file)
@@ -458,8 +458,7 @@ nis_maps (PyObject *module, PyObject *args, PyObject *kwdict)
         if (!str || PyList_Append(list, str) < 0)
         {
             Py_XDECREF(str);
-            Py_DECREF(list);
-            list = NULL;
+            Py_SETREF(list, NULL);
             break;
         }
         Py_DECREF(str);
index 45e71ee9c0598dc1156596b99f60657d69ad31b9..8185517b06b5dd102d3b622855cfe071b92c050b 100644 (file)
@@ -4036,14 +4036,12 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
                 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
             }
             if (v == NULL) {
-                Py_DECREF(list);
-                list = NULL;
+                Py_SETREF(list, NULL);
                 break;
             }
             if (PyList_Append(list, v) != 0) {
                 Py_DECREF(v);
-                Py_DECREF(list);
-                list = NULL;
+                Py_SETREF(list, NULL);
                 break;
             }
             Py_DECREF(v);
@@ -13131,15 +13129,13 @@ os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
                 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
                                                                  trace - start);
                 if (!attribute) {
-                    Py_DECREF(result);
-                    result = NULL;
+                    Py_SETREF(result, NULL);
                     goto exit;
                 }
                 error = PyList_Append(result, attribute);
                 Py_DECREF(attribute);
                 if (error) {
-                    Py_DECREF(result);
-                    result = NULL;
+                    Py_SETREF(result, NULL);
                     goto exit;
                 }
                 start = trace + 1;
index 8aa3fc17c6341b193582b8b15187486122b237ff..9dc74fb9c2608c3337919b03a9441f86be2aadbe 100644 (file)
@@ -806,8 +806,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
         PyErr_Format(PyExc_TypeError,
                      "__format__ must return a str, not %.200s",
                      Py_TYPE(result)->tp_name);
-        Py_DECREF(result);
-        result = NULL;
+        Py_SETREF(result, NULL);
         goto done;
     }
 
@@ -2791,8 +2790,7 @@ PyObject_GetIter(PyObject *o)
                          "iter() returned non-iterator "
                          "of type '%.100s'",
                          Py_TYPE(res)->tp_name);
-            Py_DECREF(res);
-            res = NULL;
+            Py_SETREF(res, NULL);
         }
         return res;
     }
@@ -2812,8 +2810,7 @@ PyObject_GetAIter(PyObject *o) {
         PyErr_Format(PyExc_TypeError,
                      "aiter() returned not an async iterator of type '%.100s'",
                      Py_TYPE(it)->tp_name);
-        Py_DECREF(it);
-        it = NULL;
+        Py_SETREF(it, NULL);
     }
     return it;
 }
index eedf8f0e1e1acf6ec509b21acb48686664897187..2cb192e725d40d076832f4c68e85053adb09923f 100644 (file)
@@ -283,8 +283,7 @@ method_repr(PyMethodObject *a)
     }
 
     if (funcname != NULL && !PyUnicode_Check(funcname)) {
-        Py_DECREF(funcname);
-        funcname = NULL;
+        Py_SETREF(funcname, NULL);
     }
 
     /* XXX Shouldn't use repr()/%R here! */
@@ -484,8 +483,7 @@ instancemethod_repr(PyObject *self)
         return NULL;
     }
     if (funcname != NULL && !PyUnicode_Check(funcname)) {
-        Py_DECREF(funcname);
-        funcname = NULL;
+        Py_SETREF(funcname, NULL);
     }
 
     result = PyUnicode_FromFormat("<instancemethod %V at %p>",
index cc204931c3fe3dd7a3c0dd19cc6f6573c1707d55..c545b90c6283e11dbdbbe92a1d3abdead29a4d89 100644 (file)
@@ -906,8 +906,7 @@ descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
         descr->d_type = (PyTypeObject*)Py_XNewRef(type);
         descr->d_name = PyUnicode_InternFromString(name);
         if (descr->d_name == NULL) {
-            Py_DECREF(descr);
-            descr = NULL;
+            Py_SETREF(descr, NULL);
         }
         else {
             descr->d_qualname = NULL;
index bf56be5f7ea7b040cb8f1f8abc2dc437bfd84755..e99e155f2b8c98addbeeec4708937ba21b7a0b01 100644 (file)
@@ -67,8 +67,7 @@ PyFile_GetLine(PyObject *f, int n)
     }
     if (result != NULL && !PyBytes_Check(result) &&
         !PyUnicode_Check(result)) {
-        Py_DECREF(result);
-        result = NULL;
+        Py_SETREF(result, NULL);
         PyErr_SetString(PyExc_TypeError,
                    "object.readline() returned non-string");
     }
@@ -77,8 +76,7 @@ PyFile_GetLine(PyObject *f, int n)
         const char *s = PyBytes_AS_STRING(result);
         Py_ssize_t len = PyBytes_GET_SIZE(result);
         if (len == 0) {
-            Py_DECREF(result);
-            result = NULL;
+            Py_SETREF(result, NULL);
             PyErr_SetString(PyExc_EOFError,
                             "EOF when reading a line");
         }
@@ -95,8 +93,7 @@ PyFile_GetLine(PyObject *f, int n)
     if (n < 0 && result != NULL && PyUnicode_Check(result)) {
         Py_ssize_t len = PyUnicode_GET_LENGTH(result);
         if (len == 0) {
-            Py_DECREF(result);
-            result = NULL;
+            Py_SETREF(result, NULL);
             PyErr_SetString(PyExc_EOFError,
                             "EOF when reading a line");
         }
index 312406993c5b23109d29ca9fce598bcf625834db..ad8a936fa7ce20dc8e28a79efac039a00cbfe0d4 100644 (file)
@@ -1213,8 +1213,7 @@ type_repr(PyTypeObject *type)
     if (mod == NULL)
         PyErr_Clear();
     else if (!PyUnicode_Check(mod)) {
-        Py_DECREF(mod);
-        mod = NULL;
+        Py_SETREF(mod, NULL);
     }
     name = type_qualname(type, NULL);
     if (name == NULL) {
@@ -1288,8 +1287,7 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
         int res = type->tp_init(obj, args, kwds);
         if (res < 0) {
             assert(_PyErr_Occurred(tstate));
-            Py_DECREF(obj);
-            obj = NULL;
+            Py_SETREF(obj, NULL);
         }
         else {
             assert(!_PyErr_Occurred(tstate));
@@ -5007,8 +5005,7 @@ object_repr(PyObject *self)
     if (mod == NULL)
         PyErr_Clear();
     else if (!PyUnicode_Check(mod)) {
-        Py_DECREF(mod);
-        mod = NULL;
+        Py_SETREF(mod, NULL);
     }
     name = type_qualname(type, NULL);
     if (name == NULL) {
@@ -8107,8 +8104,7 @@ slot_tp_hash(PyObject *self)
     func = lookup_maybe_method(self, &_Py_ID(__hash__), &unbound);
 
     if (func == Py_None) {
-        Py_DECREF(func);
-        func = NULL;
+        Py_SETREF(func, NULL);
     }
 
     if (func == NULL) {
index c2cf79a727f0a895ee06aecbc3a7794a4bfe050b..b3b7e8d6c505309e5345d8b64cadf387fd5c7ddb 100644 (file)
@@ -218,8 +218,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
                         "__class__ set to %.200R defining %.200R as %.200R";
                     PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
                 }
-                Py_DECREF(cls);
-                cls = NULL;
+                Py_SETREF(cls, NULL);
                 goto error;
             }
         }
@@ -2483,8 +2482,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
         long i_result = PyLong_AsLongAndOverflow(result, &overflow);
         /* If this already overflowed, don't even enter the loop. */
         if (overflow == 0) {
-            Py_DECREF(result);
-            result = NULL;
+            Py_SETREF(result, NULL);
         }
         while(result == NULL) {
             item = PyIter_Next(iter);
@@ -2534,8 +2532,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
 
     if (PyFloat_CheckExact(result)) {
         double f_result = PyFloat_AS_DOUBLE(result);
-        Py_DECREF(result);
-        result = NULL;
+        Py_SETREF(result, NULL);
         while(result == NULL) {
             item = PyIter_Next(iter);
             if (item == NULL) {
@@ -2582,8 +2579,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
         if (item == NULL) {
             /* error, or end-of-sequence */
             if (PyErr_Occurred()) {
-                Py_DECREF(result);
-                result = NULL;
+                Py_SETREF(result, NULL);
             }
             break;
         }
index 6a42f5912f942abcc17238d1afb70b0091d1c468..05ef62246ec0a46b14c7af49ef32a55ff6f52049 100644 (file)
@@ -37,8 +37,7 @@ _PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value,
     if (traceback != NULL && !PyTraceBack_Check(traceback)) {
         /* XXX Should never happen -- fatal error instead? */
         /* Well, it could be None. */
-        Py_DECREF(traceback);
-        traceback = NULL;
+        Py_SETREF(traceback, NULL);
     }
 
     /* Save these in locals to safeguard against recursive
index 86cf57daa7702590f825c84ab4b0e4a01fa289b3..5f392d9e1ecfff3a2e1e8b4fde1983c95f0c0704 100644 (file)
@@ -1217,8 +1217,7 @@ r_object(RFILE *p)
                 if (!PyErr_Occurred())
                     PyErr_SetString(PyExc_TypeError,
                         "NULL object in marshal data for tuple");
-                Py_DECREF(v);
-                v = NULL;
+                Py_SETREF(v, NULL);
                 break;
             }
             PyTuple_SET_ITEM(v, i, v2);
@@ -1244,8 +1243,7 @@ r_object(RFILE *p)
                 if (!PyErr_Occurred())
                     PyErr_SetString(PyExc_TypeError,
                         "NULL object in marshal data for list");
-                Py_DECREF(v);
-                v = NULL;
+                Py_SETREF(v, NULL);
                 break;
             }
             PyList_SET_ITEM(v, i, v2);
@@ -1277,8 +1275,7 @@ r_object(RFILE *p)
             Py_DECREF(val);
         }
         if (PyErr_Occurred()) {
-            Py_DECREF(v);
-            v = NULL;
+            Py_SETREF(v, NULL);
         }
         retval = v;
         break;
@@ -1322,8 +1319,7 @@ r_object(RFILE *p)
                     if (!PyErr_Occurred())
                         PyErr_SetString(PyExc_TypeError,
                             "NULL object in marshal data for set");
-                    Py_DECREF(v);
-                    v = NULL;
+                    Py_SETREF(v, NULL);
                     break;
                 }
                 if (PySet_Add(v, v2) == -1) {