summaryrefslogtreecommitdiffstats
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2016-03-24 23:33:12 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2016-03-24 23:33:12 (GMT)
commite091d32a7ac514a415161043c4a70e1765363c5a (patch)
treef1c6f637623209de8eab8ce03f8e9596f8909415 /Lib/warnings.py
parent3aac0adfe01b30fc58c638c5ab61844e80b3fd66 (diff)
parent27461683a9491efe58331a695c856fbb28bd4cba (diff)
downloadcpython-e091d32a7ac514a415161043c4a70e1765363c5a.zip
cpython-e091d32a7ac514a415161043c4a70e1765363c5a.tar.gz
cpython-e091d32a7ac514a415161043c4a70e1765363c5a.tar.bz2
Merge 3.5
Issue #21925: warnings.formatwarning() now catches exceptions when calling linecache.getline() and tracemalloc.get_object_traceback() to be able to log ResourceWarning emitted late during the Python shutdown process.
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r--Lib/warnings.py31
1 files changed, 26 insertions, 5 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py
index d4f591e..1ece514 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -33,26 +33,47 @@ def _showwarnmsg_impl(msg):
pass
def _formatwarnmsg_impl(msg):
- import linecache
s = ("%s:%s: %s: %s\n"
% (msg.filename, msg.lineno, msg.category.__name__,
msg.message))
+
if msg.line is None:
- line = linecache.getline(msg.filename, msg.lineno)
+ try:
+ import linecache
+ line = linecache.getline(msg.filename, msg.lineno)
+ except Exception:
+ # When a warning is logged during Python shutdown, linecache
+ # and the improt machinery don't work anymore
+ line = None
+ linecache = None
else:
line = msg.line
if line:
line = line.strip()
s += " %s\n" % line
+
if msg.source is not None:
- import tracemalloc
- tb = tracemalloc.get_object_traceback(msg.source)
+ try:
+ import tracemalloc
+ tb = tracemalloc.get_object_traceback(msg.source)
+ except Exception:
+ # When a warning is logged during Python shutdown, tracemalloc
+ # and the import machinery don't work anymore
+ tb = None
+
if tb is not None:
s += 'Object allocated at (most recent call first):\n'
for frame in tb:
s += (' File "%s", lineno %s\n'
% (frame.filename, frame.lineno))
- line = linecache.getline(frame.filename, frame.lineno)
+
+ try:
+ if linecache is not None:
+ line = linecache.getline(frame.filename, frame.lineno)
+ else:
+ line = None
+ except Exception:
+ line = None
if line:
line = line.strip()
s += ' %s\n' % line