warnings.formatwarning(): catch exceptions

Issue #21925: warnings.formatwarning() now catches exceptions on
linecache.getline(...) to be able to log ResourceWarning emitted late during
the Python shutdown process.
This commit is contained in:
Victor Stinner 2016-03-25 00:30:32 +01:00
parent e0511e797c
commit 27461683a9
3 changed files with 29 additions and 2 deletions

View file

@ -953,6 +953,23 @@ a=A()
# of the script
self.assertEqual(err, b'__main__:7: UserWarning: test')
def test_late_resource_warning(self):
# Issue #21925: Emitting a ResourceWarning late during the Python
# shutdown must be logged.
expected = b"sys:1: ResourceWarning: unclosed file "
# don't import the warnings module
# (_warnings will try to import it)
code = "f = open(%a)" % __file__
rc, out, err = assert_python_ok("-c", code)
self.assertTrue(err.startswith(expected), ascii(err))
# import the warnings module
code = "import warnings; f = open(%a)" % __file__
rc, out, err = assert_python_ok("-c", code)
self.assertTrue(err.startswith(expected), ascii(err))
def setUpModule():
py_warnings.onceregistry.clear()