Issue #20440: More use of Py_SETREF.
This patch is manually crafted and contains changes that couldn't be handled
automatically.
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 351304f..85f9472 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -561,12 +561,14 @@
 
     if (size == 0)
         return 0;
-    Py_CLEAR(self->code);
-    if (size == 1)
-        self->code = PyTuple_GET_ITEM(args, 0);
-    else /* size > 1 */
-        self->code = args;
-    Py_INCREF(self->code);
+    if (size == 1) {
+        Py_INCREF(PyTuple_GET_ITEM(args, 0));
+        Py_SETREF(self->code, PyTuple_GET_ITEM(args, 0));
+    }
+    else { /* size > 1 */
+        Py_INCREF(args);
+        Py_SETREF(self->code, args);
+    }
     return 0;
 }
 
@@ -625,9 +627,8 @@
 #define GET_KWD(kwd) { \
     kwd = PyDict_GetItemString(kwds, #kwd); \
     if (kwd) { \
-        Py_CLEAR(self->kwd); \
-        self->kwd = kwd;   \
-        Py_INCREF(self->kwd);\
+        Py_INCREF(kwd); \
+        Py_SETREF(self->kwd, kwd); \
         if (PyDict_DelItemString(kwds, #kwd)) \
             return -1; \
     } \
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index c5b35e1..c957929 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -14191,8 +14191,8 @@
         if (key == NULL)
             return -1;
         if (ctx->args_owned) {
-            Py_DECREF(ctx->args);
             ctx->args_owned = 0;
+            Py_DECREF(ctx->args);
         }
         ctx->args = PyObject_GetItem(ctx->dict, key);
         Py_DECREF(key);