mirror of
https://github.com/python/cpython.git
synced 2025-10-23 15:12:02 +00:00
Issue #3926: Fix the usage of the new showwarnings and formatwarning.
This commit is contained in:
parent
86b882f3a6
commit
f198ac2db2
2 changed files with 16 additions and 15 deletions
|
@ -58,20 +58,21 @@ except ImportError:
|
||||||
else:
|
else:
|
||||||
def idle_showwarning(message, category, filename, lineno,
|
def idle_showwarning(message, category, filename, lineno,
|
||||||
file=None, line=None):
|
file=None, line=None):
|
||||||
|
if file is None:
|
||||||
file = warning_stream
|
file = warning_stream
|
||||||
try:
|
try:
|
||||||
file.write(warnings.formatwarning(message, category, filename,\
|
file.write(warnings.formatwarning(message, category, filename,
|
||||||
lineno, file=file, line=line))
|
lineno, file=file, line=line))
|
||||||
except IOError:
|
except IOError:
|
||||||
pass ## file (probably __stderr__) is invalid, warning dropped.
|
pass ## file (probably __stderr__) is invalid, warning dropped.
|
||||||
warnings.showwarning = idle_showwarning
|
warnings.showwarning = idle_showwarning
|
||||||
def idle_formatwarning(message, category, filename, lineno,
|
def idle_formatwarning(message, category, filename, lineno, line=None):
|
||||||
file=None, line=None):
|
|
||||||
"""Format warnings the IDLE way"""
|
"""Format warnings the IDLE way"""
|
||||||
s = "\nWarning (from warnings module):\n"
|
s = "\nWarning (from warnings module):\n"
|
||||||
s += ' File \"%s\", line %s\n' % (filename, lineno)
|
s += ' File \"%s\", line %s\n' % (filename, lineno)
|
||||||
line = linecache.getline(filename, lineno).strip() \
|
if line is None:
|
||||||
if line is None else line
|
line = linecache.getline(filename, lineno)
|
||||||
|
line = line.strip()
|
||||||
if line:
|
if line:
|
||||||
s += " %s\n" % line
|
s += " %s\n" % line
|
||||||
s += "%s: %s\n>>> " % (category.__name__, message)
|
s += "%s: %s\n>>> " % (category.__name__, message)
|
||||||
|
@ -84,18 +85,17 @@ def extended_linecache_checkcache(filename=None,
|
||||||
|
|
||||||
Rather than repeating the linecache code, patch it to save the
|
Rather than repeating the linecache code, patch it to save the
|
||||||
<pyshell#...> entries, call the original linecache.checkcache()
|
<pyshell#...> entries, call the original linecache.checkcache()
|
||||||
(which destroys them), and then restore the saved entries.
|
(skipping them), and then restore the saved entries.
|
||||||
|
|
||||||
orig_checkcache is bound at definition time to the original
|
orig_checkcache is bound at definition time to the original
|
||||||
method, allowing it to be patched.
|
method, allowing it to be patched.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
cache = linecache.cache
|
cache = linecache.cache
|
||||||
save = {}
|
save = {}
|
||||||
for filename in cache.keys():
|
for key in list(cache):
|
||||||
if filename[:1] + filename[-1:] == '<>':
|
if key[:1] + key[-1:] == '<>':
|
||||||
save[filename] = cache[filename]
|
save[key] = cache.pop(key)
|
||||||
orig_checkcache()
|
orig_checkcache(filename)
|
||||||
cache.update(save)
|
cache.update(save)
|
||||||
|
|
||||||
# Patch linecache.checkcache():
|
# Patch linecache.checkcache():
|
||||||
|
|
|
@ -25,12 +25,13 @@ except ImportError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
def idle_formatwarning_subproc(message, category, filename, lineno,
|
def idle_formatwarning_subproc(message, category, filename, lineno,
|
||||||
file=None, line=None):
|
line=None):
|
||||||
"""Format warnings the IDLE way"""
|
"""Format warnings the IDLE way"""
|
||||||
s = "\nWarning (from warnings module):\n"
|
s = "\nWarning (from warnings module):\n"
|
||||||
s += ' File \"%s\", line %s\n' % (filename, lineno)
|
s += ' File \"%s\", line %s\n' % (filename, lineno)
|
||||||
line = linecache.getline(filename, lineno).strip() \
|
if line is None:
|
||||||
if line is None else line
|
line = linecache.getline(filename, lineno)
|
||||||
|
line = line.strip()
|
||||||
if line:
|
if line:
|
||||||
s += " %s\n" % line
|
s += " %s\n" % line
|
||||||
s += "%s: %s\n" % (category.__name__, message)
|
s += "%s: %s\n" % (category.__name__, message)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue