mirror of
https://github.com/python/cpython.git
synced 2025-08-25 19:24:42 +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
|
@ -23,36 +23,46 @@ import __main__
|
|||
|
||||
LOCALHOST = '127.0.0.1'
|
||||
|
||||
try:
|
||||
import warnings
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
def idle_formatwarning_subproc(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_subproc
|
||||
import warnings
|
||||
|
||||
def idle_showwarning_subproc(
|
||||
message, category, filename, lineno, file=None, line=None):
|
||||
"""Show Idle-format warning after replacing warnings.showwarning.
|
||||
|
||||
The only difference is the formatter called.
|
||||
"""
|
||||
if file is None:
|
||||
file = sys.stderr
|
||||
try:
|
||||
file.write(PyShell.idle_formatwarning(
|
||||
message, category, filename, lineno, line))
|
||||
except IOError:
|
||||
pass # the file (probably stderr) is invalid - this warning gets lost.
|
||||
|
||||
_warnings_showwarning = None
|
||||
|
||||
def capture_warnings(capture):
|
||||
"Replace warning.showwarning with idle_showwarning_subproc, or reverse."
|
||||
|
||||
global _warnings_showwarning
|
||||
if capture:
|
||||
if _warnings_showwarning is None:
|
||||
_warnings_showwarning = warnings.showwarning
|
||||
warnings.showwarning = idle_showwarning_subproc
|
||||
else:
|
||||
if _warnings_showwarning is not None:
|
||||
warnings.showwarning = _warnings_showwarning
|
||||
_warnings_showwarning = None
|
||||
|
||||
capture_warnings(True)
|
||||
tcl = tkinter.Tcl()
|
||||
|
||||
|
||||
def handle_tk_events(tcl=tcl):
|
||||
"""Process any tk events that are ready to be dispatched if tkinter
|
||||
has been imported, a tcl interpreter has been created and tk has been
|
||||
loaded."""
|
||||
tcl.eval("update")
|
||||
|
||||
|
||||
# Thread shared globals: Establish a queue between a subthread (which handles
|
||||
# the socket) and the main thread (which runs user code), plus global
|
||||
# completion, exit and interruptable (the main thread) flags:
|
||||
|
@ -91,6 +101,8 @@ def main(del_exitfunc=False):
|
|||
print("IDLE Subprocess: no IP port passed in sys.argv.",
|
||||
file=sys.__stderr__)
|
||||
return
|
||||
|
||||
capture_warnings(True)
|
||||
sys.argv[:] = [""]
|
||||
sockthread = threading.Thread(target=manage_socket,
|
||||
name='SockThread',
|
||||
|
@ -118,6 +130,7 @@ def main(del_exitfunc=False):
|
|||
exit_now = True
|
||||
continue
|
||||
except SystemExit:
|
||||
capture_warnings(False)
|
||||
raise
|
||||
except:
|
||||
type, value, tb = sys.exc_info()
|
||||
|
@ -247,6 +260,7 @@ def exit():
|
|||
if no_exitfunc:
|
||||
import atexit
|
||||
atexit._clear()
|
||||
capture_warnings(False)
|
||||
sys.exit(0)
|
||||
|
||||
class MyRPCServer(rpc.RPCServer):
|
||||
|
@ -386,3 +400,5 @@ class Executive(object):
|
|||
sys.last_value = val
|
||||
item = StackViewer.StackTreeItem(flist, tb)
|
||||
return RemoteObjectBrowser.remote_object_tree_item(item)
|
||||
|
||||
capture_warnings(False) # Make sure turned off; see issue 18081
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue