diff Python/_warnings.c @ 86816:13a05ed33cf7

Close #19442: warn_explicit() does nothing when called late during Python shutdown After more tests, I now think that it is the safest option.
author Victor Stinner <victor.stinner@gmail.com>
date Fri, 01 Nov 2013 00:55:30 +0100
parents e035b4656088
children 954167ce92a3
line wrap: on
line diff
--- a/Python/_warnings.c	Thu Oct 31 18:44:48 2013 +0000
+++ b/Python/_warnings.c	Fri Nov 01 00:55:30 2013 +0100
@@ -333,6 +333,13 @@
     PyObject *action;
     int rc;
 
+    /* module can be None if a warning is emitted late during Python shutdown.
+       In this case, the Python warnings module was probably unloaded, filters
+       are no more available to choose as action. It is safer to ignore the
+       warning and do nothing. */
+    if (module == Py_None)
+        Py_RETURN_NONE;
+
     if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
         PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
         return NULL;
@@ -635,15 +642,8 @@
     if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
         return NULL;
 
-    if (module != Py_None) {
-        res = warn_explicit(category, message, filename, lineno, module, registry,
-                            NULL);
-    }
-    else {
-        /* FIXME: emitting warnings at exit does crash Python */
-        res = Py_None;
-        Py_INCREF(res);
-    }
+    res = warn_explicit(category, message, filename, lineno, module, registry,
+                        NULL);
     Py_DECREF(filename);
     Py_DECREF(registry);
     Py_DECREF(module);