bpo-36370: Check for PyErr_Occurred() after PyImport_GetModule() (GH-12504)

diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 94b6d43..d8e6f8f 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -2239,8 +2239,10 @@
     PyObject *result;
     PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
     if (threading == NULL) {
-        /* threading not imported */
-        PyErr_Clear();
+        if (PyErr_Occurred()) {
+            PyErr_WriteUnraisable(NULL);
+        }
+        /* else: threading not imported */
         return;
     }
     result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL);