summaryrefslogtreecommitdiffstats
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2016-03-24 23:30:32 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2016-03-24 23:30:32 (GMT)
commit27461683a9491efe58331a695c856fbb28bd4cba (patch)
treec9e1aa4c40c2da0246bcd6ef889f26b60c30a6d8 /Lib/warnings.py
parente0511e797c483e1203a096ff96e34dc95368f843 (diff)
downloadcpython-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/warnings.py')
-rw-r--r--Lib/warnings.py10
1 files changed, 8 insertions, 2 deletions
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