Show More
@@ -193,7 +193,7 b' def assemble_logical_lines():' | |||
|
193 | 193 | line = ''.join(parts) |
|
194 | 194 | |
|
195 | 195 | # Utilities |
|
196 |
def _make_help_call(target, esc, lspace |
|
|
196 | def _make_help_call(target, esc, lspace): | |
|
197 | 197 | """Prepares a pinfo(2)/psearch call from a target name and the escape |
|
198 | 198 | (i.e. ? or ??)""" |
|
199 | 199 | method = 'pinfo2' if esc == '??' \ |
@@ -203,12 +203,13 b' def _make_help_call(target, esc, lspace, next_input=None):' | |||
|
203 | 203 | #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args) |
|
204 | 204 | t_magic_name, _, t_magic_arg_s = arg.partition(' ') |
|
205 | 205 | t_magic_name = t_magic_name.lstrip(ESC_MAGIC) |
|
206 | if next_input is None: | |
|
207 | return '%sget_ipython().run_line_magic(%r, %r)' % (lspace, t_magic_name, t_magic_arg_s) | |
|
208 | else: | |
|
209 | return '%sget_ipython().set_next_input(%r);get_ipython().run_line_magic(%r, %r)' % \ | |
|
210 | (lspace, next_input, t_magic_name, t_magic_arg_s) | |
|
211 | ||
|
206 | return "%sget_ipython().run_line_magic(%r, %r)" % ( | |
|
207 | lspace, | |
|
208 | t_magic_name, | |
|
209 | t_magic_arg_s, | |
|
210 | ) | |
|
211 | ||
|
212 | ||
|
212 | 213 | # These define the transformations for the different escape characters. |
|
213 | 214 | def _tr_system(line_info): |
|
214 | 215 | "Translate lines escaped with: !" |
@@ -349,10 +350,7 b' def help_end(line):' | |||
|
349 | 350 | esc = m.group(3) |
|
350 | 351 | lspace = _initial_space_re.match(line).group(0) |
|
351 | 352 | |
|
352 | # If we're mid-command, put it back on the next prompt for the user. | |
|
353 | next_input = line.rstrip('?') if line.strip() != m.group(0) else None | |
|
354 | ||
|
355 | return _make_help_call(target, esc, lspace, next_input) | |
|
353 | return _make_help_call(target, esc, lspace) | |
|
356 | 354 | |
|
357 | 355 | |
|
358 | 356 | @CoroutineInputTransformer.wrap |
@@ -301,7 +301,7 b" ESC_PAREN = '/' # Call first argument with rest of line as arguments" | |||
|
301 | 301 | ESCAPE_SINGLES = {'!', '?', '%', ',', ';', '/'} |
|
302 | 302 | ESCAPE_DOUBLES = {'!!', '??'} # %% (cell magic) is handled separately |
|
303 | 303 | |
|
304 |
def _make_help_call(target, esc |
|
|
304 | def _make_help_call(target, esc): | |
|
305 | 305 | """Prepares a pinfo(2)/psearch call from a target name and the escape |
|
306 | 306 | (i.e. ? or ??)""" |
|
307 | 307 | method = 'pinfo2' if esc == '??' \ |
@@ -311,11 +311,8 b' def _make_help_call(target, esc, next_input=None):' | |||
|
311 | 311 | #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args) |
|
312 | 312 | t_magic_name, _, t_magic_arg_s = arg.partition(' ') |
|
313 | 313 | t_magic_name = t_magic_name.lstrip(ESC_MAGIC) |
|
314 | if next_input is None: | |
|
315 | return 'get_ipython().run_line_magic(%r, %r)' % (t_magic_name, t_magic_arg_s) | |
|
316 | else: | |
|
317 | return 'get_ipython().set_next_input(%r);get_ipython().run_line_magic(%r, %r)' % \ | |
|
318 | (next_input, t_magic_name, t_magic_arg_s) | |
|
314 | return "get_ipython().run_line_magic(%r, %r)" % (t_magic_name, t_magic_arg_s) | |
|
315 | ||
|
319 | 316 | |
|
320 | 317 | def _tr_help(content): |
|
321 | 318 | """Translate lines escaped with: ? |
@@ -456,13 +453,8 b' class HelpEnd(TokenTransformBase):' | |||
|
456 | 453 | target = m.group(1) |
|
457 | 454 | esc = m.group(3) |
|
458 | 455 | |
|
459 | # If we're mid-command, put it back on the next prompt for the user. | |
|
460 | next_input = None | |
|
461 | if (not lines_before) and (not lines_after) \ | |
|
462 | and content.strip() != m.group(0): | |
|
463 | next_input = content.rstrip('?\n') | |
|
464 | 456 | |
|
465 |
call = _make_help_call(target, esc |
|
|
457 | call = _make_help_call(target, esc) | |
|
466 | 458 | new_line = indent + call + '\n' |
|
467 | 459 | |
|
468 | 460 | return lines_before + [new_line] + lines_after |
@@ -36,141 +36,145 b' def transform_checker(tests, transformer, **kwargs):' | |||
|
36 | 36 | # it both within single-function tests and also to validate the behavior of the |
|
37 | 37 | # larger objects |
|
38 | 38 | |
|
39 |
syntax = |
|
|
40 |
|
|
|
41 |
|
|
|
42 | [(u'a =! ls', "a = get_ipython().getoutput('ls')"), | |
|
43 |
(u |
|
|
44 |
(u |
|
|
45 | (u'd == !ls', u'd == !ls'), # Invalid syntax, but we leave == alone. | |
|
46 | ('x=1', 'x=1'), # normal input is unmodified | |
|
47 | (' ',' '), # blank lines are kept intact | |
|
48 | # Tuple unpacking | |
|
49 | (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")"), | |
|
50 | (u"a,= !echo 'a'", u"a, = get_ipython().getoutput(\"echo 'a'\")"), | |
|
51 | (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")"), | |
|
52 | # Tuple unpacking with regular Python expressions, not our syntax. | |
|
53 | (u"a, b = range(2)", u"a, b = range(2)"), | |
|
54 | (u"a, = range(1)", u"a, = range(1)"), | |
|
55 | (u"a, *bc = range(3)", u"a, *bc = range(3)"), | |
|
56 | ]], | |
|
57 | ||
|
58 | assign_magic = | |
|
59 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
60 | [(u'a =% who', "a = get_ipython().run_line_magic('who', '')"), | |
|
61 | (u'b = %who', "b = get_ipython().run_line_magic('who', '')"), | |
|
62 | (u'c= %ls', "c = get_ipython().run_line_magic('ls', '')"), | |
|
63 | (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone. | |
|
64 | ('x=1', 'x=1'), # normal input is unmodified | |
|
65 | (' ',' '), # blank lines are kept intact | |
|
66 | (u"a, b = %foo", u"a, b = get_ipython().run_line_magic('foo', '')"), | |
|
67 | ]], | |
|
68 | ||
|
69 | classic_prompt = | |
|
70 | [('>>> x=1', 'x=1'), | |
|
71 | ('x=1', 'x=1'), # normal input is unmodified | |
|
72 | (' ', ' '), # blank lines are kept intact | |
|
73 | ], | |
|
74 | ||
|
75 | ipy_prompt = | |
|
76 | [('In [1]: x=1', 'x=1'), | |
|
77 | ('x=1', 'x=1'), # normal input is unmodified | |
|
78 | (' ',' '), # blank lines are kept intact | |
|
79 | ], | |
|
80 | ||
|
81 | # Tests for the escape transformer to leave normal code alone | |
|
82 | escaped_noesc = | |
|
83 | [ (' ', ' '), | |
|
84 | ('x=1', 'x=1'), | |
|
85 | ], | |
|
86 | ||
|
87 | # System calls | |
|
88 |
|
|
|
89 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
90 | [ (u'!ls', "get_ipython().system('ls')"), | |
|
91 | # Double-escape shell, this means to capture the output of the | |
|
92 | # subprocess and return it | |
|
93 | (u'!!ls', "get_ipython().getoutput('ls')"), | |
|
94 | ]], | |
|
95 | ||
|
96 | # Help/object info | |
|
97 | escaped_help = | |
|
98 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
99 |
|
|
|
100 | (u'?x1', "get_ipython().run_line_magic('pinfo', 'x1')"), | |
|
101 | (u'??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"), | |
|
102 | (u'?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"), | |
|
103 | (u'?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"), | |
|
104 | (u'?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"), | |
|
105 | (u'?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"), | |
|
106 | ]], | |
|
107 | ||
|
108 | end_help = | |
|
109 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
110 |
|
|
|
111 |
(u |
|
|
112 |
(u |
|
|
113 | (u'%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"), | |
|
114 | (u'%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"), | |
|
115 | (u'%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"), | |
|
116 | (u'Ο.foo?', "get_ipython().run_line_magic('pinfo', 'Ο.foo')"), | |
|
117 | (u'f*?', "get_ipython().run_line_magic('psearch', 'f*')"), | |
|
118 |
(u |
|
|
119 | (u'a = abc?', "get_ipython().set_next_input('a = abc');" | |
|
120 |
|
|
|
121 | (u'a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');" | |
|
122 |
|
|
|
123 | (u'a = *.items?', "get_ipython().set_next_input('a = *.items');" | |
|
124 |
|
|
|
125 |
(u |
|
|
126 |
|
|
|
127 | (u'a*2 #comment?', 'a*2 #comment?'), | |
|
128 | ]], | |
|
129 | ||
|
130 | # Explicit magic calls | |
|
131 | escaped_magic = | |
|
132 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
133 | [ (u'%cd', "get_ipython().run_line_magic('cd', '')"), | |
|
134 | (u'%cd /home', "get_ipython().run_line_magic('cd', '/home')"), | |
|
135 | # Backslashes need to be escaped. | |
|
136 | (u'%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"), | |
|
137 | (u' %magic', " get_ipython().run_line_magic('magic', '')"), | |
|
138 | ]], | |
|
139 | ||
|
140 | # Quoting with separate arguments | |
|
141 | escaped_quote = | |
|
142 | [ (',f', 'f("")'), | |
|
143 | (',f x', 'f("x")'), | |
|
144 | (' ,f y', ' f("y")'), | |
|
145 | (',f a b', 'f("a", "b")'), | |
|
146 | ], | |
|
147 | ||
|
148 | # Quoting with single argument | |
|
149 | escaped_quote2 = | |
|
150 |
|
|
|
151 | (';f x', 'f("x")'), | |
|
152 | (' ;f y', ' f("y")'), | |
|
153 | (';f a b', 'f("a b")'), | |
|
154 | ], | |
|
155 | ||
|
156 | # Simply apply parens | |
|
157 | escaped_paren = | |
|
158 | [ ('/f', 'f()'), | |
|
159 | ('/f x', 'f(x)'), | |
|
160 | (' /f y', ' f(y)'), | |
|
161 |
|
|
|
162 | ], | |
|
163 | ||
|
164 | # Check that we transform prompts before other transforms | |
|
165 | mixed = | |
|
166 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
167 | [ (u'In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"), | |
|
168 | (u'>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"), | |
|
169 | (u'In [2]: !ls', "get_ipython().system('ls')"), | |
|
170 |
(u |
|
|
171 |
(u |
|
|
172 | ]], | |
|
173 | ) | |
|
39 | syntax = dict( | |
|
40 | assign_system=[ | |
|
41 | (i, py3compat.u_format(o)) | |
|
42 | for i, o in [ | |
|
43 | (u"a =! ls", "a = get_ipython().getoutput('ls')"), | |
|
44 | (u"b = !ls", "b = get_ipython().getoutput('ls')"), | |
|
45 | (u"c= !ls", "c = get_ipython().getoutput('ls')"), | |
|
46 | (u"d == !ls", u"d == !ls"), # Invalid syntax, but we leave == alone. | |
|
47 | ("x=1", "x=1"), # normal input is unmodified | |
|
48 | (" ", " "), # blank lines are kept intact | |
|
49 | # Tuple unpacking | |
|
50 | ( | |
|
51 | u"a, b = !echo 'a\\nb'", | |
|
52 | u"a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")", | |
|
53 | ), | |
|
54 | (u"a,= !echo 'a'", u"a, = get_ipython().getoutput(\"echo 'a'\")"), | |
|
55 | ( | |
|
56 | u"a, *bc = !echo 'a\\nb\\nc'", | |
|
57 | u"a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")", | |
|
58 | ), | |
|
59 | # Tuple unpacking with regular Python expressions, not our syntax. | |
|
60 | (u"a, b = range(2)", u"a, b = range(2)"), | |
|
61 | (u"a, = range(1)", u"a, = range(1)"), | |
|
62 | (u"a, *bc = range(3)", u"a, *bc = range(3)"), | |
|
63 | ] | |
|
64 | ], | |
|
65 | assign_magic=[ | |
|
66 | (i, py3compat.u_format(o)) | |
|
67 | for i, o in [ | |
|
68 | (u"a =% who", "a = get_ipython().run_line_magic('who', '')"), | |
|
69 | (u"b = %who", "b = get_ipython().run_line_magic('who', '')"), | |
|
70 | (u"c= %ls", "c = get_ipython().run_line_magic('ls', '')"), | |
|
71 | (u"d == %ls", u"d == %ls"), # Invalid syntax, but we leave == alone. | |
|
72 | ("x=1", "x=1"), # normal input is unmodified | |
|
73 | (" ", " "), # blank lines are kept intact | |
|
74 | (u"a, b = %foo", u"a, b = get_ipython().run_line_magic('foo', '')"), | |
|
75 | ] | |
|
76 | ], | |
|
77 | classic_prompt=[ | |
|
78 | (">>> x=1", "x=1"), | |
|
79 | ("x=1", "x=1"), # normal input is unmodified | |
|
80 | (" ", " "), # blank lines are kept intact | |
|
81 | ], | |
|
82 | ipy_prompt=[ | |
|
83 | ("In [1]: x=1", "x=1"), | |
|
84 | ("x=1", "x=1"), # normal input is unmodified | |
|
85 | (" ", " "), # blank lines are kept intact | |
|
86 | ], | |
|
87 | # Tests for the escape transformer to leave normal code alone | |
|
88 | escaped_noesc=[ | |
|
89 | (" ", " "), | |
|
90 | ("x=1", "x=1"), | |
|
91 | ], | |
|
92 | # System calls | |
|
93 | escaped_shell=[ | |
|
94 | (i, py3compat.u_format(o)) | |
|
95 | for i, o in [ | |
|
96 | (u"!ls", "get_ipython().system('ls')"), | |
|
97 | # Double-escape shell, this means to capture the output of the | |
|
98 | # subprocess and return it | |
|
99 | (u"!!ls", "get_ipython().getoutput('ls')"), | |
|
100 | ] | |
|
101 | ], | |
|
102 | # Help/object info | |
|
103 | escaped_help=[ | |
|
104 | (i, py3compat.u_format(o)) | |
|
105 | for i, o in [ | |
|
106 | (u"?", "get_ipython().show_usage()"), | |
|
107 | (u"?x1", "get_ipython().run_line_magic('pinfo', 'x1')"), | |
|
108 | (u"??x2", "get_ipython().run_line_magic('pinfo2', 'x2')"), | |
|
109 | (u"?a.*s", "get_ipython().run_line_magic('psearch', 'a.*s')"), | |
|
110 | (u"?%hist1", "get_ipython().run_line_magic('pinfo', '%hist1')"), | |
|
111 | (u"?%%hist2", "get_ipython().run_line_magic('pinfo', '%%hist2')"), | |
|
112 | (u"?abc = qwe", "get_ipython().run_line_magic('pinfo', 'abc')"), | |
|
113 | ] | |
|
114 | ], | |
|
115 | end_help=[ | |
|
116 | (i, py3compat.u_format(o)) | |
|
117 | for i, o in [ | |
|
118 | (u"x3?", "get_ipython().run_line_magic('pinfo', 'x3')"), | |
|
119 | (u"x4??", "get_ipython().run_line_magic('pinfo2', 'x4')"), | |
|
120 | (u"%hist1?", "get_ipython().run_line_magic('pinfo', '%hist1')"), | |
|
121 | (u"%hist2??", "get_ipython().run_line_magic('pinfo2', '%hist2')"), | |
|
122 | (u"%%hist3?", "get_ipython().run_line_magic('pinfo', '%%hist3')"), | |
|
123 | (u"%%hist4??", "get_ipython().run_line_magic('pinfo2', '%%hist4')"), | |
|
124 | (u"Ο.foo?", "get_ipython().run_line_magic('pinfo', 'Ο.foo')"), | |
|
125 | (u"f*?", "get_ipython().run_line_magic('psearch', 'f*')"), | |
|
126 | (u"ax.*aspe*?", "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"), | |
|
127 | (u"a = abc?", "get_ipython().run_line_magic('pinfo', 'abc')"), | |
|
128 | (u"a = abc.qe??", "get_ipython().run_line_magic('pinfo2', 'abc.qe')"), | |
|
129 | (u"a = *.items?", "get_ipython().run_line_magic('psearch', '*.items')"), | |
|
130 | (u"plot(a?", "get_ipython().run_line_magic('pinfo', 'a')"), | |
|
131 | (u"a*2 #comment?", "a*2 #comment?"), | |
|
132 | ] | |
|
133 | ], | |
|
134 | # Explicit magic calls | |
|
135 | escaped_magic=[ | |
|
136 | (i, py3compat.u_format(o)) | |
|
137 | for i, o in [ | |
|
138 | (u"%cd", "get_ipython().run_line_magic('cd', '')"), | |
|
139 | (u"%cd /home", "get_ipython().run_line_magic('cd', '/home')"), | |
|
140 | # Backslashes need to be escaped. | |
|
141 | (u"%cd C:\\User", "get_ipython().run_line_magic('cd', 'C:\\\\User')"), | |
|
142 | (u" %magic", " get_ipython().run_line_magic('magic', '')"), | |
|
143 | ] | |
|
144 | ], | |
|
145 | # Quoting with separate arguments | |
|
146 | escaped_quote=[ | |
|
147 | (",f", 'f("")'), | |
|
148 | (",f x", 'f("x")'), | |
|
149 | (" ,f y", ' f("y")'), | |
|
150 | (",f a b", 'f("a", "b")'), | |
|
151 | ], | |
|
152 | # Quoting with single argument | |
|
153 | escaped_quote2=[ | |
|
154 | (";f", 'f("")'), | |
|
155 | (";f x", 'f("x")'), | |
|
156 | (" ;f y", ' f("y")'), | |
|
157 | (";f a b", 'f("a b")'), | |
|
158 | ], | |
|
159 | # Simply apply parens | |
|
160 | escaped_paren=[ | |
|
161 | ("/f", "f()"), | |
|
162 | ("/f x", "f(x)"), | |
|
163 | (" /f y", " f(y)"), | |
|
164 | ("/f a b", "f(a, b)"), | |
|
165 | ], | |
|
166 | # Check that we transform prompts before other transforms | |
|
167 | mixed=[ | |
|
168 | (i, py3compat.u_format(o)) | |
|
169 | for i, o in [ | |
|
170 | (u"In [1]: %lsmagic", "get_ipython().run_line_magic('lsmagic', '')"), | |
|
171 | (u">>> %lsmagic", "get_ipython().run_line_magic('lsmagic', '')"), | |
|
172 | (u"In [2]: !ls", "get_ipython().system('ls')"), | |
|
173 | (u"In [3]: abs?", "get_ipython().run_line_magic('pinfo', 'abs')"), | |
|
174 | (u"In [4]: b = %who", "b = get_ipython().run_line_magic('who', '')"), | |
|
175 | ] | |
|
176 | ], | |
|
177 | ) | |
|
174 | 178 | |
|
175 | 179 | # multiline syntax examples. Each of these should be a list of lists, with |
|
176 | 180 | # each entry itself having pairs of raw/transformed input. The union (with |
@@ -15,45 +15,65 b' from IPython.core import inputtransformer2 as ipt2' | |||
|
15 | 15 | from IPython.core.inputtransformer2 import _find_assign_op, make_tokens_by_line |
|
16 | 16 | from IPython.testing.decorators import skip |
|
17 | 17 | |
|
18 |
MULTILINE_MAGIC = ( |
|
|
18 | MULTILINE_MAGIC = ( | |
|
19 | """\ | |
|
19 | 20 |
|
|
20 | 21 | %foo \\ |
|
21 | 22 |
|
|
22 | 23 |
|
|
23 | """.splitlines(keepends=True), (2, 0), """\ | |
|
24 | """.splitlines( | |
|
25 | keepends=True | |
|
26 | ), | |
|
27 | (2, 0), | |
|
28 | """\ | |
|
24 | 29 |
|
|
25 | 30 | get_ipython().run_line_magic('foo', ' bar') |
|
26 | 31 |
|
|
27 |
|
|
|
32 | """.splitlines( | |
|
33 | keepends=True | |
|
34 | ), | |
|
35 | ) | |
|
28 | 36 | |
|
29 |
INDENTED_MAGIC = ( |
|
|
37 | INDENTED_MAGIC = ( | |
|
38 | """\ | |
|
30 | 39 |
|
|
31 | 40 | %ls |
|
32 | """.splitlines(keepends=True), (2, 4), """\ | |
|
41 | """.splitlines( | |
|
42 | keepends=True | |
|
43 | ), | |
|
44 | (2, 4), | |
|
45 | """\ | |
|
33 | 46 |
|
|
34 | 47 | get_ipython().run_line_magic('ls', '') |
|
35 |
|
|
|
48 | """.splitlines( | |
|
49 | keepends=True | |
|
50 | ), | |
|
51 | ) | |
|
36 | 52 | |
|
37 |
CRLF_MAGIC = ( |
|
|
38 | "a = f()\n", | |
|
39 | "%ls\r\n", | |
|
40 | "g()\n" | |
|
41 | ], (2, 0), [ | |
|
42 | "a = f()\n", | |
|
43 | "get_ipython().run_line_magic('ls', '')\n", | |
|
44 |
" |
|
|
45 | ]) | |
|
46 | ||
|
47 | MULTILINE_MAGIC_ASSIGN = ("""\ | |
|
53 | CRLF_MAGIC = ( | |
|
54 | ["a = f()\n", "%ls\r\n", "g()\n"], | |
|
55 | (2, 0), | |
|
56 | ["a = f()\n", "get_ipython().run_line_magic('ls', '')\n", "g()\n"], | |
|
57 | ) | |
|
58 | ||
|
59 | MULTILINE_MAGIC_ASSIGN = ( | |
|
60 | """\ | |
|
48 | 61 |
|
|
49 | 62 | b = %foo \\ |
|
50 | 63 |
|
|
51 | 64 |
|
|
52 | """.splitlines(keepends=True), (2, 4), """\ | |
|
65 | """.splitlines( | |
|
66 | keepends=True | |
|
67 | ), | |
|
68 | (2, 4), | |
|
69 | """\ | |
|
53 | 70 |
|
|
54 | 71 | b = get_ipython().run_line_magic('foo', ' bar') |
|
55 | 72 |
|
|
56 |
|
|
|
73 | """.splitlines( | |
|
74 | keepends=True | |
|
75 | ), | |
|
76 | ) | |
|
57 | 77 | |
|
58 | 78 | MULTILINE_SYSTEM_ASSIGN = ("""\ |
|
59 | 79 | a = f() |
@@ -73,68 +93,70 b' def test():' | |||
|
73 | 93 | for i in range(1): |
|
74 | 94 | print(i) |
|
75 | 95 | res =! ls |
|
76 | """.splitlines(keepends=True), (4, 7), '''\ | |
|
96 | """.splitlines( | |
|
97 | keepends=True | |
|
98 | ), | |
|
99 | (4, 7), | |
|
100 | """\ | |
|
77 | 101 |
|
|
78 | 102 |
|
|
79 | 103 |
|
|
80 | 104 | res =get_ipython().getoutput(\' ls\') |
|
81 | '''.splitlines(keepends=True)) | |
|
105 | """.splitlines( | |
|
106 | keepends=True | |
|
107 | ), | |
|
108 | ) | |
|
82 | 109 | |
|
83 | 110 | ###### |
|
84 | 111 | |
|
85 | AUTOCALL_QUOTE = ( | |
|
86 | [",f 1 2 3\n"], (1, 0), | |
|
87 | ['f("1", "2", "3")\n'] | |
|
88 | ) | |
|
112 | AUTOCALL_QUOTE = ([",f 1 2 3\n"], (1, 0), ['f("1", "2", "3")\n']) | |
|
89 | 113 | |
|
90 | AUTOCALL_QUOTE2 = ( | |
|
91 | [";f 1 2 3\n"], (1, 0), | |
|
92 | ['f("1 2 3")\n'] | |
|
93 | ) | |
|
114 | AUTOCALL_QUOTE2 = ([";f 1 2 3\n"], (1, 0), ['f("1 2 3")\n']) | |
|
94 | 115 | |
|
95 | AUTOCALL_PAREN = ( | |
|
96 | ["/f 1 2 3\n"], (1, 0), | |
|
97 | ['f(1, 2, 3)\n'] | |
|
98 | ) | |
|
116 | AUTOCALL_PAREN = (["/f 1 2 3\n"], (1, 0), ["f(1, 2, 3)\n"]) | |
|
99 | 117 | |
|
100 | SIMPLE_HELP = ( | |
|
101 | ["foo?\n"], (1, 0), | |
|
102 | ["get_ipython().run_line_magic('pinfo', 'foo')\n"] | |
|
103 | ) | |
|
118 | SIMPLE_HELP = (["foo?\n"], (1, 0), ["get_ipython().run_line_magic('pinfo', 'foo')\n"]) | |
|
104 | 119 | |
|
105 | 120 | DETAILED_HELP = ( |
|
106 |
["foo??\n"], |
|
|
107 | ["get_ipython().run_line_magic('pinfo2', 'foo')\n"] | |
|
121 | ["foo??\n"], | |
|
122 | (1, 0), | |
|
123 | ["get_ipython().run_line_magic('pinfo2', 'foo')\n"], | |
|
108 | 124 | ) |
|
109 | 125 | |
|
110 | MAGIC_HELP = ( | |
|
111 | ["%foo?\n"], (1, 0), | |
|
112 | ["get_ipython().run_line_magic('pinfo', '%foo')\n"] | |
|
113 | ) | |
|
126 | MAGIC_HELP = (["%foo?\n"], (1, 0), ["get_ipython().run_line_magic('pinfo', '%foo')\n"]) | |
|
114 | 127 | |
|
115 | 128 | HELP_IN_EXPR = ( |
|
116 |
["a = b + c?\n"], |
|
|
117 | ["get_ipython().set_next_input('a = b + c');" | |
|
118 |
|
|
|
129 | ["a = b + c?\n"], | |
|
130 | (1, 0), | |
|
131 | ["get_ipython().run_line_magic('pinfo', 'c')\n"], | |
|
119 | 132 | ) |
|
120 | 133 | |
|
121 |
HELP_CONTINUED_LINE = ( |
|
|
134 | HELP_CONTINUED_LINE = ( | |
|
135 | """\ | |
|
122 | 136 | a = \\ |
|
123 | 137 |
|
|
124 | """.splitlines(keepends=True), (1, 0), | |
|
125 | [r"get_ipython().set_next_input('a = \\\nzip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"] | |
|
138 | """.splitlines( | |
|
139 | keepends=True | |
|
140 | ), | |
|
141 | (1, 0), | |
|
142 | [r"get_ipython().run_line_magic('pinfo', 'zip')" + "\n"], | |
|
126 | 143 | ) |
|
127 | 144 | |
|
128 |
HELP_MULTILINE = ( |
|
|
145 | HELP_MULTILINE = ( | |
|
146 | """\ | |
|
129 | 147 |
|
|
130 | 148 |
|
|
131 | """.splitlines(keepends=True), (1, 0), | |
|
132 | [r"get_ipython().set_next_input('(a,\nb) = zip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"] | |
|
149 | """.splitlines( | |
|
150 | keepends=True | |
|
151 | ), | |
|
152 | (1, 0), | |
|
153 | [r"get_ipython().run_line_magic('pinfo', 'zip')" + "\n"], | |
|
133 | 154 | ) |
|
134 | 155 | |
|
135 | 156 | HELP_UNICODE = ( |
|
136 |
["Ο.foo?\n"], |
|
|
137 | ["get_ipython().run_line_magic('pinfo', 'Ο.foo')\n"] | |
|
157 | ["Ο.foo?\n"], | |
|
158 | (1, 0), | |
|
159 | ["get_ipython().run_line_magic('pinfo', 'Ο.foo')\n"], | |
|
138 | 160 | ) |
|
139 | 161 | |
|
140 | 162 | |
@@ -149,13 +171,14 b' def check_make_token_by_line_never_ends_empty():' | |||
|
149 | 171 | Check that not sequence of single or double characters ends up leading to en empty list of tokens |
|
150 | 172 | """ |
|
151 | 173 | from string import printable |
|
174 | ||
|
152 | 175 | for c in printable: |
|
153 | 176 | nt.assert_not_equal(make_tokens_by_line(c)[-1], []) |
|
154 | 177 | for k in printable: |
|
155 | 178 | nt.assert_not_equal(make_tokens_by_line(c+k)[-1], []) |
|
156 | 179 | |
|
157 | 180 | def check_find(transformer, case, match=True): |
|
158 |
sample, expected_start, _ |
|
|
181 | sample, expected_start, _ = case | |
|
159 | 182 | tbl = make_tokens_by_line(sample) |
|
160 | 183 | res = transformer.find(tbl) |
|
161 | 184 | if match: |
@@ -165,25 +188,30 b' def check_find(transformer, case, match=True):' | |||
|
165 | 188 | else: |
|
166 | 189 | nt.assert_is(res, None) |
|
167 | 190 | |
|
191 | ||
|
168 | 192 | def check_transform(transformer_cls, case): |
|
169 | 193 | lines, start, expected = case |
|
170 | 194 | transformer = transformer_cls(start) |
|
171 | 195 | nt.assert_equal(transformer.transform(lines), expected) |
|
172 | 196 | |
|
197 | ||
|
173 | 198 | def test_continued_line(): |
|
174 | 199 | lines = MULTILINE_MAGIC_ASSIGN[0] |
|
175 | 200 | nt.assert_equal(ipt2.find_end_of_continued_line(lines, 1), 2) |
|
176 | 201 | |
|
177 | 202 | nt.assert_equal(ipt2.assemble_continued_line(lines, (1, 5), 2), "foo bar") |
|
178 | 203 | |
|
204 | ||
|
179 | 205 | def test_find_assign_magic(): |
|
180 | 206 | check_find(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN) |
|
181 | 207 | check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN, match=False) |
|
182 | 208 | check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT, match=False) |
|
183 | 209 | |
|
210 | ||
|
184 | 211 | def test_transform_assign_magic(): |
|
185 | 212 | check_transform(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN) |
|
186 | 213 | |
|
214 | ||
|
187 | 215 | def test_find_assign_system(): |
|
188 | 216 | check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN) |
|
189 | 217 | check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT) |
@@ -191,30 +219,36 b' def test_find_assign_system():' | |||
|
191 | 219 | check_find(ipt2.SystemAssign, (["a=!ls\n"], (1, 2), None)) |
|
192 | 220 | check_find(ipt2.SystemAssign, MULTILINE_MAGIC_ASSIGN, match=False) |
|
193 | 221 | |
|
222 | ||
|
194 | 223 | def test_transform_assign_system(): |
|
195 | 224 | check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN) |
|
196 | 225 | check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT) |
|
197 | 226 | |
|
227 | ||
|
198 | 228 | def test_find_magic_escape(): |
|
199 | 229 | check_find(ipt2.EscapedCommand, MULTILINE_MAGIC) |
|
200 | 230 | check_find(ipt2.EscapedCommand, INDENTED_MAGIC) |
|
201 | 231 | check_find(ipt2.EscapedCommand, MULTILINE_MAGIC_ASSIGN, match=False) |
|
202 | 232 | |
|
233 | ||
|
203 | 234 | def test_transform_magic_escape(): |
|
204 | 235 | check_transform(ipt2.EscapedCommand, MULTILINE_MAGIC) |
|
205 | 236 | check_transform(ipt2.EscapedCommand, INDENTED_MAGIC) |
|
206 | 237 | check_transform(ipt2.EscapedCommand, CRLF_MAGIC) |
|
207 | 238 | |
|
239 | ||
|
208 | 240 | def test_find_autocalls(): |
|
209 | 241 | for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]: |
|
210 | 242 | print("Testing %r" % case[0]) |
|
211 | 243 | check_find(ipt2.EscapedCommand, case) |
|
212 | 244 | |
|
245 | ||
|
213 | 246 | def test_transform_autocall(): |
|
214 | 247 | for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]: |
|
215 | 248 | print("Testing %r" % case[0]) |
|
216 | 249 | check_transform(ipt2.EscapedCommand, case) |
|
217 | 250 | |
|
251 | ||
|
218 | 252 | def test_find_help(): |
|
219 | 253 | for case in [SIMPLE_HELP, DETAILED_HELP, MAGIC_HELP, HELP_IN_EXPR]: |
|
220 | 254 | check_find(ipt2.HelpEnd, case) |
@@ -232,6 +266,7 b' def test_find_help():' | |||
|
232 | 266 | # Nor in a string |
|
233 | 267 | check_find(ipt2.HelpEnd, (["foo = '''bar?\n"], None, None), match=False) |
|
234 | 268 | |
|
269 | ||
|
235 | 270 | def test_transform_help(): |
|
236 | 271 | tf = ipt2.HelpEnd((1, 0), (1, 9)) |
|
237 | 272 | nt.assert_equal(tf.transform(HELP_IN_EXPR[0]), HELP_IN_EXPR[2]) |
@@ -245,10 +280,12 b' def test_transform_help():' | |||
|
245 | 280 | tf = ipt2.HelpEnd((1, 0), (1, 0)) |
|
246 | 281 | nt.assert_equal(tf.transform(HELP_UNICODE[0]), HELP_UNICODE[2]) |
|
247 | 282 | |
|
283 | ||
|
248 | 284 | def test_find_assign_op_dedent(): |
|
249 | 285 | """ |
|
250 | 286 | be careful that empty token like dedent are not counted as parens |
|
251 | 287 | """ |
|
288 | ||
|
252 | 289 | class Tk: |
|
253 | 290 | def __init__(self, s): |
|
254 | 291 | self.string = s |
@@ -289,21 +326,23 b' examples = [' | |||
|
289 | 326 | def test_check_complete(): |
|
290 | 327 | cc = ipt2.TransformerManager().check_complete |
|
291 | 328 | |
|
292 |
example = dedent( |
|
|
329 | example = dedent( | |
|
330 | """ | |
|
293 | 331 |
|
|
294 |
|
|
|
332 | a=1""" | |
|
333 | ) | |
|
295 | 334 | |
|
296 | 335 | nt.assert_equal(cc(example), ('incomplete', 4)) |
|
297 | 336 | nt.assert_equal(cc(example+'\n'), ('complete', None)) |
|
298 | 337 | nt.assert_equal(cc(example+'\n '), ('complete', None)) |
|
299 | 338 | |
|
300 | 339 | # no need to loop on all the letters/numbers. |
|
301 |
short = |
|
|
340 | short = "12abAB" + string.printable[62:] | |
|
302 | 341 | for c in short: |
|
303 | 342 | # test does not raise: |
|
304 | 343 | cc(c) |
|
305 | 344 | for k in short: |
|
306 | cc(c+k) | |
|
345 | cc(c + k) | |
|
307 | 346 | |
|
308 | 347 | nt.assert_equal(cc("def f():\n x=0\n \\\n "), ('incomplete', 2)) |
|
309 | 348 | |
@@ -324,10 +363,9 b' def test_null_cleanup_transformer():' | |||
|
324 | 363 | assert manager.transform_cell("") == "" |
|
325 | 364 | |
|
326 | 365 | |
|
327 | ||
|
328 | ||
|
329 | 366 | def test_side_effects_I(): |
|
330 | 367 | count = 0 |
|
368 | ||
|
331 | 369 | def counter(lines): |
|
332 | 370 | nonlocal count |
|
333 | 371 | count += 1 |
@@ -337,14 +375,13 b' def test_side_effects_I():' | |||
|
337 | 375 | |
|
338 | 376 | manager = ipt2.TransformerManager() |
|
339 | 377 | manager.cleanup_transforms.insert(0, counter) |
|
340 |
assert manager.check_complete("a=1\n") == ( |
|
|
378 | assert manager.check_complete("a=1\n") == ("complete", None) | |
|
341 | 379 | assert count == 0 |
|
342 | 380 | |
|
343 | 381 | |
|
344 | ||
|
345 | ||
|
346 | 382 | def test_side_effects_II(): |
|
347 | 383 | count = 0 |
|
384 | ||
|
348 | 385 | def counter(lines): |
|
349 | 386 | nonlocal count |
|
350 | 387 | count += 1 |
@@ -354,5 +391,5 b' def test_side_effects_II():' | |||
|
354 | 391 | |
|
355 | 392 | manager = ipt2.TransformerManager() |
|
356 | 393 | manager.line_transforms.insert(0, counter) |
|
357 |
assert manager.check_complete("b=1\n") == ( |
|
|
394 | assert manager.check_complete("b=1\n") == ("complete", None) | |
|
358 | 395 | assert count == 0 |
General Comments 0
You need to be logged in to leave comments.
Login now