diff Lib/idlelib/PyShell.py @ 32484:f3cb95cd413f legacy-trunk

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
author Kurt B. Kaiser <kbk@shore.net>
date Sun, 04 Jul 2004 01:25:56 +0000
parents 014b0d84422f
children c4ed14fab12a
line wrap: on
line diff
--- a/Lib/idlelib/PyShell.py	Sat Jul 03 13:48:56 2004 +0000
+++ b/Lib/idlelib/PyShell.py	Sun Jul 04 01:25:56 2004 +0000
@@ -44,16 +44,34 @@
 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 @@
     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",