diff --git a/IPython/Magic.py b/IPython/Magic.py index 2f1fe71..8e8e758 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -3381,8 +3381,13 @@ Defaulting color scheme to 'NoColor'""" You can also pass a variable name as an argument, e.g. '%paste foo'. This assigns the pasted block to variable 'foo' as string, without dedenting or executing it (preceding >>> and + is still stripped) + + Options + ------- - '%paste -r' re-executes the block previously entered by cpaste. + -r: re-executes the block previously entered by cpaste. + + -e: echo the pasted text back to the terminal. IPython statements (magics, shell escapes) are not supported (yet). @@ -3390,7 +3395,7 @@ Defaulting color scheme to 'NoColor'""" -------- cpaste: manually paste code into terminal until you mark its end. """ - opts,args = self.parse_options(parameter_s,'r:',mode='string') + opts,args = self.parse_options(parameter_s,'re',mode='string') par = args.strip() if opts.has_key('r'): self._rerun_pasted() @@ -3398,6 +3403,15 @@ Defaulting color scheme to 'NoColor'""" text = self.shell.hooks.clipboard_get() block = self._strip_pasted_lines_for_code(text.splitlines()) + + if opts.has_key('e'): + # Echo back to terminal. + write = self.shell.write + write(block) + if not block.endswith('\n'): + write('\n') + write("## -- End pasted text --\n") + self._execute_block(block, par) def magic_quickref(self,arg): diff --git a/IPython/tests/test_magic.py b/IPython/tests/test_magic.py index a17f130..32265ff 100644 --- a/IPython/tests/test_magic.py +++ b/IPython/tests/test_magic.py @@ -7,6 +7,7 @@ import os import sys import tempfile import types +from cStringIO import StringIO import nose.tools as nt @@ -261,9 +262,9 @@ class TestMagicRun(object): # Multiple tests for clipboard pasting def test_paste(): - def paste(txt): + def paste(txt, flags=''): hooks.clipboard_get = lambda : txt - _ip.magic('paste') + _ip.magic('paste '+flags) # Inject fake clipboard hook but save original so we can restore it later hooks = _ip.IP.hooks @@ -294,9 +295,31 @@ def test_paste(): """) yield (nt.assert_equal, user_ns['x'], [1,2,3]) yield (nt.assert_equal, user_ns['y'], [1,4,9]) - except: - pass - # This should be in a finally clause, instead of the bare except above. - # Restore original hook - hooks.clipboard_get = original_clip + # Now, test that paste -r works + user_ns.pop('x', None) + yield (nt.assert_false, 'x' in user_ns) + _ip.magic('paste -r') + yield (nt.assert_equal, user_ns['x'], [1,2,3]) + + # Also test paste -e, by temporarily faking the writer + w = StringIO() + writer = _ip.IP.write + _ip.IP.write = w.write + code = """ + a = 100 + b = 200""" + try: + paste(code,'-e') + out = w.getvalue() + finally: + _ip.IP.write = writer + yield (nt.assert_equal, user_ns['a'], 100) + yield (nt.assert_equal, user_ns['b'], 200) + yield (nt.assert_equal, out, code+"\n## -- End pasted text --\n") + + finally: + # This should be in a finally clause, instead of the bare except above. + # Restore original hook + hooks.clipboard_get = original_clip +