1 | import sys, itertools, unittest
|
---|
2 | from test import test_support
|
---|
3 | import ast
|
---|
4 |
|
---|
5 | def to_tuple(t):
|
---|
6 | if t is None or isinstance(t, (basestring, int, long, complex)):
|
---|
7 | return t
|
---|
8 | elif isinstance(t, list):
|
---|
9 | return [to_tuple(e) for e in t]
|
---|
10 | result = [t.__class__.__name__]
|
---|
11 | if hasattr(t, 'lineno') and hasattr(t, 'col_offset'):
|
---|
12 | result.append((t.lineno, t.col_offset))
|
---|
13 | if t._fields is None:
|
---|
14 | return tuple(result)
|
---|
15 | for f in t._fields:
|
---|
16 | result.append(to_tuple(getattr(t, f)))
|
---|
17 | return tuple(result)
|
---|
18 |
|
---|
19 |
|
---|
20 | # These tests are compiled through "exec"
|
---|
21 | # There should be at least one test per statement
|
---|
22 | exec_tests = [
|
---|
23 | # None
|
---|
24 | "None",
|
---|
25 | # FunctionDef
|
---|
26 | "def f(): pass",
|
---|
27 | # FunctionDef with arg
|
---|
28 | "def f(a): pass",
|
---|
29 | # FunctionDef with arg and default value
|
---|
30 | "def f(a=0): pass",
|
---|
31 | # FunctionDef with varargs
|
---|
32 | "def f(*args): pass",
|
---|
33 | # FunctionDef with kwargs
|
---|
34 | "def f(**kwargs): pass",
|
---|
35 | # FunctionDef with all kind of args
|
---|
36 | "def f(a, b=1, c=None, d=[], e={}, *args, **kwargs): pass",
|
---|
37 | # ClassDef
|
---|
38 | "class C:pass",
|
---|
39 | # ClassDef, new style class
|
---|
40 | "class C(object): pass",
|
---|
41 | # Return
|
---|
42 | "def f():return 1",
|
---|
43 | # Delete
|
---|
44 | "del v",
|
---|
45 | # Assign
|
---|
46 | "v = 1",
|
---|
47 | # AugAssign
|
---|
48 | "v += 1",
|
---|
49 | # Print
|
---|
50 | "print >>f, 1, ",
|
---|
51 | # For
|
---|
52 | "for v in v:pass",
|
---|
53 | # While
|
---|
54 | "while v:pass",
|
---|
55 | # If
|
---|
56 | "if v:pass",
|
---|
57 | # Raise
|
---|
58 | "raise Exception, 'string'",
|
---|
59 | # TryExcept
|
---|
60 | "try:\n pass\nexcept Exception:\n pass",
|
---|
61 | # TryFinally
|
---|
62 | "try:\n pass\nfinally:\n pass",
|
---|
63 | # Assert
|
---|
64 | "assert v",
|
---|
65 | # Import
|
---|
66 | "import sys",
|
---|
67 | # ImportFrom
|
---|
68 | "from sys import v",
|
---|
69 | # Exec
|
---|
70 | "exec 'v'",
|
---|
71 | # Global
|
---|
72 | "global v",
|
---|
73 | # Expr
|
---|
74 | "1",
|
---|
75 | # Pass,
|
---|
76 | "pass",
|
---|
77 | # Break
|
---|
78 | "break",
|
---|
79 | # Continue
|
---|
80 | "continue",
|
---|
81 | # for statements with naked tuples (see http://bugs.python.org/issue6704)
|
---|
82 | "for a,b in c: pass",
|
---|
83 | "[(a,b) for a,b in c]",
|
---|
84 | "((a,b) for a,b in c)",
|
---|
85 | "((a,b) for (a,b) in c)",
|
---|
86 | # Multiline generator expression (test for .lineno & .col_offset)
|
---|
87 | """(
|
---|
88 | (
|
---|
89 | Aa
|
---|
90 | ,
|
---|
91 | Bb
|
---|
92 | )
|
---|
93 | for
|
---|
94 | Aa
|
---|
95 | ,
|
---|
96 | Bb in Cc
|
---|
97 | )""",
|
---|
98 | # dictcomp
|
---|
99 | "{a : b for w in x for m in p if g}",
|
---|
100 | # dictcomp with naked tuple
|
---|
101 | "{a : b for v,w in x}",
|
---|
102 | # setcomp
|
---|
103 | "{r for l in x if g}",
|
---|
104 | # setcomp with naked tuple
|
---|
105 | "{r for l,m in x}",
|
---|
106 | ]
|
---|
107 |
|
---|
108 | # These are compiled through "single"
|
---|
109 | # because of overlap with "eval", it just tests what
|
---|
110 | # can't be tested with "eval"
|
---|
111 | single_tests = [
|
---|
112 | "1+2"
|
---|
113 | ]
|
---|
114 |
|
---|
115 | # These are compiled through "eval"
|
---|
116 | # It should test all expressions
|
---|
117 | eval_tests = [
|
---|
118 | # None
|
---|
119 | "None",
|
---|
120 | # BoolOp
|
---|
121 | "a and b",
|
---|
122 | # BinOp
|
---|
123 | "a + b",
|
---|
124 | # UnaryOp
|
---|
125 | "not v",
|
---|
126 | # Lambda
|
---|
127 | "lambda:None",
|
---|
128 | # Dict
|
---|
129 | "{ 1:2 }",
|
---|
130 | # Empty dict
|
---|
131 | "{}",
|
---|
132 | # Set
|
---|
133 | "{None,}",
|
---|
134 | # Multiline dict (test for .lineno & .col_offset)
|
---|
135 | """{
|
---|
136 | 1
|
---|
137 | :
|
---|
138 | 2
|
---|
139 | }""",
|
---|
140 | # ListComp
|
---|
141 | "[a for b in c if d]",
|
---|
142 | # GeneratorExp
|
---|
143 | "(a for b in c if d)",
|
---|
144 | # Yield - yield expressions can't work outside a function
|
---|
145 | #
|
---|
146 | # Compare
|
---|
147 | "1 < 2 < 3",
|
---|
148 | # Call
|
---|
149 | "f(1,2,c=3,*d,**e)",
|
---|
150 | # Repr
|
---|
151 | "`v`",
|
---|
152 | # Num
|
---|
153 | "10L",
|
---|
154 | # Str
|
---|
155 | "'string'",
|
---|
156 | # Attribute
|
---|
157 | "a.b",
|
---|
158 | # Subscript
|
---|
159 | "a[b:c]",
|
---|
160 | # Name
|
---|
161 | "v",
|
---|
162 | # List
|
---|
163 | "[1,2,3]",
|
---|
164 | # Empty list
|
---|
165 | "[]",
|
---|
166 | # Tuple
|
---|
167 | "1,2,3",
|
---|
168 | # Tuple
|
---|
169 | "(1,2,3)",
|
---|
170 | # Empty tuple
|
---|
171 | "()",
|
---|
172 | # Combination
|
---|
173 | "a.b.c.d(a.b[1:2])",
|
---|
174 |
|
---|
175 | ]
|
---|
176 |
|
---|
177 | # TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
|
---|
178 | # excepthandler, arguments, keywords, alias
|
---|
179 |
|
---|
180 | class AST_Tests(unittest.TestCase):
|
---|
181 |
|
---|
182 | def _assertTrueorder(self, ast_node, parent_pos):
|
---|
183 | if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
|
---|
184 | return
|
---|
185 | if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
|
---|
186 | node_pos = (ast_node.lineno, ast_node.col_offset)
|
---|
187 | self.assertTrue(node_pos >= parent_pos)
|
---|
188 | parent_pos = (ast_node.lineno, ast_node.col_offset)
|
---|
189 | for name in ast_node._fields:
|
---|
190 | value = getattr(ast_node, name)
|
---|
191 | if isinstance(value, list):
|
---|
192 | for child in value:
|
---|
193 | self._assertTrueorder(child, parent_pos)
|
---|
194 | elif value is not None:
|
---|
195 | self._assertTrueorder(value, parent_pos)
|
---|
196 |
|
---|
197 | def test_AST_objects(self):
|
---|
198 | x = ast.AST()
|
---|
199 | self.assertEqual(x._fields, ())
|
---|
200 |
|
---|
201 | with self.assertRaises(AttributeError):
|
---|
202 | x.vararg
|
---|
203 |
|
---|
204 | with self.assertRaises(AttributeError):
|
---|
205 | x.foobar = 21
|
---|
206 |
|
---|
207 | with self.assertRaises(AttributeError):
|
---|
208 | ast.AST(lineno=2)
|
---|
209 |
|
---|
210 | with self.assertRaises(TypeError):
|
---|
211 | # "_ast.AST constructor takes 0 positional arguments"
|
---|
212 | ast.AST(2)
|
---|
213 |
|
---|
214 | def test_snippets(self):
|
---|
215 | for input, output, kind in ((exec_tests, exec_results, "exec"),
|
---|
216 | (single_tests, single_results, "single"),
|
---|
217 | (eval_tests, eval_results, "eval")):
|
---|
218 | for i, o in itertools.izip(input, output):
|
---|
219 | ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
|
---|
220 | self.assertEqual(to_tuple(ast_tree), o)
|
---|
221 | self._assertTrueorder(ast_tree, (0, 0))
|
---|
222 |
|
---|
223 | def test_slice(self):
|
---|
224 | slc = ast.parse("x[::]").body[0].value.slice
|
---|
225 | self.assertIsNone(slc.upper)
|
---|
226 | self.assertIsNone(slc.lower)
|
---|
227 | self.assertIsInstance(slc.step, ast.Name)
|
---|
228 | self.assertEqual(slc.step.id, "None")
|
---|
229 |
|
---|
230 | def test_from_import(self):
|
---|
231 | im = ast.parse("from . import y").body[0]
|
---|
232 | self.assertIsNone(im.module)
|
---|
233 |
|
---|
234 | def test_non_interned_future_from_ast(self):
|
---|
235 | mod = ast.parse("from __future__ import division")
|
---|
236 | self.assertIsInstance(mod.body[0], ast.ImportFrom)
|
---|
237 | mod.body[0].module = " __future__ ".strip()
|
---|
238 | compile(mod, "<test>", "exec")
|
---|
239 |
|
---|
240 | def test_base_classes(self):
|
---|
241 | self.assertTrue(issubclass(ast.For, ast.stmt))
|
---|
242 | self.assertTrue(issubclass(ast.Name, ast.expr))
|
---|
243 | self.assertTrue(issubclass(ast.stmt, ast.AST))
|
---|
244 | self.assertTrue(issubclass(ast.expr, ast.AST))
|
---|
245 | self.assertTrue(issubclass(ast.comprehension, ast.AST))
|
---|
246 | self.assertTrue(issubclass(ast.Gt, ast.AST))
|
---|
247 |
|
---|
248 | def test_field_attr_existence(self):
|
---|
249 | for name, item in ast.__dict__.iteritems():
|
---|
250 | if isinstance(item, type) and name != 'AST' and name[0].isupper():
|
---|
251 | x = item()
|
---|
252 | if isinstance(x, ast.AST):
|
---|
253 | self.assertEqual(type(x._fields), tuple)
|
---|
254 |
|
---|
255 | def test_arguments(self):
|
---|
256 | x = ast.arguments()
|
---|
257 | self.assertEqual(x._fields, ('args', 'vararg', 'kwarg', 'defaults'))
|
---|
258 |
|
---|
259 | with self.assertRaises(AttributeError):
|
---|
260 | x.vararg
|
---|
261 |
|
---|
262 | x = ast.arguments(1, 2, 3, 4)
|
---|
263 | self.assertEqual(x.vararg, 2)
|
---|
264 |
|
---|
265 | def test_field_attr_writable(self):
|
---|
266 | x = ast.Num()
|
---|
267 | # We can assign to _fields
|
---|
268 | x._fields = 666
|
---|
269 | self.assertEqual(x._fields, 666)
|
---|
270 |
|
---|
271 | def test_classattrs(self):
|
---|
272 | x = ast.Num()
|
---|
273 | self.assertEqual(x._fields, ('n',))
|
---|
274 |
|
---|
275 | with self.assertRaises(AttributeError):
|
---|
276 | x.n
|
---|
277 |
|
---|
278 | x = ast.Num(42)
|
---|
279 | self.assertEqual(x.n, 42)
|
---|
280 |
|
---|
281 | with self.assertRaises(AttributeError):
|
---|
282 | x.lineno
|
---|
283 |
|
---|
284 | with self.assertRaises(AttributeError):
|
---|
285 | x.foobar
|
---|
286 |
|
---|
287 | x = ast.Num(lineno=2)
|
---|
288 | self.assertEqual(x.lineno, 2)
|
---|
289 |
|
---|
290 | x = ast.Num(42, lineno=0)
|
---|
291 | self.assertEqual(x.lineno, 0)
|
---|
292 | self.assertEqual(x._fields, ('n',))
|
---|
293 | self.assertEqual(x.n, 42)
|
---|
294 |
|
---|
295 | self.assertRaises(TypeError, ast.Num, 1, 2)
|
---|
296 | self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0)
|
---|
297 |
|
---|
298 | def test_module(self):
|
---|
299 | body = [ast.Num(42)]
|
---|
300 | x = ast.Module(body)
|
---|
301 | self.assertEqual(x.body, body)
|
---|
302 |
|
---|
303 | def test_nodeclasses(self):
|
---|
304 | # Zero arguments constructor explicitely allowed
|
---|
305 | x = ast.BinOp()
|
---|
306 | self.assertEqual(x._fields, ('left', 'op', 'right'))
|
---|
307 |
|
---|
308 | # Random attribute allowed too
|
---|
309 | x.foobarbaz = 5
|
---|
310 | self.assertEqual(x.foobarbaz, 5)
|
---|
311 |
|
---|
312 | n1 = ast.Num(1)
|
---|
313 | n3 = ast.Num(3)
|
---|
314 | addop = ast.Add()
|
---|
315 | x = ast.BinOp(n1, addop, n3)
|
---|
316 | self.assertEqual(x.left, n1)
|
---|
317 | self.assertEqual(x.op, addop)
|
---|
318 | self.assertEqual(x.right, n3)
|
---|
319 |
|
---|
320 | x = ast.BinOp(1, 2, 3)
|
---|
321 | self.assertEqual(x.left, 1)
|
---|
322 | self.assertEqual(x.op, 2)
|
---|
323 | self.assertEqual(x.right, 3)
|
---|
324 |
|
---|
325 | x = ast.BinOp(1, 2, 3, lineno=0)
|
---|
326 | self.assertEqual(x.left, 1)
|
---|
327 | self.assertEqual(x.op, 2)
|
---|
328 | self.assertEqual(x.right, 3)
|
---|
329 | self.assertEqual(x.lineno, 0)
|
---|
330 |
|
---|
331 | # node raises exception when not given enough arguments
|
---|
332 | self.assertRaises(TypeError, ast.BinOp, 1, 2)
|
---|
333 | # node raises exception when given too many arguments
|
---|
334 | self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
|
---|
335 | # node raises exception when not given enough arguments
|
---|
336 | self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0)
|
---|
337 | # node raises exception when given too many arguments
|
---|
338 | self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
|
---|
339 |
|
---|
340 | # can set attributes through kwargs too
|
---|
341 | x = ast.BinOp(left=1, op=2, right=3, lineno=0)
|
---|
342 | self.assertEqual(x.left, 1)
|
---|
343 | self.assertEqual(x.op, 2)
|
---|
344 | self.assertEqual(x.right, 3)
|
---|
345 | self.assertEqual(x.lineno, 0)
|
---|
346 |
|
---|
347 | # Random kwargs also allowed
|
---|
348 | x = ast.BinOp(1, 2, 3, foobarbaz=42)
|
---|
349 | self.assertEqual(x.foobarbaz, 42)
|
---|
350 |
|
---|
351 | def test_no_fields(self):
|
---|
352 | # this used to fail because Sub._fields was None
|
---|
353 | x = ast.Sub()
|
---|
354 | self.assertEqual(x._fields, ())
|
---|
355 |
|
---|
356 | def test_pickling(self):
|
---|
357 | import pickle
|
---|
358 | mods = [pickle]
|
---|
359 | try:
|
---|
360 | import cPickle
|
---|
361 | mods.append(cPickle)
|
---|
362 | except ImportError:
|
---|
363 | pass
|
---|
364 | protocols = [0, 1, 2]
|
---|
365 | for mod in mods:
|
---|
366 | for protocol in protocols:
|
---|
367 | for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
|
---|
368 | ast2 = mod.loads(mod.dumps(ast, protocol))
|
---|
369 | self.assertEqual(to_tuple(ast2), to_tuple(ast))
|
---|
370 |
|
---|
371 | def test_invalid_identitifer(self):
|
---|
372 | m = ast.Module([ast.Expr(ast.Name(u"x", ast.Load()))])
|
---|
373 | ast.fix_missing_locations(m)
|
---|
374 | with self.assertRaises(TypeError) as cm:
|
---|
375 | compile(m, "<test>", "exec")
|
---|
376 | self.assertIn("identifier must be of type str", str(cm.exception))
|
---|
377 |
|
---|
378 | def test_invalid_string(self):
|
---|
379 | m = ast.Module([ast.Expr(ast.Str(43))])
|
---|
380 | ast.fix_missing_locations(m)
|
---|
381 | with self.assertRaises(TypeError) as cm:
|
---|
382 | compile(m, "<test>", "exec")
|
---|
383 | self.assertIn("string must be of type str or uni", str(cm.exception))
|
---|
384 |
|
---|
385 |
|
---|
386 | class ASTHelpers_Test(unittest.TestCase):
|
---|
387 |
|
---|
388 | def test_parse(self):
|
---|
389 | a = ast.parse('foo(1 + 1)')
|
---|
390 | b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
|
---|
391 | self.assertEqual(ast.dump(a), ast.dump(b))
|
---|
392 |
|
---|
393 | def test_dump(self):
|
---|
394 | node = ast.parse('spam(eggs, "and cheese")')
|
---|
395 | self.assertEqual(ast.dump(node),
|
---|
396 | "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
|
---|
397 | "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
|
---|
398 | "keywords=[], starargs=None, kwargs=None))])"
|
---|
399 | )
|
---|
400 | self.assertEqual(ast.dump(node, annotate_fields=False),
|
---|
401 | "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
|
---|
402 | "Str('and cheese')], [], None, None))])"
|
---|
403 | )
|
---|
404 | self.assertEqual(ast.dump(node, include_attributes=True),
|
---|
405 | "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
|
---|
406 | "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
|
---|
407 | "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
|
---|
408 | "col_offset=11)], keywords=[], starargs=None, kwargs=None, "
|
---|
409 | "lineno=1, col_offset=0), lineno=1, col_offset=0)])"
|
---|
410 | )
|
---|
411 |
|
---|
412 | def test_copy_location(self):
|
---|
413 | src = ast.parse('1 + 1', mode='eval')
|
---|
414 | src.body.right = ast.copy_location(ast.Num(2), src.body.right)
|
---|
415 | self.assertEqual(ast.dump(src, include_attributes=True),
|
---|
416 | 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
|
---|
417 | 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
|
---|
418 | 'col_offset=0))'
|
---|
419 | )
|
---|
420 |
|
---|
421 | def test_fix_missing_locations(self):
|
---|
422 | src = ast.parse('write("spam")')
|
---|
423 | src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
|
---|
424 | [ast.Str('eggs')], [], None, None)))
|
---|
425 | self.assertEqual(src, ast.fix_missing_locations(src))
|
---|
426 | self.assertEqual(ast.dump(src, include_attributes=True),
|
---|
427 | "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
|
---|
428 | "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
|
---|
429 | "col_offset=6)], keywords=[], starargs=None, kwargs=None, "
|
---|
430 | "lineno=1, col_offset=0), lineno=1, col_offset=0), "
|
---|
431 | "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
|
---|
432 | "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
|
---|
433 | "keywords=[], starargs=None, kwargs=None, lineno=1, "
|
---|
434 | "col_offset=0), lineno=1, col_offset=0)])"
|
---|
435 | )
|
---|
436 |
|
---|
437 | def test_increment_lineno(self):
|
---|
438 | src = ast.parse('1 + 1', mode='eval')
|
---|
439 | self.assertEqual(ast.increment_lineno(src, n=3), src)
|
---|
440 | self.assertEqual(ast.dump(src, include_attributes=True),
|
---|
441 | 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
|
---|
442 | 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
|
---|
443 | 'col_offset=0))'
|
---|
444 | )
|
---|
445 | # issue10869: do not increment lineno of root twice
|
---|
446 | src = ast.parse('1 + 1', mode='eval')
|
---|
447 | self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
|
---|
448 | self.assertEqual(ast.dump(src, include_attributes=True),
|
---|
449 | 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
|
---|
450 | 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
|
---|
451 | 'col_offset=0))'
|
---|
452 | )
|
---|
453 |
|
---|
454 | def test_iter_fields(self):
|
---|
455 | node = ast.parse('foo()', mode='eval')
|
---|
456 | d = dict(ast.iter_fields(node.body))
|
---|
457 | self.assertEqual(d.pop('func').id, 'foo')
|
---|
458 | self.assertEqual(d, {'keywords': [], 'kwargs': None,
|
---|
459 | 'args': [], 'starargs': None})
|
---|
460 |
|
---|
461 | def test_iter_child_nodes(self):
|
---|
462 | node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
|
---|
463 | self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
|
---|
464 | iterator = ast.iter_child_nodes(node.body)
|
---|
465 | self.assertEqual(next(iterator).id, 'spam')
|
---|
466 | self.assertEqual(next(iterator).n, 23)
|
---|
467 | self.assertEqual(next(iterator).n, 42)
|
---|
468 | self.assertEqual(ast.dump(next(iterator)),
|
---|
469 | "keyword(arg='eggs', value=Str(s='leek'))"
|
---|
470 | )
|
---|
471 |
|
---|
472 | def test_get_docstring(self):
|
---|
473 | node = ast.parse('def foo():\n """line one\n line two"""')
|
---|
474 | self.assertEqual(ast.get_docstring(node.body[0]),
|
---|
475 | 'line one\nline two')
|
---|
476 |
|
---|
477 | def test_literal_eval(self):
|
---|
478 | self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
|
---|
479 | self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
|
---|
480 | self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
|
---|
481 | self.assertRaises(ValueError, ast.literal_eval, 'foo()')
|
---|
482 |
|
---|
483 | def test_literal_eval_issue4907(self):
|
---|
484 | self.assertEqual(ast.literal_eval('2j'), 2j)
|
---|
485 | self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
|
---|
486 | self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
|
---|
487 | self.assertRaises(ValueError, ast.literal_eval, '2 + (3 + 4j)')
|
---|
488 |
|
---|
489 |
|
---|
490 | def test_main():
|
---|
491 | with test_support.check_py3k_warnings(("backquote not supported",
|
---|
492 | SyntaxWarning)):
|
---|
493 | test_support.run_unittest(AST_Tests, ASTHelpers_Test)
|
---|
494 |
|
---|
495 | def main():
|
---|
496 | if __name__ != '__main__':
|
---|
497 | return
|
---|
498 | if sys.argv[1:] == ['-g']:
|
---|
499 | for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
|
---|
500 | (eval_tests, "eval")):
|
---|
501 | print kind+"_results = ["
|
---|
502 | for s in statements:
|
---|
503 | print repr(to_tuple(compile(s, "?", kind, 0x400)))+","
|
---|
504 | print "]"
|
---|
505 | print "main()"
|
---|
506 | raise SystemExit
|
---|
507 | test_main()
|
---|
508 |
|
---|
509 | #### EVERYTHING BELOW IS GENERATED #####
|
---|
510 | exec_results = [
|
---|
511 | ('Module', [('Expr', (1, 0), ('Name', (1, 0), 'None', ('Load',)))]),
|
---|
512 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]),
|
---|
513 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',))], None, None, []), [('Pass', (1, 10))], [])]),
|
---|
514 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',))], None, None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [])]),
|
---|
515 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], 'args', None, []), [('Pass', (1, 14))], [])]),
|
---|
516 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, 'kwargs', []), [('Pass', (1, 17))], [])]),
|
---|
517 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',)), ('Name', (1, 9), 'b', ('Param',)), ('Name', (1, 14), 'c', ('Param',)), ('Name', (1, 22), 'd', ('Param',)), ('Name', (1, 28), 'e', ('Param',))], 'args', 'kwargs', [('Num', (1, 11), 1), ('Name', (1, 16), 'None', ('Load',)), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Pass', (1, 52))], [])]),
|
---|
518 | ('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))], [])]),
|
---|
519 | ('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [('Pass', (1, 17))], [])]),
|
---|
520 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [])]),
|
---|
521 | ('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
|
---|
522 | ('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
|
---|
523 | ('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
|
---|
524 | ('Module', [('Print', (1, 0), ('Name', (1, 8), 'f', ('Load',)), [('Num', (1, 11), 1)], False)]),
|
---|
525 | ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
|
---|
526 | ('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
|
---|
527 | ('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
|
---|
528 | ('Module', [('Raise', (1, 0), ('Name', (1, 6), 'Exception', ('Load',)), ('Str', (1, 17), 'string'), None)]),
|
---|
529 | ('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [])]),
|
---|
530 | ('Module', [('TryFinally', (1, 0), [('Pass', (2, 2))], [('Pass', (4, 2))])]),
|
---|
531 | ('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
|
---|
532 | ('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
|
---|
533 | ('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]),
|
---|
534 | ('Module', [('Exec', (1, 0), ('Str', (1, 5), 'v'), None, None)]),
|
---|
535 | ('Module', [('Global', (1, 0), ['v'])]),
|
---|
536 | ('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
|
---|
537 | ('Module', [('Pass', (1, 0))]),
|
---|
538 | ('Module', [('Break', (1, 0))]),
|
---|
539 | ('Module', [('Continue', (1, 0))]),
|
---|
540 | ('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 4), 'a', ('Store',)), ('Name', (1, 6), 'b', ('Store',))], ('Store',)), ('Name', (1, 11), 'c', ('Load',)), [('Pass', (1, 14))], [])]),
|
---|
541 | ('Module', [('Expr', (1, 0), ('ListComp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]),
|
---|
542 | ('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]),
|
---|
543 | ('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 12), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [])]))]),
|
---|
544 | ('Module', [('Expr', (1, 0), ('GeneratorExp', (2, 4), ('Tuple', (3, 4), [('Name', (3, 4), 'Aa', ('Load',)), ('Name', (5, 7), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4), [('Name', (8, 4), 'Aa', ('Store',)), ('Name', (10, 4), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10), 'Cc', ('Load',)), [])]))]),
|
---|
545 | ('Module', [('Expr', (1, 0), ('DictComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Name', (1, 11), 'w', ('Store',)), ('Name', (1, 16), 'x', ('Load',)), []), ('comprehension', ('Name', (1, 22), 'm', ('Store',)), ('Name', (1, 27), 'p', ('Load',)), [('Name', (1, 32), 'g', ('Load',))])]))]),
|
---|
546 | ('Module', [('Expr', (1, 0), ('DictComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'v', ('Store',)), ('Name', (1, 13), 'w', ('Store',))], ('Store',)), ('Name', (1, 18), 'x', ('Load',)), [])]))]),
|
---|
547 | ('Module', [('Expr', (1, 0), ('SetComp', (1, 1), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))])]))]),
|
---|
548 | ('Module', [('Expr', (1, 0), ('SetComp', (1, 1), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Tuple', (1, 7), [('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 9), 'm', ('Store',))], ('Store',)), ('Name', (1, 14), 'x', ('Load',)), [])]))]),
|
---|
549 | ]
|
---|
550 | single_results = [
|
---|
551 | ('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
|
---|
552 | ]
|
---|
553 | eval_results = [
|
---|
554 | ('Expression', ('Name', (1, 0), 'None', ('Load',))),
|
---|
555 | ('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
|
---|
556 | ('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
|
---|
557 | ('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
|
---|
558 | ('Expression', ('Lambda', (1, 0), ('arguments', [], None, None, []), ('Name', (1, 7), 'None', ('Load',)))),
|
---|
559 | ('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
|
---|
560 | ('Expression', ('Dict', (1, 0), [], [])),
|
---|
561 | ('Expression', ('Set', (1, 0), [('Name', (1, 1), 'None', ('Load',))])),
|
---|
562 | ('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
|
---|
563 | ('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
|
---|
564 | ('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
|
---|
565 | ('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
|
---|
566 | ('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2)], [('keyword', 'c', ('Num', (1, 8), 3))], ('Name', (1, 11), 'd', ('Load',)), ('Name', (1, 15), 'e', ('Load',)))),
|
---|
567 | ('Expression', ('Repr', (1, 0), ('Name', (1, 1), 'v', ('Load',)))),
|
---|
568 | ('Expression', ('Num', (1, 0), 10L)),
|
---|
569 | ('Expression', ('Str', (1, 0), 'string')),
|
---|
570 | ('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
|
---|
571 | ('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
|
---|
572 | ('Expression', ('Name', (1, 0), 'v', ('Load',))),
|
---|
573 | ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
|
---|
574 | ('Expression', ('List', (1, 0), [], ('Load',))),
|
---|
575 | ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
|
---|
576 | ('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
|
---|
577 | ('Expression', ('Tuple', (1, 0), [], ('Load',))),
|
---|
578 | ('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [], None, None)),
|
---|
579 | ]
|
---|
580 | main()
|
---|