diff options
author | Kurt B. Kaiser <kbk@shore.net> | 2004-07-04 01:25:56 (GMT) |
---|---|---|
committer | Kurt B. Kaiser <kbk@shore.net> | 2004-07-04 01:25:56 (GMT) |
commit | 49a5fe107fceb0239d87119842b20a7421043b5a (patch) | |
tree | 1d265bc88514c8069ea096650870b87c06698c86 /Lib/idlelib/PyShell.py | |
parent | 9ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0 (diff) | |
download | cpython-49a5fe107fceb0239d87119842b20a7421043b5a.zip cpython-49a5fe107fceb0239d87119842b20a7421043b5a.tar.gz cpython-49a5fe107fceb0239d87119842b20a7421043b5a.tar.bz2 |
Redirect the warning stream to the shell during the ScriptBinding check of user code
and format the warning similarly to an exception for both that check and for
warnings raised in the subprocess.
M NEWS.txt
M Pyshell.py
M ScriptBinding.py
M run.py
Diffstat (limited to 'Lib/idlelib/PyShell.py')
-rw-r--r-- | Lib/idlelib/PyShell.py | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/PyShell.py index 951fde2..0ee70d9 100644 --- a/Lib/idlelib/PyShell.py +++ b/Lib/idlelib/PyShell.py @@ -44,16 +44,34 @@ try: except ImportError: SIGTERM = 15 -# Change warnings module to write to sys.__stderr__ +# Override warnings module to write to warning_stream. Initialize to send IDLE +# 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 = sys.__stderr__ - file.write(warnings.formatwarning(message, category, filename, lineno)) + file = warning_stream + try: + file.write(warnings.formatwarning(message, category, filename, lineno)) + except IOError: + pass ## file (probably __stderr__) is invalid, warning dropped. warnings.showwarning = idle_showwarning + def idle_formatwarning(message, category, filename, lineno): + """Format warnings the IDLE way""" + s = "\nWarning (from warnings module):\n" + s += ' File \"%s\", line %s\n' % (filename, lineno) + line = linecache.getline(filename, lineno).strip() + if line: + s += " %s\n" % line + s += "%s: %s\n>>> " % (category.__name__, message) + return s + warnings.formatwarning = idle_formatwarning def extended_linecache_checkcache(orig_checkcache=linecache.checkcache): """Extend linecache.checkcache to preserve the <pyshell#...> entries @@ -815,6 +833,13 @@ class PyShell(OutputWindow): endoffile = False closing = False + def set_warning_stream(self, stream): + global warning_stream + warning_stream = stream + + def get_warning_stream(self): + return warning_stream + def toggle_debugger(self, event=None): if self.executing: tkMessageBox.showerror("Don't debug now", |