Issue #20440: Cleaning up the code by using Py_SETREF and Py_CLEAR.
Old code is correct, but with Py_SETREF and Py_CLEAR it can be cleaner.
This patch doesn't fix bugs and hence there is no need to backport it.
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index e3d1910..6a53584 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -1222,7 +1222,6 @@
 static int
 deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
 {
-    PyObject *old_value;
     block *b;
     Py_ssize_t n, len=Py_SIZE(deque), halflen=(len+1)>>1, index=i;
 
@@ -1249,9 +1248,7 @@
             b = b->leftlink;
     }
     Py_INCREF(v);
-    old_value = b->data[i];
-    b->data[i] = v;
-    Py_DECREF(old_value);
+    Py_SETREF(b->data[i], v);
     return 0;
 }
 
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 5db9f90..dddeba0 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -5123,23 +5123,22 @@
 comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
 {
     PyObject *hresult, *text, *details;
-    PyBaseExceptionObject *bself;
     PyObject *a;
     int status;
 
     if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
-    return -1;
+        return -1;
 
     if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details))
         return -1;
 
     a = PySequence_GetSlice(args, 1, PySequence_Size(args));
     if (!a)
-    return -1;
+        return -1;
     status = PyObject_SetAttrString(self, "args", a);
     Py_DECREF(a);
     if (status < 0)
-    return -1;
+        return -1;
 
     if (PyObject_SetAttrString(self, "hresult", hresult) < 0)
         return -1;
@@ -5150,9 +5149,8 @@
     if (PyObject_SetAttrString(self, "details", details) < 0)
         return -1;
 
-    bself = (PyBaseExceptionObject *)self;
     Py_INCREF(args);
-    Py_SETREF(bself->args, args);
+    Py_SETREF((PyBaseExceptionObject *)self->args, args);
 
     return 0;
 }
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index f16d48f..580c53a 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -2338,13 +2338,9 @@
                                        PyObject *element_factory)
 /*[clinic end generated code: output=91cfa7558970ee96 input=1b424eeefc35249c]*/
 {
-    PyObject *tmp;
-
     if (element_factory) {
         Py_INCREF(element_factory);
-        tmp = self->element_factory;
-        self->element_factory = element_factory;
-        Py_XDECREF(tmp);
+        Py_SETREF(self->element_factory, element_factory);
     }
 
     return 0;
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index 99e71bc..6333e46 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -87,7 +87,7 @@
 static int
 unshare_buffer(bytesio *self, size_t size)
 {
-    PyObject *new_buf, *old_buf;
+    PyObject *new_buf;
     assert(SHARED_BUF(self));
     assert(self->exports == 0);
     assert(size >= (size_t)self->string_size);
@@ -96,9 +96,7 @@
         return -1;
     memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf),
            self->string_size);
-    old_buf = self->buf;
-    self->buf = new_buf;
-    Py_DECREF(old_buf);
+    Py_SETREF(self->buf, new_buf);
     return 0;
 }
 
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 2bd0594..a4ffa06 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -77,7 +77,7 @@
 static PyObject *
 groupby_next(groupbyobject *gbo)
 {
-    PyObject *newvalue, *newkey, *r, *grouper, *tmp;
+    PyObject *newvalue, *newkey, *r, *grouper;
 
     /* skip to next iteration group */
     for (;;) {
@@ -110,19 +110,12 @@
             }
         }
 
-        tmp = gbo->currkey;
-        gbo->currkey = newkey;
-        Py_XDECREF(tmp);
-
-        tmp = gbo->currvalue;
-        gbo->currvalue = newvalue;
-        Py_XDECREF(tmp);
+        Py_SETREF(gbo->currkey, newkey);
+        Py_SETREF(gbo->currvalue, newvalue);
     }
 
     Py_INCREF(gbo->currkey);
-    tmp = gbo->tgtkey;
-    gbo->tgtkey = gbo->currkey;
-    Py_XDECREF(tmp);
+    Py_SETREF(gbo->tgtkey, gbo->currkey);
 
     grouper = _grouper_create(gbo, gbo->tgtkey);
     if (grouper == NULL)
@@ -3445,7 +3438,7 @@
 static PyObject *
 accumulate_next(accumulateobject *lz)
 {
-    PyObject *val, *oldtotal, *newtotal;
+    PyObject *val, *newtotal;
 
     val = (*Py_TYPE(lz->it)->tp_iternext)(lz->it);
     if (val == NULL)
@@ -3465,11 +3458,8 @@
     if (newtotal == NULL)
         return NULL;
 
-    oldtotal = lz->total;
-    lz->total = newtotal;
-    Py_DECREF(oldtotal);
-
     Py_INCREF(newtotal);
+    Py_SETREF(lz->total, newtotal);
     return newtotal;
 }
 
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index 9267c69..c6d3515 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -1226,12 +1226,8 @@
     self->itself = NULL;
 
     if (self->handlers != NULL) {
-        PyObject *temp;
-        for (i = 0; handler_info[i].name != NULL; i++) {
-            temp = self->handlers[i];
-            self->handlers[i] = NULL;
-            Py_XDECREF(temp);
-        }
+        for (i = 0; handler_info[i].name != NULL; i++)
+            Py_CLEAR(self->handlers[i]);
         PyMem_Free(self->handlers);
         self->handlers = NULL;
     }
@@ -1345,7 +1341,6 @@
     int handlernum = handlername2int(name);
     if (handlernum >= 0) {
         xmlhandler c_handler = NULL;
-        PyObject *temp = self->handlers[handlernum];
 
         if (v == Py_None) {
             /* If this is the character data handler, and a character
@@ -1367,8 +1362,7 @@
             Py_INCREF(v);
             c_handler = handler_info[handlernum].handler;
         }
-        self->handlers[handlernum] = v;
-        Py_XDECREF(temp);
+        Py_SETREF(self->handlers[handlernum], v);
         handler_info[handlernum].setter(self->itself, c_handler);
         return 1;
     }
@@ -1898,15 +1892,12 @@
 clear_handlers(xmlparseobject *self, int initial)
 {
     int i = 0;
-    PyObject *temp;
 
     for (; handler_info[i].name != NULL; i++) {
         if (initial)
             self->handlers[i] = NULL;
         else {
-            temp = self->handlers[i];
-            self->handlers[i] = NULL;
-            Py_XDECREF(temp);
+            Py_CLEAR(self->handlers[i]);
             handler_info[i].setter(self->itself, NULL);
         }
     }