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