Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dcdd05b

Browse files
committed
Close #19442: warn_explicit() does nothing when called late during Python shutdown
After more tests, I now think that it is the safest option.
1 parent 6ec518b commit dcdd05b

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

Python/_warnings.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ warn_explicit(PyObject *category, PyObject *message,
333333
PyObject *action;
334334
int rc;
335335

336+
/* module can be None if a warning is emitted late during Python shutdown.
337+
In this case, the Python warnings module was probably unloaded, filters
338+
are no more available to choose as action. It is safer to ignore the
339+
warning and do nothing. */
340+
if (module == Py_None)
341+
Py_RETURN_NONE;
342+
336343
if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
337344
PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
338345
return NULL;
@@ -635,15 +642,8 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level)
635642
if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
636643
return NULL;
637644

638-
if (module != Py_None) {
639-
res = warn_explicit(category, message, filename, lineno, module, registry,
640-
NULL);
641-
}
642-
else {
643-
/* FIXME: emitting warnings at exit does crash Python */
644-
res = Py_None;
645-
Py_INCREF(res);
646-
}
645+
res = warn_explicit(category, message, filename, lineno, module, registry,
646+
NULL);
647647
Py_DECREF(filename);
648648
Py_DECREF(registry);
649649
Py_DECREF(module);

0 commit comments

Comments
 (0)