From e0511e797c483e1203a096ff96e34dc95368f843 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 25 Mar 2016 00:28:56 +0100 Subject: Fix test_warnings.test_improper_option() test_warnings: only run test_improper_option() and test_warnings_bootstrap() once. The unit test doesn't depend on self.module. --- Lib/test/test_warnings/__init__.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index cea9c57..eda755d 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -534,6 +534,14 @@ class WCmdLineTests(BaseTest): self.module._setoption('error::Warning::0') self.assertRaises(UserWarning, self.module.warn, 'convert to error') + +class CWCmdLineTests(WCmdLineTests, unittest.TestCase): + module = c_warnings + + +class PyWCmdLineTests(WCmdLineTests, unittest.TestCase): + module = py_warnings + def test_improper_option(self): # Same as above, but check that the message is printed out when # the interpreter is executed. This also checks that options are @@ -550,12 +558,6 @@ class WCmdLineTests(BaseTest): self.assertFalse(out.strip()) self.assertNotIn(b'RuntimeWarning', err) -class CWCmdLineTests(WCmdLineTests, unittest.TestCase): - module = c_warnings - -class PyWCmdLineTests(WCmdLineTests, unittest.TestCase): - module = py_warnings - class _WarningsTests(BaseTest, unittest.TestCase): @@ -931,6 +933,7 @@ class BootstrapTest(unittest.TestCase): # Use -W to load warnings module at startup assert_python_ok('-c', 'pass', '-W', 'always', PYTHONPATH=cwd) + class FinalizationTest(unittest.TestCase): def test_finalization(self): # Issue #19421: warnings.warn() should not crash -- cgit v0.12 From 27461683a9491efe58331a695c856fbb28bd4cba Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 25 Mar 2016 00:30:32 +0100 Subject: 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. --- Lib/test/test_warnings/__init__.py | 17 +++++++++++++++++ Lib/warnings.py | 10 ++++++++-- Misc/NEWS | 4 ++++ 3 files changed, 29 insertions(+), 2 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() diff --git a/Lib/warnings.py b/Lib/warnings.py index 1d4fb20..cf9f5b2 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -21,9 +21,15 @@ def showwarning(message, category, filename, lineno, file=None, line=None): def formatwarning(message, category, filename, lineno, line=None): """Function to format a warning the standard way.""" - import linecache s = "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message) - line = linecache.getline(filename, lineno) if line is None else line + if line is None: + try: + import linecache + line = linecache.getline(filename, lineno) + except Exception: + # When a warning is logged during Python shutdown, linecache + # and the improt machinery don't work anymore + line = None if line: line = line.strip() s += " %s\n" % line diff --git a/Misc/NEWS b/Misc/NEWS index 0058124..3baeeec 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -94,6 +94,10 @@ Core and Builtins Library ------- +- Issue #21925: :func:`warnings.formatwarning` now catches exceptions on + ``linecache.getline(...)`` to be able to log :exc:`ResourceWarning` emitted + late during the Python shutdown process. + - Issue #24266: Ctrl+C during Readline history search now cancels the search mode when compiled with Readline 7. -- cgit v0.12