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