annotate Lib/test/test_ast.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 6e33aeaee77d
children 0c357e3fc3d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
1 import sys, itertools, unittest
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
2 from test import test_support
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
3 import ast
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
4
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
5 def to_tuple(t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
6 if t is None or isinstance(t, (basestring, int, long, complex)):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
7 return t
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
8 elif isinstance(t, list):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
9 return [to_tuple(e) for e in t]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
10 result = [t.__class__.__name__]
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
11 if hasattr(t, 'lineno') and hasattr(t, 'col_offset'):
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
12 result.append((t.lineno, t.col_offset))
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
13 if t._fields is None:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
14 return tuple(result)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
15 for f in t._fields:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
16 result.append(to_tuple(getattr(t, f)))
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
17 return tuple(result)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
18
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
19
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
20 # These tests are compiled through "exec"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
21 # There should be atleast one test per statement
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
22 exec_tests = [
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
23 # FunctionDef
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
24 "def f(): pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
25 # ClassDef
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
26 "class C:pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
27 # Return
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
28 "def f():return 1",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
29 # Delete
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
30 "del v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
31 # Assign
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
32 "v = 1",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
33 # AugAssign
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
34 "v += 1",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
35 # Print
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
36 "print >>f, 1, ",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
37 # For
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
38 "for v in v:pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
39 # While
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
40 "while v:pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
41 # If
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
42 "if v:pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
43 # Raise
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
44 "raise Exception, 'string'",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
45 # TryExcept
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
46 "try:\n pass\nexcept Exception:\n pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
47 # TryFinally
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
48 "try:\n pass\nfinally:\n pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
49 # Assert
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
50 "assert v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
51 # Import
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
52 "import sys",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
53 # ImportFrom
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
54 "from sys import v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
55 # Exec
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
56 "exec 'v'",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
57 # Global
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
58 "global v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
59 # Expr
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
60 "1",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
61 # Pass,
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
62 "pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
63 # Break
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
64 "break",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
65 # Continue
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
66 "continue",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
67 ]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
68
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
69 # These are compiled through "single"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
70 # because of overlap with "eval", it just tests what
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
71 # can't be tested with "eval"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
72 single_tests = [
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
73 "1+2"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
74 ]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
75
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
76 # These are compiled through "eval"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
77 # It should test all expressions
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
78 eval_tests = [
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
79 # BoolOp
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
80 "a and b",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
81 # BinOp
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
82 "a + b",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
83 # UnaryOp
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
84 "not v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
85 # Lambda
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
86 "lambda:None",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
87 # Dict
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
88 "{ 1:2 }",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
89 # ListComp
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
90 "[a for b in c if d]",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
91 # GeneratorExp
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
92 "(a for b in c if d)",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
93 # Yield - yield expressions can't work outside a function
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
94 #
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
95 # Compare
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
96 "1 < 2 < 3",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
97 # Call
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
98 "f(1,2,c=3,*d,**e)",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
99 # Repr
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
100 "`v`",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
101 # Num
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
102 "10L",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
103 # Str
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
104 "'string'",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
105 # Attribute
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
106 "a.b",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
107 # Subscript
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
108 "a[b:c]",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
109 # Name
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
110 "v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
111 # List
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
112 "[1,2,3]",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
113 # Tuple
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
114 "1,2,3",
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
115 # Combination
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
116 "a.b.c.d(a.b[1:2])",
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
117
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
118 ]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
119
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
120 # TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
121 # excepthandler, arguments, keywords, alias
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
122
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
123 class AST_Tests(unittest.TestCase):
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
124
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
125 def _assert_order(self, ast_node, parent_pos):
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
126 if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
127 return
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
128 if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
129 node_pos = (ast_node.lineno, ast_node.col_offset)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
130 self.assert_(node_pos >= parent_pos)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
131 parent_pos = (ast_node.lineno, ast_node.col_offset)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
132 for name in ast_node._fields:
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
133 value = getattr(ast_node, name)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
134 if isinstance(value, list):
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
135 for child in value:
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
136 self._assert_order(child, parent_pos)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
137 elif value is not None:
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
138 self._assert_order(value, parent_pos)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
139
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
140 def test_snippets(self):
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
141 for input, output, kind in ((exec_tests, exec_results, "exec"),
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
142 (single_tests, single_results, "single"),
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
143 (eval_tests, eval_results, "eval")):
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
144 for i, o in itertools.izip(input, output):
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
145 ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
146 self.assertEquals(to_tuple(ast_tree), o)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
147 self._assert_order(ast_tree, (0, 0))
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
148
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
149 def test_nodeclasses(self):
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
150 x = ast.BinOp(1, 2, 3, lineno=0)
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
151 self.assertEquals(x.left, 1)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
152 self.assertEquals(x.op, 2)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
153 self.assertEquals(x.right, 3)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
154 self.assertEquals(x.lineno, 0)
36788
f666bc9f617d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36786
diff changeset
155
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
156 # node raises exception when not given enough arguments
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
157 self.assertRaises(TypeError, ast.BinOp, 1, 2)
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
158
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
159 # can set attributes through kwargs too
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
160 x = ast.BinOp(left=1, op=2, right=3, lineno=0)
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
161 self.assertEquals(x.left, 1)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
162 self.assertEquals(x.op, 2)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
163 self.assertEquals(x.right, 3)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
164 self.assertEquals(x.lineno, 0)
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
165
46204
29ea507fd998 Make _fields attr for no fields consistent with _attributes attr.
Georg Brandl <georg@python.org>
parents: 46203
diff changeset
166 # this used to fail because Sub._fields was None
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
167 x = ast.Sub()
46204
29ea507fd998 Make _fields attr for no fields consistent with _attributes attr.
Georg Brandl <georg@python.org>
parents: 46203
diff changeset
168
46212
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
169 def test_pickling(self):
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
170 import pickle
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
171 mods = [pickle]
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
172 try:
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
173 import cPickle
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
174 mods.append(cPickle)
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
175 except ImportError:
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
176 pass
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
177 protocols = [0, 1, 2]
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
178 for mod in mods:
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
179 for protocol in protocols:
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
180 for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
181 ast2 = mod.loads(mod.dumps(ast, protocol))
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
182 self.assertEquals(to_tuple(ast2), to_tuple(ast))
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
183
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
184
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
185 class ASTHelpers_Test(unittest.TestCase):
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
186
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
187 def test_parse(self):
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
188 a = ast.parse('foo(1 + 1)')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
189 b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
190 self.assertEqual(ast.dump(a), ast.dump(b))
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
191
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
192 def test_dump(self):
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
193 node = ast.parse('spam(eggs, "and cheese")')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
194 self.assertEqual(ast.dump(node),
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
195 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
196 "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
197 "keywords=[], starargs=None, kwargs=None))])"
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
198 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
199 self.assertEqual(ast.dump(node, annotate_fields=False),
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
200 "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
201 "Str('and cheese')], [], None, None))])"
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
202 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
203 self.assertEqual(ast.dump(node, include_attributes=True),
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
204 "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
205 "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
206 "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
207 "col_offset=11)], keywords=[], starargs=None, kwargs=None, "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
208 "lineno=1, col_offset=0), lineno=1, col_offset=0)])"
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
209 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
210
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
211 def test_copy_location(self):
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
212 src = ast.parse('1 + 1', mode='eval')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
213 src.body.right = ast.copy_location(ast.Num(2), src.body.right)
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
214 self.assertEqual(ast.dump(src, include_attributes=True),
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
215 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
216 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
217 'col_offset=0))'
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
218 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
219
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
220 def test_fix_missing_locations(self):
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
221 src = ast.parse('write("spam")')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
222 src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
223 [ast.Str('eggs')], [], None, None)))
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
224 self.assertEqual(src, ast.fix_missing_locations(src))
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
225 self.assertEqual(ast.dump(src, include_attributes=True),
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
226 "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
227 "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
228 "col_offset=6)], keywords=[], starargs=None, kwargs=None, "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
229 "lineno=1, col_offset=0), lineno=1, col_offset=0), "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
230 "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
231 "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
232 "keywords=[], starargs=None, kwargs=None, lineno=1, "
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
233 "col_offset=0), lineno=1, col_offset=0)])"
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
234 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
235
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
236 def test_increment_lineno(self):
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
237 src = ast.parse('1 + 1', mode='eval')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
238 self.assertEqual(ast.increment_lineno(src, n=3), src)
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
239 self.assertEqual(ast.dump(src, include_attributes=True),
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
240 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
241 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
242 'col_offset=0))'
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
243 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
244
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
245 def test_iter_fields(self):
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
246 node = ast.parse('foo()', mode='eval')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
247 d = dict(ast.iter_fields(node.body))
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
248 self.assertEqual(d.pop('func').id, 'foo')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
249 self.assertEqual(d, {'keywords': [], 'kwargs': None,
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
250 'args': [], 'starargs': None})
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
251
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
252 def test_iter_child_nodes(self):
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
253 node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
254 self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
255 iterator = ast.iter_child_nodes(node.body)
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
256 self.assertEqual(next(iterator).id, 'spam')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
257 self.assertEqual(next(iterator).n, 23)
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
258 self.assertEqual(next(iterator).n, 42)
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
259 self.assertEqual(ast.dump(next(iterator)),
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
260 "keyword(arg='eggs', value=Str(s='leek'))"
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
261 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
262
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
263 def test_get_docstring(self):
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
264 node = ast.parse('def foo():\n """line one\n line two"""')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
265 self.assertEqual(ast.get_docstring(node.body[0]),
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
266 'line one\nline two')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
267
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
268 def test_literal_eval(self):
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
269 self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
270 self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
271 self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
272 self.assertRaises(ValueError, ast.literal_eval, 'foo()')
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
273
50765
c776b6d814c2 ast.literal_eval can properly evaluate complex numbers now. This fixes issue4907.
Armin Ronacher <armin.ronacher@active-4.com>
parents: 47541
diff changeset
274 def test_literal_eval_issue4907(self):
c776b6d814c2 ast.literal_eval can properly evaluate complex numbers now. This fixes issue4907.
Armin Ronacher <armin.ronacher@active-4.com>
parents: 47541
diff changeset
275 self.assertEqual(ast.literal_eval('2j'), 2j)
c776b6d814c2 ast.literal_eval can properly evaluate complex numbers now. This fixes issue4907.
Armin Ronacher <armin.ronacher@active-4.com>
parents: 47541
diff changeset
276 self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
c776b6d814c2 ast.literal_eval can properly evaluate complex numbers now. This fixes issue4907.
Armin Ronacher <armin.ronacher@active-4.com>
parents: 47541
diff changeset
277 self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
50771
6e33aeaee77d Use assertRaises.
Georg Brandl <georg@python.org>
parents: 50765
diff changeset
278 self.assertRaises(ValueError, ast.literal_eval, '2 + (3 + 4j)')
50765
c776b6d814c2 ast.literal_eval can properly evaluate complex numbers now. This fixes issue4907.
Armin Ronacher <armin.ronacher@active-4.com>
parents: 47541
diff changeset
279
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
280
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
281 def test_main():
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
282 test_support.run_unittest(AST_Tests, ASTHelpers_Test)
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
283
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
284 def main():
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
285 if __name__ != '__main__':
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
286 return
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
287 if sys.argv[1:] == ['-g']:
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
288 for statements, kind in ((exec_tests, "exec"), (single_tests, "single"),
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
289 (eval_tests, "eval")):
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
290 print kind+"_results = ["
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
291 for s in statements:
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
292 print repr(to_tuple(compile(s, "?", kind, 0x400)))+","
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
293 print "]"
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
294 print "main()"
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
295 raise SystemExit
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
296 test_main()
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
297
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
298 #### EVERYTHING BELOW IS GENERATED #####
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
299 exec_results = [
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
300 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]),
45447
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 39037
diff changeset
301 ('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))], [])]),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
302 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [])]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
303 ('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
304 ('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
39037
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 37466
diff changeset
305 ('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
306 ('Module', [('Print', (1, 0), ('Name', (1, 8), 'f', ('Load',)), [('Num', (1, 11), 1)], False)]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
307 ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
308 ('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
309 ('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
310 ('Module', [('Raise', (1, 0), ('Name', (1, 6), 'Exception', ('Load',)), ('Str', (1, 17), 'string'), None)]),
46201
e370d9cd149e Adapt test_ast to the new ExceptHandler type.
Georg Brandl <georg@python.org>
parents: 46197
diff changeset
311 ('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [])]),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
312 ('Module', [('TryFinally', (1, 0), [('Pass', (2, 2))], [('Pass', (4, 2))])]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
313 ('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
314 ('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
315 ('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
316 ('Module', [('Exec', (1, 0), ('Str', (1, 5), 'v'), None, None)]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
317 ('Module', [('Global', (1, 0), ['v'])]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
318 ('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
319 ('Module', [('Pass', (1, 0))]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
320 ('Module', [('Break', (1, 0))]),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
321 ('Module', [('Continue', (1, 0))]),
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
322 ]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
323 single_results = [
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
324 ('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
325 ]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
326 eval_results = [
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
327 ('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
328 ('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
329 ('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
330 ('Expression', ('Lambda', (1, 0), ('arguments', [], None, None, []), ('Name', (1, 7), 'None', ('Load',)))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
331 ('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
332 ('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',))])])),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
333 ('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',))])])),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
334 ('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
335 ('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',)))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
336 ('Expression', ('Repr', (1, 0), ('Name', (1, 1), 'v', ('Load',)))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
337 ('Expression', ('Num', (1, 0), 10L)),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
338 ('Expression', ('Str', (1, 0), 'string')),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
339 ('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
340 ('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
341 ('Expression', ('Name', (1, 0), 'v', ('Load',))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
342 ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
343 ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
344 ('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)),
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
345 ]
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
346 main()