From d0c455d92997efdb05178443aa622c86310d3970 2006-09-27 05:47:28 From: fperez Date: 2006-09-27 05:47:28 Subject: [PATCH] Apply Ville's patch, closes #87 --- diff --git a/IPython/Debugger.py b/IPython/Debugger.py index fe7a785..7ff50b6 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 1324 2006-05-24 20:25:11Z fperez $""" +$Id: Debugger.py 1786 2006-09-27 05:47:28Z fperez $""" #***************************************************************************** # @@ -68,44 +68,122 @@ class Pdb(pdb.Pdb): # Ugly hack: we can't call the parent constructor, because it binds # readline and breaks tab-completion. This means we have to COPY the # constructor here, and that requires tracking various python versions. - - def __init__(self,color_scheme='NoColor'): - bdb.Bdb.__init__(self) - cmd.Cmd.__init__(self,completekey=None) # don't load readline - self.prompt = 'ipdb> ' # The default prompt is '(Pdb)' - self.aliases = {} - - # These two lines are part of the py2.4 constructor, let's put them - # unconditionally here as they won't cause any problems in 2.3. - self.mainpyfile = '' - self._wait_for_mainpyfile = 0 - - # Read $HOME/.pdbrc and ./.pdbrc - try: - self.rcLines = _file_lines(os.path.join(os.environ['HOME'], - ".pdbrc")) - except KeyError: + + if sys.version[:3] == '2.5': + def __init__(self,color_scheme='NoColor',completekey=None, + stdin=None, stdout=None): + bdb.Bdb.__init__(self) + + # IPython change + # don't load readline + cmd.Cmd.__init__(self,completekey,stdin,stdout) + #cmd.Cmd.__init__(self, completekey, stdin, stdout) + # /IPython change + + if stdout: + self.use_rawinput = 0 + self.prompt = '(Pdb) ' + self.aliases = {} + self.mainpyfile = '' + self._wait_for_mainpyfile = 0 + # Try to load readline if it exists + try: + import readline + except ImportError: + pass + + # Read $HOME/.pdbrc and ./.pdbrc self.rcLines = [] - self.rcLines.extend(_file_lines(".pdbrc")) + if 'HOME' in os.environ: + envHome = os.environ['HOME'] + try: + rcFile = open(os.path.join(envHome, ".pdbrc")) + except IOError: + pass + else: + for line in rcFile.readlines(): + self.rcLines.append(line) + rcFile.close() + try: + rcFile = open(".pdbrc") + except IOError: + pass + else: + for line in rcFile.readlines(): + self.rcLines.append(line) + rcFile.close() - # Create color table: we copy the default one from the traceback - # module and add a few attributes needed for debugging - self.color_scheme_table = ExceptionColors.copy() - - # shorthands - C = ColorANSI.TermColors - cst = self.color_scheme_table + self.commands = {} # associates a command list to breakpoint numbers + self.commands_doprompt = {} # for each bp num, tells if the prompt must be disp. after execing the cmd list + self.commands_silent = {} # for each bp num, tells if the stack trace must be disp. after execing the cmd list + self.commands_defining = False # True while in the process of defining a command list + self.commands_bnum = None # The breakpoint number for which we are defining a list + + + # IPython changes... + + self.prompt = 'ipdb> ' # The default prompt is '(Pdb)' + self.aliases = {} + + # Create color table: we copy the default one from the traceback + # module and add a few attributes needed for debugging + self.color_scheme_table = ExceptionColors.copy() + + # shorthands + C = ColorANSI.TermColors + cst = self.color_scheme_table + + cst['NoColor'].colors.breakpoint_enabled = C.NoColor + cst['NoColor'].colors.breakpoint_disabled = C.NoColor + + cst['Linux'].colors.breakpoint_enabled = C.LightRed + cst['Linux'].colors.breakpoint_disabled = C.Red + + cst['LightBG'].colors.breakpoint_enabled = C.LightRed + cst['LightBG'].colors.breakpoint_disabled = C.Red + + self.set_colors(color_scheme) + + + else: + + def __init__(self,color_scheme='NoColor'): + bdb.Bdb.__init__(self) + cmd.Cmd.__init__(self,completekey=None) # don't load readline + self.prompt = 'ipdb> ' # The default prompt is '(Pdb)' + self.aliases = {} + + # These two lines are part of the py2.4 constructor, let's put them + # unconditionally here as they won't cause any problems in 2.3. + self.mainpyfile = '' + self._wait_for_mainpyfile = 0 + + # Read $HOME/.pdbrc and ./.pdbrc + try: + self.rcLines = _file_lines(os.path.join(os.environ['HOME'], + ".pdbrc")) + except KeyError: + self.rcLines = [] + self.rcLines.extend(_file_lines(".pdbrc")) + + # Create color table: we copy the default one from the traceback + # module and add a few attributes needed for debugging + self.color_scheme_table = ExceptionColors.copy() + + # shorthands + C = ColorANSI.TermColors + cst = self.color_scheme_table - cst['NoColor'].colors.breakpoint_enabled = C.NoColor - cst['NoColor'].colors.breakpoint_disabled = C.NoColor + cst['NoColor'].colors.breakpoint_enabled = C.NoColor + cst['NoColor'].colors.breakpoint_disabled = C.NoColor - cst['Linux'].colors.breakpoint_enabled = C.LightRed - cst['Linux'].colors.breakpoint_disabled = C.Red + cst['Linux'].colors.breakpoint_enabled = C.LightRed + cst['Linux'].colors.breakpoint_disabled = C.Red - cst['LightBG'].colors.breakpoint_enabled = C.LightRed - cst['LightBG'].colors.breakpoint_disabled = C.Red + cst['LightBG'].colors.breakpoint_enabled = C.LightRed + cst['LightBG'].colors.breakpoint_disabled = C.Red - self.set_colors(color_scheme) + self.set_colors(color_scheme) def set_colors(self, scheme): """Shorthand access to the color table scheme selector method.""" diff --git a/IPython/iplib.py b/IPython/iplib.py index d61dbd3..8b5ab46 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -6,7 +6,7 @@ Requires Python 2.3 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 1785 2006-09-26 21:08:22Z vivainio $ +$Id: iplib.py 1786 2006-09-27 05:47:28Z fperez $ """ #***************************************************************************** @@ -2029,8 +2029,22 @@ want to merge them back into the new files.""" % locals() """simple prefilter function, for debugging""" return self.handle_normal(line,continue_prompt) + + def multiline_prefilter(self, line, continue_prompt): + """ Run _prefilter for each line of input + + Covers cases where there are multiple lines in the user entry, + which is the case when the user goes back to a multiline history + entry and presses enter. + + """ + out = [] + for l in line.rstrip('\n').split('\n'): + out.append(self._prefilter(l, continue_prompt)) + return '\n'.join(out) + # Set the default prefilter() function (this can be user-overridden) - prefilter = _prefilter + prefilter = multiline_prefilter def handle_normal(self,line,continue_prompt=None, pre=None,iFun=None,theRest=None): diff --git a/IPython/irunner.py b/IPython/irunner.py index 2f28cab..5ebc6ea 100755 --- a/IPython/irunner.py +++ b/IPython/irunner.py @@ -32,6 +32,7 @@ NOTES: # Stdlib imports import optparse +import os import sys # Third-party modules. @@ -105,7 +106,7 @@ class InteractiveRunner(object): self.run_source(fobj,interact) finally: fobj.close() - + def run_source(self,source,interact=False): """Run the given source code interactively. @@ -127,8 +128,11 @@ class InteractiveRunner(object): # grab the true write method of stdout, in case anything later # reassigns sys.stdout, so that we really are writing to the true - # stdout and not to something else. - write = sys.stdout.write + # stdout and not to something else. We also normalize all strings we + # write to use the native OS line separators. + linesep = os.linesep + stdwrite = sys.stdout.write + write = lambda s: stdwrite(s.replace('\r\n',linesep)) c = pexpect.spawn(self.program,self.args,timeout=None) c.delaybeforesend = self.delaybeforesend @@ -142,7 +146,9 @@ class InteractiveRunner(object): for cmd in source: # skip blank lines for all matches to the 'main' prompt, while the # secondary prompts do not - if prompt_idx==0 and cmd.isspace(): + if prompt_idx==0 and \ + (cmd.isspace() or cmd.lstrip().startswith('#')): + print cmd, continue write(c.after) @@ -156,9 +162,6 @@ class InteractiveRunner(object): break write(c.before) - if isinstance(source,file): - source.close() - if end_normal: if interact: c.send('\n') @@ -232,6 +235,28 @@ class PythonRunner(InteractiveRunner): InteractiveRunner.__init__(self,program,prompts,args) +class DocTestRunner(PythonRunner): + """A python runner customized for doctest usage.""" + + def run_source(self,source,interact=False): + """Run the given source code interactively. + + See the parent docstring for details. + """ + + # if the source is a string, chop it up in lines so we can iterate + # over it just as if it were an open file. + if not isinstance(source,file): + source = source.splitlines(True) + + + for line in source: + pass + # finish by calling the parent run_source method + super(DocTestRunner,self).run_source(dsource,interact) + + + class SAGERunner(InteractiveRunner): """Interactive SAGE runner. diff --git a/IPython/ultraTB.py b/IPython/ultraTB.py index b0b1676..2cf0a24 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 1154 2006-02-11 23:20:05Z fperez $""" +$Id: ultraTB.py 1786 2006-09-27 05:47:28Z fperez $""" #***************************************************************************** # Copyright (C) 2001 Nathaniel Gray @@ -692,7 +692,7 @@ class VerboseTB(TBTools): etb = etb.tb_next self.pdb.botframe = etb.tb_frame self.pdb.interaction(self.tb.tb_frame, self.tb) - except: + except 'ha': # dbg print '*** ERROR ***' print 'This version of pdb has a bug and crashed.' print 'Returning to IPython...'