From e687d7972e65b307c94b7d8b62193c14e9c23e47 2010-01-13 04:30:40 From: Fernando Perez Date: 2010-01-13 04:30:40 Subject: [PATCH] Fix exception color problems in win32. In all the recent work to have the test suite play nice with doctest of full ipython sessions, I inadvertedly started sending exceptions directly to sys.stderr. On windows we MUST go via Term.cerr, which uses pyreadline to handle color escapes, while sys.stderr just shows garbage on screen. As of this revision, the test suite passes fully on win32 and linux, and interactive use also seems OK on all fronts. We're getting closer to RC status... --- diff --git a/IPython/core/release.py b/IPython/core/release.py index ea64271..c2d7bae 100644 --- a/IPython/core/release.py +++ b/IPython/core/release.py @@ -23,7 +23,7 @@ name = 'ipython' development = True # change this to False to do a release version_base = '0.11' branch = 'ipython' -revision = '1284' +revision = '1312' if development: if branch == 'ipython': diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py index 3a517d2..c32fad9 100644 --- a/IPython/core/ultratb.py +++ b/IPython/core/ultratb.py @@ -88,7 +88,6 @@ import types from inspect import getsourcefile, getfile, getmodule,\ ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode - # IPython's own modules # Modified pdb which doesn't damage IPython's readline handling from IPython.utils import PyColorize @@ -312,11 +311,15 @@ def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None): # Module classes class TBTools: """Basic tools used by all traceback printer classes.""" + #: Default output stream, can be overridden at call time. A special value #: of 'stdout' *as a string* can be given to force extraction of sys.stdout #: at runtime. This allows testing exception printing with doctests, that #: swap sys.stdout just at execution time. - out_stream = sys.stderr + #: Warning: be VERY careful to set this to one of the Term streams, NEVER + #: directly to sys.stdout/err, because under win32 the Term streams come from + #: pyreadline and know how to handle color correctly, whie stdout/err don't. + out_stream = Term.cerr def __init__(self,color_scheme = 'NoColor',call_pdb=False): # Whether to call the interactive pdb debugger after printing @@ -381,8 +384,7 @@ class ListTB(TBTools): def __call__(self, etype, value, elist): Term.cout.flush() - print >> Term.cerr, self.text(etype,value,elist) - Term.cerr.flush() + Term.cerr.writeln(self.text(etype,value,elist)) def text(self, etype, value, elist, context=5): """Return a color formatted string with the traceback info. @@ -533,7 +535,7 @@ class ListTB(TBTools): # a subclass whose signature or behavior may be different Term.cout.flush() ostream = sys.stdout if self.out_stream == 'stdout' else Term.cerr - print >> ostream, ListTB.text(self, etype, value, []), + ostream.write(ListTB.text(self, etype, value, [])) ostream.flush() def _some_str(self, value): @@ -907,8 +909,7 @@ class VerboseTB(TBTools): (etype, evalue, etb) = info or sys.exc_info() self.tb = etb Term.cout.flush() - print >> Term.cerr, self.text(etype, evalue, etb) - Term.cerr.flush() + Term.cerr.writeln(self.text(etype, evalue, etb)) # Changed so an instance can just be called as VerboseTB_inst() and print # out the right info on its own. @@ -1032,10 +1033,12 @@ class AutoFormattedTB(FormattedTB): Term.cout.flush() if tb_offset is not None: tb_offset, self.tb_offset = self.tb_offset, tb_offset - print >> out, self.text(etype, evalue, etb) + out.write(self.text(etype, evalue, etb)) + out.write('\n') self.tb_offset = tb_offset else: - print >> out, self.text(etype, evalue, etb) + out.write(self.text(etype, evalue, etb)) + out.write('\n') out.flush() try: self.debugger()