1 | n/a | """Tests for the unparse.py script in the Tools/parser directory.""" |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | import test.support |
---|
5 | n/a | import io |
---|
6 | n/a | import os |
---|
7 | n/a | import random |
---|
8 | n/a | import tokenize |
---|
9 | n/a | import ast |
---|
10 | n/a | |
---|
11 | n/a | from test.test_tools import basepath, toolsdir, skip_if_missing |
---|
12 | n/a | |
---|
13 | n/a | skip_if_missing() |
---|
14 | n/a | |
---|
15 | n/a | parser_path = os.path.join(toolsdir, "parser") |
---|
16 | n/a | |
---|
17 | n/a | with test.support.DirsOnSysPath(parser_path): |
---|
18 | n/a | import unparse |
---|
19 | n/a | |
---|
20 | n/a | def read_pyfile(filename): |
---|
21 | n/a | """Read and return the contents of a Python source file (as a |
---|
22 | n/a | string), taking into account the file encoding.""" |
---|
23 | n/a | with open(filename, "rb") as pyfile: |
---|
24 | n/a | encoding = tokenize.detect_encoding(pyfile.readline)[0] |
---|
25 | n/a | with open(filename, "r", encoding=encoding) as pyfile: |
---|
26 | n/a | source = pyfile.read() |
---|
27 | n/a | return source |
---|
28 | n/a | |
---|
29 | n/a | for_else = """\ |
---|
30 | n/a | def f(): |
---|
31 | n/a | for x in range(10): |
---|
32 | n/a | break |
---|
33 | n/a | else: |
---|
34 | n/a | y = 2 |
---|
35 | n/a | z = 3 |
---|
36 | n/a | """ |
---|
37 | n/a | |
---|
38 | n/a | while_else = """\ |
---|
39 | n/a | def g(): |
---|
40 | n/a | while True: |
---|
41 | n/a | break |
---|
42 | n/a | else: |
---|
43 | n/a | y = 2 |
---|
44 | n/a | z = 3 |
---|
45 | n/a | """ |
---|
46 | n/a | |
---|
47 | n/a | relative_import = """\ |
---|
48 | n/a | from . import fred |
---|
49 | n/a | from .. import barney |
---|
50 | n/a | from .australia import shrimp as prawns |
---|
51 | n/a | """ |
---|
52 | n/a | |
---|
53 | n/a | nonlocal_ex = """\ |
---|
54 | n/a | def f(): |
---|
55 | n/a | x = 1 |
---|
56 | n/a | def g(): |
---|
57 | n/a | nonlocal x |
---|
58 | n/a | x = 2 |
---|
59 | n/a | y = 7 |
---|
60 | n/a | def h(): |
---|
61 | n/a | nonlocal x, y |
---|
62 | n/a | """ |
---|
63 | n/a | |
---|
64 | n/a | # also acts as test for 'except ... as ...' |
---|
65 | n/a | raise_from = """\ |
---|
66 | n/a | try: |
---|
67 | n/a | 1 / 0 |
---|
68 | n/a | except ZeroDivisionError as e: |
---|
69 | n/a | raise ArithmeticError from e |
---|
70 | n/a | """ |
---|
71 | n/a | |
---|
72 | n/a | class_decorator = """\ |
---|
73 | n/a | @f1(arg) |
---|
74 | n/a | @f2 |
---|
75 | n/a | class Foo: pass |
---|
76 | n/a | """ |
---|
77 | n/a | |
---|
78 | n/a | elif1 = """\ |
---|
79 | n/a | if cond1: |
---|
80 | n/a | suite1 |
---|
81 | n/a | elif cond2: |
---|
82 | n/a | suite2 |
---|
83 | n/a | else: |
---|
84 | n/a | suite3 |
---|
85 | n/a | """ |
---|
86 | n/a | |
---|
87 | n/a | elif2 = """\ |
---|
88 | n/a | if cond1: |
---|
89 | n/a | suite1 |
---|
90 | n/a | elif cond2: |
---|
91 | n/a | suite2 |
---|
92 | n/a | """ |
---|
93 | n/a | |
---|
94 | n/a | try_except_finally = """\ |
---|
95 | n/a | try: |
---|
96 | n/a | suite1 |
---|
97 | n/a | except ex1: |
---|
98 | n/a | suite2 |
---|
99 | n/a | except ex2: |
---|
100 | n/a | suite3 |
---|
101 | n/a | else: |
---|
102 | n/a | suite4 |
---|
103 | n/a | finally: |
---|
104 | n/a | suite5 |
---|
105 | n/a | """ |
---|
106 | n/a | |
---|
107 | n/a | with_simple = """\ |
---|
108 | n/a | with f(): |
---|
109 | n/a | suite1 |
---|
110 | n/a | """ |
---|
111 | n/a | |
---|
112 | n/a | with_as = """\ |
---|
113 | n/a | with f() as x: |
---|
114 | n/a | suite1 |
---|
115 | n/a | """ |
---|
116 | n/a | |
---|
117 | n/a | with_two_items = """\ |
---|
118 | n/a | with f() as x, g() as y: |
---|
119 | n/a | suite1 |
---|
120 | n/a | """ |
---|
121 | n/a | |
---|
122 | n/a | class ASTTestCase(unittest.TestCase): |
---|
123 | n/a | def assertASTEqual(self, ast1, ast2): |
---|
124 | n/a | self.assertEqual(ast.dump(ast1), ast.dump(ast2)) |
---|
125 | n/a | |
---|
126 | n/a | def check_roundtrip(self, code1, filename="internal"): |
---|
127 | n/a | ast1 = compile(code1, filename, "exec", ast.PyCF_ONLY_AST) |
---|
128 | n/a | unparse_buffer = io.StringIO() |
---|
129 | n/a | unparse.Unparser(ast1, unparse_buffer) |
---|
130 | n/a | code2 = unparse_buffer.getvalue() |
---|
131 | n/a | ast2 = compile(code2, filename, "exec", ast.PyCF_ONLY_AST) |
---|
132 | n/a | self.assertASTEqual(ast1, ast2) |
---|
133 | n/a | |
---|
134 | n/a | class UnparseTestCase(ASTTestCase): |
---|
135 | n/a | # Tests for specific bugs found in earlier versions of unparse |
---|
136 | n/a | |
---|
137 | n/a | def test_fstrings(self): |
---|
138 | n/a | # See issue 25180 |
---|
139 | n/a | self.check_roundtrip(r"""f'{f"{0}"*3}'""") |
---|
140 | n/a | self.check_roundtrip(r"""f'{f"{y}"*3}'""") |
---|
141 | n/a | |
---|
142 | n/a | def test_del_statement(self): |
---|
143 | n/a | self.check_roundtrip("del x, y, z") |
---|
144 | n/a | |
---|
145 | n/a | def test_shifts(self): |
---|
146 | n/a | self.check_roundtrip("45 << 2") |
---|
147 | n/a | self.check_roundtrip("13 >> 7") |
---|
148 | n/a | |
---|
149 | n/a | def test_for_else(self): |
---|
150 | n/a | self.check_roundtrip(for_else) |
---|
151 | n/a | |
---|
152 | n/a | def test_while_else(self): |
---|
153 | n/a | self.check_roundtrip(while_else) |
---|
154 | n/a | |
---|
155 | n/a | def test_unary_parens(self): |
---|
156 | n/a | self.check_roundtrip("(-1)**7") |
---|
157 | n/a | self.check_roundtrip("(-1.)**8") |
---|
158 | n/a | self.check_roundtrip("(-1j)**6") |
---|
159 | n/a | self.check_roundtrip("not True or False") |
---|
160 | n/a | self.check_roundtrip("True or not False") |
---|
161 | n/a | |
---|
162 | n/a | def test_integer_parens(self): |
---|
163 | n/a | self.check_roundtrip("3 .__abs__()") |
---|
164 | n/a | |
---|
165 | n/a | def test_huge_float(self): |
---|
166 | n/a | self.check_roundtrip("1e1000") |
---|
167 | n/a | self.check_roundtrip("-1e1000") |
---|
168 | n/a | self.check_roundtrip("1e1000j") |
---|
169 | n/a | self.check_roundtrip("-1e1000j") |
---|
170 | n/a | |
---|
171 | n/a | def test_min_int(self): |
---|
172 | n/a | self.check_roundtrip(str(-2**31)) |
---|
173 | n/a | self.check_roundtrip(str(-2**63)) |
---|
174 | n/a | |
---|
175 | n/a | def test_imaginary_literals(self): |
---|
176 | n/a | self.check_roundtrip("7j") |
---|
177 | n/a | self.check_roundtrip("-7j") |
---|
178 | n/a | self.check_roundtrip("0j") |
---|
179 | n/a | self.check_roundtrip("-0j") |
---|
180 | n/a | |
---|
181 | n/a | def test_lambda_parentheses(self): |
---|
182 | n/a | self.check_roundtrip("(lambda: int)()") |
---|
183 | n/a | |
---|
184 | n/a | def test_chained_comparisons(self): |
---|
185 | n/a | self.check_roundtrip("1 < 4 <= 5") |
---|
186 | n/a | self.check_roundtrip("a is b is c is not d") |
---|
187 | n/a | |
---|
188 | n/a | def test_function_arguments(self): |
---|
189 | n/a | self.check_roundtrip("def f(): pass") |
---|
190 | n/a | self.check_roundtrip("def f(a): pass") |
---|
191 | n/a | self.check_roundtrip("def f(b = 2): pass") |
---|
192 | n/a | self.check_roundtrip("def f(a, b): pass") |
---|
193 | n/a | self.check_roundtrip("def f(a, b = 2): pass") |
---|
194 | n/a | self.check_roundtrip("def f(a = 5, b = 2): pass") |
---|
195 | n/a | self.check_roundtrip("def f(*, a = 1, b = 2): pass") |
---|
196 | n/a | self.check_roundtrip("def f(*, a = 1, b): pass") |
---|
197 | n/a | self.check_roundtrip("def f(*, a, b = 2): pass") |
---|
198 | n/a | self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass") |
---|
199 | n/a | self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass") |
---|
200 | n/a | self.check_roundtrip("def f(*args, **kwargs): pass") |
---|
201 | n/a | |
---|
202 | n/a | def test_relative_import(self): |
---|
203 | n/a | self.check_roundtrip(relative_import) |
---|
204 | n/a | |
---|
205 | n/a | def test_nonlocal(self): |
---|
206 | n/a | self.check_roundtrip(nonlocal_ex) |
---|
207 | n/a | |
---|
208 | n/a | def test_raise_from(self): |
---|
209 | n/a | self.check_roundtrip(raise_from) |
---|
210 | n/a | |
---|
211 | n/a | def test_bytes(self): |
---|
212 | n/a | self.check_roundtrip("b'123'") |
---|
213 | n/a | |
---|
214 | n/a | def test_annotations(self): |
---|
215 | n/a | self.check_roundtrip("def f(a : int): pass") |
---|
216 | n/a | self.check_roundtrip("def f(a: int = 5): pass") |
---|
217 | n/a | self.check_roundtrip("def f(*args: [int]): pass") |
---|
218 | n/a | self.check_roundtrip("def f(**kwargs: dict): pass") |
---|
219 | n/a | self.check_roundtrip("def f() -> None: pass") |
---|
220 | n/a | |
---|
221 | n/a | def test_set_literal(self): |
---|
222 | n/a | self.check_roundtrip("{'a', 'b', 'c'}") |
---|
223 | n/a | |
---|
224 | n/a | def test_set_comprehension(self): |
---|
225 | n/a | self.check_roundtrip("{x for x in range(5)}") |
---|
226 | n/a | |
---|
227 | n/a | def test_dict_comprehension(self): |
---|
228 | n/a | self.check_roundtrip("{x: x*x for x in range(10)}") |
---|
229 | n/a | |
---|
230 | n/a | def test_class_decorators(self): |
---|
231 | n/a | self.check_roundtrip(class_decorator) |
---|
232 | n/a | |
---|
233 | n/a | def test_class_definition(self): |
---|
234 | n/a | self.check_roundtrip("class A(metaclass=type, *[], **{}): pass") |
---|
235 | n/a | |
---|
236 | n/a | def test_elifs(self): |
---|
237 | n/a | self.check_roundtrip(elif1) |
---|
238 | n/a | self.check_roundtrip(elif2) |
---|
239 | n/a | |
---|
240 | n/a | def test_try_except_finally(self): |
---|
241 | n/a | self.check_roundtrip(try_except_finally) |
---|
242 | n/a | |
---|
243 | n/a | def test_starred_assignment(self): |
---|
244 | n/a | self.check_roundtrip("a, *b, c = seq") |
---|
245 | n/a | self.check_roundtrip("a, (*b, c) = seq") |
---|
246 | n/a | self.check_roundtrip("a, *b[0], c = seq") |
---|
247 | n/a | self.check_roundtrip("a, *(b, c) = seq") |
---|
248 | n/a | |
---|
249 | n/a | def test_with_simple(self): |
---|
250 | n/a | self.check_roundtrip(with_simple) |
---|
251 | n/a | |
---|
252 | n/a | def test_with_as(self): |
---|
253 | n/a | self.check_roundtrip(with_as) |
---|
254 | n/a | |
---|
255 | n/a | def test_with_two_items(self): |
---|
256 | n/a | self.check_roundtrip(with_two_items) |
---|
257 | n/a | |
---|
258 | n/a | def test_dict_unpacking_in_dict(self): |
---|
259 | n/a | # See issue 26489 |
---|
260 | n/a | self.check_roundtrip(r"""{**{'y': 2}, 'x': 1}""") |
---|
261 | n/a | self.check_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""") |
---|
262 | n/a | |
---|
263 | n/a | |
---|
264 | n/a | class DirectoryTestCase(ASTTestCase): |
---|
265 | n/a | """Test roundtrip behaviour on all files in Lib and Lib/test.""" |
---|
266 | n/a | |
---|
267 | n/a | # test directories, relative to the root of the distribution |
---|
268 | n/a | test_directories = 'Lib', os.path.join('Lib', 'test') |
---|
269 | n/a | |
---|
270 | n/a | def test_files(self): |
---|
271 | n/a | # get names of files to test |
---|
272 | n/a | |
---|
273 | n/a | names = [] |
---|
274 | n/a | for d in self.test_directories: |
---|
275 | n/a | test_dir = os.path.join(basepath, d) |
---|
276 | n/a | for n in os.listdir(test_dir): |
---|
277 | n/a | if n.endswith('.py') and not n.startswith('bad'): |
---|
278 | n/a | names.append(os.path.join(test_dir, n)) |
---|
279 | n/a | |
---|
280 | n/a | # Test limited subset of files unless the 'cpu' resource is specified. |
---|
281 | n/a | if not test.support.is_resource_enabled("cpu"): |
---|
282 | n/a | names = random.sample(names, 10) |
---|
283 | n/a | |
---|
284 | n/a | for filename in names: |
---|
285 | n/a | if test.support.verbose: |
---|
286 | n/a | print('Testing %s' % filename) |
---|
287 | n/a | |
---|
288 | n/a | # Some f-strings are not correctly round-tripped by |
---|
289 | n/a | # Tools/parser/unparse.py. See issue 28002 for details. |
---|
290 | n/a | # We need to skip files that contain such f-strings. |
---|
291 | n/a | if os.path.basename(filename) in ('test_fstring.py', ): |
---|
292 | n/a | if test.support.verbose: |
---|
293 | n/a | print(f'Skipping {filename}: see issue 28002') |
---|
294 | n/a | continue |
---|
295 | n/a | |
---|
296 | n/a | with self.subTest(filename=filename): |
---|
297 | n/a | source = read_pyfile(filename) |
---|
298 | n/a | self.check_roundtrip(source) |
---|
299 | n/a | |
---|
300 | n/a | |
---|
301 | n/a | if __name__ == '__main__': |
---|
302 | n/a | unittest.main() |
---|