##// END OF EJS Templates
Merge pull request #13629 from Carreau/auto-backport-of-pr-13625-on-7.x...
Matthias Bussonnier -
r27633:4828b80e merge
parent child Browse files
Show More
@@ -193,7 +193,7 b' 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,11 +203,12 b' 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):
@@ -349,10 +350,7 b' 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 b" 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 b' 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 b' 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,140 +36,144 b' 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 (" ", " "), # blank lines are kept intact
48 # Tuple unpacking
49 # Tuple unpacking
49 (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")"),
50 (
51 u"a, b = !echo 'a\\nb'",
52 u"a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")",
53 ),
50 (u"a,= !echo 'a'", u"a, = get_ipython().getoutput(\"echo 'a'\")"),
54 (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'\")"),
55 (
56 u"a, *bc = !echo 'a\\nb\\nc'",
57 u"a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")",
58 ),
52 # Tuple unpacking with regular Python expressions, not our syntax.
59 # Tuple unpacking with regular Python expressions, not our syntax.
53 (u"a, b = range(2)", u"a, b = range(2)"),
60 (u"a, b = range(2)", u"a, b = range(2)"),
54 (u"a, = range(1)", u"a, = range(1)"),
61 (u"a, = range(1)", u"a, = range(1)"),
55 (u"a, *bc = range(3)", u"a, *bc = range(3)"),
62 (u"a, *bc = range(3)", u"a, *bc = range(3)"),
56 ]],
63 ]
57
64 ],
58 assign_magic =
65 assign_magic=[
59 [(i,py3compat.u_format(o)) for i,o in \
66 (i, py3compat.u_format(o))
60 [(u'a =% who', "a = get_ipython().run_line_magic('who', '')"),
67 for i, o in [
61 (u'b = %who', "b = get_ipython().run_line_magic('who', '')"),
68 (u"a =% who", "a = get_ipython().run_line_magic('who', '')"),
62 (u'c= %ls', "c = get_ipython().run_line_magic('ls', '')"),
69 (u"b = %who", "b = get_ipython().run_line_magic('who', '')"),
63 (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone.
70 (u"c= %ls", "c = get_ipython().run_line_magic('ls', '')"),
64 ('x=1', 'x=1'), # normal input is unmodified
71 (u"d == %ls", u"d == %ls"), # Invalid syntax, but we leave == alone.
65 (' ',' '), # blank lines are kept intact
72 ("x=1", "x=1"), # normal input is unmodified
73 (" ", " "), # blank lines are kept intact
66 (u"a, b = %foo", u"a, b = get_ipython().run_line_magic('foo', '')"),
74 (u"a, b = %foo", u"a, b = get_ipython().run_line_magic('foo', '')"),
67 ]],
75 ]
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 ],
76 ],
74
77 classic_prompt=[
75 ipy_prompt =
78 (">>> x=1", "x=1"),
76 [('In [1]: x=1', 'x=1'),
79 ("x=1", "x=1"), # normal input is unmodified
77 ('x=1', 'x=1'), # normal input is unmodified
80 (" ", " "), # blank lines are kept intact
78 (' ',' '), # 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
79 ],
86 ],
80
81 # Tests for the escape transformer to leave normal code alone
87 # Tests for the escape transformer to leave normal code alone
82 escaped_noesc =
88 escaped_noesc=[
83 [ (' ', ' '),
89 (" ", " "),
84 ('x=1', 'x=1'),
90 ("x=1", "x=1"),
85 ],
91 ],
86
87 # System calls
92 # System calls
88 escaped_shell =
93 escaped_shell=[
89 [(i,py3compat.u_format(o)) for i,o in \
94 (i, py3compat.u_format(o))
90 [ (u'!ls', "get_ipython().system('ls')"),
95 for i, o in [
96 (u"!ls", "get_ipython().system('ls')"),
91 # Double-escape shell, this means to capture the output of the
97 # Double-escape shell, this means to capture the output of the
92 # subprocess and return it
98 # subprocess and return it
93 (u'!!ls', "get_ipython().getoutput('ls')"),
99 (u"!!ls", "get_ipython().getoutput('ls')"),
94 ]],
100 ]
95
101 ],
96 # Help/object info
102 # Help/object info
97 escaped_help =
103 escaped_help=[
98 [(i,py3compat.u_format(o)) for i,o in \
104 (i, py3compat.u_format(o))
99 [ (u'?', 'get_ipython().show_usage()'),
105 for i, o in [
100 (u'?x1', "get_ipython().run_line_magic('pinfo', 'x1')"),
106 (u"?", "get_ipython().show_usage()"),
101 (u'??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"),
107 (u"?x1", "get_ipython().run_line_magic('pinfo', 'x1')"),
102 (u'?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"),
108 (u"??x2", "get_ipython().run_line_magic('pinfo2', 'x2')"),
103 (u'?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"),
109 (u"?a.*s", "get_ipython().run_line_magic('psearch', 'a.*s')"),
104 (u'?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"),
110 (u"?%hist1", "get_ipython().run_line_magic('pinfo', '%hist1')"),
105 (u'?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"),
111 (u"?%%hist2", "get_ipython().run_line_magic('pinfo', '%%hist2')"),
106 ]],
112 (u"?abc = qwe", "get_ipython().run_line_magic('pinfo', 'abc')"),
107
113 ]
108 end_help =
114 ],
109 [(i,py3compat.u_format(o)) for i,o in \
115 end_help=[
110 [ (u'x3?', "get_ipython().run_line_magic('pinfo', 'x3')"),
116 (i, py3compat.u_format(o))
111 (u'x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"),
117 for i, o in [
112 (u'%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"),
118 (u"x3?", "get_ipython().run_line_magic('pinfo', 'x3')"),
113 (u'%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"),
119 (u"x4??", "get_ipython().run_line_magic('pinfo2', 'x4')"),
114 (u'%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"),
120 (u"%hist1?", "get_ipython().run_line_magic('pinfo', '%hist1')"),
115 (u'%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"),
121 (u"%hist2??", "get_ipython().run_line_magic('pinfo2', '%hist2')"),
116 (u'Ο€.foo?', "get_ipython().run_line_magic('pinfo', 'Ο€.foo')"),
122 (u"%%hist3?", "get_ipython().run_line_magic('pinfo', '%%hist3')"),
117 (u'f*?', "get_ipython().run_line_magic('psearch', 'f*')"),
123 (u"%%hist4??", "get_ipython().run_line_magic('pinfo2', '%%hist4')"),
118 (u'ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"),
124 (u"Ο€.foo?", "get_ipython().run_line_magic('pinfo', 'Ο€.foo')"),
119 (u'a = abc?', "get_ipython().set_next_input('a = abc');"
125 (u"f*?", "get_ipython().run_line_magic('psearch', 'f*')"),
120 "get_ipython().run_line_magic('pinfo', 'abc')"),
126 (u"ax.*aspe*?", "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"),
121 (u'a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');"
127 (u"a = abc?", "get_ipython().run_line_magic('pinfo', 'abc')"),
122 "get_ipython().run_line_magic('pinfo2', 'abc.qe')"),
128 (u"a = abc.qe??", "get_ipython().run_line_magic('pinfo2', 'abc.qe')"),
123 (u'a = *.items?', "get_ipython().set_next_input('a = *.items');"
129 (u"a = *.items?", "get_ipython().run_line_magic('psearch', '*.items')"),
124 "get_ipython().run_line_magic('psearch', '*.items')"),
130 (u"plot(a?", "get_ipython().run_line_magic('pinfo', 'a')"),
125 (u'plot(a?', "get_ipython().set_next_input('plot(a');"
131 (u"a*2 #comment?", "a*2 #comment?"),
126 "get_ipython().run_line_magic('pinfo', 'a')"),
132 ]
127 (u'a*2 #comment?', 'a*2 #comment?'),
133 ],
128 ]],
129
130 # Explicit magic calls
134 # Explicit magic calls
131 escaped_magic =
135 escaped_magic=[
132 [(i,py3compat.u_format(o)) for i,o in \
136 (i, py3compat.u_format(o))
133 [ (u'%cd', "get_ipython().run_line_magic('cd', '')"),
137 for i, o in [
134 (u'%cd /home', "get_ipython().run_line_magic('cd', '/home')"),
138 (u"%cd", "get_ipython().run_line_magic('cd', '')"),
139 (u"%cd /home", "get_ipython().run_line_magic('cd', '/home')"),
135 # Backslashes need to be escaped.
140 # Backslashes need to be escaped.
136 (u'%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"),
141 (u"%cd C:\\User", "get_ipython().run_line_magic('cd', 'C:\\\\User')"),
137 (u' %magic', " get_ipython().run_line_magic('magic', '')"),
142 (u" %magic", " get_ipython().run_line_magic('magic', '')"),
138 ]],
143 ]
139
144 ],
140 # Quoting with separate arguments
145 # Quoting with separate arguments
141 escaped_quote =
146 escaped_quote=[
142 [ (',f', 'f("")'),
147 (",f", 'f("")'),
143 (',f x', 'f("x")'),
148 (",f x", 'f("x")'),
144 (' ,f y', ' f("y")'),
149 (" ,f y", ' f("y")'),
145 (',f a b', 'f("a", "b")'),
150 (",f a b", 'f("a", "b")'),
146 ],
151 ],
147
148 # Quoting with single argument
152 # Quoting with single argument
149 escaped_quote2 =
153 escaped_quote2=[
150 [ (';f', 'f("")'),
154 (";f", 'f("")'),
151 (';f x', 'f("x")'),
155 (";f x", 'f("x")'),
152 (' ;f y', ' f("y")'),
156 (" ;f y", ' f("y")'),
153 (';f a b', 'f("a b")'),
157 (";f a b", 'f("a b")'),
154 ],
158 ],
155
156 # Simply apply parens
159 # Simply apply parens
157 escaped_paren =
160 escaped_paren=[
158 [ ('/f', 'f()'),
161 ("/f", "f()"),
159 ('/f x', 'f(x)'),
162 ("/f x", "f(x)"),
160 (' /f y', ' f(y)'),
163 (" /f y", " f(y)"),
161 ('/f a b', 'f(a, b)'),
164 ("/f a b", "f(a, b)"),
162 ],
165 ],
163
164 # Check that we transform prompts before other transforms
166 # Check that we transform prompts before other transforms
165 mixed =
167 mixed=[
166 [(i,py3compat.u_format(o)) for i,o in \
168 (i, py3compat.u_format(o))
167 [ (u'In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
169 for i, o in [
168 (u'>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
170 (u"In [1]: %lsmagic", "get_ipython().run_line_magic('lsmagic', '')"),
169 (u'In [2]: !ls', "get_ipython().system('ls')"),
171 (u">>> %lsmagic", "get_ipython().run_line_magic('lsmagic', '')"),
170 (u'In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"),
172 (u"In [2]: !ls", "get_ipython().system('ls')"),
171 (u'In [4]: b = %who', "b = get_ipython().run_line_magic('who', '')"),
173 (u"In [3]: abs?", "get_ipython().run_line_magic('pinfo', 'abs')"),
172 ]],
174 (u"In [4]: b = %who", "b = get_ipython().run_line_magic('who', '')"),
175 ]
176 ],
173 )
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
@@ -15,45 +15,65 b' 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 )
52
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 )
36
58
37 CRLF_MAGIC = ([
59 MULTILINE_MAGIC_ASSIGN = (
38 "a = f()\n",
60 """\
39 "%ls\r\n",
40 "g()\n"
41 ], (2, 0), [
42 "a = f()\n",
43 "get_ipython().run_line_magic('ls', '')\n",
44 "g()\n"
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 b' 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,6 +171,7 b' 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:
@@ -165,25 +188,30 b' 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 b' 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 b' 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 b' 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,16 +326,18 b' 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)
@@ -324,10 +363,9 b' 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 b' 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 b' 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