summaryrefslogtreecommitdiff
path: root/Python/_warnings.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2013-10-30 00:04:59 +0100
committerVictor Stinner <victor.stinner@gmail.com>2013-10-30 00:04:59 +0100
commitc841e5e08ebc10d98fb86da19bbcba2a7b9e0957 (patch)
tree6bcd8ae0984e71d0b23c3ef5f264199763d37117 /Python/_warnings.c
parentd7e014fb06d66ec8c76212d717ff03b963546622 (diff)
downloadcpython-c841e5e08ebc10d98fb86da19bbcba2a7b9e0957.tar.gz
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.
Diffstat (limited to 'Python/_warnings.c')
-rw-r--r--Python/_warnings.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c
index cbc64e3b94..8d9666afcc 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -540,7 +540,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
}
else {
*filename = NULL;
- if (PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) {
+ if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) {
PyObject *argv = PySys_GetObject("argv");
/* PyList_Check() is needed because sys.argv is set to None during
Python finalization */
@@ -564,8 +564,8 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
else {
/* embedded interpreters don't have sys.argv, see bug #839151 */
*filename = PyUnicode_FromString("__main__");
- if (*filename == NULL)
- goto handle_error;
+ if (*filename == NULL)
+ goto handle_error;
}
}
if (*filename == NULL) {
@@ -621,8 +621,15 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level)
if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
return NULL;
- res = warn_explicit(category, message, filename, lineno, module, registry,
- 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);
+ }
Py_DECREF(filename);
Py_DECREF(registry);
Py_DECREF(module);