annotate Lib/test/test_ast.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 1729ec440bb6
children c75b8d5fa016
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 = [
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
23 # None
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
24 "None",
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
25 # FunctionDef
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
26 "def f(): pass",
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
27 # FunctionDef with arg
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
28 "def f(a): pass",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
29 # FunctionDef with arg and default value
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
30 "def f(a=0): pass",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
31 # FunctionDef with varargs
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
32 "def f(*args): pass",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
33 # FunctionDef with kwargs
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
34 "def f(**kwargs): pass",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
35 # FunctionDef with all kind of args
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
36 "def f(a, b=1, c=None, d=[], e={}, *args, **kwargs): pass",
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
37 # ClassDef
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
38 "class C:pass",
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
39 # ClassDef, new style class
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
40 "class C(object): pass",
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
41 # Return
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
42 "def f():return 1",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
43 # Delete
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
44 "del v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
45 # Assign
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
46 "v = 1",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
47 # AugAssign
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
48 "v += 1",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
49 # Print
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
50 "print >>f, 1, ",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
51 # For
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
52 "for v in v:pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
53 # While
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
54 "while v:pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
55 # If
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
56 "if v:pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
57 # Raise
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
58 "raise Exception, 'string'",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
59 # TryExcept
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
60 "try:\n pass\nexcept Exception:\n pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
61 # TryFinally
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
62 "try:\n pass\nfinally:\n pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
63 # Assert
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
64 "assert v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
65 # Import
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
66 "import sys",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
67 # ImportFrom
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
68 "from sys import v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
69 # Exec
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
70 "exec 'v'",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
71 # Global
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
72 "global v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
73 # Expr
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
74 "1",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
75 # Pass,
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
76 "pass",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
77 # Break
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
78 "break",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
79 # Continue
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
80 "continue",
55651
1e2877026435 better col_offsets for "for" statements with tuple unpacking #6704
Benjamin Peterson <benjamin@python.org>
parents: 55362
diff changeset
81 # for statements with naked tuples (see http://bugs.python.org/issue6704)
1e2877026435 better col_offsets for "for" statements with tuple unpacking #6704
Benjamin Peterson <benjamin@python.org>
parents: 55362
diff changeset
82 "for a,b in c: pass",
1e2877026435 better col_offsets for "for" statements with tuple unpacking #6704
Benjamin Peterson <benjamin@python.org>
parents: 55362
diff changeset
83 "[(a,b) for a,b in c]",
1e2877026435 better col_offsets for "for" statements with tuple unpacking #6704
Benjamin Peterson <benjamin@python.org>
parents: 55362
diff changeset
84 "((a,b) for a,b in c)",
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
85 "((a,b) for (a,b) in c)",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
86 # Multiline generator expression (test for .lineno & .col_offset)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
87 """(
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
88 (
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
89 Aa
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
90 ,
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
91 Bb
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
92 )
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
93 for
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
94 Aa
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
95 ,
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
96 Bb in Cc
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
97 )""",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
98 # dictcomp
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
99 "{a : b for w in x for m in p if g}",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
100 # dictcomp with naked tuple
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
101 "{a : b for v,w in x}",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
102 # setcomp
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
103 "{r for l in x if g}",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
104 # setcomp with naked tuple
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
105 "{r for l,m in x}",
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
106 ]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
107
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
108 # These are compiled through "single"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
109 # 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
110 # can't be tested with "eval"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
111 single_tests = [
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
112 "1+2"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
113 ]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
114
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
115 # These are compiled through "eval"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
116 # It should test all expressions
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
117 eval_tests = [
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
118 # None
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
119 "None",
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
120 # BoolOp
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
121 "a and b",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
122 # BinOp
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
123 "a + b",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
124 # UnaryOp
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
125 "not v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
126 # Lambda
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
127 "lambda:None",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
128 # Dict
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
129 "{ 1:2 }",
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
130 # Empty dict
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
131 "{}",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
132 # Set
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
133 "{None,}",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
134 # Multiline dict (test for .lineno & .col_offset)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
135 """{
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
136 1
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
137 :
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
138 2
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
139 }""",
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
140 # ListComp
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
141 "[a for b in c if d]",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
142 # GeneratorExp
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
143 "(a for b in c if d)",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
144 # 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
145 #
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
146 # Compare
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
147 "1 < 2 < 3",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
148 # Call
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
149 "f(1,2,c=3,*d,**e)",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
150 # Repr
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
151 "`v`",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
152 # Num
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
153 "10L",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
154 # Str
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
155 "'string'",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
156 # Attribute
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
157 "a.b",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
158 # Subscript
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
159 "a[b:c]",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
160 # Name
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
161 "v",
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
162 # List
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
163 "[1,2,3]",
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
164 # Empty list
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
165 "[]",
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
166 # Tuple
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
167 "1,2,3",
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
168 # Tuple
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
169 "(1,2,3)",
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
170 # Empty tuple
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
171 "()",
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
172 # Combination
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
173 "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
174
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
175 ]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
176
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
177 # 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
178 # excepthandler, arguments, keywords, alias
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
179
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
180 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
181
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54743
diff changeset
182 def _assertTrueorder(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
183 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
184 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
185 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
186 node_pos = (ast_node.lineno, ast_node.col_offset)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54743
diff changeset
187 self.assertTrue(node_pos >= parent_pos)
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
188 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
189 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
190 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
191 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
192 for child in value:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54743
diff changeset
193 self._assertTrueorder(child, parent_pos)
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
194 elif value is not None:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54743
diff changeset
195 self._assertTrueorder(value, parent_pos)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
196
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
197 def test_AST_objects(self):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
198 x = ast.AST()
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
199 self.assertEqual(x._fields, ())
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
200
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
201 with self.assertRaises(AttributeError):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
202 x.vararg
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
203
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
204 with self.assertRaises(AttributeError):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
205 x.foobar = 21
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
206
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
207 with self.assertRaises(AttributeError):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
208 ast.AST(lineno=2)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
209
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
210 with self.assertRaises(TypeError):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
211 # "_ast.AST constructor takes 0 positional arguments"
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
212 ast.AST(2)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
213
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
214 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
215 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
216 (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
217 (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
218 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
219 ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
220 self.assertEqual(to_tuple(ast_tree), o)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54743
diff changeset
221 self._assertTrueorder(ast_tree, (0, 0))
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
222
54727
0c357e3fc3d0 keep the slice.step field as NULL if no step expression is given
Benjamin Peterson <benjamin@python.org>
parents: 50771
diff changeset
223 def test_slice(self):
0c357e3fc3d0 keep the slice.step field as NULL if no step expression is given
Benjamin Peterson <benjamin@python.org>
parents: 50771
diff changeset
224 slc = ast.parse("x[::]").body[0].value.slice
0c357e3fc3d0 keep the slice.step field as NULL if no step expression is given
Benjamin Peterson <benjamin@python.org>
parents: 50771
diff changeset
225 self.assertIsNone(slc.upper)
0c357e3fc3d0 keep the slice.step field as NULL if no step expression is given
Benjamin Peterson <benjamin@python.org>
parents: 50771
diff changeset
226 self.assertIsNone(slc.lower)
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
227 self.assertIsInstance(slc.step, ast.Name)
55362
7bf8fc20037a the Slice in x[::] has to have step as None to help the interpreter
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
228 self.assertEqual(slc.step.id, "None")
54727
0c357e3fc3d0 keep the slice.step field as NULL if no step expression is given
Benjamin Peterson <benjamin@python.org>
parents: 50771
diff changeset
229
54743
40c8c35492f1 when no module is given in a 'from' relative import, make ImportFrom.module NULL
Benjamin Peterson <benjamin@python.org>
parents: 54727
diff changeset
230 def test_from_import(self):
40c8c35492f1 when no module is given in a 'from' relative import, make ImportFrom.module NULL
Benjamin Peterson <benjamin@python.org>
parents: 54727
diff changeset
231 im = ast.parse("from . import y").body[0]
40c8c35492f1 when no module is given in a 'from' relative import, make ImportFrom.module NULL
Benjamin Peterson <benjamin@python.org>
parents: 54727
diff changeset
232 self.assertIsNone(im.module)
40c8c35492f1 when no module is given in a 'from' relative import, make ImportFrom.module NULL
Benjamin Peterson <benjamin@python.org>
parents: 54727
diff changeset
233
75872
1729ec440bb6 check by equality for __future__ not identity (closes #14378)
Benjamin Peterson <benjamin@python.org>
parents: 71452
diff changeset
234 def test_non_interned_future_from_ast(self):
1729ec440bb6 check by equality for __future__ not identity (closes #14378)
Benjamin Peterson <benjamin@python.org>
parents: 71452
diff changeset
235 mod = ast.parse("from __future__ import division")
1729ec440bb6 check by equality for __future__ not identity (closes #14378)
Benjamin Peterson <benjamin@python.org>
parents: 71452
diff changeset
236 self.assertIsInstance(mod.body[0], ast.ImportFrom)
1729ec440bb6 check by equality for __future__ not identity (closes #14378)
Benjamin Peterson <benjamin@python.org>
parents: 71452
diff changeset
237 mod.body[0].module = " __future__ ".strip()
1729ec440bb6 check by equality for __future__ not identity (closes #14378)
Benjamin Peterson <benjamin@python.org>
parents: 71452
diff changeset
238 compile(mod, "<test>", "exec")
1729ec440bb6 check by equality for __future__ not identity (closes #14378)
Benjamin Peterson <benjamin@python.org>
parents: 71452
diff changeset
239
56898
411b6acbcf12 test expect base classes
Benjamin Peterson <benjamin@python.org>
parents: 55651
diff changeset
240 def test_base_classes(self):
411b6acbcf12 test expect base classes
Benjamin Peterson <benjamin@python.org>
parents: 55651
diff changeset
241 self.assertTrue(issubclass(ast.For, ast.stmt))
411b6acbcf12 test expect base classes
Benjamin Peterson <benjamin@python.org>
parents: 55651
diff changeset
242 self.assertTrue(issubclass(ast.Name, ast.expr))
411b6acbcf12 test expect base classes
Benjamin Peterson <benjamin@python.org>
parents: 55651
diff changeset
243 self.assertTrue(issubclass(ast.stmt, ast.AST))
411b6acbcf12 test expect base classes
Benjamin Peterson <benjamin@python.org>
parents: 55651
diff changeset
244 self.assertTrue(issubclass(ast.expr, ast.AST))
411b6acbcf12 test expect base classes
Benjamin Peterson <benjamin@python.org>
parents: 55651
diff changeset
245 self.assertTrue(issubclass(ast.comprehension, ast.AST))
411b6acbcf12 test expect base classes
Benjamin Peterson <benjamin@python.org>
parents: 55651
diff changeset
246 self.assertTrue(issubclass(ast.Gt, ast.AST))
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
247
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
248 def test_field_attr_existence(self):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
249 for name, item in ast.__dict__.iteritems():
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
250 if isinstance(item, type) and name != 'AST' and name[0].isupper():
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
251 x = item()
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
252 if isinstance(x, ast.AST):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
253 self.assertEqual(type(x._fields), tuple)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
254
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
255 def test_arguments(self):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
256 x = ast.arguments()
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
257 self.assertEqual(x._fields, ('args', 'vararg', 'kwarg', 'defaults'))
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
258
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
259 with self.assertRaises(AttributeError):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
260 x.vararg
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
261
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
262 x = ast.arguments(1, 2, 3, 4)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
263 self.assertEqual(x.vararg, 2)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
264
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
265 def test_field_attr_writable(self):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
266 x = ast.Num()
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
267 # We can assign to _fields
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
268 x._fields = 666
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
269 self.assertEqual(x._fields, 666)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
270
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
271 def test_classattrs(self):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
272 x = ast.Num()
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
273 self.assertEqual(x._fields, ('n',))
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
274
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
275 with self.assertRaises(AttributeError):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
276 x.n
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
277
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
278 x = ast.Num(42)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
279 self.assertEqual(x.n, 42)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
280
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
281 with self.assertRaises(AttributeError):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
282 x.lineno
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
283
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
284 with self.assertRaises(AttributeError):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
285 x.foobar
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
286
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
287 x = ast.Num(lineno=2)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
288 self.assertEqual(x.lineno, 2)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
289
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
290 x = ast.Num(42, lineno=0)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
291 self.assertEqual(x.lineno, 0)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
292 self.assertEqual(x._fields, ('n',))
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
293 self.assertEqual(x.n, 42)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
294
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
295 self.assertRaises(TypeError, ast.Num, 1, 2)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
296 self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
297
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
298 def test_module(self):
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
299 body = [ast.Num(42)]
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
300 x = ast.Module(body)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
301 self.assertEqual(x.body, body)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
302
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
303 def test_nodeclasses(self):
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
304 # Zero arguments constructor explicitely allowed
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
305 x = ast.BinOp()
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
306 self.assertEqual(x._fields, ('left', 'op', 'right'))
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
307
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
308 # Random attribute allowed too
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
309 x.foobarbaz = 5
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
310 self.assertEqual(x.foobarbaz, 5)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
311
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
312 n1 = ast.Num(1)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
313 n3 = ast.Num(3)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
314 addop = ast.Add()
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
315 x = ast.BinOp(n1, addop, n3)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
316 self.assertEqual(x.left, n1)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
317 self.assertEqual(x.op, addop)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
318 self.assertEqual(x.right, n3)
71022
Benjamin Peterson <benjamin@python.org>
parents: 71019
diff changeset
319
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
320 x = ast.BinOp(1, 2, 3)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
321 self.assertEqual(x.left, 1)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
322 self.assertEqual(x.op, 2)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
323 self.assertEqual(x.right, 3)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
324
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
325 x = ast.BinOp(1, 2, 3, lineno=0)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
326 self.assertEqual(x.left, 1)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
327 self.assertEqual(x.op, 2)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
328 self.assertEqual(x.right, 3)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
329 self.assertEqual(x.lineno, 0)
36788
f666bc9f617d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36786
diff changeset
330
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
331 # 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
332 self.assertRaises(TypeError, ast.BinOp, 1, 2)
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
333 # node raises exception when given too many arguments
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
334 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
335 # node raises exception when not given enough arguments
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
336 self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
337 # node raises exception when given too many arguments
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
338 self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
339
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
340 # 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
341 x = ast.BinOp(left=1, op=2, right=3, lineno=0)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
342 self.assertEqual(x.left, 1)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
343 self.assertEqual(x.op, 2)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
344 self.assertEqual(x.right, 3)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
345 self.assertEqual(x.lineno, 0)
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
346
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
347 # Random kwargs also allowed
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
348 x = ast.BinOp(1, 2, 3, foobarbaz=42)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
349 self.assertEqual(x.foobarbaz, 42)
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
350
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
351 def test_no_fields(self):
46204
29ea507fd998 Make _fields attr for no fields consistent with _attributes attr.
Georg Brandl <georg@python.org>
parents: 46203
diff changeset
352 # 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
353 x = ast.Sub()
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
354 self.assertEqual(x._fields, ())
46204
29ea507fd998 Make _fields attr for no fields consistent with _attributes attr.
Georg Brandl <georg@python.org>
parents: 46203
diff changeset
355
46212
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
356 def test_pickling(self):
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
357 import pickle
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
358 mods = [pickle]
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
359 try:
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
360 import cPickle
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
361 mods.append(cPickle)
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
362 except ImportError:
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
363 pass
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
364 protocols = [0, 1, 2]
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
365 for mod in mods:
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
366 for protocol in protocols:
be18df634405 Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents: 46204
diff changeset
367 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
368 ast2 = mod.loads(mod.dumps(ast, protocol))
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
369 self.assertEqual(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
370
71452
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
371 def test_invalid_identitifer(self):
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
372 m = ast.Module([ast.Expr(ast.Name(u"x", ast.Load()))])
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
373 ast.fix_missing_locations(m)
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
374 with self.assertRaises(TypeError) as cm:
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
375 compile(m, "<test>", "exec")
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
376 self.assertIn("identifier must be of type str", str(cm.exception))
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
377
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
378 def test_invalid_string(self):
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
379 m = ast.Module([ast.Expr(ast.Str(43))])
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
380 ast.fix_missing_locations(m)
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
381 with self.assertRaises(TypeError) as cm:
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
382 compile(m, "<test>", "exec")
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
383 self.assertIn("string must be of type str or uni", str(cm.exception))
a21829180423 verify the types of AST strings and identifiers (closes #12609 and #12610)
Benjamin Peterson <benjamin@python.org>
parents: 71022
diff changeset
384
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
385
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
386 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
387
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
388 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
389 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
390 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
391 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
392
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
393 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
394 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
395 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
396 "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
397 "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
398 "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
399 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
400 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
401 "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
402 "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
403 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
404 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
405 "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
406 "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
407 "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
408 "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
409 "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
410 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
411
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
412 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
413 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
414 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
415 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
416 '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
417 '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
418 '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
419 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
420
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
421 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
422 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
423 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
424 [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
425 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
426 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
427 "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
428 "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
429 "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
430 "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
431 "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
432 "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
433 "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
434 "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
435 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
436
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
437 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
438 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
439 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
440 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
441 '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
442 '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
443 '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
444 )
67482
7ec21653a4d1 Merged revisions 87876-87877 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
445 # issue10869: do not increment lineno of root twice
7ec21653a4d1 Merged revisions 87876-87877 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
446 src = ast.parse('1 + 1', mode='eval')
7ec21653a4d1 Merged revisions 87876-87877 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
447 self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
7ec21653a4d1 Merged revisions 87876-87877 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
448 self.assertEqual(ast.dump(src, include_attributes=True),
7ec21653a4d1 Merged revisions 87876-87877 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
449 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
7ec21653a4d1 Merged revisions 87876-87877 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
450 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
7ec21653a4d1 Merged revisions 87876-87877 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
451 'col_offset=0))'
7ec21653a4d1 Merged revisions 87876-87877 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
452 )
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
453
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
454 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
455 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
456 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
457 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
458 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
459 '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
460
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
461 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
462 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
463 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
464 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
465 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
466 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
467 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
468 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
469 "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
470 )
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
471
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
472 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
473 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
474 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
475 '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
476
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
477 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
478 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
479 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
480 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
481 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
482
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
483 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
484 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
485 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
486 self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
50771
6e33aeaee77d Use assertRaises.
Georg Brandl <georg@python.org>
parents: 50765
diff changeset
487 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
488
47541
ed572d260fa7 Add the "ast" module, containing helpers to ease use of the "_ast" classes.
Georg Brandl <georg@python.org>
parents: 47507
diff changeset
489
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
490 def test_main():
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58472
diff changeset
491 with test_support.check_py3k_warnings(("backquote not supported",
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51809
diff changeset
492 SyntaxWarning)):
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51809
diff changeset
493 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
494
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
495 def main():
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
496 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
497 return
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
498 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
499 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
500 (eval_tests, "eval")):
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
501 print kind+"_results = ["
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
502 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
503 print repr(to_tuple(compile(s, "?", kind, 0x400)))+","
55651
1e2877026435 better col_offsets for "for" statements with tuple unpacking #6704
Benjamin Peterson <benjamin@python.org>
parents: 55362
diff changeset
504 print "]"
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
505 print "main()"
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
506 raise SystemExit
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
507 test_main()
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
508
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
509 #### EVERYTHING BELOW IS GENERATED #####
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
510 exec_results = [
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
511 ('Module', [('Expr', (1, 0), ('Name', (1, 0), 'None', ('Load',)))]),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
512 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]),
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
513 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',))], None, None, []), [('Pass', (1, 10))], [])]),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
514 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',))], None, None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [])]),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
515 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], 'args', None, []), [('Pass', (1, 14))], [])]),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
516 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, 'kwargs', []), [('Pass', (1, 17))], [])]),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
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))], [])]),
45447
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 39037
diff changeset
518 ('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))], [])]),
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
519 ('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [('Pass', (1, 17))], [])]),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
520 ('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
521 ('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
522 ('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
523 ('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
524 ('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
525 ('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
526 ('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
527 ('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
528 ('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
529 ('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
530 ('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
531 ('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
532 ('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
533 ('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
534 ('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
535 ('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
536 ('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
537 ('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
538 ('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
539 ('Module', [('Continue', (1, 0))]),
55651
1e2877026435 better col_offsets for "for" statements with tuple unpacking #6704
Benjamin Peterson <benjamin@python.org>
parents: 55362
diff changeset
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))], [])]),
1e2877026435 better col_offsets for "for" statements with tuple unpacking #6704
Benjamin Peterson <benjamin@python.org>
parents: 55362
diff changeset
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',)), [])]))]),
1e2877026435 better col_offsets for "for" statements with tuple unpacking #6704
Benjamin Peterson <benjamin@python.org>
parents: 55362
diff changeset
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',)), [])]))]),
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
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',)), [])]))]),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
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',)), [])]))]),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
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',))])]))]),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
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',)), [])]))]),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
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',))])]))]),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
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',)), [])]))]),
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
549 ]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
550 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
551 ('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
552 ]
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
553 eval_results = [
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
554 ('Expression', ('Name', (1, 0), 'None', ('Load',))),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
555 ('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
556 ('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
557 ('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
558 ('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
559 ('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
560 ('Expression', ('Dict', (1, 0), [], [])),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
561 ('Expression', ('Set', (1, 0), [('Name', (1, 1), 'None', ('Load',))])),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
562 ('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
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',))])])),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
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',))])])),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
565 ('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
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',)))),
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
567 ('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
568 ('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
569 ('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
570 ('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
571 ('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
572 ('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
573 ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
574 ('Expression', ('List', (1, 0), [], ('Load',))),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
575 ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
71019
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
576 ('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
3143cadb2f2d add more ast tests (closes #11302)
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
577 ('Expression', ('Tuple', (1, 0), [], ('Load',))),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
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)),
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
579 ]
46203
f051e7a80acc Convert test_ast to unittest and add a test for r62049.
Georg Brandl <georg@python.org>
parents: 46201
diff changeset
580 main()