Zachary Ware | 2b0a610 | 2014-07-16 14:26:09 -0500 | [diff] [blame] | 1 | """Tests for the unparse.py script in the Tools/parser directory.""" |
| 2 | |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | import test.support |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 5 | import pathlib |
Mark Dickinson | be4fb69 | 2012-06-23 09:27:47 +0100 | [diff] [blame] | 6 | import random |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 7 | import tokenize |
Mark Dickinson | be4fb69 | 2012-06-23 09:27:47 +0100 | [diff] [blame] | 8 | import ast |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 9 | |
Zachary Ware | 2b0a610 | 2014-07-16 14:26:09 -0500 | [diff] [blame] | 10 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 11 | def read_pyfile(filename): |
| 12 | """Read and return the contents of a Python source file (as a |
| 13 | string), taking into account the file encoding.""" |
| 14 | with open(filename, "rb") as pyfile: |
| 15 | encoding = tokenize.detect_encoding(pyfile.readline)[0] |
| 16 | with open(filename, "r", encoding=encoding) as pyfile: |
| 17 | source = pyfile.read() |
| 18 | return source |
| 19 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 20 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 21 | for_else = """\ |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 22 | def f(): |
| 23 | for x in range(10): |
| 24 | break |
| 25 | else: |
| 26 | y = 2 |
| 27 | z = 3 |
| 28 | """ |
| 29 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 30 | while_else = """\ |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 31 | def g(): |
| 32 | while True: |
| 33 | break |
| 34 | else: |
| 35 | y = 2 |
| 36 | z = 3 |
| 37 | """ |
| 38 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 39 | relative_import = """\ |
| 40 | from . import fred |
| 41 | from .. import barney |
| 42 | from .australia import shrimp as prawns |
| 43 | """ |
| 44 | |
| 45 | nonlocal_ex = """\ |
| 46 | def f(): |
| 47 | x = 1 |
| 48 | def g(): |
| 49 | nonlocal x |
| 50 | x = 2 |
| 51 | y = 7 |
| 52 | def h(): |
| 53 | nonlocal x, y |
| 54 | """ |
| 55 | |
| 56 | # also acts as test for 'except ... as ...' |
| 57 | raise_from = """\ |
| 58 | try: |
| 59 | 1 / 0 |
| 60 | except ZeroDivisionError as e: |
| 61 | raise ArithmeticError from e |
| 62 | """ |
| 63 | |
| 64 | class_decorator = """\ |
| 65 | @f1(arg) |
| 66 | @f2 |
| 67 | class Foo: pass |
| 68 | """ |
| 69 | |
Mark Dickinson | 8d6d760 | 2010-06-30 08:32:11 +0000 | [diff] [blame] | 70 | elif1 = """\ |
| 71 | if cond1: |
| 72 | suite1 |
| 73 | elif cond2: |
| 74 | suite2 |
| 75 | else: |
| 76 | suite3 |
| 77 | """ |
| 78 | |
| 79 | elif2 = """\ |
| 80 | if cond1: |
| 81 | suite1 |
| 82 | elif cond2: |
| 83 | suite2 |
| 84 | """ |
| 85 | |
Mark Dickinson | 81ad8cc | 2010-06-30 08:46:53 +0000 | [diff] [blame] | 86 | try_except_finally = """\ |
| 87 | try: |
| 88 | suite1 |
| 89 | except ex1: |
| 90 | suite2 |
| 91 | except ex2: |
| 92 | suite3 |
| 93 | else: |
| 94 | suite4 |
| 95 | finally: |
| 96 | suite5 |
| 97 | """ |
Mark Dickinson | 8d6d760 | 2010-06-30 08:32:11 +0000 | [diff] [blame] | 98 | |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 99 | with_simple = """\ |
| 100 | with f(): |
| 101 | suite1 |
| 102 | """ |
| 103 | |
| 104 | with_as = """\ |
| 105 | with f() as x: |
| 106 | suite1 |
| 107 | """ |
| 108 | |
| 109 | with_two_items = """\ |
| 110 | with f() as x, g() as y: |
| 111 | suite1 |
| 112 | """ |
| 113 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 114 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 115 | class ASTTestCase(unittest.TestCase): |
| 116 | def assertASTEqual(self, ast1, ast2): |
| 117 | self.assertEqual(ast.dump(ast1), ast.dump(ast2)) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 118 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 119 | def check_roundtrip(self, code1): |
| 120 | ast1 = ast.parse(code1) |
| 121 | code2 = ast.unparse(ast1) |
| 122 | ast2 = ast.parse(code2) |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 123 | self.assertASTEqual(ast1, ast2) |
| 124 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 125 | def check_invalid(self, node, raises=ValueError): |
| 126 | self.assertRaises(raises, ast.unparse, node) |
| 127 | |
| 128 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 129 | class UnparseTestCase(ASTTestCase): |
| 130 | # Tests for specific bugs found in earlier versions of unparse |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 131 | |
Eric V. Smith | 608adf9 | 2015-09-20 15:09:15 -0400 | [diff] [blame] | 132 | def test_fstrings(self): |
| 133 | # See issue 25180 |
| 134 | self.check_roundtrip(r"""f'{f"{0}"*3}'""") |
| 135 | self.check_roundtrip(r"""f'{f"{y}"*3}'""") |
Eric V. Smith | 608adf9 | 2015-09-20 15:09:15 -0400 | [diff] [blame] | 136 | |
Chih-Hsuan Yen | aaf47ca | 2019-05-27 01:08:20 +0800 | [diff] [blame] | 137 | def test_strings(self): |
| 138 | self.check_roundtrip("u'foo'") |
| 139 | self.check_roundtrip("r'foo'") |
| 140 | self.check_roundtrip("b'foo'") |
| 141 | |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 142 | def test_del_statement(self): |
| 143 | self.check_roundtrip("del x, y, z") |
| 144 | |
| 145 | def test_shifts(self): |
| 146 | self.check_roundtrip("45 << 2") |
| 147 | self.check_roundtrip("13 >> 7") |
| 148 | |
| 149 | def test_for_else(self): |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 150 | self.check_roundtrip(for_else) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 151 | |
| 152 | def test_while_else(self): |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 153 | self.check_roundtrip(while_else) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 154 | |
| 155 | def test_unary_parens(self): |
| 156 | self.check_roundtrip("(-1)**7") |
Mark Dickinson | cba8c10 | 2010-06-30 11:45:53 +0000 | [diff] [blame] | 157 | self.check_roundtrip("(-1.)**8") |
| 158 | self.check_roundtrip("(-1j)**6") |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 159 | self.check_roundtrip("not True or False") |
| 160 | self.check_roundtrip("True or not False") |
| 161 | |
Mark Dickinson | 3eb0290 | 2010-06-29 08:52:36 +0000 | [diff] [blame] | 162 | def test_integer_parens(self): |
| 163 | self.check_roundtrip("3 .__abs__()") |
| 164 | |
Mark Dickinson | 8042e28 | 2010-06-29 10:01:48 +0000 | [diff] [blame] | 165 | def test_huge_float(self): |
| 166 | self.check_roundtrip("1e1000") |
| 167 | self.check_roundtrip("-1e1000") |
Mark Dickinson | cba8c10 | 2010-06-30 11:45:53 +0000 | [diff] [blame] | 168 | self.check_roundtrip("1e1000j") |
| 169 | self.check_roundtrip("-1e1000j") |
| 170 | |
| 171 | def test_min_int(self): |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 172 | self.check_roundtrip(str(-(2 ** 31))) |
| 173 | self.check_roundtrip(str(-(2 ** 63))) |
Mark Dickinson | cba8c10 | 2010-06-30 11:45:53 +0000 | [diff] [blame] | 174 | |
| 175 | def test_imaginary_literals(self): |
| 176 | self.check_roundtrip("7j") |
| 177 | self.check_roundtrip("-7j") |
| 178 | self.check_roundtrip("0j") |
| 179 | self.check_roundtrip("-0j") |
Mark Dickinson | 8042e28 | 2010-06-29 10:01:48 +0000 | [diff] [blame] | 180 | |
| 181 | def test_lambda_parentheses(self): |
| 182 | self.check_roundtrip("(lambda: int)()") |
| 183 | |
Mark Dickinson | f5451e5 | 2010-06-28 20:09:18 +0000 | [diff] [blame] | 184 | def test_chained_comparisons(self): |
| 185 | self.check_roundtrip("1 < 4 <= 5") |
| 186 | self.check_roundtrip("a is b is c is not d") |
| 187 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 188 | def test_function_arguments(self): |
| 189 | self.check_roundtrip("def f(): pass") |
| 190 | self.check_roundtrip("def f(a): pass") |
| 191 | self.check_roundtrip("def f(b = 2): pass") |
| 192 | self.check_roundtrip("def f(a, b): pass") |
| 193 | self.check_roundtrip("def f(a, b = 2): pass") |
| 194 | self.check_roundtrip("def f(a = 5, b = 2): pass") |
| 195 | self.check_roundtrip("def f(*, a = 1, b = 2): pass") |
| 196 | self.check_roundtrip("def f(*, a = 1, b): pass") |
| 197 | self.check_roundtrip("def f(*, a, b = 2): pass") |
| 198 | self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass") |
| 199 | self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass") |
| 200 | self.check_roundtrip("def f(*args, **kwargs): pass") |
| 201 | |
| 202 | def test_relative_import(self): |
| 203 | self.check_roundtrip(relative_import) |
| 204 | |
| 205 | def test_nonlocal(self): |
| 206 | self.check_roundtrip(nonlocal_ex) |
| 207 | |
| 208 | def test_raise_from(self): |
| 209 | self.check_roundtrip(raise_from) |
| 210 | |
| 211 | def test_bytes(self): |
| 212 | self.check_roundtrip("b'123'") |
| 213 | |
| 214 | def test_annotations(self): |
| 215 | self.check_roundtrip("def f(a : int): pass") |
| 216 | self.check_roundtrip("def f(a: int = 5): pass") |
| 217 | self.check_roundtrip("def f(*args: [int]): pass") |
| 218 | self.check_roundtrip("def f(**kwargs: dict): pass") |
| 219 | self.check_roundtrip("def f() -> None: pass") |
| 220 | |
| 221 | def test_set_literal(self): |
| 222 | self.check_roundtrip("{'a', 'b', 'c'}") |
| 223 | |
| 224 | def test_set_comprehension(self): |
| 225 | self.check_roundtrip("{x for x in range(5)}") |
| 226 | |
| 227 | def test_dict_comprehension(self): |
| 228 | self.check_roundtrip("{x: x*x for x in range(10)}") |
| 229 | |
| 230 | def test_class_decorators(self): |
| 231 | self.check_roundtrip(class_decorator) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 232 | |
Mark Dickinson | 578aa56 | 2010-06-29 18:38:59 +0000 | [diff] [blame] | 233 | def test_class_definition(self): |
| 234 | self.check_roundtrip("class A(metaclass=type, *[], **{}): pass") |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 235 | |
Mark Dickinson | 8d6d760 | 2010-06-30 08:32:11 +0000 | [diff] [blame] | 236 | def test_elifs(self): |
| 237 | self.check_roundtrip(elif1) |
| 238 | self.check_roundtrip(elif2) |
| 239 | |
Mark Dickinson | 81ad8cc | 2010-06-30 08:46:53 +0000 | [diff] [blame] | 240 | def test_try_except_finally(self): |
| 241 | self.check_roundtrip(try_except_finally) |
| 242 | |
Mark Dickinson | 1b2e944 | 2012-05-06 17:27:39 +0100 | [diff] [blame] | 243 | def test_starred_assignment(self): |
| 244 | self.check_roundtrip("a, *b, c = seq") |
| 245 | self.check_roundtrip("a, (*b, c) = seq") |
| 246 | self.check_roundtrip("a, *b[0], c = seq") |
| 247 | self.check_roundtrip("a, *(b, c) = seq") |
| 248 | |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 249 | def test_with_simple(self): |
| 250 | self.check_roundtrip(with_simple) |
| 251 | |
| 252 | def test_with_as(self): |
| 253 | self.check_roundtrip(with_as) |
| 254 | |
| 255 | def test_with_two_items(self): |
| 256 | self.check_roundtrip(with_two_items) |
| 257 | |
Berker Peksag | d66dd5c | 2016-03-06 16:50:15 +0200 | [diff] [blame] | 258 | def test_dict_unpacking_in_dict(self): |
| 259 | # See issue 26489 |
| 260 | self.check_roundtrip(r"""{**{'y': 2}, 'x': 1}""") |
| 261 | self.check_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""") |
| 262 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 263 | def test_invalid_raise(self): |
| 264 | self.check_invalid(ast.Raise(exc=None, cause=ast.Name(id="X"))) |
| 265 | |
| 266 | def test_invalid_fstring_constant(self): |
| 267 | self.check_invalid(ast.JoinedStr(values=[ast.Constant(value=100)])) |
| 268 | |
| 269 | def test_invalid_fstring_conversion(self): |
| 270 | self.check_invalid( |
| 271 | ast.FormattedValue( |
| 272 | value=ast.Constant(value="a", kind=None), |
| 273 | conversion=ord("Y"), # random character |
| 274 | format_spec=None, |
| 275 | ) |
| 276 | ) |
| 277 | |
| 278 | def test_invalid_set(self): |
| 279 | self.check_invalid(ast.Set(elts=[])) |
| 280 | |
Batuhan Taşkaya | 7b35bef | 2020-01-02 21:20:04 +0300 | [diff] [blame] | 281 | def test_invalid_yield_from(self): |
| 282 | self.check_invalid(ast.YieldFrom(value=None)) |
Mark Dickinson | 1b2e944 | 2012-05-06 17:27:39 +0100 | [diff] [blame] | 283 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 284 | class DirectoryTestCase(ASTTestCase): |
| 285 | """Test roundtrip behaviour on all files in Lib and Lib/test.""" |
| 286 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 287 | lib_dir = pathlib.Path(__file__).parent / ".." |
| 288 | test_directories = (lib_dir, lib_dir / "test") |
| 289 | skip_files = {"test_fstring.py"} |
Pablo Galindo | 23a226b | 2019-12-29 19:20:55 +0000 | [diff] [blame] | 290 | run_always_files = {"test_grammar.py", "test_syntax.py", "test_compile.py", |
| 291 | "test_ast.py", "test_asdl_parser.py"} |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 292 | |
Pablo Galindo | ac22911 | 2019-12-09 17:57:50 +0000 | [diff] [blame] | 293 | _files_to_test = None |
| 294 | |
| 295 | @classmethod |
| 296 | def files_to_test(cls): |
| 297 | |
| 298 | if cls._files_to_test is not None: |
| 299 | return cls._files_to_test |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 300 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 301 | items = [ |
| 302 | item.resolve() |
Pablo Galindo | ac22911 | 2019-12-09 17:57:50 +0000 | [diff] [blame] | 303 | for directory in cls.test_directories |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 304 | for item in directory.glob("*.py") |
| 305 | if not item.name.startswith("bad") |
| 306 | ] |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 307 | |
Mark Dickinson | be4fb69 | 2012-06-23 09:27:47 +0100 | [diff] [blame] | 308 | # Test limited subset of files unless the 'cpu' resource is specified. |
| 309 | if not test.support.is_resource_enabled("cpu"): |
Pablo Galindo | be287c3 | 2019-12-29 20:18:36 +0000 | [diff] [blame] | 310 | |
| 311 | tests_to_run_always = {item for item in items if |
| 312 | item.name in cls.run_always_files} |
| 313 | |
Pablo Galindo | 23a226b | 2019-12-29 19:20:55 +0000 | [diff] [blame] | 314 | items = set(random.sample(items, 10)) |
| 315 | |
Pablo Galindo | be287c3 | 2019-12-29 20:18:36 +0000 | [diff] [blame] | 316 | # Make sure that at least tests that heavily use grammar features are |
| 317 | # always considered in order to reduce the chance of missing something. |
| 318 | items = list(items | tests_to_run_always) |
Pablo Galindo | ac22911 | 2019-12-09 17:57:50 +0000 | [diff] [blame] | 319 | |
| 320 | # bpo-31174: Store the names sample to always test the same files. |
| 321 | # It prevents false alarms when hunting reference leaks. |
| 322 | cls._files_to_test = items |
| 323 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 324 | return items |
Victor Stinner | 8e482be | 2017-10-24 03:33:36 -0700 | [diff] [blame] | 325 | |
| 326 | def test_files(self): |
Pablo Galindo | ac22911 | 2019-12-09 17:57:50 +0000 | [diff] [blame] | 327 | for item in self.files_to_test(): |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 328 | if test.support.verbose: |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 329 | print(f"Testing {item.absolute()}") |
Eric V. Smith | 06cf601 | 2016-09-03 12:33:38 -0400 | [diff] [blame] | 330 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 331 | # Some f-strings are not correctly round-tripped by |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 332 | # Tools/parser/unparse.py. See issue 28002 for details. |
| 333 | # We need to skip files that contain such f-strings. |
| 334 | if item.name in self.skip_files: |
Eric V. Smith | 06cf601 | 2016-09-03 12:33:38 -0400 | [diff] [blame] | 335 | if test.support.verbose: |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 336 | print(f"Skipping {item.absolute()}: see issue 28002") |
Eric V. Smith | 06cf601 | 2016-09-03 12:33:38 -0400 | [diff] [blame] | 337 | continue |
| 338 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 339 | with self.subTest(filename=item): |
| 340 | source = read_pyfile(item) |
Yury Selivanov | d04e417 | 2016-09-09 11:14:59 -0700 | [diff] [blame] | 341 | self.check_roundtrip(source) |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 342 | |
| 343 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 344 | if __name__ == "__main__": |
Zachary Ware | 2b0a610 | 2014-07-16 14:26:09 -0500 | [diff] [blame] | 345 | unittest.main() |