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 |
[( |
|
40 | [('a =! ls', "a = get_ipython().getoutput('ls')"), | |
41 |
( |
|
41 | ('b = !ls', "b = get_ipython().getoutput('ls')"), | |
42 |
( |
|
42 | ('c= !ls', "c = get_ipython().getoutput('ls')"), | |
43 |
( |
|
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 |
( |
|
47 | ("a, b = !echo 'a\\nb'", "a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")"), | |
48 |
( |
|
48 | ("a,= !echo 'a'", "a, = get_ipython().getoutput(\"echo 'a'\")"), | |
49 |
( |
|
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 |
( |
|
51 | ("a, b = range(2)", "a, b = range(2)"), | |
52 |
( |
|
52 | ("a, = range(1)", "a, = range(1)"), | |
53 |
( |
|
53 | ("a, *bc = range(3)", "a, *bc = range(3)"), | |
54 | ], |
|
54 | ], | |
55 |
|
55 | |||
56 | assign_magic = |
|
56 | assign_magic = | |
57 |
[( |
|
57 | [('a =% who', "a = get_ipython().run_line_magic('who', '')"), | |
58 |
( |
|
58 | ('b = %who', "b = get_ipython().run_line_magic('who', '')"), | |
59 |
( |
|
59 | ('c= %ls', "c = get_ipython().run_line_magic('ls', '')"), | |
60 |
( |
|
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 |
( |
|
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 |
[ ( |
|
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 |
( |
|
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 |
[ ( |
|
94 | [ ('?', 'get_ipython().show_usage()'), | |
95 |
( |
|
95 | ('?x1', "get_ipython().run_line_magic('pinfo', 'x1')"), | |
96 |
( |
|
96 | ('??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"), | |
97 |
( |
|
97 | ('?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"), | |
98 |
( |
|
98 | ('?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"), | |
99 |
( |
|
99 | ('?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"), | |
100 |
( |
|
100 | ('?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"), | |
101 | ], |
|
101 | ], | |
102 |
|
102 | |||
103 | end_help = |
|
103 | end_help = | |
104 |
[ ( |
|
104 | [ ('x3?', "get_ipython().run_line_magic('pinfo', 'x3')"), | |
105 |
( |
|
105 | ('x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"), | |
106 |
( |
|
106 | ('%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"), | |
107 |
( |
|
107 | ('%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"), | |
108 |
( |
|
108 | ('%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"), | |
109 |
( |
|
109 | ('%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"), | |
110 |
( |
|
110 | ('Ο.foo?', "get_ipython().run_line_magic('pinfo', 'Ο.foo')"), | |
111 |
( |
|
111 | ('f*?', "get_ipython().run_line_magic('psearch', 'f*')"), | |
112 |
( |
|
112 | ('ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"), | |
113 |
( |
|
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 |
( |
|
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 |
( |
|
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 |
( |
|
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 |
( |
|
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 |
[ ( |
|
126 | [ ('%cd', "get_ipython().run_line_magic('cd', '')"), | |
127 |
( |
|
127 | ('%cd /home', "get_ipython().run_line_magic('cd', '/home')"), | |
128 | # Backslashes need to be escaped. |
|
128 | # Backslashes need to be escaped. | |
129 |
( |
|
129 | ('%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"), | |
130 |
( |
|
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 |
[ ( |
|
159 | [ ('In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"), | |
160 |
( |
|
160 | ('>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"), | |
161 |
( |
|
161 | ('In [2]: !ls', "get_ipython().system('ls')"), | |
162 |
( |
|
162 | ('In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"), | |
163 |
( |
|
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 |
[ [( |
|
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 |
[( |
|
281 | [('%%bar 123', None), | |
282 |
( |
|
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 |
[( |
|
285 | [('a=5', 'a=5'), | |
286 |
( |
|
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 |
[ [( |
|
301 | [ [('a = %bc de \\', None), | |
302 |
( |
|
302 | ('fg', "a = get_ipython().run_line_magic('bc', 'de fg')"), | |
303 | ], |
|
303 | ], | |
304 |
[( |
|
304 | [('a = %bc de \\', None), | |
305 |
( |
|
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 |
[ [( |
|
311 | [ [('a = !bc de \\', None), | |
312 |
( |
|
312 | ('fg', "a = get_ipython().getoutput('bc de fg')"), | |
313 | ], |
|
313 | ], | |
314 |
[( |
|
314 | [('a = !bc de \\', None), | |
315 |
( |
|
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 |
( |
|
338 | ('%foo', '%foo'), | |
339 |
( |
|
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 |
( |
|
350 | ('%%foo', '%%foo'), | |
351 |
( |
|
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 |
[ [( |
|
356 | [ [("a = \\", None), | |
357 |
( |
|
357 | ("123", "a = 123"), | |
358 | ], |
|
358 | ], | |
359 |
[( |
|
359 | [("a = \\", None), # Test resetting when within a multi-line string | |
360 |
( |
|
360 | ("12 *\\", None), | |
361 |
(None, |
|
361 | (None, "a = 12 *"), | |
362 | ], |
|
362 | ], | |
363 |
[( |
|
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 |
[ [( |
|
371 | [ [("a = '''", None), | |
372 |
( |
|
372 | ("abc'''", "a = '''\nabc'''"), | |
373 | ], |
|
373 | ], | |
374 |
[( |
|
374 | [("a = '''", None), # Test resetting when within a multi-line string | |
375 |
( |
|
375 | ("def", None), | |
376 |
(None, |
|
376 | (None, "a = '''\ndef"), | |
377 | ], |
|
377 | ], | |
378 |
[( |
|
378 | [("a = [1,", None), | |
379 |
( |
|
379 | ("2]", "a = [1,\n2]"), | |
380 | ], |
|
380 | ], | |
381 |
[( |
|
381 | [("a = [1,", None), # Test resetting when within a multi-line string | |
382 |
( |
|
382 | ("2,", None), | |
383 |
(None, |
|
383 | (None, "a = [1,\n2,"), | |
384 | ], |
|
384 | ], | |
385 |
[( |
|
385 | [("a = '''", None), # Test line continuation within a multi-line string | |
386 |
( |
|
386 | ("abc\\", None), | |
387 |
( |
|
387 | ("def", None), | |
388 |
( |
|
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 = [( |
|
430 | line_example = [('%%bar 123', None), | |
431 |
( |
|
431 | ('hello', None), | |
432 |
( |
|
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 |
|
|
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 = [( |
|
469 | tests = [('1.2', "Decimal ('1.2')"), | |
471 |
( |
|
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 |
[ [( |
|
474 | [ [("a = 1.2; b = '''x", None), | |
476 |
( |
|
475 | ("y'''", "a =Decimal ('1.2');b ='''x\ny'''"), | |
477 | ], |
|
476 | ], | |
478 |
[( |
|
477 | [("a = [1.2,", None), | |
479 |
( |
|
478 | ("3]", "a =[Decimal ('1.2'),\n3 ]"), | |
480 | ], |
|
479 | ], | |
481 |
[( |
|
480 | [("a = '''foo", None), # Test resetting when within a multi-line string | |
482 |
( |
|
481 | ("bar", None), | |
483 |
(None, |
|
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