##// END OF EJS Templates
ipapi, ileo: fix _ip.runlines() cleanup routine for better handling of secondary blocks (else etc)
Ville M. Vainio -
Show More
@@ -373,8 +373,7 b' def push_ipython_script(node):'
373 ohist = ip.IP.output_hist
373 ohist = ip.IP.output_hist
374 hstart = len(ip.IP.input_hist)
374 hstart = len(ip.IP.input_hist)
375 script = node.script()
375 script = node.script()
376
376
377 script = g.splitLines(script + '\n')
378 ip.user_ns['_p'] = node
377 ip.user_ns['_p'] = node
379 ip.runlines(script)
378 ip.runlines(script)
380 ip.user_ns.pop('_p',None)
379 ip.user_ns.pop('_p',None)
@@ -273,16 +273,27 b' class IPApi:'
273 """
273 """
274 res = []
274 res = []
275 lines = script.splitlines()
275 lines = script.splitlines()
276
276 level = 0
277 level = 0
277 for l in lines:
278 for l in lines:
278 lstripped = l.lstrip()
279 lstripped = l.lstrip()
279 stripped = l.strip()
280 stripped = l.strip()
280 if not stripped:
281 if not stripped:
281 continue
282 continue
282 newlevel = len(l) - len(lstripped)
283 newlevel = len(l) - len(lstripped)
283 if level > 0 and newlevel == 0 and not stripped.endswith(':'):
284 def is_secondary_block_start(s):
285 if not s.endswith(':'):
286 return False
287 if (s.startswith('elif') or
288 s.startswith('else') or
289 s.startswith('except') or
290 s.startswith('finally')):
291 return True
292
293 if level > 0 and newlevel == 0 and not is_secondary_block_start(stripped):
284 # add empty line
294 # add empty line
285 res.append('')
295 res.append('')
296
286 res.append(l)
297 res.append(l)
287 level = newlevel
298 level = newlevel
288 return '\n'.join(res) + '\n'
299 return '\n'.join(res) + '\n'
@@ -292,7 +303,7 b' class IPApi:'
292 else:
303 else:
293 script = '\n'.join(lines)
304 script = '\n'.join(lines)
294 clean=cleanup_ipy_script(script)
305 clean=cleanup_ipy_script(script)
295
306 # print "_ip.runlines() script:\n",clean #dbg
296 self.IP.runlines(clean)
307 self.IP.runlines(clean)
297 def to_user_ns(self,vars, interactive = True):
308 def to_user_ns(self,vars, interactive = True):
298 """Inject a group of variables into the IPython user namespace.
309 """Inject a group of variables into the IPython user namespace.
@@ -3,7 +3,7 b''
3 <leo_file>
3 <leo_file>
4 <leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/>
4 <leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/>
5 <globals body_outline_ratio="0.307814992026">
5 <globals body_outline_ratio="0.307814992026">
6 <global_window_position top="180" left="223" height="627" width="1280"/>
6 <global_window_position top="257" left="131" height="627" width="1280"/>
7 <global_log_window_position top="0" left="0" height="0" width="0"/>
7 <global_log_window_position top="0" left="0" height="0" width="0"/>
8 </globals>
8 </globals>
9 <preferences/>
9 <preferences/>
@@ -38,18 +38,18 b''
38 <v t="vivainio.20080219230342"><vh>slist to leo</vh></v>
38 <v t="vivainio.20080219230342"><vh>slist to leo</vh></v>
39 </v>
39 </v>
40 </v>
40 </v>
41 <v t="vivainio.20080218195413"><vh>Class tests</vh>
41 <v t="vivainio.20080218195413" a="E"><vh>Class tests</vh>
42 <v t="vivainio.20080218200509"><vh>csvr</vh></v>
42 <v t="vivainio.20080218200509"><vh>csvr</vh></v>
43 <v t="vivainio.20080218191007"><vh>tempfile</vh></v>
43 <v t="vivainio.20080218191007"><vh>tempfile</vh></v>
44 <v t="vivainio.20080218195413.1"><vh>rfile</vh></v>
44 <v t="vivainio.20080218195413.1"><vh>rfile</vh></v>
45 <v t="vivainio.20080219225804"><vh>strlist</vh></v>
45 <v t="vivainio.20080219225804"><vh>strlist</vh></v>
46 </v>
46 </v>
47 <v t="vivainio.20080222201226" a="V"><vh>IPython script push tests</vh></v>
47 <v t="vivainio.20080218201219" a="E"><vh>Direct variables</vh>
48 <v t="vivainio.20080218201219" a="E"><vh>Direct variables</vh>
48 <v t="vivainio.20080222201226"><vh>NewHeadline</vh></v>
49 <v t="vivainio.20080218201219.2"><vh>bar</vh></v>
49 <v t="vivainio.20080218201219.2"><vh>bar</vh></v>
50 </v>
50 </v>
51 <v t="vivainio.20080316144536" a="E"><vh>pylab tests</vh>
51 <v t="vivainio.20080316144536" a="E"><vh>pylab tests</vh>
52 <v t="vivainio.20080316145539.2" a="TV"><vh>Generate testarr</vh></v>
52 <v t="vivainio.20080316145539.2"><vh>Generate testarr</vh></v>
53 <v t="vivainio.20080316085925"><vh>testarr</vh></v>
53 <v t="vivainio.20080316085925"><vh>testarr</vh></v>
54 <v t="vivainio.20080316085950"><vh>Call plotter on testarr</vh></v>
54 <v t="vivainio.20080316085950"><vh>Call plotter on testarr</vh></v>
55 </v>
55 </v>
@@ -128,21 +128,45 b' def format_slist(obj):'
128 <t tx="vivainio.20080222193236">?</t>
128 <t tx="vivainio.20080222193236">?</t>
129 <t tx="vivainio.20080222193236.1">@wrap
129 <t tx="vivainio.20080222193236.1">@wrap
130 @nocolor</t>
130 @nocolor</t>
131 <t tx="vivainio.20080222201226">1+2
131 <t tx="vivainio.20080222201226"># test ipython script 'cleanup' with complex blocks
132 1+2
132 print "hello"
133 print "hello"
133 3+4
134 3+4
134
135
135 def f(x):
136 def f(x):
136 return x.upper()
137 return x.upper()
137
138
138 f('hello world')
139
139
140 if 0:
140 if 0:
141 print "foo"
141 print "foo"
142 else:
142 else:
143 print "bar"
143 print "bar"
144
144
145 </t>
145 def g():
146 pass
147
148 g()
149
150 if 1:
151 if 1:
152 print "hello"
153
154 print "world"
155
156 if 1:
157 print "hello"
158
159 print "word"
160 else:
161 print "foo"
162
163 print "bar"
164 print "baz"
165
166 try:
167 raise Exception
168 except:
169 print "exc ok"</t>
146 <t tx="vivainio.20080222202211"></t>
170 <t tx="vivainio.20080222202211"></t>
147 <t tx="vivainio.20080222202211.1" ipython="7d71005506636f6f7264737101284b0c4bde747102732e">@cl rfile
171 <t tx="vivainio.20080222202211.1" ipython="7d71005506636f6f7264737101284b0c4bde747102732e">@cl rfile
148 hello
172 hello
General Comments 0
You need to be logged in to leave comments. Login now