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