Show More
@@ -1,495 +1,487 b'' | |||
|
1 | 1 | import tokenize |
|
2 | 2 | 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 | |
|
10 | 9 | def transform_and_reset(transformer): |
|
11 | 10 | transformer = transformer() |
|
12 | 11 | def transform(inp): |
|
13 | 12 | try: |
|
14 | 13 | return transformer.push(inp) |
|
15 | 14 | finally: |
|
16 | 15 | transformer.reset() |
|
17 | 16 | |
|
18 | 17 | return transform |
|
19 | 18 | |
|
20 | 19 | # Transformer tests |
|
21 | 20 | def transform_checker(tests, transformer, **kwargs): |
|
22 | 21 | """Utility to loop over test inputs""" |
|
23 | 22 | transformer = transformer(**kwargs) |
|
24 | 23 | try: |
|
25 | 24 | for inp, tr in tests: |
|
26 | 25 | if inp is None: |
|
27 | 26 | out = transformer.reset() |
|
28 | 27 | else: |
|
29 | 28 | out = transformer.push(inp) |
|
30 | 29 | nt.assert_equal(out, tr) |
|
31 | 30 | finally: |
|
32 | 31 | transformer.reset() |
|
33 | 32 | |
|
34 | 33 | # Data for all the syntax tests in the form of lists of pairs of |
|
35 | 34 | # raw/transformed input. We store it here as a global dict so that we can use |
|
36 | 35 | # it both within single-function tests and also to validate the behavior of the |
|
37 | 36 | # larger objects |
|
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')"), |
|
45 | 43 | (u'd == !ls', u'd == !ls'), # Invalid syntax, but we leave == alone. |
|
46 | 44 | ('x=1', 'x=1'), # normal input is unmodified |
|
47 | 45 | (' ',' '), # blank lines are kept intact |
|
48 | 46 | # Tuple unpacking |
|
49 | 47 | (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")"), |
|
50 | 48 | (u"a,= !echo 'a'", u"a, = get_ipython().getoutput(\"echo 'a'\")"), |
|
51 | 49 | (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")"), |
|
52 | 50 | # Tuple unpacking with regular Python expressions, not our 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', '')"), |
|
63 | 60 | (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone. |
|
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'), |
|
71 | 68 | ('x=1', 'x=1'), # normal input is unmodified |
|
72 | 69 | (' ', ' '), # blank lines are kept intact |
|
73 | 70 | ], |
|
74 | 71 | |
|
75 | 72 | ipy_prompt = |
|
76 | 73 | [('In [1]: x=1', 'x=1'), |
|
77 | 74 | ('x=1', 'x=1'), # normal input is unmodified |
|
78 | 75 | (' ',' '), # blank lines are kept intact |
|
79 | 76 | ], |
|
80 | 77 | |
|
81 | 78 | # Tests for the escape transformer to leave normal code alone |
|
82 | 79 | escaped_noesc = |
|
83 | 80 | [ (' ', ' '), |
|
84 | 81 | ('x=1', 'x=1'), |
|
85 | 82 | ], |
|
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')"), |
|
102 | 97 | (u'?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"), |
|
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')"), |
|
113 | 107 | (u'%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"), |
|
114 | 108 | (u'%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"), |
|
115 | 109 | (u'%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"), |
|
116 | 110 | (u'Ο.foo?', "get_ipython().run_line_magic('pinfo', 'Ο.foo')"), |
|
117 | 111 | (u'f*?', "get_ipython().run_line_magic('psearch', 'f*')"), |
|
118 | 112 | (u'ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"), |
|
119 | 113 | (u'a = abc?', "get_ipython().set_next_input('a = abc');" |
|
120 | 114 | "get_ipython().run_line_magic('pinfo', 'abc')"), |
|
121 | 115 | (u'a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');" |
|
122 | 116 | "get_ipython().run_line_magic('pinfo2', 'abc.qe')"), |
|
123 | 117 | (u'a = *.items?', "get_ipython().set_next_input('a = *.items');" |
|
124 | 118 | "get_ipython().run_line_magic('psearch', '*.items')"), |
|
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 = |
|
142 | 135 | [ (',f', 'f("")'), |
|
143 | 136 | (',f x', 'f("x")'), |
|
144 | 137 | (' ,f y', ' f("y")'), |
|
145 | 138 | (',f a b', 'f("a", "b")'), |
|
146 | 139 | ], |
|
147 | 140 | |
|
148 | 141 | # Quoting with single argument |
|
149 | 142 | escaped_quote2 = |
|
150 | 143 | [ (';f', 'f("")'), |
|
151 | 144 | (';f x', 'f("x")'), |
|
152 | 145 | (' ;f y', ' f("y")'), |
|
153 | 146 | (';f a b', 'f("a b")'), |
|
154 | 147 | ], |
|
155 | 148 | |
|
156 | 149 | # Simply apply parens |
|
157 | 150 | escaped_paren = |
|
158 | 151 | [ ('/f', 'f()'), |
|
159 | 152 | ('/f x', 'f(x)'), |
|
160 | 153 | (' /f y', ' f(y)'), |
|
161 | 154 | ('/f a b', 'f(a, b)'), |
|
162 | 155 | ], |
|
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 |
|
176 | 168 | # each entry itself having pairs of raw/transformed input. The union (with |
|
177 | 169 | # '\n'.join() of the transformed inputs is what the splitter should produce |
|
178 | 170 | # when fed the raw lines one at a time via push. |
|
179 | 171 | syntax_ml = \ |
|
180 | 172 | dict(classic_prompt = |
|
181 | 173 | [ [('>>> for i in range(10):','for i in range(10):'), |
|
182 | 174 | ('... print i',' print i'), |
|
183 | 175 | ('... ', ''), |
|
184 | 176 | ], |
|
185 | 177 | [('>>> a="""','a="""'), |
|
186 | 178 | ('... 123"""','123"""'), |
|
187 | 179 | ], |
|
188 | 180 | [('a="""','a="""'), |
|
189 | 181 | ('... 123','123'), |
|
190 | 182 | ('... 456"""','456"""'), |
|
191 | 183 | ], |
|
192 | 184 | [('a="""','a="""'), |
|
193 | 185 | ('>>> 123','123'), |
|
194 | 186 | ('... 456"""','456"""'), |
|
195 | 187 | ], |
|
196 | 188 | [('a="""','a="""'), |
|
197 | 189 | ('123','123'), |
|
198 | 190 | ('... 456"""','... 456"""'), |
|
199 | 191 | ], |
|
200 | 192 | [('....__class__','....__class__'), |
|
201 | 193 | ], |
|
202 | 194 | [('a=5', 'a=5'), |
|
203 | 195 | ('...', ''), |
|
204 | 196 | ], |
|
205 | 197 | [('>>> def f(x):', 'def f(x):'), |
|
206 | 198 | ('...', ''), |
|
207 | 199 | ('... return x', ' return x'), |
|
208 | 200 | ], |
|
209 | 201 | [('board = """....', 'board = """....'), |
|
210 | 202 | ('....', '....'), |
|
211 | 203 | ('...."""', '...."""'), |
|
212 | 204 | ], |
|
213 | 205 | ], |
|
214 | 206 | |
|
215 | 207 | ipy_prompt = |
|
216 | 208 | [ [('In [24]: for i in range(10):','for i in range(10):'), |
|
217 | 209 | (' ....: print i',' print i'), |
|
218 | 210 | (' ....: ', ''), |
|
219 | 211 | ], |
|
220 | 212 | [('In [24]: for i in range(10):','for i in range(10):'), |
|
221 | 213 | # Qt console prompts expand with spaces, not dots |
|
222 | 214 | (' ...: print i',' print i'), |
|
223 | 215 | (' ...: ', ''), |
|
224 | 216 | ], |
|
225 | 217 | [('In [24]: for i in range(10):','for i in range(10):'), |
|
226 | 218 | # Sometimes whitespace preceding '...' has been removed |
|
227 | 219 | ('...: print i',' print i'), |
|
228 | 220 | ('...: ', ''), |
|
229 | 221 | ], |
|
230 | 222 | [('In [24]: for i in range(10):','for i in range(10):'), |
|
231 | 223 | # Space after last continuation prompt has been removed (issue #6674) |
|
232 | 224 | ('...: print i',' print i'), |
|
233 | 225 | ('...:', ''), |
|
234 | 226 | ], |
|
235 | 227 | [('In [2]: a="""','a="""'), |
|
236 | 228 | (' ...: 123"""','123"""'), |
|
237 | 229 | ], |
|
238 | 230 | [('a="""','a="""'), |
|
239 | 231 | (' ...: 123','123'), |
|
240 | 232 | (' ...: 456"""','456"""'), |
|
241 | 233 | ], |
|
242 | 234 | [('a="""','a="""'), |
|
243 | 235 | ('In [1]: 123','123'), |
|
244 | 236 | (' ...: 456"""','456"""'), |
|
245 | 237 | ], |
|
246 | 238 | [('a="""','a="""'), |
|
247 | 239 | ('123','123'), |
|
248 | 240 | (' ...: 456"""',' ...: 456"""'), |
|
249 | 241 | ], |
|
250 | 242 | ], |
|
251 | 243 | |
|
252 | 244 | multiline_datastructure_prompt = |
|
253 | 245 | [ [('>>> a = [1,','a = [1,'), |
|
254 | 246 | ('... 2]','2]'), |
|
255 | 247 | ], |
|
256 | 248 | ], |
|
257 | 249 | |
|
258 | 250 | multiline_datastructure = |
|
259 | 251 | [ [('b = ("%s"', None), |
|
260 | 252 | ('# comment', None), |
|
261 | 253 | ('%foo )', 'b = ("%s"\n# comment\n%foo )'), |
|
262 | 254 | ], |
|
263 | 255 | ], |
|
264 | 256 | |
|
265 | 257 | multiline_string = |
|
266 | 258 | [ [("'''foo?", None), |
|
267 | 259 | ("bar'''", "'''foo?\nbar'''"), |
|
268 | 260 | ], |
|
269 | 261 | ], |
|
270 | 262 | |
|
271 | 263 | leading_indent = |
|
272 | 264 | [ [(' print "hi"','print "hi"'), |
|
273 | 265 | ], |
|
274 | 266 | [(' for a in range(5):','for a in range(5):'), |
|
275 | 267 | (' a*2',' a*2'), |
|
276 | 268 | ], |
|
277 | 269 | [(' a="""','a="""'), |
|
278 | 270 | (' 123"""','123"""'), |
|
279 | 271 | ], |
|
280 | 272 | [('a="""','a="""'), |
|
281 | 273 | (' 123"""',' 123"""'), |
|
282 | 274 | ], |
|
283 | 275 | ], |
|
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'), |
|
295 | 287 | ], |
|
296 | 288 | ], |
|
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 | ) |
|
328 | 320 | |
|
329 | 321 | |
|
330 | 322 | def test_assign_system(): |
|
331 | 323 | tt.check_pairs(transform_and_reset(ipt.assign_from_system), syntax['assign_system']) |
|
332 | 324 | |
|
333 | 325 | def test_assign_magic(): |
|
334 | 326 | tt.check_pairs(transform_and_reset(ipt.assign_from_magic), syntax['assign_magic']) |
|
335 | 327 | |
|
336 | 328 | def test_classic_prompt(): |
|
337 | 329 | tt.check_pairs(transform_and_reset(ipt.classic_prompt), syntax['classic_prompt']) |
|
338 | 330 | for example in syntax_ml['classic_prompt']: |
|
339 | 331 | transform_checker(example, ipt.classic_prompt) |
|
340 | 332 | for example in syntax_ml['multiline_datastructure_prompt']: |
|
341 | 333 | transform_checker(example, ipt.classic_prompt) |
|
342 | 334 | |
|
343 | 335 | # Check that we don't transform the second line if the first is obviously |
|
344 | 336 | # IPython syntax |
|
345 | 337 | transform_checker([ |
|
346 | 338 | (u'%foo', '%foo'), |
|
347 | 339 | (u'>>> bar', '>>> bar'), |
|
348 | 340 | ], ipt.classic_prompt) |
|
349 | 341 | |
|
350 | 342 | |
|
351 | 343 | def test_ipy_prompt(): |
|
352 | 344 | tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt']) |
|
353 | 345 | for example in syntax_ml['ipy_prompt']: |
|
354 | 346 | transform_checker(example, ipt.ipy_prompt) |
|
355 | 347 | |
|
356 | 348 | # Check that we don't transform the second line if we're inside a cell magic |
|
357 | 349 | transform_checker([ |
|
358 | 350 | (u'%%foo', '%%foo'), |
|
359 | 351 | (u'In [1]: bar', 'In [1]: bar'), |
|
360 | 352 | ], ipt.ipy_prompt) |
|
361 | 353 | |
|
362 | 354 | def test_assemble_logical_lines(): |
|
363 | 355 | tests = \ |
|
364 | 356 | [ [(u"a = \\", None), |
|
365 | 357 | (u"123", u"a = 123"), |
|
366 | 358 | ], |
|
367 | 359 | [(u"a = \\", None), # Test resetting when within a multi-line string |
|
368 | 360 | (u"12 *\\", None), |
|
369 | 361 | (None, u"a = 12 *"), |
|
370 | 362 | ], |
|
371 | 363 | [(u"# foo\\", u"# foo\\"), # Comments can't be continued like this |
|
372 | 364 | ], |
|
373 | 365 | ] |
|
374 | 366 | for example in tests: |
|
375 | 367 | transform_checker(example, ipt.assemble_logical_lines) |
|
376 | 368 | |
|
377 | 369 | def test_assemble_python_lines(): |
|
378 | 370 | tests = \ |
|
379 | 371 | [ [(u"a = '''", None), |
|
380 | 372 | (u"abc'''", u"a = '''\nabc'''"), |
|
381 | 373 | ], |
|
382 | 374 | [(u"a = '''", None), # Test resetting when within a multi-line string |
|
383 | 375 | (u"def", None), |
|
384 | 376 | (None, u"a = '''\ndef"), |
|
385 | 377 | ], |
|
386 | 378 | [(u"a = [1,", None), |
|
387 | 379 | (u"2]", u"a = [1,\n2]"), |
|
388 | 380 | ], |
|
389 | 381 | [(u"a = [1,", None), # Test resetting when within a multi-line string |
|
390 | 382 | (u"2,", None), |
|
391 | 383 | (None, u"a = [1,\n2,"), |
|
392 | 384 | ], |
|
393 | 385 | [(u"a = '''", None), # Test line continuation within a multi-line string |
|
394 | 386 | (u"abc\\", None), |
|
395 | 387 | (u"def", None), |
|
396 | 388 | (u"'''", u"a = '''\nabc\\\ndef\n'''"), |
|
397 | 389 | ], |
|
398 | 390 | ] + syntax_ml['multiline_datastructure'] |
|
399 | 391 | for example in tests: |
|
400 | 392 | transform_checker(example, ipt.assemble_python_lines) |
|
401 | 393 | |
|
402 | 394 | |
|
403 | 395 | def test_help_end(): |
|
404 | 396 | tt.check_pairs(transform_and_reset(ipt.help_end), syntax['end_help']) |
|
405 | 397 | |
|
406 | 398 | def test_escaped_noesc(): |
|
407 | 399 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_noesc']) |
|
408 | 400 | |
|
409 | 401 | |
|
410 | 402 | def test_escaped_shell(): |
|
411 | 403 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_shell']) |
|
412 | 404 | |
|
413 | 405 | |
|
414 | 406 | def test_escaped_help(): |
|
415 | 407 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_help']) |
|
416 | 408 | |
|
417 | 409 | |
|
418 | 410 | def test_escaped_magic(): |
|
419 | 411 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_magic']) |
|
420 | 412 | |
|
421 | 413 | |
|
422 | 414 | def test_escaped_quote(): |
|
423 | 415 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote']) |
|
424 | 416 | |
|
425 | 417 | |
|
426 | 418 | def test_escaped_quote2(): |
|
427 | 419 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_quote2']) |
|
428 | 420 | |
|
429 | 421 | |
|
430 | 422 | def test_escaped_paren(): |
|
431 | 423 | tt.check_pairs(transform_and_reset(ipt.escaped_commands), syntax['escaped_paren']) |
|
432 | 424 | |
|
433 | 425 | |
|
434 | 426 | def test_cellmagic(): |
|
435 | 427 | for example in syntax_ml['cellmagic']: |
|
436 | 428 | transform_checker(example, ipt.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 | |
|
444 | 436 | def test_has_comment(): |
|
445 | 437 | tests = [('text', False), |
|
446 | 438 | ('text #comment', True), |
|
447 | 439 | ('text #comment\n', True), |
|
448 | 440 | ('#comment', True), |
|
449 | 441 | ('#comment\n', True), |
|
450 | 442 | ('a = "#string"', False), |
|
451 | 443 | ('a = "#string" # comment', True), |
|
452 | 444 | ('a #comment not "string"', True), |
|
453 | 445 | ] |
|
454 | 446 | tt.check_pairs(ipt.has_comment, tests) |
|
455 | 447 | |
|
456 | 448 | @ipt.TokenInputTransformer.wrap |
|
457 | 449 | def decistmt(tokens): |
|
458 | 450 | """Substitute Decimals for floats in a string of statements. |
|
459 | 451 | |
|
460 | 452 | Based on an example from the tokenize module docs. |
|
461 | 453 | """ |
|
462 | 454 | result = [] |
|
463 | 455 | for toknum, tokval, _, _, _ in tokens: |
|
464 | 456 | if toknum == tokenize.NUMBER and '.' in tokval: # replace NUMBER tokens |
|
465 | 457 | for newtok in [ |
|
466 | 458 | (tokenize.NAME, 'Decimal'), |
|
467 | 459 | (tokenize.OP, '('), |
|
468 | 460 | (tokenize.STRING, repr(tokval)), |
|
469 | 461 | (tokenize.OP, ')') |
|
470 | 462 | ]: |
|
471 | 463 | yield newtok |
|
472 | 464 | else: |
|
473 | 465 | yield (toknum, tokval) |
|
474 | 466 | |
|
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), |
|
491 | 483 | (None, u"a = '''foo\nbar"), |
|
492 | 484 | ], |
|
493 | 485 | ] |
|
494 | 486 | for example in ml_tests: |
|
495 | 487 | transform_checker(example, decistmt) |
@@ -1,191 +1,159 b'' | |||
|
1 | 1 | # coding: utf-8 |
|
2 | 2 | """Compatibility tricks for Python 3. Mainly to do with unicode. |
|
3 | 3 | |
|
4 | 4 | This file is deprecated and will be removed in a future version. |
|
5 | 5 | """ |
|
6 | 6 | import functools |
|
7 | 7 | import os |
|
8 | 8 | import sys |
|
9 | 9 | import re |
|
10 | 10 | import shutil |
|
11 | 11 | import types |
|
12 | 12 | import platform |
|
13 | 13 | |
|
14 | 14 | from .encoding import DEFAULT_ENCODING |
|
15 | 15 | |
|
16 | 16 | |
|
17 | 17 | def decode(s, encoding=None): |
|
18 | 18 | encoding = encoding or DEFAULT_ENCODING |
|
19 | 19 | return s.decode(encoding, "replace") |
|
20 | 20 | |
|
21 | 21 | def encode(u, encoding=None): |
|
22 | 22 | encoding = encoding or DEFAULT_ENCODING |
|
23 | 23 | return u.encode(encoding, "replace") |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | def cast_unicode(s, encoding=None): |
|
27 | 27 | if isinstance(s, bytes): |
|
28 | 28 | return decode(s, encoding) |
|
29 | 29 | return s |
|
30 | 30 | |
|
31 | 31 | def cast_bytes(s, encoding=None): |
|
32 | 32 | if not isinstance(s, bytes): |
|
33 | 33 | return encode(s, encoding) |
|
34 | 34 | return s |
|
35 | 35 | |
|
36 | 36 | def buffer_to_bytes(buf): |
|
37 | 37 | """Cast a buffer object to bytes""" |
|
38 | 38 | if not isinstance(buf, bytes): |
|
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. |
|
65 | 45 | """ |
|
66 | 46 | try: |
|
67 | 47 | return str(e) |
|
68 | 48 | except UnicodeError: |
|
69 | 49 | pass |
|
70 | 50 | |
|
71 | 51 | try: |
|
72 | 52 | return repr(e) |
|
73 | 53 | except UnicodeError: |
|
74 | 54 | pass |
|
75 | 55 | |
|
76 | 56 | return u'Unrecoverably corrupt evalue' |
|
77 | 57 | |
|
78 | 58 | # shutil.which from Python 3.4 |
|
79 | 59 | def _shutil_which(cmd, mode=os.F_OK | os.X_OK, path=None): |
|
80 | 60 | """Given a command, mode, and a PATH string, return the path which |
|
81 | 61 | conforms to the given mode on the PATH, or None if there is no such |
|
82 | 62 | file. |
|
83 | 63 | |
|
84 | 64 | `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result |
|
85 | 65 | of os.environ.get("PATH"), or can be overridden with a custom search |
|
86 | 66 | path. |
|
87 | 67 | |
|
88 | 68 | This is a backport of shutil.which from Python 3.4 |
|
89 | 69 | """ |
|
90 | 70 | # Check that a given file can be accessed with the correct mode. |
|
91 | 71 | # Additionally check that `file` is not a directory, as on Windows |
|
92 | 72 | # directories pass the os.access check. |
|
93 | 73 | def _access_check(fn, mode): |
|
94 | 74 | return (os.path.exists(fn) and os.access(fn, mode) |
|
95 | 75 | and not os.path.isdir(fn)) |
|
96 | 76 | |
|
97 | 77 | # If we're given a path with a directory part, look it up directly rather |
|
98 | 78 | # than referring to PATH directories. This includes checking relative to the |
|
99 | 79 | # current directory, e.g. ./script |
|
100 | 80 | if os.path.dirname(cmd): |
|
101 | 81 | if _access_check(cmd, mode): |
|
102 | 82 | return cmd |
|
103 | 83 | return None |
|
104 | 84 | |
|
105 | 85 | if path is None: |
|
106 | 86 | path = os.environ.get("PATH", os.defpath) |
|
107 | 87 | if not path: |
|
108 | 88 | return None |
|
109 | 89 | path = path.split(os.pathsep) |
|
110 | 90 | |
|
111 | 91 | if sys.platform == "win32": |
|
112 | 92 | # The current directory takes precedence on Windows. |
|
113 | 93 | if not os.curdir in path: |
|
114 | 94 | path.insert(0, os.curdir) |
|
115 | 95 | |
|
116 | 96 | # PATHEXT is necessary to check on Windows. |
|
117 | 97 | pathext = os.environ.get("PATHEXT", "").split(os.pathsep) |
|
118 | 98 | # See if the given file matches any of the expected path extensions. |
|
119 | 99 | # This will allow us to short circuit when given "python.exe". |
|
120 | 100 | # If it does match, only test that one, otherwise we have to try |
|
121 | 101 | # others. |
|
122 | 102 | if any(cmd.lower().endswith(ext.lower()) for ext in pathext): |
|
123 | 103 | files = [cmd] |
|
124 | 104 | else: |
|
125 | 105 | files = [cmd + ext for ext in pathext] |
|
126 | 106 | else: |
|
127 | 107 | # On other platforms you don't have things like PATHEXT to tell you |
|
128 | 108 | # what file suffixes are executable, so just pass on cmd as-is. |
|
129 | 109 | files = [cmd] |
|
130 | 110 | |
|
131 | 111 | seen = set() |
|
132 | 112 | for dir in path: |
|
133 | 113 | normdir = os.path.normcase(dir) |
|
134 | 114 | if not normdir in seen: |
|
135 | 115 | seen.add(normdir) |
|
136 | 116 | for thefile in files: |
|
137 | 117 | name = os.path.join(dir, thefile) |
|
138 | 118 | if _access_check(name, mode): |
|
139 | 119 | return name |
|
140 | 120 | return None |
|
141 | 121 | |
|
142 | 122 | PY3 = True |
|
143 | 123 | |
|
144 | 124 | # keep reference to builtin_mod because the kernel overrides that value |
|
145 | 125 | # to forward requests to a frontend. |
|
146 | 126 | def input(prompt=''): |
|
147 | 127 | return builtin_mod.input(prompt) |
|
148 | 128 | |
|
149 | 129 | builtin_mod_name = "builtins" |
|
150 | 130 | import builtins as builtin_mod |
|
151 | 131 | |
|
152 | 132 | |
|
153 | 133 | which = shutil.which |
|
154 | 134 | |
|
155 | 135 | def isidentifier(s, dotted=False): |
|
156 | 136 | if dotted: |
|
157 | 137 | return all(isidentifier(a) for a in s.split(".")) |
|
158 | 138 | return s.isidentifier() |
|
159 | 139 | |
|
160 | 140 | getcwd = os.getcwd |
|
161 | 141 | |
|
162 | 142 | MethodType = types.MethodType |
|
163 | 143 | |
|
164 | 144 | def execfile(fname, glob, loc=None, compiler=None): |
|
165 | 145 | loc = loc if (loc is not None) else glob |
|
166 | 146 | with open(fname, 'rb') as f: |
|
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 | |
|
185 | 153 | # Cython still rely on that as a Dec 28 2019 |
|
186 | 154 | # See https://github.com/cython/cython/pull/3291 and |
|
187 | 155 | # https://github.com/ipython/ipython/issues/12068 |
|
188 | 156 | def no_code(x, encoding=None): |
|
189 | 157 | return x |
|
190 | 158 | unicode_to_str = cast_bytes_py2 = no_code |
|
191 | 159 |
General Comments 0
You need to be logged in to leave comments.
Login now