##// END OF EJS Templates
_ip.runlines() is now much more liberal about indentation - it cleans up the scripts it gets
vivainio -
Show More
@@ -263,11 +263,36 b' class IPApi:'
263
263
264 Takes either all lines in one string or list of lines.
264 Takes either all lines in one string or list of lines.
265 """
265 """
266
267 def cleanup_ipy_script(script):
268 """ Make a script safe for _ip.runlines()
269
270 - Removes empty lines
271 - Suffixes all indented blocks that end with unindented lines with empty lines
272
273 """
274 res = []
275 lines = script.splitlines()
276 level = 0
277 for l in lines:
278 stripped = l.lstrip()
279 if not l.strip():
280 continue
281 newlevel = len(l) - len(stripped)
282 if level > 0 and newlevel == 0:
283 # add empty line
284 res.append('')
285 res.append(l)
286 level = newlevel
287 return '\n'.join(res) + '\n'
288
266 if isinstance(lines,basestring):
289 if isinstance(lines,basestring):
267 self.IP.runlines(lines)
290 script = lines
268 else:
291 else:
269 self.IP.runlines('\n'.join(lines))
292 script = '\n'.join(lines)
293 clean=cleanup_ipy_script(script)
270
294
295 self.IP.runlines(clean)
271 def to_user_ns(self,vars, interactive = True):
296 def to_user_ns(self,vars, interactive = True):
272 """Inject a group of variables into the IPython user namespace.
297 """Inject a group of variables into the IPython user namespace.
273
298
@@ -568,3 +593,4 b' def make_session(user_ns = None):'
568 import IPython.Shell
593 import IPython.Shell
569 return IPython.Shell.start(user_ns)
594 return IPython.Shell.start(user_ns)
570
595
596
@@ -2,14 +2,34 b' import sys'
2 sys.path.append('..')
2 sys.path.append('..')
3
3
4 import IPython.ipapi
4 import IPython.ipapi
5
6 IPython.ipapi.make_session()
5 IPython.ipapi.make_session()
7 ip = IPython.ipapi.get()
6 ip = IPython.ipapi.get()
8
7
9 def test_runlines():
8 def test_runlines():
9 import textwrap
10 ip.runlines(['a = 10', 'a+=1'])
10 ip.runlines(['a = 10', 'a+=1'])
11 ip.runlines('assert a == 11')
11 ip.runlines('assert a == 11\nassert 1')
12
12 assert ip.user_ns['a'] == 11
13 assert ip.user_ns['a'] == 11
14 complex = textwrap.dedent("""\
15 if 1:
16 print "hello"
17 if 1:
18 print "world"
19
20 if 1:
21 print "foo"
22 if 1:
23 print "bar"
24
25 if 1:
26 print "bar"
27
28 """)
29
30
31 ip.runlines(complex)
32
13
33
14 def test_db():
34 def test_db():
15 ip.db['__unittest_'] = 12
35 ip.db['__unittest_'] = 12
General Comments 0
You need to be logged in to leave comments. Login now