From 9c054047c017ba4393fb318c895ffb2d8a50d6c3 2008-03-29 11:41:22 From: Ville M. Vainio Date: 2008-03-29 11:41:22 Subject: [PATCH] ipapi, ileo: fix _ip.runlines() cleanup routine for better handling of secondary blocks (else etc) --- diff --git a/IPython/Extensions/ipy_leo.py b/IPython/Extensions/ipy_leo.py index 546fa4d..9d78021 100644 --- a/IPython/Extensions/ipy_leo.py +++ b/IPython/Extensions/ipy_leo.py @@ -373,8 +373,7 @@ def push_ipython_script(node): ohist = ip.IP.output_hist hstart = len(ip.IP.input_hist) script = node.script() - - script = g.splitLines(script + '\n') + ip.user_ns['_p'] = node ip.runlines(script) ip.user_ns.pop('_p',None) diff --git a/IPython/ipapi.py b/IPython/ipapi.py index 307fe8c..de23da0 100644 --- a/IPython/ipapi.py +++ b/IPython/ipapi.py @@ -273,16 +273,27 @@ class IPApi: """ res = [] lines = script.splitlines() + level = 0 for l in lines: lstripped = l.lstrip() - stripped = l.strip() + stripped = l.strip() if not stripped: continue newlevel = len(l) - len(lstripped) - if level > 0 and newlevel == 0 and not stripped.endswith(':'): + def is_secondary_block_start(s): + if not s.endswith(':'): + return False + if (s.startswith('elif') or + s.startswith('else') or + s.startswith('except') or + s.startswith('finally')): + return True + + if level > 0 and newlevel == 0 and not is_secondary_block_start(stripped): # add empty line res.append('') + res.append(l) level = newlevel return '\n'.join(res) + '\n' @@ -292,7 +303,7 @@ class IPApi: else: script = '\n'.join(lines) clean=cleanup_ipy_script(script) - + # print "_ip.runlines() script:\n",clean #dbg self.IP.runlines(clean) def to_user_ns(self,vars, interactive = True): """Inject a group of variables into the IPython user namespace. diff --git a/doc/examples/leo_bridge_demo.leo b/doc/examples/leo_bridge_demo.leo index ca4aa17..052a930 100644 --- a/doc/examples/leo_bridge_demo.leo +++ b/doc/examples/leo_bridge_demo.leo @@ -3,7 +3,7 @@ - + @@ -38,18 +38,18 @@ slist to leo -Class tests +Class tests csvr tempfile rfile strlist +IPython script push tests Direct variables -NewHeadline bar pylab tests -Generate testarr +Generate testarr testarr Call plotter on testarr @@ -128,21 +128,45 @@ def format_slist(obj): ? @wrap @nocolor -1+2 +# test ipython script 'cleanup' with complex blocks +1+2 print "hello" 3+4 def f(x): return x.upper() -f('hello world') if 0: print "foo" else: print "bar" - +def g(): + pass + +g() + +if 1: + if 1: + print "hello" + + print "world" + +if 1: + print "hello" + + print "word" +else: + print "foo" + + print "bar" + print "baz" + +try: + raise Exception +except: + print "exc ok" @cl rfile hello