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


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

* Fix a null pointer dereference.
(cherry picked from commit 8905fcc85a6fc3ac394bc89b0bbf40897e9497a6)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 29e475d..80dca65 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -256,7 +256,11 @@
     version_obj = _PyDict_GetItemId(registry, &PyId_version);
     if (version_obj == NULL
         || !PyLong_CheckExact(version_obj)
-        || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
+        || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version)
+    {
+        if (PyErr_Occurred()) {
+            return -1;
+        }
         PyDict_Clear(registry);
         version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
         if (version_obj == NULL)