mirror of
https://github.com/python/cpython.git
synced 2025-08-26 03:34:43 +00:00
Issue *18081, #18242: Change Idle warnings capture in PyShell and run to stop
replacing warnings.formatwarnings and to reverse replacement of warnings.showwarnings when import is complete and when main function exits. Add test_warning.py. Vinay Sajip provided capture_warnings function.
This commit is contained in:
parent
70d2c711f1
commit
95a3f11f95
3 changed files with 162 additions and 49 deletions
|
@ -45,35 +45,55 @@ PORT = 0 # someday pass in host, port for remote debug capability
|
|||
# internal warnings to the console. ScriptBinding.check_syntax() will
|
||||
# temporarily redirect the stream to the shell window to display warnings when
|
||||
# checking user's code.
|
||||
global warning_stream
|
||||
warning_stream = sys.__stderr__
|
||||
try:
|
||||
import warnings
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
def idle_showwarning(message, category, filename, lineno,
|
||||
file=None, line=None):
|
||||
if file is None:
|
||||
file = warning_stream
|
||||
try:
|
||||
file.write(warnings.formatwarning(message, category, filename,
|
||||
lineno, line=line))
|
||||
except OSError:
|
||||
pass ## file (probably __stderr__) is invalid, warning dropped.
|
||||
warnings.showwarning = idle_showwarning
|
||||
def idle_formatwarning(message, category, filename, lineno, line=None):
|
||||
"""Format warnings the IDLE way"""
|
||||
s = "\nWarning (from warnings module):\n"
|
||||
s += ' File \"%s\", line %s\n' % (filename, lineno)
|
||||
if line is None:
|
||||
line = linecache.getline(filename, lineno)
|
||||
line = line.strip()
|
||||
if line:
|
||||
s += " %s\n" % line
|
||||
s += "%s: %s\n>>> " % (category.__name__, message)
|
||||
return s
|
||||
warnings.formatwarning = idle_formatwarning
|
||||
warning_stream = sys.__stderr__ # None, at least on Windows, if no console.
|
||||
import warnings
|
||||
|
||||
def idle_formatwarning(message, category, filename, lineno, line=None):
|
||||
"""Format warnings the IDLE way."""
|
||||
|
||||
s = "\nWarning (from warnings module):\n"
|
||||
s += ' File \"%s\", line %s\n' % (filename, lineno)
|
||||
if line is None:
|
||||
line = linecache.getline(filename, lineno)
|
||||
line = line.strip()
|
||||
if line:
|
||||
s += " %s\n" % line
|
||||
s += "%s: %s\n" % (category.__name__, message)
|
||||
return s
|
||||
|
||||
def idle_showwarning(
|
||||
message, category, filename, lineno, file=None, line=None):
|
||||
"""Show Idle-format warning (after replacing warnings.showwarning).
|
||||
|
||||
The differences are the formatter called, the file=None replacement,
|
||||
which can be None, the capture of the consequence AttributeError,
|
||||
and the output of a hard-coded prompt.
|
||||
"""
|
||||
if file is None:
|
||||
file = warning_stream
|
||||
try:
|
||||
file.write(idle_formatwarning(
|
||||
message, category, filename, lineno, line=line))
|
||||
file.write(">>> ")
|
||||
except (AttributeError, OSError):
|
||||
pass # if file (probably __stderr__) is invalid, skip warning.
|
||||
|
||||
_warnings_showwarning = None
|
||||
|
||||
def capture_warnings(capture):
|
||||
"Replace warning.showwarning with idle_showwarning, or reverse."
|
||||
|
||||
global _warnings_showwarning
|
||||
if capture:
|
||||
if _warnings_showwarning is None:
|
||||
_warnings_showwarning = warnings.showwarning
|
||||
warnings.showwarning = idle_showwarning
|
||||
else:
|
||||
if _warnings_showwarning is not None:
|
||||
warnings.showwarning = _warnings_showwarning
|
||||
_warnings_showwarning = None
|
||||
|
||||
capture_warnings(True)
|
||||
|
||||
def extended_linecache_checkcache(filename=None,
|
||||
orig_checkcache=linecache.checkcache):
|
||||
|
@ -1421,6 +1441,7 @@ echo "import sys; print(sys.argv)" | idle - "foobar"
|
|||
def main():
|
||||
global flist, root, use_subprocess
|
||||
|
||||
capture_warnings(True)
|
||||
use_subprocess = True
|
||||
enable_shell = False
|
||||
enable_edit = False
|
||||
|
@ -1553,7 +1574,10 @@ def main():
|
|||
while flist.inversedict: # keep IDLE running while files are open.
|
||||
root.mainloop()
|
||||
root.destroy()
|
||||
capture_warnings(False)
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.modules['PyShell'] = sys.modules['__main__']
|
||||
main()
|
||||
|
||||
capture_warnings(False) # Make sure turned off; see issue 18081
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue