bpo-35454: Fix miscellaneous minor issues in error handling. (#11077)

* bpo-35454: Fix miscellaneous minor issues in error handling.

* Fix a null pointer dereference.
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 62374d8..12e418d 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -352,7 +352,10 @@
             return NULL;
         }
         attrib = PyDict_Copy(attrib);
-        PyDict_DelItem(kwds, attrib_str);
+        if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
+            Py_DECREF(attrib);
+            attrib = NULL;
+        }
     } else {
         attrib = PyDict_New();
     }
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index d075ef7..aacce69 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -777,9 +777,11 @@
         for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
             tstate;
             tstate = PyThreadState_Next(tstate))
-            if (tstate->dict &&
-                PyDict_GetItem(tstate->dict, self->key))
-                PyDict_DelItem(tstate->dict, self->key);
+            if (tstate->dict && PyDict_GetItem(tstate->dict, self->key)) {
+                if (PyDict_DelItem(tstate->dict, self->key)) {
+                    PyErr_Clear();
+                }
+            }
     }
     return 0;
 }
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 40f1ca6..73d3e1a 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -362,10 +362,14 @@
         else {
             if (PyDict_GetItemString(
                     dict,
-                    win_runtime_flags[i].flag_name) != NULL) {
-                PyDict_DelItemString(
-                    dict,
-                    win_runtime_flags[i].flag_name);
+                    win_runtime_flags[i].flag_name) != NULL)
+            {
+                if (PyDict_DelItemString(
+                        dict,
+                        win_runtime_flags[i].flag_name))
+                {
+                    PyErr_Clear();
+                }
             }
         }
     }