##// END OF EJS Templates
Apply pygrade --36plus on IPython/core/tests/test_inputtransformer.py....
Matthias Bussonnier -
Show More
@@ -37,30 +37,30 b' def transform_checker(tests, transformer, **kwargs):'
37
37
38 syntax = \
38 syntax = \
39 dict(assign_system =
39 dict(assign_system =
40 [(u'a =! ls', "a = get_ipython().getoutput('ls')"),
40 [('a =! ls', "a = get_ipython().getoutput('ls')"),
41 (u'b = !ls', "b = get_ipython().getoutput('ls')"),
41 ('b = !ls', "b = get_ipython().getoutput('ls')"),
42 (u'c= !ls', "c = get_ipython().getoutput('ls')"),
42 ('c= !ls', "c = get_ipython().getoutput('ls')"),
43 (u'd == !ls', u'd == !ls'), # Invalid syntax, but we leave == alone.
43 ('d == !ls', 'd == !ls'), # Invalid syntax, but we leave == alone.
44 ('x=1', 'x=1'), # normal input is unmodified
44 ('x=1', 'x=1'), # normal input is unmodified
45 (' ',' '), # blank lines are kept intact
45 (' ',' '), # blank lines are kept intact
46 # Tuple unpacking
46 # Tuple unpacking
47 (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")"),
47 ("a, b = !echo 'a\\nb'", "a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")"),
48 (u"a,= !echo 'a'", u"a, = get_ipython().getoutput(\"echo 'a'\")"),
48 ("a,= !echo 'a'", "a, = get_ipython().getoutput(\"echo 'a'\")"),
49 (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")"),
49 ("a, *bc = !echo 'a\\nb\\nc'", "a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")"),
50 # Tuple unpacking with regular Python expressions, not our syntax.
50 # Tuple unpacking with regular Python expressions, not our syntax.
51 (u"a, b = range(2)", u"a, b = range(2)"),
51 ("a, b = range(2)", "a, b = range(2)"),
52 (u"a, = range(1)", u"a, = range(1)"),
52 ("a, = range(1)", "a, = range(1)"),
53 (u"a, *bc = range(3)", u"a, *bc = range(3)"),
53 ("a, *bc = range(3)", "a, *bc = range(3)"),
54 ],
54 ],
55
55
56 assign_magic =
56 assign_magic =
57 [(u'a =% who', "a = get_ipython().run_line_magic('who', '')"),
57 [('a =% who', "a = get_ipython().run_line_magic('who', '')"),
58 (u'b = %who', "b = get_ipython().run_line_magic('who', '')"),
58 ('b = %who', "b = get_ipython().run_line_magic('who', '')"),
59 (u'c= %ls', "c = get_ipython().run_line_magic('ls', '')"),
59 ('c= %ls', "c = get_ipython().run_line_magic('ls', '')"),
60 (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone.
60 ('d == %ls', 'd == %ls'), # Invalid syntax, but we leave == alone.
61 ('x=1', 'x=1'), # normal input is unmodified
61 ('x=1', 'x=1'), # normal input is unmodified
62 (' ',' '), # blank lines are kept intact
62 (' ',' '), # blank lines are kept intact
63 (u"a, b = %foo", u"a, b = get_ipython().run_line_magic('foo', '')"),
63 ("a, b = %foo", "a, b = get_ipython().run_line_magic('foo', '')"),
64 ],
64 ],
65
65
66 classic_prompt =
66 classic_prompt =
@@ -83,51 +83,51 b' syntax = \\'
83
83
84 # System calls
84 # System calls
85 escaped_shell =
85 escaped_shell =
86 [ (u'!ls', "get_ipython().system('ls')"),
86 [ ('!ls', "get_ipython().system('ls')"),
87 # Double-escape shell, this means to capture the output of the
87 # Double-escape shell, this means to capture the output of the
88 # subprocess and return it
88 # subprocess and return it
89 (u'!!ls', "get_ipython().getoutput('ls')"),
89 ('!!ls', "get_ipython().getoutput('ls')"),
90 ],
90 ],
91
91
92 # Help/object info
92 # Help/object info
93 escaped_help =
93 escaped_help =
94 [ (u'?', 'get_ipython().show_usage()'),
94 [ ('?', 'get_ipython().show_usage()'),
95 (u'?x1', "get_ipython().run_line_magic('pinfo', 'x1')"),
95 ('?x1', "get_ipython().run_line_magic('pinfo', 'x1')"),
96 (u'??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"),
96 ('??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"),
97 (u'?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"),
97 ('?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"),
98 (u'?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"),
98 ('?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"),
99 (u'?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"),
99 ('?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"),
100 (u'?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"),
100 ('?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"),
101 ],
101 ],
102
102
103 end_help =
103 end_help =
104 [ (u'x3?', "get_ipython().run_line_magic('pinfo', 'x3')"),
104 [ ('x3?', "get_ipython().run_line_magic('pinfo', 'x3')"),
105 (u'x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"),
105 ('x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"),
106 (u'%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"),
106 ('%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"),
107 (u'%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"),
107 ('%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"),
108 (u'%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"),
108 ('%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"),
109 (u'%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"),
109 ('%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"),
110 (u'Ο€.foo?', "get_ipython().run_line_magic('pinfo', 'Ο€.foo')"),
110 ('Ο€.foo?', "get_ipython().run_line_magic('pinfo', 'Ο€.foo')"),
111 (u'f*?', "get_ipython().run_line_magic('psearch', 'f*')"),
111 ('f*?', "get_ipython().run_line_magic('psearch', 'f*')"),
112 (u'ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"),
112 ('ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"),
113 (u'a = abc?', "get_ipython().set_next_input('a = abc');"
113 ('a = abc?', "get_ipython().set_next_input('a = abc');"
114 "get_ipython().run_line_magic('pinfo', 'abc')"),
114 "get_ipython().run_line_magic('pinfo', 'abc')"),
115 (u'a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');"
115 ('a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');"
116 "get_ipython().run_line_magic('pinfo2', 'abc.qe')"),
116 "get_ipython().run_line_magic('pinfo2', 'abc.qe')"),
117 (u'a = *.items?', "get_ipython().set_next_input('a = *.items');"
117 ('a = *.items?', "get_ipython().set_next_input('a = *.items');"
118 "get_ipython().run_line_magic('psearch', '*.items')"),
118 "get_ipython().run_line_magic('psearch', '*.items')"),
119 (u'plot(a?', "get_ipython().set_next_input('plot(a');"
119 ('plot(a?', "get_ipython().set_next_input('plot(a');"
120 "get_ipython().run_line_magic('pinfo', 'a')"),
120 "get_ipython().run_line_magic('pinfo', 'a')"),
121 (u'a*2 #comment?', 'a*2 #comment?'),
121 ('a*2 #comment?', 'a*2 #comment?'),
122 ],
122 ],
123
123
124 # Explicit magic calls
124 # Explicit magic calls
125 escaped_magic =
125 escaped_magic =
126 [ (u'%cd', "get_ipython().run_line_magic('cd', '')"),
126 [ ('%cd', "get_ipython().run_line_magic('cd', '')"),
127 (u'%cd /home', "get_ipython().run_line_magic('cd', '/home')"),
127 ('%cd /home', "get_ipython().run_line_magic('cd', '/home')"),
128 # Backslashes need to be escaped.
128 # Backslashes need to be escaped.
129 (u'%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"),
129 ('%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"),
130 (u' %magic', " get_ipython().run_line_magic('magic', '')"),
130 (' %magic', " get_ipython().run_line_magic('magic', '')"),
131 ],
131 ],
132
132
133 # Quoting with separate arguments
133 # Quoting with separate arguments
@@ -156,11 +156,11 b' syntax = \\'
156
156
157 # Check that we transform prompts before other transforms
157 # Check that we transform prompts before other transforms
158 mixed =
158 mixed =
159 [ (u'In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
159 [ ('In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
160 (u'>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
160 ('>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
161 (u'In [2]: !ls', "get_ipython().system('ls')"),
161 ('In [2]: !ls', "get_ipython().system('ls')"),
162 (u'In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"),
162 ('In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"),
163 (u'In [4]: b = %who', "b = get_ipython().run_line_magic('who', '')"),
163 ('In [4]: b = %who', "b = get_ipython().run_line_magic('who', '')"),
164 ],
164 ],
165 )
165 )
166
166
@@ -275,15 +275,15 b' syntax_ml = \\'
275 ],
275 ],
276
276
277 cellmagic =
277 cellmagic =
278 [ [(u'%%foo a', None),
278 [ [('%%foo a', None),
279 (None, "get_ipython().run_cell_magic('foo', 'a', '')"),
279 (None, "get_ipython().run_cell_magic('foo', 'a', '')"),
280 ],
280 ],
281 [(u'%%bar 123', None),
281 [('%%bar 123', None),
282 (u'hello', None),
282 ('hello', None),
283 (None , "get_ipython().run_cell_magic('bar', '123', 'hello')"),
283 (None , "get_ipython().run_cell_magic('bar', '123', 'hello')"),
284 ],
284 ],
285 [(u'a=5', 'a=5'),
285 [('a=5', 'a=5'),
286 (u'%%cellmagic', '%%cellmagic'),
286 ('%%cellmagic', '%%cellmagic'),
287 ],
287 ],
288 ],
288 ],
289
289
@@ -298,21 +298,21 b' syntax_ml = \\'
298 ],
298 ],
299
299
300 assign_magic =
300 assign_magic =
301 [ [(u'a = %bc de \\', None),
301 [ [('a = %bc de \\', None),
302 (u'fg', "a = get_ipython().run_line_magic('bc', 'de fg')"),
302 ('fg', "a = get_ipython().run_line_magic('bc', 'de fg')"),
303 ],
303 ],
304 [(u'a = %bc de \\', None),
304 [('a = %bc de \\', None),
305 (u'fg\\', None),
305 ('fg\\', None),
306 (None, "a = get_ipython().run_line_magic('bc', 'de fg')"),
306 (None, "a = get_ipython().run_line_magic('bc', 'de fg')"),
307 ],
307 ],
308 ],
308 ],
309
309
310 assign_system =
310 assign_system =
311 [ [(u'a = !bc de \\', None),
311 [ [('a = !bc de \\', None),
312 (u'fg', "a = get_ipython().getoutput('bc de fg')"),
312 ('fg', "a = get_ipython().getoutput('bc de fg')"),
313 ],
313 ],
314 [(u'a = !bc de \\', None),
314 [('a = !bc de \\', None),
315 (u'fg\\', None),
315 ('fg\\', None),
316 (None, "a = get_ipython().getoutput('bc de fg')"),
316 (None, "a = get_ipython().getoutput('bc de fg')"),
317 ],
317 ],
318 ],
318 ],
@@ -335,8 +335,8 b' def test_classic_prompt():'
335 # Check that we don't transform the second line if the first is obviously
335 # Check that we don't transform the second line if the first is obviously
336 # IPython syntax
336 # IPython syntax
337 transform_checker([
337 transform_checker([
338 (u'%foo', '%foo'),
338 ('%foo', '%foo'),
339 (u'>>> bar', '>>> bar'),
339 ('>>> bar', '>>> bar'),
340 ], ipt.classic_prompt)
340 ], ipt.classic_prompt)
341
341
342
342
@@ -347,20 +347,20 b' def test_ipy_prompt():'
347
347
348 # Check that we don't transform the second line if we're inside a cell magic
348 # Check that we don't transform the second line if we're inside a cell magic
349 transform_checker([
349 transform_checker([
350 (u'%%foo', '%%foo'),
350 ('%%foo', '%%foo'),
351 (u'In [1]: bar', 'In [1]: bar'),
351 ('In [1]: bar', 'In [1]: bar'),
352 ], ipt.ipy_prompt)
352 ], ipt.ipy_prompt)
353
353
354 def test_assemble_logical_lines():
354 def test_assemble_logical_lines():
355 tests = \
355 tests = \
356 [ [(u"a = \\", None),
356 [ [("a = \\", None),
357 (u"123", u"a = 123"),
357 ("123", "a = 123"),
358 ],
358 ],
359 [(u"a = \\", None), # Test resetting when within a multi-line string
359 [("a = \\", None), # Test resetting when within a multi-line string
360 (u"12 *\\", None),
360 ("12 *\\", None),
361 (None, u"a = 12 *"),
361 (None, "a = 12 *"),
362 ],
362 ],
363 [(u"# foo\\", u"# foo\\"), # Comments can't be continued like this
363 [("# foo\\", "# foo\\"), # Comments can't be continued like this
364 ],
364 ],
365 ]
365 ]
366 for example in tests:
366 for example in tests:
@@ -368,24 +368,24 b' def test_assemble_logical_lines():'
368
368
369 def test_assemble_python_lines():
369 def test_assemble_python_lines():
370 tests = \
370 tests = \
371 [ [(u"a = '''", None),
371 [ [("a = '''", None),
372 (u"abc'''", u"a = '''\nabc'''"),
372 ("abc'''", "a = '''\nabc'''"),
373 ],
373 ],
374 [(u"a = '''", None), # Test resetting when within a multi-line string
374 [("a = '''", None), # Test resetting when within a multi-line string
375 (u"def", None),
375 ("def", None),
376 (None, u"a = '''\ndef"),
376 (None, "a = '''\ndef"),
377 ],
377 ],
378 [(u"a = [1,", None),
378 [("a = [1,", None),
379 (u"2]", u"a = [1,\n2]"),
379 ("2]", "a = [1,\n2]"),
380 ],
380 ],
381 [(u"a = [1,", None), # Test resetting when within a multi-line string
381 [("a = [1,", None), # Test resetting when within a multi-line string
382 (u"2,", None),
382 ("2,", None),
383 (None, u"a = [1,\n2,"),
383 (None, "a = [1,\n2,"),
384 ],
384 ],
385 [(u"a = '''", None), # Test line continuation within a multi-line string
385 [("a = '''", None), # Test line continuation within a multi-line string
386 (u"abc\\", None),
386 ("abc\\", None),
387 (u"def", None),
387 ("def", None),
388 (u"'''", u"a = '''\nabc\\\ndef\n'''"),
388 ("'''", "a = '''\nabc\\\ndef\n'''"),
389 ],
389 ],
390 ] + syntax_ml['multiline_datastructure']
390 ] + syntax_ml['multiline_datastructure']
391 for example in tests:
391 for example in tests:
@@ -427,9 +427,9 b' def test_cellmagic():'
427 for example in syntax_ml['cellmagic']:
427 for example in syntax_ml['cellmagic']:
428 transform_checker(example, ipt.cellmagic)
428 transform_checker(example, ipt.cellmagic)
429
429
430 line_example = [(u'%%bar 123', None),
430 line_example = [('%%bar 123', None),
431 (u'hello', None),
431 ('hello', None),
432 (u'' , "get_ipython().run_cell_magic('bar', '123', 'hello')"),
432 ('' , "get_ipython().run_cell_magic('bar', '123', 'hello')"),
433 ]
433 ]
434 transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True)
434 transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True)
435
435
@@ -454,33 +454,32 b' def decistmt(tokens):'
454 result = []
454 result = []
455 for toknum, tokval, _, _, _ in tokens:
455 for toknum, tokval, _, _, _ in tokens:
456 if toknum == tokenize.NUMBER and '.' in tokval: # replace NUMBER tokens
456 if toknum == tokenize.NUMBER and '.' in tokval: # replace NUMBER tokens
457 for newtok in [
457 yield from [
458 (tokenize.NAME, 'Decimal'),
458 (tokenize.NAME, 'Decimal'),
459 (tokenize.OP, '('),
459 (tokenize.OP, '('),
460 (tokenize.STRING, repr(tokval)),
460 (tokenize.STRING, repr(tokval)),
461 (tokenize.OP, ')')
461 (tokenize.OP, ')')
462 ]:
462 ]
463 yield newtok
464 else:
463 else:
465 yield (toknum, tokval)
464 yield (toknum, tokval)
466
465
467
466
468
467
469 def test_token_input_transformer():
468 def test_token_input_transformer():
470 tests = [(u'1.2', u"Decimal ('1.2')"),
469 tests = [('1.2', "Decimal ('1.2')"),
471 (u'"1.2"', u'"1.2"'),
470 ('"1.2"', '"1.2"'),
472 ]
471 ]
473 tt.check_pairs(transform_and_reset(decistmt), tests)
472 tt.check_pairs(transform_and_reset(decistmt), tests)
474 ml_tests = \
473 ml_tests = \
475 [ [(u"a = 1.2; b = '''x", None),
474 [ [("a = 1.2; b = '''x", None),
476 (u"y'''", u"a =Decimal ('1.2');b ='''x\ny'''"),
475 ("y'''", "a =Decimal ('1.2');b ='''x\ny'''"),
477 ],
476 ],
478 [(u"a = [1.2,", None),
477 [("a = [1.2,", None),
479 (u"3]", u"a =[Decimal ('1.2'),\n3 ]"),
478 ("3]", "a =[Decimal ('1.2'),\n3 ]"),
480 ],
479 ],
481 [(u"a = '''foo", None), # Test resetting when within a multi-line string
480 [("a = '''foo", None), # Test resetting when within a multi-line string
482 (u"bar", None),
481 ("bar", None),
483 (None, u"a = '''foo\nbar"),
482 (None, "a = '''foo\nbar"),
484 ],
483 ],
485 ]
484 ]
486 for example in ml_tests:
485 for example in ml_tests:
General Comments 0
You need to be logged in to leave comments. Login now