Issue #19442: Fix warnings emitted during Python shutdown

Warnings may be emitted during Python shutdown, like "unclosed file XXX".
During shutdown, globals()['__main__'] may be None.
This commit is contained in:
Victor Stinner 2013-10-30 00:04:59 +01:00
parent c0e07a3ea0
commit 856f45f09c

View file

@ -540,7 +540,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
} }
else { else {
*filename = NULL; *filename = NULL;
if (PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) { if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) {
PyObject *argv = PySys_GetObject("argv"); PyObject *argv = PySys_GetObject("argv");
/* PyList_Check() is needed because sys.argv is set to None during /* PyList_Check() is needed because sys.argv is set to None during
Python finalization */ Python finalization */
@ -621,8 +621,15 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level)
if (!setup_context(stack_level, &filename, &lineno, &module, &registry)) if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
return NULL; return NULL;
if (module != Py_None) {
res = warn_explicit(category, message, filename, lineno, module, registry, res = warn_explicit(category, message, filename, lineno, module, registry,
NULL); NULL);
}
else {
/* FIXME: emitting warnings at exit does crash Python */
res = Py_None;
Py_INCREF(res);
}
Py_DECREF(filename); Py_DECREF(filename);
Py_DECREF(registry); Py_DECREF(registry);
Py_DECREF(module); Py_DECREF(module);