diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2016-03-24 23:30:32 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2016-03-24 23:30:32 (GMT) |
commit | 27461683a9491efe58331a695c856fbb28bd4cba (patch) | |
tree | c9e1aa4c40c2da0246bcd6ef889f26b60c30a6d8 /Lib/test/test_warnings | |
parent | e0511e797c483e1203a096ff96e34dc95368f843 (diff) | |
download | cpython-27461683a9491efe58331a695c856fbb28bd4cba.zip cpython-27461683a9491efe58331a695c856fbb28bd4cba.tar.gz cpython-27461683a9491efe58331a695c856fbb28bd4cba.tar.bz2 |
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.
Diffstat (limited to 'Lib/test/test_warnings')
-rw-r--r-- | Lib/test/test_warnings/__init__.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index eda755d..633b2ac 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -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() |