[Python-checkins] python/dist/src/Lib/idlelib NEWS.txt, 1.35, 1.36 PyShell.py, 1.88, 1.89 ScriptBinding.py, 1.27, 1.28 run.py, 1.28, 1.29

kbk at users.sourceforge.net kbk at users.sourceforge.net
Sat Jul 3 21:25:59 EDT 2004


Update of /cvsroot/python/python/dist/src/Lib/idlelib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22465

Modified Files:
	NEWS.txt PyShell.py ScriptBinding.py run.py 
Log Message:
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


Index: NEWS.txt
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/idlelib/NEWS.txt,v
retrieving revision 1.35
retrieving revision 1.36
diff -C2 -d -r1.35 -r1.36
*** NEWS.txt	6 Jun 2004 01:29:21 -0000	1.35
--- NEWS.txt	4 Jul 2004 01:25:56 -0000	1.36
***************
*** 4,7 ****
--- 4,11 ----
  *Release date: XX-XXX-2004*
  
+ - 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 runtime warnings raised in the subprocess.
+ 
  - CodeContext hint pane visibility state is now persistent across sessions.
    The pane no longer appears in the shell window.  Added capability to limit

Index: PyShell.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/idlelib/PyShell.py,v
retrieving revision 1.88
retrieving revision 1.89
diff -C2 -d -r1.88 -r1.89
*** PyShell.py	6 Jun 2004 01:29:21 -0000	1.88
--- PyShell.py	4 Jul 2004 01:25:56 -0000	1.89
***************
*** 45,49 ****
      SIGTERM = 15
  
! # Change warnings module to write to sys.__stderr__
  try:
      import warnings
--- 45,54 ----
      SIGTERM = 15
  
! # 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
***************
*** 52,58 ****
  else:
      def idle_showwarning(message, category, filename, lineno):
!         file = sys.__stderr__
!         file.write(warnings.formatwarning(message, category, filename, lineno))
      warnings.showwarning = idle_showwarning
  
  def extended_linecache_checkcache(orig_checkcache=linecache.checkcache):
--- 57,76 ----
  else:
      def idle_showwarning(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):
***************
*** 816,819 ****
--- 834,844 ----
      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:

Index: ScriptBinding.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/idlelib/ScriptBinding.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** ScriptBinding.py	12 Feb 2004 17:35:09 -0000	1.27
--- ScriptBinding.py	4 Jul 2004 01:25:56 -0000	1.28
***************
*** 83,86 ****
--- 83,89 ----
  
      def checksyntax(self, filename):
+         self.shell = shell = self.flist.open_shell()
+         saved_stream = shell.get_warning_stream()
+         shell.set_warning_stream(shell.stderr)
          f = open(filename, 'r')
          source = f.read()
***************
*** 93,110 ****
          text.tag_remove("ERROR", "1.0", "end")
          try:
-             # If successful, return the compiled code
-             return compile(source, filename, "exec")
-         except (SyntaxError, OverflowError), err:
              try:
!                 msg, (errorfilename, lineno, offset, line) = err
!                 if not errorfilename:
!                     err.args = msg, (filename, lineno, offset, line)
!                     err.filename = filename
!                 self.colorize_syntax_error(msg, lineno, offset)
!             except:
!                 msg = "*** " + str(err)
!             self.errorbox("Syntax error",
!                           "There's an error in your program:\n" + msg)
!             return False
  
      def colorize_syntax_error(self, msg, lineno, offset):
--- 96,116 ----
          text.tag_remove("ERROR", "1.0", "end")
          try:
              try:
!                 # If successful, return the compiled code
!                 return compile(source, filename, "exec")
!             except (SyntaxError, OverflowError), err:
!                 try:
!                     msg, (errorfilename, lineno, offset, line) = err
!                     if not errorfilename:
!                         err.args = msg, (filename, lineno, offset, line)
!                         err.filename = filename
!                     self.colorize_syntax_error(msg, lineno, offset)
!                 except:
!                     msg = "*** " + str(err)
!                 self.errorbox("Syntax error",
!                               "There's an error in your program:\n" + msg)
!                 return False
!         finally:
!             shell.set_warning_stream(saved_stream)
  
      def colorize_syntax_error(self, msg, lineno, offset):
***************
*** 136,143 ****
          if not code:
              return
!         flist = self.editwin.flist
!         shell = flist.open_shell()
!         if not shell:
!             return  # couldn't open the shell
          interp = shell.interp
          if PyShell.use_subprocess:
--- 142,146 ----
          if not code:
              return
!         shell = self.shell
          interp = shell.interp
          if PyShell.use_subprocess:
***************
*** 157,160 ****
--- 160,166 ----
              \n""" % (filename, dirname))
          interp.prepend_syspath(filename)
+         # XXX KBK 03Jul04 When run w/o subprocess, runtime warnings still
+         #         go to __stderr__.  With subprocess, they go to the shell.
+         #         Need to change streams in PyShell.ModifiedInterpreter.
          interp.runcode(code)
  

Index: run.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/idlelib/run.py,v
retrieving revision 1.28
retrieving revision 1.29
diff -C2 -d -r1.28 -r1.29
*** run.py	21 Jan 2004 18:54:30 -0000	1.28
--- run.py	4 Jul 2004 01:25:56 -0000	1.29
***************
*** 1,4 ****
--- 1,5 ----
  import sys
  import os
+ import linecache
  import time
  import socket
***************
*** 18,21 ****
--- 19,38 ----
  LOCALHOST = '127.0.0.1'
  
+ try:
+     import warnings
+ except ImportError:
+     pass
+ else:
+     def idle_formatwarning_subproc(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_subproc
+ 
  # Thread shared globals: Establish a queue between a subthread (which handles
  # the socket) and the main thread (which runs user code), plus global




More information about the Python-checkins mailing list