##// END OF EJS Templates
Improvements and bugfixes to %paste....
Fernando Perez -
Show More
@@ -3381,8 +3381,13 b' Defaulting color scheme to \'NoColor\'"""'
3381 You can also pass a variable name as an argument, e.g. '%paste foo'.
3381 You can also pass a variable name as an argument, e.g. '%paste foo'.
3382 This assigns the pasted block to variable 'foo' as string, without
3382 This assigns the pasted block to variable 'foo' as string, without
3383 dedenting or executing it (preceding >>> and + is still stripped)
3383 dedenting or executing it (preceding >>> and + is still stripped)
3384
3385 Options
3386 -------
3384
3387
3385 '%paste -r' re-executes the block previously entered by cpaste.
3388 -r: re-executes the block previously entered by cpaste.
3389
3390 -e: echo the pasted text back to the terminal.
3386
3391
3387 IPython statements (magics, shell escapes) are not supported (yet).
3392 IPython statements (magics, shell escapes) are not supported (yet).
3388
3393
@@ -3390,7 +3395,7 b' Defaulting color scheme to \'NoColor\'"""'
3390 --------
3395 --------
3391 cpaste: manually paste code into terminal until you mark its end.
3396 cpaste: manually paste code into terminal until you mark its end.
3392 """
3397 """
3393 opts,args = self.parse_options(parameter_s,'r:',mode='string')
3398 opts,args = self.parse_options(parameter_s,'re',mode='string')
3394 par = args.strip()
3399 par = args.strip()
3395 if opts.has_key('r'):
3400 if opts.has_key('r'):
3396 self._rerun_pasted()
3401 self._rerun_pasted()
@@ -3398,6 +3403,15 b' Defaulting color scheme to \'NoColor\'"""'
3398
3403
3399 text = self.shell.hooks.clipboard_get()
3404 text = self.shell.hooks.clipboard_get()
3400 block = self._strip_pasted_lines_for_code(text.splitlines())
3405 block = self._strip_pasted_lines_for_code(text.splitlines())
3406
3407 if opts.has_key('e'):
3408 # Echo back to terminal.
3409 write = self.shell.write
3410 write(block)
3411 if not block.endswith('\n'):
3412 write('\n')
3413 write("## -- End pasted text --\n")
3414
3401 self._execute_block(block, par)
3415 self._execute_block(block, par)
3402
3416
3403 def magic_quickref(self,arg):
3417 def magic_quickref(self,arg):
@@ -7,6 +7,7 b' import os'
7 import sys
7 import sys
8 import tempfile
8 import tempfile
9 import types
9 import types
10 from cStringIO import StringIO
10
11
11 import nose.tools as nt
12 import nose.tools as nt
12
13
@@ -261,9 +262,9 b' class TestMagicRun(object):'
261 # Multiple tests for clipboard pasting
262 # Multiple tests for clipboard pasting
262 def test_paste():
263 def test_paste():
263
264
264 def paste(txt):
265 def paste(txt, flags=''):
265 hooks.clipboard_get = lambda : txt
266 hooks.clipboard_get = lambda : txt
266 _ip.magic('paste')
267 _ip.magic('paste '+flags)
267
268
268 # Inject fake clipboard hook but save original so we can restore it later
269 # Inject fake clipboard hook but save original so we can restore it later
269 hooks = _ip.IP.hooks
270 hooks = _ip.IP.hooks
@@ -294,9 +295,31 b' def test_paste():'
294 """)
295 """)
295 yield (nt.assert_equal, user_ns['x'], [1,2,3])
296 yield (nt.assert_equal, user_ns['x'], [1,2,3])
296 yield (nt.assert_equal, user_ns['y'], [1,4,9])
297 yield (nt.assert_equal, user_ns['y'], [1,4,9])
297 except:
298 pass
299
298
300 # This should be in a finally clause, instead of the bare except above.
299 # Now, test that paste -r works
301 # Restore original hook
300 user_ns.pop('x', None)
302 hooks.clipboard_get = original_clip
301 yield (nt.assert_false, 'x' in user_ns)
302 _ip.magic('paste -r')
303 yield (nt.assert_equal, user_ns['x'], [1,2,3])
304
305 # Also test paste -e, by temporarily faking the writer
306 w = StringIO()
307 writer = _ip.IP.write
308 _ip.IP.write = w.write
309 code = """
310 a = 100
311 b = 200"""
312 try:
313 paste(code,'-e')
314 out = w.getvalue()
315 finally:
316 _ip.IP.write = writer
317 yield (nt.assert_equal, user_ns['a'], 100)
318 yield (nt.assert_equal, user_ns['b'], 200)
319 yield (nt.assert_equal, out, code+"\n## -- End pasted text --\n")
320
321 finally:
322 # This should be in a finally clause, instead of the bare except above.
323 # Restore original hook
324 hooks.clipboard_get = original_clip
325
General Comments 0
You need to be logged in to leave comments. Login now