mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
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:
parent
e0511e797c
commit
27461683a9
3 changed files with 29 additions and 2 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue