From 94d39235868f7270380dacfbd720ca4fa0ec4382 2006-10-28 19:48:13 From: fptest Date: 2006-10-28 19:48:13 Subject: [PATCH] - Work around pexcept buglet which causes wraparound problems with long input lines in irunner. - Move arg_split to genutils so it can be more generally reused. - Modify OInspect to expose a publicly available getsource() routine. At the request of SAGE, which knows how to extract docstrings from Pyrex extensions. --- diff --git a/IPython/OInspect.py b/IPython/OInspect.py index 2507a72..af586c3 100644 --- a/IPython/OInspect.py +++ b/IPython/OInspect.py @@ -6,7 +6,7 @@ Uses syntax highlighting for presenting the various information elements. Similar in spirit to the inspect module, but all calls take a name argument to reference the name under which an object is being read. -$Id: OInspect.py 1625 2006-08-12 10:34:44Z vivainio $ +$Id: OInspect.py 1850 2006-10-28 19:48:13Z fptest $ """ #***************************************************************************** @@ -136,6 +136,27 @@ def getdoc(obj): ds = '%s\n%s' % (ds,ds2) return ds +def getsource(obj,is_binary=False): + """Wrapper around inspect.getsource. + + This can be modified by other projects to provide customized source + extraction. + + Inputs: + + - obj: an object whose source code we will attempt to extract. + + Optional inputs: + + - is_binary: whether the object is known to come from a binary source. + This implementation will skip returning any output for binary objects, but + custom extractors may know how to meaninfully process them.""" + + if is_binary: + return None + else: + return inspect.getsource(obj) + #**************************************************************************** # Class definitions @@ -262,7 +283,7 @@ class Inspector: # Flush the source cache because inspect can return out-of-date source linecache.checkcache() try: - src = inspect.getsource(obj) + src = getsource(obj) except: self.noinfo('source',oname) else: @@ -402,15 +423,16 @@ class Inspector: linecache.checkcache() source_success = False try: - if not binary_file: - source = self.format(inspect.getsource(obj)) + source = self.format(getsource(obj,binary_file)) + if source: out.write(header('Source:\n')+source.rstrip()) source_success = True - except: + except Exception, msg: pass if ds and not source_success: - out.writeln(header('Docstring [source file open failed]:\n') + indent(ds)) + out.writeln(header('Docstring [source file open failed]:\n') + + indent(ds)) # Constructor docstring for classes if obj_type is types.ClassType: diff --git a/IPython/Prompts.py b/IPython/Prompts.py index c033f65..c0f2610 100644 --- a/IPython/Prompts.py +++ b/IPython/Prompts.py @@ -2,7 +2,7 @@ """ Classes for handling input/output prompts. -$Id: Prompts.py 1366 2006-06-15 19:45:50Z vivainio $""" +$Id: Prompts.py 1850 2006-10-28 19:48:13Z fptest $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez @@ -244,7 +244,7 @@ class BasePrompt: ('${self.sep}${self.col_p}', multiple_replace(prompt_specials, self.p_template), '${self.col_norm}'),self.cache.user_ns,loc) - + self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor, self.p_template), self.cache.user_ns,loc) diff --git a/IPython/iplib.py b/IPython/iplib.py index 0aef214..7dc13df 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 1828 2006-10-16 02:04:33Z fptest $ +$Id: iplib.py 1850 2006-10-28 19:48:13Z fptest $ """ #***************************************************************************** @@ -665,12 +665,14 @@ class InteractiveShell(object,Magic): except AttributeError: pass - # I don't like assigning globally to sys, because it means when embedding - # instances, each embedded instance overrides the previous choice. But - # sys.displayhook seems to be called internally by exec, so I don't see a - # way around it. + # I don't like assigning globally to sys, because it means when + # embedding instances, each embedded instance overrides the previous + # choice. But sys.displayhook seems to be called internally by exec, + # so I don't see a way around it. We first save the original and then + # overwrite it. + self.sys_displayhook = sys.displayhook sys.displayhook = self.outputcache - + # Set user colors (don't do it in the constructor above so that it # doesn't crash if colors option is invalid) self.magic_colors(rc.colors) diff --git a/IPython/irunner.py b/IPython/irunner.py index 6b38780..8f1b050 100755 --- a/IPython/irunner.py +++ b/IPython/irunner.py @@ -136,6 +136,14 @@ class InteractiveRunner(object): c = pexpect.spawn(self.program,self.args,timeout=None) c.delaybeforesend = self.delaybeforesend + + # pexpect hard-codes the terminal size as (24,80) (rows,columns). + # This causes problems because any line longer than 80 characters gets + # completely overwrapped on the printed outptut (even though + # internally the code runs fine). We reset this to 99 rows X 200 + # columns (arbitrarily chosen), which should avoid problems in all + # reasonable cases. + c.setwinsize(99,200) prompts = c.compile_pattern_list(self.prompts) diff --git a/doc/ChangeLog b/doc/ChangeLog index a49620e..f9c0108 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,15 @@ +2006-10-28 Fernando Perez + + * IPython/genutils.py (arg_split): moved to genutils, since it's a + pretty generic function and useful for other things. + + * IPython/OInspect.py (getsource): Add customizable source + extractor. After a request/patch form W. Stein (SAGE). + + * IPython/irunner.py (InteractiveRunner.run_source): reset tty + window size to a more reasonable value from what pexpect does, + since their choice causes wrapping bugs with long input lines. + 2006-10-28 Ville Vainio * Magic.py (%run): Save and restore the readline history from