diff --git a/IPython/Debugger.py b/IPython/Debugger.py index 02018a7..195503d 100644 --- a/IPython/Debugger.py +++ b/IPython/Debugger.py @@ -15,7 +15,7 @@ details on the PSF (Python Software Foundation) standard license, see: http://www.python.org/2.2.3/license.html -$Id: Debugger.py 2154 2007-03-19 00:10:07Z fperez $""" +$Id: Debugger.py 2155 2007-03-19 00:45:51Z fperez $""" #***************************************************************************** # @@ -178,7 +178,7 @@ class Pdb(OldPdb): # Parent constructor: if has_pydb and completekey is None: - OldPdb.__init__(self,stdin=stdin,stdout=Term.cout) #stdout) + OldPdb.__init__(self,stdin=stdin,stdout=Term.cout) else: OldPdb.__init__(self,completekey,stdin,stdout) @@ -228,6 +228,11 @@ class Pdb(OldPdb): self.set_colors(color_scheme) + # Add a python parser so we can syntax highlight source while + # debugging. + self.parser = PyColorize.Parser() + + else: # Ugly hack: for Python 2.3-2.4, we can't call the parent constructor, # because it binds readline and breaks tab-completion. This means we @@ -270,6 +275,10 @@ class Pdb(OldPdb): cst['LightBG'].colors.breakpoint_disabled = C.Red self.set_colors(color_scheme) + + # Add a python parser so we can syntax highlight source while + # debugging. + self.parser = PyColorize.Parser() def set_colors(self, scheme): """Shorthand access to the color table scheme selector method.""" @@ -393,6 +402,10 @@ class Pdb(OldPdb): bp_mark = "" bp_mark_color = "" + scheme = self.color_scheme_table.active_scheme_name + new_line, err = self.parser.format2(line, 'str', scheme) + if not err: line = new_line + bp = None if lineno in self.get_file_breaks(filename): bps = self.get_breaks(filename, lineno) diff --git a/IPython/PyColorize.py b/IPython/PyColorize.py index ed20759..18ae2f7 100755 --- a/IPython/PyColorize.py +++ b/IPython/PyColorize.py @@ -28,7 +28,7 @@ scan Python source code and re-emit it with no changes to its original formatting (which is the hard part). - $Id: PyColorize.py 958 2005-12-27 23:17:51Z fperez $""" + $Id: PyColorize.py 2155 2007-03-19 00:45:51Z fperez $""" __all__ = ['ANSICodeColors','Parser'] @@ -120,6 +120,9 @@ class Parser: self.out = out def format(self, raw, out = None, scheme = ''): + return self.format2(raw, out, scheme)[0] + + def format2(self, raw, out = None, scheme = ''): """ Parse and send the colored source. If out and scheme are not specified, the defaults (given to @@ -153,6 +156,8 @@ class Parser: self.pos = 0 text = cStringIO.StringIO(self.raw) #self.out.write('
')
+
+        error = False
         try:
             tokenize.tokenize(text.readline, self)
         except tokenize.TokenError, ex:
@@ -163,11 +168,13 @@ class Parser:
                             msg, self.raw[self.lines[line]:],
                             colors.normal)
                            )
+            error = True
         self.out.write(colors.normal+'\n')
         if string_output:
             output = self.out.getvalue()
             self.out = out_old
-            return output
+            return (output, error)
+        return (None, error)
 
     def __call__(self, toktype, toktext, (srow,scol), (erow,ecol), line):
         """ Token handler, with syntax highlighting."""
diff --git a/IPython/ultraTB.py b/IPython/ultraTB.py
index ce4c740..f4945e1 100644
--- a/IPython/ultraTB.py
+++ b/IPython/ultraTB.py
@@ -60,7 +60,7 @@ You can implement other color schemes easily, the syntax is fairly
 self-explanatory. Please send back new schemes you develop to the author for
 possible inclusion in future releases.
 
-$Id: ultraTB.py 2027 2007-01-19 00:55:09Z fperez $"""
+$Id: ultraTB.py 2155 2007-03-19 00:45:51Z fperez $"""
 
 #*****************************************************************************
 #       Copyright (C) 2001 Nathaniel Gray 
@@ -90,7 +90,7 @@ import types
 
 # IPython's own modules
 # Modified pdb which doesn't damage IPython's readline handling
-from IPython import Debugger
+from IPython import Debugger, PyColorize
 from IPython.ipstruct import Struct
 from IPython.excolors import ExceptionColors
 from IPython.genutils import Term,uniq_stable,error,info
@@ -99,6 +99,12 @@ from IPython.genutils import Term,uniq_stable,error,info
 # amount of space to put line numbers before verbose tracebacks
 INDENT_SIZE = 8
 
+# Default color scheme.  This is used, for example, by the traceback
+# formatter.  When running in an actual IPython instance, the user's rc.colors
+# value is used, but havinga module global makes this functionality available
+# to users of ultraTB who are NOT running inside ipython.
+DEFAULT_SCHEME = 'NoColors'
+
 #---------------------------------------------------------------------------
 # Code begins
 
@@ -151,11 +157,25 @@ def _fixed_getinnerframes(etb, context=1,tb_offset=0):
 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
 # can be recognized properly by ipython.el's py-traceback-line-re
 # (SyntaxErrors have to be treated specially because they have no traceback)
+
+_parser = PyColorize.Parser()
+    
 def _formatTracebackLines(lnum, index, lines, Colors, lvals=None):
     numbers_width = INDENT_SIZE - 1
     res = []
     i = lnum - index
+
+    # This lets us get fully syntax-highlighted tracebacks.
+    try:
+        scheme = __IPYTHON__.rc.colors
+    except:
+        scheme = DEFAULT_SCHEME
+    _line_format = _parser.format2
+
     for line in lines:
+        new_line, err = _line_format(line,'str',scheme)
+        if not err: line = new_line
+        
         if i == lnum:
             # This is the line with the error
             pad = numbers_width - len(str(i))
@@ -181,6 +201,7 @@ def _formatTracebackLines(lnum, index, lines, Colors, lvals=None):
         i = i + 1
     return res
 
+
 #---------------------------------------------------------------------------
 # Module classes
 class TBTools:
diff --git a/doc/ChangeLog b/doc/ChangeLog
index 08ddfe8..1293c51 100644
--- a/doc/ChangeLog
+++ b/doc/ChangeLog
@@ -1,5 +1,13 @@
 2007-03-18  Fernando Perez  
 
+	* IPython/ultraTB.py (_formatTracebackLines): Added (slightly
+	modified) R. Bernstein's patch for fully syntax highlighted
+	tracebacks.  The functionality is also available under ultraTB for
+	non-ipython users (someone using ultraTB but outside an ipython
+	session).  They can select the color scheme by setting the
+	module-level global DEFAULT_SCHEME.  The highlight functionality
+	also works when debugging.
+
 	* IPython/genutils.py (IOStream.close): small patch by
 	R. Bernstein for improved pydb support.