diff -r 23ab1197df0b Python/_warnings.c --- a/Python/_warnings.c Wed Nov 19 13:21:40 2014 +0200 +++ b/Python/_warnings.c Wed Nov 19 16:41:32 2014 +0100 @@ -520,26 +520,32 @@ setup_context(Py_ssize_t stack_level, Py *module = NULL; - /* Setup registry. */ - assert(globals != NULL); - assert(PyDict_Check(globals)); - *registry = PyDict_GetItemString(globals, "__warningregistry__"); - if (*registry == NULL) { - int rc; + /* during Python finalization, warnings may be emited after interp->sysdict + is cleared: see issue #22898 */ + if (globals != NULL) { + /* Setup registry. */ + assert(PyDict_Check(globals)); + *registry = PyDict_GetItemString(globals, "__warningregistry__"); + if (*registry == NULL) { + int rc; - *registry = PyDict_New(); - if (*registry == NULL) - return 0; + *registry = PyDict_New(); + if (*registry == NULL) + return 0; - rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); - if (rc < 0) - goto handle_error; + rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); + if (rc < 0) + goto handle_error; + } + else + Py_INCREF(*registry); + + /* Setup module. */ + *module = PyDict_GetItemString(globals, "__name__"); } else - Py_INCREF(*registry); + *module = NULL; - /* Setup module. */ - *module = PyDict_GetItemString(globals, "__name__"); if (*module == NULL) { *module = PyUnicode_FromString(""); if (*module == NULL) @@ -549,7 +555,10 @@ setup_context(Py_ssize_t stack_level, Py Py_INCREF(*module); /* Setup filename. */ - *filename = PyDict_GetItemString(globals, "__file__"); + if (globals != NULL) + *filename = PyDict_GetItemString(globals, "__file__"); + else + *filename = NULL; if (*filename != NULL && PyUnicode_Check(*filename)) { Py_ssize_t len; int kind;