Show More
@@ -3,7 +3,6 b' import nose.tools as nt' | |||
|
3 | 3 | |
|
4 | 4 | from IPython.testing import tools as tt |
|
5 | 5 | from IPython.utils import py3compat |
|
6 | u_fmt = py3compat.u_format | |
|
7 | 6 | |
|
8 | 7 | from IPython.core import inputtransformer as ipt |
|
9 | 8 | |
@@ -38,7 +37,6 b' def transform_checker(tests, transformer, **kwargs):' | |||
|
38 | 37 | |
|
39 | 38 | syntax = \ |
|
40 | 39 | dict(assign_system = |
|
41 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
42 | 40 | [(u'a =! ls', "a = get_ipython().getoutput('ls')"), |
|
43 | 41 | (u'b = !ls', "b = get_ipython().getoutput('ls')"), |
|
44 | 42 | (u'c= !ls', "c = get_ipython().getoutput('ls')"), |
@@ -53,10 +51,9 b' syntax = \\' | |||
|
53 | 51 | (u"a, b = range(2)", u"a, b = range(2)"), |
|
54 | 52 | (u"a, = range(1)", u"a, = range(1)"), |
|
55 | 53 | (u"a, *bc = range(3)", u"a, *bc = range(3)"), |
|
56 |
] |
|
|
54 | ], | |
|
57 | 55 | |
|
58 | 56 | assign_magic = |
|
59 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
60 | 57 | [(u'a =% who', "a = get_ipython().run_line_magic('who', '')"), |
|
61 | 58 | (u'b = %who', "b = get_ipython().run_line_magic('who', '')"), |
|
62 | 59 | (u'c= %ls', "c = get_ipython().run_line_magic('ls', '')"), |
@@ -64,7 +61,7 b' syntax = \\' | |||
|
64 | 61 | ('x=1', 'x=1'), # normal input is unmodified |
|
65 | 62 | (' ',' '), # blank lines are kept intact |
|
66 | 63 | (u"a, b = %foo", u"a, b = get_ipython().run_line_magic('foo', '')"), |
|
67 |
] |
|
|
64 | ], | |
|
68 | 65 | |
|
69 | 66 | classic_prompt = |
|
70 | 67 | [('>>> x=1', 'x=1'), |
@@ -86,16 +83,14 b' syntax = \\' | |||
|
86 | 83 | |
|
87 | 84 | # System calls |
|
88 | 85 | escaped_shell = |
|
89 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
90 | 86 | [ (u'!ls', "get_ipython().system('ls')"), |
|
91 | 87 | # Double-escape shell, this means to capture the output of the |
|
92 | 88 | # subprocess and return it |
|
93 | 89 | (u'!!ls', "get_ipython().getoutput('ls')"), |
|
94 |
] |
|
|
90 | ], | |
|
95 | 91 | |
|
96 | 92 | # Help/object info |
|
97 | 93 | escaped_help = |
|
98 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
99 | 94 | [ (u'?', 'get_ipython().show_usage()'), |
|
100 | 95 | (u'?x1', "get_ipython().run_line_magic('pinfo', 'x1')"), |
|
101 | 96 | (u'??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"), |
@@ -103,10 +98,9 b' syntax = \\' | |||
|
103 | 98 | (u'?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"), |
|
104 | 99 | (u'?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"), |
|
105 | 100 | (u'?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"), |
|
106 |
] |
|
|
101 | ], | |
|
107 | 102 | |
|
108 | 103 | end_help = |
|
109 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
110 | 104 | [ (u'x3?', "get_ipython().run_line_magic('pinfo', 'x3')"), |
|
111 | 105 | (u'x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"), |
|
112 | 106 | (u'%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"), |
@@ -125,17 +119,16 b' syntax = \\' | |||
|
125 | 119 | (u'plot(a?', "get_ipython().set_next_input('plot(a');" |
|
126 | 120 | "get_ipython().run_line_magic('pinfo', 'a')"), |
|
127 | 121 | (u'a*2 #comment?', 'a*2 #comment?'), |
|
128 |
] |
|
|
122 | ], | |
|
129 | 123 | |
|
130 | 124 | # Explicit magic calls |
|
131 | 125 | escaped_magic = |
|
132 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
133 | 126 | [ (u'%cd', "get_ipython().run_line_magic('cd', '')"), |
|
134 | 127 | (u'%cd /home', "get_ipython().run_line_magic('cd', '/home')"), |
|
135 | 128 | # Backslashes need to be escaped. |
|
136 | 129 | (u'%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"), |
|
137 | 130 | (u' %magic', " get_ipython().run_line_magic('magic', '')"), |
|
138 |
] |
|
|
131 | ], | |
|
139 | 132 | |
|
140 | 133 | # Quoting with separate arguments |
|
141 | 134 | escaped_quote = |
@@ -163,13 +156,12 b' syntax = \\' | |||
|
163 | 156 | |
|
164 | 157 | # Check that we transform prompts before other transforms |
|
165 | 158 | mixed = |
|
166 | [(i,py3compat.u_format(o)) for i,o in \ | |
|
167 | 159 | [ (u'In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"), |
|
168 | 160 | (u'>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"), |
|
169 | 161 | (u'In [2]: !ls', "get_ipython().system('ls')"), |
|
170 | 162 | (u'In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"), |
|
171 | 163 | (u'In [4]: b = %who', "b = get_ipython().run_line_magic('who', '')"), |
|
172 |
] |
|
|
164 | ], | |
|
173 | 165 | ) |
|
174 | 166 | |
|
175 | 167 | # multiline syntax examples. Each of these should be a list of lists, with |
@@ -284,11 +276,11 b' syntax_ml = \\' | |||
|
284 | 276 | |
|
285 | 277 | cellmagic = |
|
286 | 278 | [ [(u'%%foo a', None), |
|
287 |
(None, |
|
|
279 | (None, "get_ipython().run_cell_magic('foo', 'a', '')"), | |
|
288 | 280 | ], |
|
289 | 281 | [(u'%%bar 123', None), |
|
290 | 282 | (u'hello', None), |
|
291 |
(None , |
|
|
283 | (None , "get_ipython().run_cell_magic('bar', '123', 'hello')"), | |
|
292 | 284 | ], |
|
293 | 285 | [(u'a=5', 'a=5'), |
|
294 | 286 | (u'%%cellmagic', '%%cellmagic'), |
@@ -297,31 +289,31 b' syntax_ml = \\' | |||
|
297 | 289 | |
|
298 | 290 | escaped = |
|
299 | 291 | [ [('%abc def \\', None), |
|
300 |
('ghi', |
|
|
292 | ('ghi', "get_ipython().run_line_magic('abc', 'def ghi')"), | |
|
301 | 293 | ], |
|
302 | 294 | [('%abc def \\', None), |
|
303 | 295 | ('ghi\\', None), |
|
304 |
(None, |
|
|
296 | (None, "get_ipython().run_line_magic('abc', 'def ghi')"), | |
|
305 | 297 | ], |
|
306 | 298 | ], |
|
307 | 299 | |
|
308 | 300 | assign_magic = |
|
309 | 301 | [ [(u'a = %bc de \\', None), |
|
310 |
(u'fg', |
|
|
302 | (u'fg', "a = get_ipython().run_line_magic('bc', 'de fg')"), | |
|
311 | 303 | ], |
|
312 | 304 | [(u'a = %bc de \\', None), |
|
313 | 305 | (u'fg\\', None), |
|
314 |
(None, |
|
|
306 | (None, "a = get_ipython().run_line_magic('bc', 'de fg')"), | |
|
315 | 307 | ], |
|
316 | 308 | ], |
|
317 | 309 | |
|
318 | 310 | assign_system = |
|
319 | 311 | [ [(u'a = !bc de \\', None), |
|
320 |
(u'fg', |
|
|
312 | (u'fg', "a = get_ipython().getoutput('bc de fg')"), | |
|
321 | 313 | ], |
|
322 | 314 | [(u'a = !bc de \\', None), |
|
323 | 315 | (u'fg\\', None), |
|
324 |
(None, |
|
|
316 | (None, "a = get_ipython().getoutput('bc de fg')"), | |
|
325 | 317 | ], |
|
326 | 318 | ], |
|
327 | 319 | ) |
@@ -437,7 +429,7 b' def test_cellmagic():' | |||
|
437 | 429 | |
|
438 | 430 | line_example = [(u'%%bar 123', None), |
|
439 | 431 | (u'hello', None), |
|
440 |
(u'' , |
|
|
432 | (u'' , "get_ipython().run_cell_magic('bar', '123', 'hello')"), | |
|
441 | 433 | ] |
|
442 | 434 | transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True) |
|
443 | 435 | |
@@ -475,16 +467,16 b' def decistmt(tokens):' | |||
|
475 | 467 | |
|
476 | 468 | |
|
477 | 469 | def test_token_input_transformer(): |
|
478 |
tests = [(u'1.2', |
|
|
470 | tests = [(u'1.2', u"Decimal ('1.2')"), | |
|
479 | 471 | (u'"1.2"', u'"1.2"'), |
|
480 | 472 | ] |
|
481 | 473 | tt.check_pairs(transform_and_reset(decistmt), tests) |
|
482 | 474 | ml_tests = \ |
|
483 | 475 | [ [(u"a = 1.2; b = '''x", None), |
|
484 |
(u"y'''", |
|
|
476 | (u"y'''", u"a =Decimal ('1.2');b ='''x\ny'''"), | |
|
485 | 477 | ], |
|
486 | 478 | [(u"a = [1.2,", None), |
|
487 |
(u"3]", |
|
|
479 | (u"3]", u"a =[Decimal ('1.2'),\n3 ]"), | |
|
488 | 480 | ], |
|
489 | 481 | [(u"a = '''foo", None), # Test resetting when within a multi-line string |
|
490 | 482 | (u"bar", None), |
@@ -39,26 +39,6 b' def buffer_to_bytes(buf):' | |||
|
39 | 39 | buf = bytes(buf) |
|
40 | 40 | return buf |
|
41 | 41 | |
|
42 | def _modify_str_or_docstring(str_change_func): | |
|
43 | @functools.wraps(str_change_func) | |
|
44 | def wrapper(func_or_str): | |
|
45 | if isinstance(func_or_str, (str,)): | |
|
46 | func = None | |
|
47 | doc = func_or_str | |
|
48 | else: | |
|
49 | func = func_or_str | |
|
50 | doc = func.__doc__ | |
|
51 | ||
|
52 | # PYTHONOPTIMIZE=2 strips docstrings, so they can disappear unexpectedly | |
|
53 | if doc is not None: | |
|
54 | doc = str_change_func(doc) | |
|
55 | ||
|
56 | if func: | |
|
57 | func.__doc__ = doc | |
|
58 | return func | |
|
59 | return doc | |
|
60 | return wrapper | |
|
61 | ||
|
62 | 42 | def safe_unicode(e): |
|
63 | 43 | """unicode(e) with various fallbacks. Used for exceptions, which may not be |
|
64 | 44 | safe to call unicode() on. |
@@ -167,18 +147,6 b' def execfile(fname, glob, loc=None, compiler=None):' | |||
|
167 | 147 | compiler = compiler or compile |
|
168 | 148 | exec(compiler(f.read(), fname, 'exec'), glob, loc) |
|
169 | 149 | |
|
170 | # Refactor print statements in doctests. | |
|
171 | _print_statement_re = re.compile(r"\bprint (?P<expr>.*)$", re.MULTILINE) | |
|
172 | ||
|
173 | # Abstract u'abc' syntax: | |
|
174 | @_modify_str_or_docstring | |
|
175 | def u_format(s): | |
|
176 | """"{u}'abc'" --> "'abc'" (Python 3) | |
|
177 | ||
|
178 | Accepts a string or a function, so it can be used as a decorator.""" | |
|
179 | return s.format(u='') | |
|
180 | ||
|
181 | ||
|
182 | 150 | PY2 = not PY3 |
|
183 | 151 | PYPY = platform.python_implementation() == "PyPy" |
|
184 | 152 |
General Comments 0
You need to be logged in to leave comments.
Login now