Merge lp:~ipython-dev/ipython/cpaste-fixes into lp:ipython/0.11

Proposed by Fernando Perez
Status: Merged
Merged at revision: not available
Proposed branch: lp:~ipython-dev/ipython/cpaste-fixes
Merge into: lp:ipython/0.11
Diff against target: None lines
To merge this branch: bzr merge lp:~ipython-dev/ipython/cpaste-fixes
Reviewer Review Type Date Requested Status
Brian Granger Approve
Review via email: mp+10016@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Fernando Perez (fdo.perez) wrote :

Fixes a bug (that was untested, added tests) and adds a useful feature to %paste.

This is very isolated, but we can merge it any time in the cycle that doesn't conflict with Brian's major work.

lp:~ipython-dev/ipython/cpaste-fixes updated
1210. By Fernando Perez

Make %paste echo by default, introduce -q for quiet mode.

Revision history for this message
Brian Granger (ellisonbg) wrote :

Looks good, I am going to go ahead and merge this.

review: Approve
Revision history for this message
Fernando Perez (fdo.perez) wrote :

On Thu, Aug 13, 2009 at 4:25 PM, Brian Granger<email address hidden> wrote:
> Review: Approve
> Looks good, I am going to go ahead and merge this.

Awesome if you do it: I'm swamped with last-minute things for scipy
and visitors who will go down to LA from here with us, so I'm a bit
short on time.

I will find a space to review your other branch though, so we don't
stall that. Promised.

Thanks!

f

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'IPython/Magic.py'
2--- IPython/Magic.py 2009-08-02 00:11:07 +0000
3+++ IPython/Magic.py 2009-08-12 04:57:19 +0000
4@@ -3381,8 +3381,13 @@
5 You can also pass a variable name as an argument, e.g. '%paste foo'.
6 This assigns the pasted block to variable 'foo' as string, without
7 dedenting or executing it (preceding >>> and + is still stripped)
8+
9+ Options
10+ -------
11
12- '%paste -r' re-executes the block previously entered by cpaste.
13+ -r: re-executes the block previously entered by cpaste.
14+
15+ -e: echo the pasted text back to the terminal.
16
17 IPython statements (magics, shell escapes) are not supported (yet).
18
19@@ -3390,7 +3395,7 @@
20 --------
21 cpaste: manually paste code into terminal until you mark its end.
22 """
23- opts,args = self.parse_options(parameter_s,'r:',mode='string')
24+ opts,args = self.parse_options(parameter_s,'re',mode='string')
25 par = args.strip()
26 if opts.has_key('r'):
27 self._rerun_pasted()
28@@ -3398,6 +3403,15 @@
29
30 text = self.shell.hooks.clipboard_get()
31 block = self._strip_pasted_lines_for_code(text.splitlines())
32+
33+ if opts.has_key('e'):
34+ # Echo back to terminal.
35+ write = self.shell.write
36+ write(block)
37+ if not block.endswith('\n'):
38+ write('\n')
39+ write("## -- End pasted text --\n")
40+
41 self._execute_block(block, par)
42
43 def magic_quickref(self,arg):
44
45=== modified file 'IPython/tests/test_magic.py'
46--- IPython/tests/test_magic.py 2009-08-04 10:12:31 +0000
47+++ IPython/tests/test_magic.py 2009-08-12 04:57:19 +0000
48@@ -7,6 +7,7 @@
49 import sys
50 import tempfile
51 import types
52+from cStringIO import StringIO
53
54 import nose.tools as nt
55
56@@ -261,9 +262,9 @@
57 # Multiple tests for clipboard pasting
58 def test_paste():
59
60- def paste(txt):
61+ def paste(txt, flags=''):
62 hooks.clipboard_get = lambda : txt
63- _ip.magic('paste')
64+ _ip.magic('paste '+flags)
65
66 # Inject fake clipboard hook but save original so we can restore it later
67 hooks = _ip.IP.hooks
68@@ -294,9 +295,31 @@
69 """)
70 yield (nt.assert_equal, user_ns['x'], [1,2,3])
71 yield (nt.assert_equal, user_ns['y'], [1,4,9])
72- except:
73- pass
74-
75- # This should be in a finally clause, instead of the bare except above.
76- # Restore original hook
77- hooks.clipboard_get = original_clip
78+
79+ # Now, test that paste -r works
80+ user_ns.pop('x', None)
81+ yield (nt.assert_false, 'x' in user_ns)
82+ _ip.magic('paste -r')
83+ yield (nt.assert_equal, user_ns['x'], [1,2,3])
84+
85+ # Also test paste -e, by temporarily faking the writer
86+ w = StringIO()
87+ writer = _ip.IP.write
88+ _ip.IP.write = w.write
89+ code = """
90+ a = 100
91+ b = 200"""
92+ try:
93+ paste(code,'-e')
94+ out = w.getvalue()
95+ finally:
96+ _ip.IP.write = writer
97+ yield (nt.assert_equal, user_ns['a'], 100)
98+ yield (nt.assert_equal, user_ns['b'], 200)
99+ yield (nt.assert_equal, out, code+"\n## -- End pasted text --\n")
100+
101+ finally:
102+ # This should be in a finally clause, instead of the bare except above.
103+ # Restore original hook
104+ hooks.clipboard_get = original_clip
105+

Subscribers

People subscribed via source and target branches