Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1 | import ast |
| 2 | import dis |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 3 | import os |
| 4 | import sys |
| 5 | import unittest |
Benjamin Peterson | 9ed3743 | 2012-07-08 11:13:36 -0700 | [diff] [blame] | 6 | import weakref |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 7 | from textwrap import dedent |
Benjamin Peterson | 9ed3743 | 2012-07-08 11:13:36 -0700 | [diff] [blame] | 8 | |
| 9 | from test import support |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 10 | |
| 11 | def to_tuple(t): |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 12 | if t is None or isinstance(t, (str, int, complex)): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 13 | return t |
| 14 | elif isinstance(t, list): |
| 15 | return [to_tuple(e) for e in t] |
| 16 | result = [t.__class__.__name__] |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 17 | if hasattr(t, 'lineno') and hasattr(t, 'col_offset'): |
| 18 | result.append((t.lineno, t.col_offset)) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 19 | if t._fields is None: |
| 20 | return tuple(result) |
| 21 | for f in t._fields: |
| 22 | result.append(to_tuple(getattr(t, f))) |
| 23 | return tuple(result) |
| 24 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 25 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 26 | # These tests are compiled through "exec" |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 27 | # There should be at least one test per statement |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 28 | exec_tests = [ |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 29 | # None |
| 30 | "None", |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 31 | # Module docstring |
| 32 | "'module docstring'", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 33 | # FunctionDef |
| 34 | "def f(): pass", |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 35 | # FunctionDef with docstring |
| 36 | "def f(): 'function docstring'", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 37 | # FunctionDef with arg |
| 38 | "def f(a): pass", |
| 39 | # FunctionDef with arg and default value |
| 40 | "def f(a=0): pass", |
| 41 | # FunctionDef with varargs |
| 42 | "def f(*args): pass", |
| 43 | # FunctionDef with kwargs |
| 44 | "def f(**kwargs): pass", |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 45 | # FunctionDef with all kind of args and docstring |
| 46 | "def f(a, b=1, c=None, d=[], e={}, *args, f=42, **kwargs): 'doc for f()'", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 47 | # ClassDef |
| 48 | "class C:pass", |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 49 | # ClassDef with docstring |
| 50 | "class C: 'docstring for class C'", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 51 | # ClassDef, new style class |
| 52 | "class C(object): pass", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 53 | # Return |
| 54 | "def f():return 1", |
| 55 | # Delete |
| 56 | "del v", |
| 57 | # Assign |
| 58 | "v = 1", |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 59 | "a,b = c", |
| 60 | "(a,b) = c", |
| 61 | "[a,b] = c", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 62 | # AugAssign |
| 63 | "v += 1", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 64 | # For |
| 65 | "for v in v:pass", |
| 66 | # While |
| 67 | "while v:pass", |
| 68 | # If |
| 69 | "if v:pass", |
Benjamin Peterson | aeabd5f | 2011-05-27 15:02:03 -0500 | [diff] [blame] | 70 | # With |
| 71 | "with x as y: pass", |
| 72 | "with x as y, z as q: pass", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 73 | # Raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 74 | "raise Exception('string')", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 75 | # TryExcept |
| 76 | "try:\n pass\nexcept Exception:\n pass", |
| 77 | # TryFinally |
| 78 | "try:\n pass\nfinally:\n pass", |
| 79 | # Assert |
| 80 | "assert v", |
| 81 | # Import |
| 82 | "import sys", |
| 83 | # ImportFrom |
| 84 | "from sys import v", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 85 | # Global |
| 86 | "global v", |
| 87 | # Expr |
| 88 | "1", |
| 89 | # Pass, |
| 90 | "pass", |
| 91 | # Break |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 92 | "for v in v:break", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 93 | # Continue |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 94 | "for v in v:continue", |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 95 | # for statements with naked tuples (see http://bugs.python.org/issue6704) |
| 96 | "for a,b in c: pass", |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 97 | "for (a,b) in c: pass", |
| 98 | "for [a,b] in c: pass", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 99 | # Multiline generator expression (test for .lineno & .col_offset) |
| 100 | """( |
| 101 | ( |
| 102 | Aa |
| 103 | , |
| 104 | Bb |
| 105 | ) |
| 106 | for |
| 107 | Aa |
| 108 | , |
| 109 | Bb in Cc |
| 110 | )""", |
| 111 | # dictcomp |
| 112 | "{a : b for w in x for m in p if g}", |
| 113 | # dictcomp with naked tuple |
| 114 | "{a : b for v,w in x}", |
| 115 | # setcomp |
| 116 | "{r for l in x if g}", |
| 117 | # setcomp with naked tuple |
| 118 | "{r for l,m in x}", |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 119 | # AsyncFunctionDef |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 120 | "async def f():\n 'async function'\n await something()", |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 121 | # AsyncFor |
| 122 | "async def f():\n async for e in i: 1\n else: 2", |
| 123 | # AsyncWith |
| 124 | "async def f():\n async with a as b: 1", |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 125 | # PEP 448: Additional Unpacking Generalizations |
| 126 | "{**{1:2}, 2:3}", |
| 127 | "{*{1, 2}, 3}", |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 128 | # Asynchronous comprehensions |
| 129 | "async def f():\n [i async for b in c]", |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 130 | # Decorated FunctionDef |
| 131 | "@deco1\n@deco2()\ndef f(): pass", |
| 132 | # Decorated AsyncFunctionDef |
| 133 | "@deco1\n@deco2()\nasync def f(): pass", |
| 134 | # Decorated ClassDef |
| 135 | "@deco1\n@deco2()\nclass C: pass", |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 136 | # Decorator with generator argument |
| 137 | "@deco(a for a in b)\ndef f(): pass", |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 138 | # Simple assignment expression |
| 139 | "(a := 1)", |
| 140 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 141 | ] |
| 142 | |
| 143 | # These are compiled through "single" |
| 144 | # because of overlap with "eval", it just tests what |
| 145 | # can't be tested with "eval" |
| 146 | single_tests = [ |
| 147 | "1+2" |
| 148 | ] |
| 149 | |
| 150 | # These are compiled through "eval" |
| 151 | # It should test all expressions |
| 152 | eval_tests = [ |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 153 | # None |
| 154 | "None", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 155 | # BoolOp |
| 156 | "a and b", |
| 157 | # BinOp |
| 158 | "a + b", |
| 159 | # UnaryOp |
| 160 | "not v", |
| 161 | # Lambda |
| 162 | "lambda:None", |
| 163 | # Dict |
| 164 | "{ 1:2 }", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 165 | # Empty dict |
| 166 | "{}", |
| 167 | # Set |
| 168 | "{None,}", |
| 169 | # Multiline dict (test for .lineno & .col_offset) |
| 170 | """{ |
| 171 | 1 |
| 172 | : |
| 173 | 2 |
| 174 | }""", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 175 | # ListComp |
| 176 | "[a for b in c if d]", |
| 177 | # GeneratorExp |
| 178 | "(a for b in c if d)", |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 179 | # Comprehensions with multiple for targets |
| 180 | "[(a,b) for a,b in c]", |
| 181 | "[(a,b) for (a,b) in c]", |
| 182 | "[(a,b) for [a,b] in c]", |
| 183 | "{(a,b) for a,b in c}", |
| 184 | "{(a,b) for (a,b) in c}", |
| 185 | "{(a,b) for [a,b] in c}", |
| 186 | "((a,b) for a,b in c)", |
| 187 | "((a,b) for (a,b) in c)", |
| 188 | "((a,b) for [a,b] in c)", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 189 | # Yield - yield expressions can't work outside a function |
| 190 | # |
| 191 | # Compare |
| 192 | "1 < 2 < 3", |
| 193 | # Call |
| 194 | "f(1,2,c=3,*d,**e)", |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 195 | # Call with a generator argument |
| 196 | "f(a for a in b)", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 197 | # Num |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 198 | "10", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 199 | # Str |
| 200 | "'string'", |
| 201 | # Attribute |
| 202 | "a.b", |
| 203 | # Subscript |
| 204 | "a[b:c]", |
| 205 | # Name |
| 206 | "v", |
| 207 | # List |
| 208 | "[1,2,3]", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 209 | # Empty list |
| 210 | "[]", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 211 | # Tuple |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 212 | "1,2,3", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 213 | # Tuple |
| 214 | "(1,2,3)", |
| 215 | # Empty tuple |
| 216 | "()", |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 217 | # Combination |
| 218 | "a.b.c.d(a.b[1:2])", |
| 219 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 220 | ] |
| 221 | |
| 222 | # TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension |
| 223 | # excepthandler, arguments, keywords, alias |
| 224 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 225 | class AST_Tests(unittest.TestCase): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 226 | |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 227 | def _assertTrueorder(self, ast_node, parent_pos): |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 228 | if not isinstance(ast_node, ast.AST) or ast_node._fields is None: |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 229 | return |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 230 | if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)): |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 231 | node_pos = (ast_node.lineno, ast_node.col_offset) |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 232 | self.assertGreaterEqual(node_pos, parent_pos) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 233 | parent_pos = (ast_node.lineno, ast_node.col_offset) |
| 234 | for name in ast_node._fields: |
| 235 | value = getattr(ast_node, name) |
| 236 | if isinstance(value, list): |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 237 | first_pos = parent_pos |
| 238 | if value and name == 'decorator_list': |
| 239 | first_pos = (value[0].lineno, value[0].col_offset) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 240 | for child in value: |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 241 | self._assertTrueorder(child, first_pos) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 242 | elif value is not None: |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 243 | self._assertTrueorder(value, parent_pos) |
Tim Peters | 5ddfe41 | 2006-03-01 23:02:57 +0000 | [diff] [blame] | 244 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 245 | def test_AST_objects(self): |
| 246 | x = ast.AST() |
| 247 | self.assertEqual(x._fields, ()) |
Benjamin Peterson | 7e0dbfb | 2012-03-12 09:46:44 -0700 | [diff] [blame] | 248 | x.foobar = 42 |
| 249 | self.assertEqual(x.foobar, 42) |
| 250 | self.assertEqual(x.__dict__["foobar"], 42) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 251 | |
| 252 | with self.assertRaises(AttributeError): |
| 253 | x.vararg |
| 254 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 255 | with self.assertRaises(TypeError): |
| 256 | # "_ast.AST constructor takes 0 positional arguments" |
| 257 | ast.AST(2) |
| 258 | |
Benjamin Peterson | 9ed3743 | 2012-07-08 11:13:36 -0700 | [diff] [blame] | 259 | def test_AST_garbage_collection(self): |
| 260 | class X: |
| 261 | pass |
| 262 | a = ast.AST() |
| 263 | a.x = X() |
| 264 | a.x.a = a |
| 265 | ref = weakref.ref(a.x) |
| 266 | del a |
| 267 | support.gc_collect() |
| 268 | self.assertIsNone(ref()) |
| 269 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 270 | def test_snippets(self): |
| 271 | for input, output, kind in ((exec_tests, exec_results, "exec"), |
| 272 | (single_tests, single_results, "single"), |
| 273 | (eval_tests, eval_results, "eval")): |
| 274 | for i, o in zip(input, output): |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 275 | with self.subTest(action="parsing", input=i): |
| 276 | ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST) |
| 277 | self.assertEqual(to_tuple(ast_tree), o) |
| 278 | self._assertTrueorder(ast_tree, (0, 0)) |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 279 | with self.subTest(action="compiling", input=i, kind=kind): |
| 280 | compile(ast_tree, "?", kind) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 281 | |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 282 | def test_ast_validation(self): |
| 283 | # compile() is the only function that calls PyAST_Validate |
| 284 | snippets_to_validate = exec_tests + single_tests + eval_tests |
| 285 | for snippet in snippets_to_validate: |
| 286 | tree = ast.parse(snippet) |
| 287 | compile(tree, '<string>', 'exec') |
| 288 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 289 | def test_slice(self): |
| 290 | slc = ast.parse("x[::]").body[0].value.slice |
| 291 | self.assertIsNone(slc.upper) |
| 292 | self.assertIsNone(slc.lower) |
| 293 | self.assertIsNone(slc.step) |
| 294 | |
| 295 | def test_from_import(self): |
| 296 | im = ast.parse("from . import y").body[0] |
| 297 | self.assertIsNone(im.module) |
| 298 | |
Benjamin Peterson | a4e4e35 | 2012-03-22 08:19:04 -0400 | [diff] [blame] | 299 | def test_non_interned_future_from_ast(self): |
| 300 | mod = ast.parse("from __future__ import division") |
| 301 | self.assertIsInstance(mod.body[0], ast.ImportFrom) |
| 302 | mod.body[0].module = " __future__ ".strip() |
| 303 | compile(mod, "<test>", "exec") |
| 304 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 305 | def test_base_classes(self): |
| 306 | self.assertTrue(issubclass(ast.For, ast.stmt)) |
| 307 | self.assertTrue(issubclass(ast.Name, ast.expr)) |
| 308 | self.assertTrue(issubclass(ast.stmt, ast.AST)) |
| 309 | self.assertTrue(issubclass(ast.expr, ast.AST)) |
| 310 | self.assertTrue(issubclass(ast.comprehension, ast.AST)) |
| 311 | self.assertTrue(issubclass(ast.Gt, ast.AST)) |
| 312 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 313 | def test_field_attr_existence(self): |
| 314 | for name, item in ast.__dict__.items(): |
| 315 | if isinstance(item, type) and name != 'AST' and name[0].isupper(): |
| 316 | x = item() |
| 317 | if isinstance(x, ast.AST): |
| 318 | self.assertEqual(type(x._fields), tuple) |
| 319 | |
| 320 | def test_arguments(self): |
| 321 | x = ast.arguments() |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 322 | self.assertEqual(x._fields, ('args', 'posonlyargs', 'vararg', 'kwonlyargs', |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 323 | 'kw_defaults', 'kwarg', 'defaults')) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 324 | |
| 325 | with self.assertRaises(AttributeError): |
| 326 | x.vararg |
| 327 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 328 | x = ast.arguments(*range(1, 8)) |
| 329 | self.assertEqual(x.vararg, 3) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 330 | |
| 331 | def test_field_attr_writable(self): |
| 332 | x = ast.Num() |
| 333 | # We can assign to _fields |
| 334 | x._fields = 666 |
| 335 | self.assertEqual(x._fields, 666) |
| 336 | |
| 337 | def test_classattrs(self): |
| 338 | x = ast.Num() |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 339 | self.assertEqual(x._fields, ('value', 'kind')) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 340 | |
| 341 | with self.assertRaises(AttributeError): |
| 342 | x.value |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 343 | |
| 344 | with self.assertRaises(AttributeError): |
| 345 | x.n |
| 346 | |
| 347 | x = ast.Num(42) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 348 | self.assertEqual(x.value, 42) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 349 | self.assertEqual(x.n, 42) |
| 350 | |
| 351 | with self.assertRaises(AttributeError): |
| 352 | x.lineno |
| 353 | |
| 354 | with self.assertRaises(AttributeError): |
| 355 | x.foobar |
| 356 | |
| 357 | x = ast.Num(lineno=2) |
| 358 | self.assertEqual(x.lineno, 2) |
| 359 | |
| 360 | x = ast.Num(42, lineno=0) |
| 361 | self.assertEqual(x.lineno, 0) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 362 | self.assertEqual(x._fields, ('value', 'kind')) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 363 | self.assertEqual(x.value, 42) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 364 | self.assertEqual(x.n, 42) |
| 365 | |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 366 | self.assertRaises(TypeError, ast.Num, 1, None, 2) |
| 367 | self.assertRaises(TypeError, ast.Num, 1, None, 2, lineno=0) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 368 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 369 | self.assertEqual(ast.Num(42).n, 42) |
| 370 | self.assertEqual(ast.Num(4.25).n, 4.25) |
| 371 | self.assertEqual(ast.Num(4.25j).n, 4.25j) |
| 372 | self.assertEqual(ast.Str('42').s, '42') |
| 373 | self.assertEqual(ast.Bytes(b'42').s, b'42') |
| 374 | self.assertIs(ast.NameConstant(True).value, True) |
| 375 | self.assertIs(ast.NameConstant(False).value, False) |
| 376 | self.assertIs(ast.NameConstant(None).value, None) |
| 377 | |
| 378 | self.assertEqual(ast.Constant(42).value, 42) |
| 379 | self.assertEqual(ast.Constant(4.25).value, 4.25) |
| 380 | self.assertEqual(ast.Constant(4.25j).value, 4.25j) |
| 381 | self.assertEqual(ast.Constant('42').value, '42') |
| 382 | self.assertEqual(ast.Constant(b'42').value, b'42') |
| 383 | self.assertIs(ast.Constant(True).value, True) |
| 384 | self.assertIs(ast.Constant(False).value, False) |
| 385 | self.assertIs(ast.Constant(None).value, None) |
| 386 | self.assertIs(ast.Constant(...).value, ...) |
| 387 | |
| 388 | def test_realtype(self): |
| 389 | self.assertEqual(type(ast.Num(42)), ast.Constant) |
| 390 | self.assertEqual(type(ast.Num(4.25)), ast.Constant) |
| 391 | self.assertEqual(type(ast.Num(4.25j)), ast.Constant) |
| 392 | self.assertEqual(type(ast.Str('42')), ast.Constant) |
| 393 | self.assertEqual(type(ast.Bytes(b'42')), ast.Constant) |
| 394 | self.assertEqual(type(ast.NameConstant(True)), ast.Constant) |
| 395 | self.assertEqual(type(ast.NameConstant(False)), ast.Constant) |
| 396 | self.assertEqual(type(ast.NameConstant(None)), ast.Constant) |
| 397 | self.assertEqual(type(ast.Ellipsis()), ast.Constant) |
| 398 | |
| 399 | def test_isinstance(self): |
| 400 | self.assertTrue(isinstance(ast.Num(42), ast.Num)) |
| 401 | self.assertTrue(isinstance(ast.Num(4.2), ast.Num)) |
| 402 | self.assertTrue(isinstance(ast.Num(4.2j), ast.Num)) |
| 403 | self.assertTrue(isinstance(ast.Str('42'), ast.Str)) |
| 404 | self.assertTrue(isinstance(ast.Bytes(b'42'), ast.Bytes)) |
| 405 | self.assertTrue(isinstance(ast.NameConstant(True), ast.NameConstant)) |
| 406 | self.assertTrue(isinstance(ast.NameConstant(False), ast.NameConstant)) |
| 407 | self.assertTrue(isinstance(ast.NameConstant(None), ast.NameConstant)) |
| 408 | self.assertTrue(isinstance(ast.Ellipsis(), ast.Ellipsis)) |
| 409 | |
| 410 | self.assertTrue(isinstance(ast.Constant(42), ast.Num)) |
| 411 | self.assertTrue(isinstance(ast.Constant(4.2), ast.Num)) |
| 412 | self.assertTrue(isinstance(ast.Constant(4.2j), ast.Num)) |
| 413 | self.assertTrue(isinstance(ast.Constant('42'), ast.Str)) |
| 414 | self.assertTrue(isinstance(ast.Constant(b'42'), ast.Bytes)) |
| 415 | self.assertTrue(isinstance(ast.Constant(True), ast.NameConstant)) |
| 416 | self.assertTrue(isinstance(ast.Constant(False), ast.NameConstant)) |
| 417 | self.assertTrue(isinstance(ast.Constant(None), ast.NameConstant)) |
| 418 | self.assertTrue(isinstance(ast.Constant(...), ast.Ellipsis)) |
| 419 | |
| 420 | self.assertFalse(isinstance(ast.Str('42'), ast.Num)) |
| 421 | self.assertFalse(isinstance(ast.Num(42), ast.Str)) |
| 422 | self.assertFalse(isinstance(ast.Str('42'), ast.Bytes)) |
| 423 | self.assertFalse(isinstance(ast.Num(42), ast.NameConstant)) |
| 424 | self.assertFalse(isinstance(ast.Num(42), ast.Ellipsis)) |
Anthony Sottile | 7417622 | 2019-01-18 11:30:28 -0800 | [diff] [blame] | 425 | self.assertFalse(isinstance(ast.NameConstant(True), ast.Num)) |
| 426 | self.assertFalse(isinstance(ast.NameConstant(False), ast.Num)) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 427 | |
| 428 | self.assertFalse(isinstance(ast.Constant('42'), ast.Num)) |
| 429 | self.assertFalse(isinstance(ast.Constant(42), ast.Str)) |
| 430 | self.assertFalse(isinstance(ast.Constant('42'), ast.Bytes)) |
| 431 | self.assertFalse(isinstance(ast.Constant(42), ast.NameConstant)) |
| 432 | self.assertFalse(isinstance(ast.Constant(42), ast.Ellipsis)) |
Anthony Sottile | 7417622 | 2019-01-18 11:30:28 -0800 | [diff] [blame] | 433 | self.assertFalse(isinstance(ast.Constant(True), ast.Num)) |
| 434 | self.assertFalse(isinstance(ast.Constant(False), ast.Num)) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 435 | |
| 436 | self.assertFalse(isinstance(ast.Constant(), ast.Num)) |
| 437 | self.assertFalse(isinstance(ast.Constant(), ast.Str)) |
| 438 | self.assertFalse(isinstance(ast.Constant(), ast.Bytes)) |
| 439 | self.assertFalse(isinstance(ast.Constant(), ast.NameConstant)) |
| 440 | self.assertFalse(isinstance(ast.Constant(), ast.Ellipsis)) |
| 441 | |
Serhiy Storchaka | 6015cc5 | 2018-10-28 13:43:03 +0200 | [diff] [blame] | 442 | class S(str): pass |
| 443 | self.assertTrue(isinstance(ast.Constant(S('42')), ast.Str)) |
| 444 | self.assertFalse(isinstance(ast.Constant(S('42')), ast.Num)) |
| 445 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 446 | def test_subclasses(self): |
| 447 | class N(ast.Num): |
| 448 | def __init__(self, *args, **kwargs): |
| 449 | super().__init__(*args, **kwargs) |
| 450 | self.z = 'spam' |
| 451 | class N2(ast.Num): |
| 452 | pass |
| 453 | |
| 454 | n = N(42) |
| 455 | self.assertEqual(n.n, 42) |
| 456 | self.assertEqual(n.z, 'spam') |
| 457 | self.assertEqual(type(n), N) |
| 458 | self.assertTrue(isinstance(n, N)) |
| 459 | self.assertTrue(isinstance(n, ast.Num)) |
| 460 | self.assertFalse(isinstance(n, N2)) |
| 461 | self.assertFalse(isinstance(ast.Num(42), N)) |
| 462 | n = N(n=42) |
| 463 | self.assertEqual(n.n, 42) |
| 464 | self.assertEqual(type(n), N) |
| 465 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 466 | def test_module(self): |
| 467 | body = [ast.Num(42)] |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 468 | x = ast.Module(body, []) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 469 | self.assertEqual(x.body, body) |
| 470 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 471 | def test_nodeclasses(self): |
Florent Xicluna | 992d9e0 | 2011-11-11 19:35:42 +0100 | [diff] [blame] | 472 | # Zero arguments constructor explicitly allowed |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 473 | x = ast.BinOp() |
| 474 | self.assertEqual(x._fields, ('left', 'op', 'right')) |
| 475 | |
| 476 | # Random attribute allowed too |
| 477 | x.foobarbaz = 5 |
| 478 | self.assertEqual(x.foobarbaz, 5) |
| 479 | |
| 480 | n1 = ast.Num(1) |
| 481 | n3 = ast.Num(3) |
| 482 | addop = ast.Add() |
| 483 | x = ast.BinOp(n1, addop, n3) |
| 484 | self.assertEqual(x.left, n1) |
| 485 | self.assertEqual(x.op, addop) |
| 486 | self.assertEqual(x.right, n3) |
Benjamin Peterson | 68b543a | 2011-06-27 17:51:18 -0500 | [diff] [blame] | 487 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 488 | x = ast.BinOp(1, 2, 3) |
| 489 | self.assertEqual(x.left, 1) |
| 490 | self.assertEqual(x.op, 2) |
| 491 | self.assertEqual(x.right, 3) |
| 492 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 493 | x = ast.BinOp(1, 2, 3, lineno=0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 494 | self.assertEqual(x.left, 1) |
| 495 | self.assertEqual(x.op, 2) |
| 496 | self.assertEqual(x.right, 3) |
| 497 | self.assertEqual(x.lineno, 0) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 498 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 499 | # node raises exception when given too many arguments |
| 500 | self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 501 | # node raises exception when given too many arguments |
| 502 | self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 503 | |
| 504 | # can set attributes through kwargs too |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 505 | x = ast.BinOp(left=1, op=2, right=3, lineno=0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 506 | self.assertEqual(x.left, 1) |
| 507 | self.assertEqual(x.op, 2) |
| 508 | self.assertEqual(x.right, 3) |
| 509 | self.assertEqual(x.lineno, 0) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 510 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 511 | # Random kwargs also allowed |
| 512 | x = ast.BinOp(1, 2, 3, foobarbaz=42) |
| 513 | self.assertEqual(x.foobarbaz, 42) |
| 514 | |
| 515 | def test_no_fields(self): |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 516 | # this used to fail because Sub._fields was None |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 517 | x = ast.Sub() |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 518 | self.assertEqual(x._fields, ()) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 519 | |
| 520 | def test_pickling(self): |
| 521 | import pickle |
| 522 | mods = [pickle] |
| 523 | try: |
| 524 | import cPickle |
| 525 | mods.append(cPickle) |
| 526 | except ImportError: |
| 527 | pass |
| 528 | protocols = [0, 1, 2] |
| 529 | for mod in mods: |
| 530 | for protocol in protocols: |
| 531 | for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests): |
| 532 | ast2 = mod.loads(mod.dumps(ast, protocol)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 533 | self.assertEqual(to_tuple(ast2), to_tuple(ast)) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 534 | |
Benjamin Peterson | 5b06681 | 2010-11-20 01:38:49 +0000 | [diff] [blame] | 535 | def test_invalid_sum(self): |
| 536 | pos = dict(lineno=2, col_offset=3) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 537 | m = ast.Module([ast.Expr(ast.expr(**pos), **pos)], []) |
Benjamin Peterson | 5b06681 | 2010-11-20 01:38:49 +0000 | [diff] [blame] | 538 | with self.assertRaises(TypeError) as cm: |
| 539 | compile(m, "<test>", "exec") |
| 540 | self.assertIn("but got <_ast.expr", str(cm.exception)) |
| 541 | |
Benjamin Peterson | 2193d2b | 2011-07-22 10:50:23 -0500 | [diff] [blame] | 542 | def test_invalid_identitifer(self): |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 543 | m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))], []) |
Benjamin Peterson | 2193d2b | 2011-07-22 10:50:23 -0500 | [diff] [blame] | 544 | ast.fix_missing_locations(m) |
| 545 | with self.assertRaises(TypeError) as cm: |
| 546 | compile(m, "<test>", "exec") |
| 547 | self.assertIn("identifier must be of type str", str(cm.exception)) |
| 548 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 549 | def test_empty_yield_from(self): |
| 550 | # Issue 16546: yield from value is not optional. |
| 551 | empty_yield_from = ast.parse("def f():\n yield from g()") |
| 552 | empty_yield_from.body[0].body[0].value.value = None |
| 553 | with self.assertRaises(ValueError) as cm: |
| 554 | compile(empty_yield_from, "<test>", "exec") |
| 555 | self.assertIn("field value is required", str(cm.exception)) |
| 556 | |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 557 | @support.cpython_only |
| 558 | def test_issue31592(self): |
| 559 | # There shouldn't be an assertion failure in case of a bad |
| 560 | # unicodedata.normalize(). |
| 561 | import unicodedata |
| 562 | def bad_normalize(*args): |
| 563 | return None |
| 564 | with support.swap_attr(unicodedata, 'normalize', bad_normalize): |
| 565 | self.assertRaises(TypeError, ast.parse, '\u03D5') |
| 566 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 567 | |
| 568 | class ASTHelpers_Test(unittest.TestCase): |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 569 | maxDiff = None |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 570 | |
| 571 | def test_parse(self): |
| 572 | a = ast.parse('foo(1 + 1)') |
| 573 | b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST) |
| 574 | self.assertEqual(ast.dump(a), ast.dump(b)) |
| 575 | |
Benjamin Peterson | 2e2c903 | 2012-09-02 14:23:15 -0400 | [diff] [blame] | 576 | def test_parse_in_error(self): |
| 577 | try: |
| 578 | 1/0 |
| 579 | except Exception: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 580 | with self.assertRaises(SyntaxError) as e: |
| 581 | ast.literal_eval(r"'\U'") |
| 582 | self.assertIsNotNone(e.exception.__context__) |
Benjamin Peterson | 2e2c903 | 2012-09-02 14:23:15 -0400 | [diff] [blame] | 583 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 584 | def test_dump(self): |
| 585 | node = ast.parse('spam(eggs, "and cheese")') |
| 586 | self.assertEqual(ast.dump(node), |
| 587 | "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), " |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 588 | "args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese', kind=None)], " |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 589 | "keywords=[]))], type_ignores=[])" |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 590 | ) |
| 591 | self.assertEqual(ast.dump(node, annotate_fields=False), |
| 592 | "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), " |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 593 | "Constant('and cheese', None)], []))], [])" |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 594 | ) |
| 595 | self.assertEqual(ast.dump(node, include_attributes=True), |
| 596 | "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), " |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 597 | "lineno=1, col_offset=0, end_lineno=1, end_col_offset=4), " |
| 598 | "args=[Name(id='eggs', ctx=Load(), lineno=1, col_offset=5, " |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 599 | "end_lineno=1, end_col_offset=9), Constant(value='and cheese', kind=None, " |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 600 | "lineno=1, col_offset=11, end_lineno=1, end_col_offset=23)], keywords=[], " |
| 601 | "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24), " |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 602 | "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)], type_ignores=[])" |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 603 | ) |
| 604 | |
| 605 | def test_copy_location(self): |
| 606 | src = ast.parse('1 + 1', mode='eval') |
| 607 | src.body.right = ast.copy_location(ast.Num(2), src.body.right) |
| 608 | self.assertEqual(ast.dump(src, include_attributes=True), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 609 | 'Expression(body=BinOp(left=Constant(value=1, kind=None, lineno=1, col_offset=0, ' |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 610 | 'end_lineno=1, end_col_offset=1), op=Add(), right=Constant(value=2, ' |
| 611 | 'lineno=1, col_offset=4, end_lineno=1, end_col_offset=5), lineno=1, ' |
| 612 | 'col_offset=0, end_lineno=1, end_col_offset=5))' |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 613 | ) |
| 614 | |
| 615 | def test_fix_missing_locations(self): |
| 616 | src = ast.parse('write("spam")') |
| 617 | src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()), |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 618 | [ast.Str('eggs')], []))) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 619 | self.assertEqual(src, ast.fix_missing_locations(src)) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 620 | self.maxDiff = None |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 621 | self.assertEqual(ast.dump(src, include_attributes=True), |
| 622 | "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), " |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 623 | "lineno=1, col_offset=0, end_lineno=1, end_col_offset=5), " |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 624 | "args=[Constant(value='spam', kind=None, lineno=1, col_offset=6, end_lineno=1, " |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 625 | "end_col_offset=12)], keywords=[], lineno=1, col_offset=0, end_lineno=1, " |
| 626 | "end_col_offset=13), lineno=1, col_offset=0, end_lineno=1, " |
| 627 | "end_col_offset=13), Expr(value=Call(func=Name(id='spam', ctx=Load(), " |
| 628 | "lineno=1, col_offset=0, end_lineno=1, end_col_offset=0), " |
| 629 | "args=[Constant(value='eggs', lineno=1, col_offset=0, end_lineno=1, " |
| 630 | "end_col_offset=0)], keywords=[], lineno=1, col_offset=0, end_lineno=1, " |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 631 | "end_col_offset=0), lineno=1, col_offset=0, end_lineno=1, end_col_offset=0)], " |
| 632 | "type_ignores=[])" |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 633 | ) |
| 634 | |
| 635 | def test_increment_lineno(self): |
| 636 | src = ast.parse('1 + 1', mode='eval') |
| 637 | self.assertEqual(ast.increment_lineno(src, n=3), src) |
| 638 | self.assertEqual(ast.dump(src, include_attributes=True), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 639 | 'Expression(body=BinOp(left=Constant(value=1, kind=None, lineno=4, col_offset=0, ' |
| 640 | 'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, kind=None, ' |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 641 | 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, ' |
| 642 | 'col_offset=0, end_lineno=4, end_col_offset=5))' |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 643 | ) |
Georg Brandl | 619e7ba | 2011-01-09 07:38:51 +0000 | [diff] [blame] | 644 | # issue10869: do not increment lineno of root twice |
Georg Brandl | efb6902 | 2011-01-09 07:50:48 +0000 | [diff] [blame] | 645 | src = ast.parse('1 + 1', mode='eval') |
Georg Brandl | 619e7ba | 2011-01-09 07:38:51 +0000 | [diff] [blame] | 646 | self.assertEqual(ast.increment_lineno(src.body, n=3), src.body) |
| 647 | self.assertEqual(ast.dump(src, include_attributes=True), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 648 | 'Expression(body=BinOp(left=Constant(value=1, kind=None, lineno=4, col_offset=0, ' |
| 649 | 'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, kind=None, ' |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 650 | 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, ' |
| 651 | 'col_offset=0, end_lineno=4, end_col_offset=5))' |
Georg Brandl | 619e7ba | 2011-01-09 07:38:51 +0000 | [diff] [blame] | 652 | ) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 653 | |
| 654 | def test_iter_fields(self): |
| 655 | node = ast.parse('foo()', mode='eval') |
| 656 | d = dict(ast.iter_fields(node.body)) |
| 657 | self.assertEqual(d.pop('func').id, 'foo') |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 658 | self.assertEqual(d, {'keywords': [], 'args': []}) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 659 | |
| 660 | def test_iter_child_nodes(self): |
| 661 | node = ast.parse("spam(23, 42, eggs='leek')", mode='eval') |
| 662 | self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4) |
| 663 | iterator = ast.iter_child_nodes(node.body) |
| 664 | self.assertEqual(next(iterator).id, 'spam') |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 665 | self.assertEqual(next(iterator).value, 23) |
| 666 | self.assertEqual(next(iterator).value, 42) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 667 | self.assertEqual(ast.dump(next(iterator)), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 668 | "keyword(arg='eggs', value=Constant(value='leek', kind=None))" |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 669 | ) |
| 670 | |
| 671 | def test_get_docstring(self): |
Serhiy Storchaka | 08f127a | 2018-06-15 11:05:15 +0300 | [diff] [blame] | 672 | node = ast.parse('"""line one\n line two"""') |
| 673 | self.assertEqual(ast.get_docstring(node), |
| 674 | 'line one\nline two') |
| 675 | |
| 676 | node = ast.parse('class foo:\n """line one\n line two"""') |
| 677 | self.assertEqual(ast.get_docstring(node.body[0]), |
| 678 | 'line one\nline two') |
| 679 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 680 | node = ast.parse('def foo():\n """line one\n line two"""') |
| 681 | self.assertEqual(ast.get_docstring(node.body[0]), |
| 682 | 'line one\nline two') |
| 683 | |
Yury Selivanov | 2f07a66 | 2015-07-23 08:54:35 +0300 | [diff] [blame] | 684 | node = ast.parse('async def foo():\n """spam\n ham"""') |
| 685 | self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham') |
Serhiy Storchaka | 08f127a | 2018-06-15 11:05:15 +0300 | [diff] [blame] | 686 | |
| 687 | def test_get_docstring_none(self): |
Matthias Bussonnier | 41cea70 | 2017-02-23 22:44:19 -0800 | [diff] [blame] | 688 | self.assertIsNone(ast.get_docstring(ast.parse(''))) |
Serhiy Storchaka | 08f127a | 2018-06-15 11:05:15 +0300 | [diff] [blame] | 689 | node = ast.parse('x = "not docstring"') |
| 690 | self.assertIsNone(ast.get_docstring(node)) |
| 691 | node = ast.parse('def foo():\n pass') |
| 692 | self.assertIsNone(ast.get_docstring(node)) |
| 693 | |
| 694 | node = ast.parse('class foo:\n pass') |
| 695 | self.assertIsNone(ast.get_docstring(node.body[0])) |
| 696 | node = ast.parse('class foo:\n x = "not docstring"') |
| 697 | self.assertIsNone(ast.get_docstring(node.body[0])) |
| 698 | node = ast.parse('class foo:\n def bar(self): pass') |
| 699 | self.assertIsNone(ast.get_docstring(node.body[0])) |
| 700 | |
| 701 | node = ast.parse('def foo():\n pass') |
| 702 | self.assertIsNone(ast.get_docstring(node.body[0])) |
| 703 | node = ast.parse('def foo():\n x = "not docstring"') |
| 704 | self.assertIsNone(ast.get_docstring(node.body[0])) |
| 705 | |
| 706 | node = ast.parse('async def foo():\n pass') |
| 707 | self.assertIsNone(ast.get_docstring(node.body[0])) |
| 708 | node = ast.parse('async def foo():\n x = "not docstring"') |
| 709 | self.assertIsNone(ast.get_docstring(node.body[0])) |
Yury Selivanov | 2f07a66 | 2015-07-23 08:54:35 +0300 | [diff] [blame] | 710 | |
Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 711 | def test_multi_line_docstring_col_offset_and_lineno_issue16806(self): |
| 712 | node = ast.parse( |
| 713 | '"""line one\nline two"""\n\n' |
| 714 | 'def foo():\n """line one\n line two"""\n\n' |
| 715 | ' def bar():\n """line one\n line two"""\n' |
| 716 | ' """line one\n line two"""\n' |
| 717 | '"""line one\nline two"""\n\n' |
| 718 | ) |
| 719 | self.assertEqual(node.body[0].col_offset, 0) |
| 720 | self.assertEqual(node.body[0].lineno, 1) |
| 721 | self.assertEqual(node.body[1].body[0].col_offset, 2) |
| 722 | self.assertEqual(node.body[1].body[0].lineno, 5) |
| 723 | self.assertEqual(node.body[1].body[1].body[0].col_offset, 4) |
| 724 | self.assertEqual(node.body[1].body[1].body[0].lineno, 9) |
| 725 | self.assertEqual(node.body[1].body[2].col_offset, 2) |
| 726 | self.assertEqual(node.body[1].body[2].lineno, 11) |
| 727 | self.assertEqual(node.body[2].col_offset, 0) |
| 728 | self.assertEqual(node.body[2].lineno, 13) |
| 729 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 730 | def test_literal_eval(self): |
| 731 | self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3]) |
| 732 | self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42}) |
| 733 | self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None)) |
Benjamin Peterson | 3e74289 | 2010-07-11 12:59:24 +0000 | [diff] [blame] | 734 | self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3}) |
Benjamin Peterson | 5ef96e5 | 2010-07-11 23:06:06 +0000 | [diff] [blame] | 735 | self.assertEqual(ast.literal_eval('b"hi"'), b"hi") |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 736 | self.assertRaises(ValueError, ast.literal_eval, 'foo()') |
Serhiy Storchaka | d8ac4d1 | 2018-01-04 11:15:39 +0200 | [diff] [blame] | 737 | self.assertEqual(ast.literal_eval('6'), 6) |
| 738 | self.assertEqual(ast.literal_eval('+6'), 6) |
Raymond Hettinger | bc95973 | 2010-10-08 00:47:45 +0000 | [diff] [blame] | 739 | self.assertEqual(ast.literal_eval('-6'), -6) |
Raymond Hettinger | bc95973 | 2010-10-08 00:47:45 +0000 | [diff] [blame] | 740 | self.assertEqual(ast.literal_eval('3.25'), 3.25) |
Serhiy Storchaka | d8ac4d1 | 2018-01-04 11:15:39 +0200 | [diff] [blame] | 741 | self.assertEqual(ast.literal_eval('+3.25'), 3.25) |
| 742 | self.assertEqual(ast.literal_eval('-3.25'), -3.25) |
| 743 | self.assertEqual(repr(ast.literal_eval('-0.0')), '-0.0') |
| 744 | self.assertRaises(ValueError, ast.literal_eval, '++6') |
| 745 | self.assertRaises(ValueError, ast.literal_eval, '+True') |
| 746 | self.assertRaises(ValueError, ast.literal_eval, '2+3') |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 747 | |
Serhiy Storchaka | d8ac4d1 | 2018-01-04 11:15:39 +0200 | [diff] [blame] | 748 | def test_literal_eval_complex(self): |
| 749 | # Issue #4907 |
| 750 | self.assertEqual(ast.literal_eval('6j'), 6j) |
| 751 | self.assertEqual(ast.literal_eval('-6j'), -6j) |
| 752 | self.assertEqual(ast.literal_eval('6.75j'), 6.75j) |
| 753 | self.assertEqual(ast.literal_eval('-6.75j'), -6.75j) |
| 754 | self.assertEqual(ast.literal_eval('3+6j'), 3+6j) |
| 755 | self.assertEqual(ast.literal_eval('-3+6j'), -3+6j) |
| 756 | self.assertEqual(ast.literal_eval('3-6j'), 3-6j) |
| 757 | self.assertEqual(ast.literal_eval('-3-6j'), -3-6j) |
| 758 | self.assertEqual(ast.literal_eval('3.25+6.75j'), 3.25+6.75j) |
| 759 | self.assertEqual(ast.literal_eval('-3.25+6.75j'), -3.25+6.75j) |
| 760 | self.assertEqual(ast.literal_eval('3.25-6.75j'), 3.25-6.75j) |
| 761 | self.assertEqual(ast.literal_eval('-3.25-6.75j'), -3.25-6.75j) |
| 762 | self.assertEqual(ast.literal_eval('(3+6j)'), 3+6j) |
| 763 | self.assertRaises(ValueError, ast.literal_eval, '-6j+3') |
| 764 | self.assertRaises(ValueError, ast.literal_eval, '-6j+3j') |
| 765 | self.assertRaises(ValueError, ast.literal_eval, '3+-6j') |
| 766 | self.assertRaises(ValueError, ast.literal_eval, '3+(0+6j)') |
| 767 | self.assertRaises(ValueError, ast.literal_eval, '-(3+6j)') |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 768 | |
Amaury Forgeot d'Arc | 58e8761 | 2011-11-22 21:51:55 +0100 | [diff] [blame] | 769 | def test_bad_integer(self): |
| 770 | # issue13436: Bad error message with invalid numeric values |
| 771 | body = [ast.ImportFrom(module='time', |
| 772 | names=[ast.alias(name='sleep')], |
| 773 | level=None, |
| 774 | lineno=None, col_offset=None)] |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 775 | mod = ast.Module(body, []) |
Amaury Forgeot d'Arc | 58e8761 | 2011-11-22 21:51:55 +0100 | [diff] [blame] | 776 | with self.assertRaises(ValueError) as cm: |
| 777 | compile(mod, 'test', 'exec') |
| 778 | self.assertIn("invalid integer value: None", str(cm.exception)) |
| 779 | |
Berker Peksag | 0a5bd51 | 2016-04-29 19:50:02 +0300 | [diff] [blame] | 780 | def test_level_as_none(self): |
| 781 | body = [ast.ImportFrom(module='time', |
| 782 | names=[ast.alias(name='sleep')], |
| 783 | level=None, |
| 784 | lineno=0, col_offset=0)] |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 785 | mod = ast.Module(body, []) |
Berker Peksag | 0a5bd51 | 2016-04-29 19:50:02 +0300 | [diff] [blame] | 786 | code = compile(mod, 'test', 'exec') |
| 787 | ns = {} |
| 788 | exec(code, ns) |
| 789 | self.assertIn('sleep', ns) |
| 790 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 791 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 792 | class ASTValidatorTests(unittest.TestCase): |
| 793 | |
| 794 | def mod(self, mod, msg=None, mode="exec", *, exc=ValueError): |
| 795 | mod.lineno = mod.col_offset = 0 |
| 796 | ast.fix_missing_locations(mod) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 797 | if msg is None: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 798 | compile(mod, "<test>", mode) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 799 | else: |
| 800 | with self.assertRaises(exc) as cm: |
| 801 | compile(mod, "<test>", mode) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 802 | self.assertIn(msg, str(cm.exception)) |
| 803 | |
| 804 | def expr(self, node, msg=None, *, exc=ValueError): |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 805 | mod = ast.Module([ast.Expr(node)], []) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 806 | self.mod(mod, msg, exc=exc) |
| 807 | |
| 808 | def stmt(self, stmt, msg=None): |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 809 | mod = ast.Module([stmt], []) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 810 | self.mod(mod, msg) |
| 811 | |
| 812 | def test_module(self): |
| 813 | m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))]) |
| 814 | self.mod(m, "must have Load context", "single") |
| 815 | m = ast.Expression(ast.Name("x", ast.Store())) |
| 816 | self.mod(m, "must have Load context", "eval") |
| 817 | |
| 818 | def _check_arguments(self, fac, check): |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 819 | def arguments(args=None, posonlyargs=None, vararg=None, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 820 | kwonlyargs=None, kwarg=None, |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 821 | defaults=None, kw_defaults=None): |
| 822 | if args is None: |
| 823 | args = [] |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 824 | if posonlyargs is None: |
| 825 | posonlyargs = [] |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 826 | if kwonlyargs is None: |
| 827 | kwonlyargs = [] |
| 828 | if defaults is None: |
| 829 | defaults = [] |
| 830 | if kw_defaults is None: |
| 831 | kw_defaults = [] |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 832 | args = ast.arguments(args, posonlyargs, vararg, kwonlyargs, |
| 833 | kw_defaults, kwarg, defaults) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 834 | return fac(args) |
| 835 | args = [ast.arg("x", ast.Name("x", ast.Store()))] |
| 836 | check(arguments(args=args), "must have Load context") |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 837 | check(arguments(posonlyargs=args), "must have Load context") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 838 | check(arguments(kwonlyargs=args), "must have Load context") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 839 | check(arguments(defaults=[ast.Num(3)]), |
| 840 | "more positional defaults than args") |
| 841 | check(arguments(kw_defaults=[ast.Num(4)]), |
| 842 | "length of kwonlyargs is not the same as kw_defaults") |
| 843 | args = [ast.arg("x", ast.Name("x", ast.Load()))] |
| 844 | check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]), |
| 845 | "must have Load context") |
| 846 | args = [ast.arg("a", ast.Name("x", ast.Load())), |
| 847 | ast.arg("b", ast.Name("y", ast.Load()))] |
| 848 | check(arguments(kwonlyargs=args, |
| 849 | kw_defaults=[None, ast.Name("x", ast.Store())]), |
| 850 | "must have Load context") |
| 851 | |
| 852 | def test_funcdef(self): |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 853 | a = ast.arguments([], [], None, [], [], None, []) |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 854 | f = ast.FunctionDef("x", a, [], [], None) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 855 | self.stmt(f, "empty body on FunctionDef") |
| 856 | f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())], |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 857 | None) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 858 | self.stmt(f, "must have Load context") |
| 859 | f = ast.FunctionDef("x", a, [ast.Pass()], [], |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 860 | ast.Name("x", ast.Store())) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 861 | self.stmt(f, "must have Load context") |
| 862 | def fac(args): |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 863 | return ast.FunctionDef("x", args, [ast.Pass()], [], None) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 864 | self._check_arguments(fac, self.stmt) |
| 865 | |
| 866 | def test_classdef(self): |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 867 | def cls(bases=None, keywords=None, body=None, decorator_list=None): |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 868 | if bases is None: |
| 869 | bases = [] |
| 870 | if keywords is None: |
| 871 | keywords = [] |
| 872 | if body is None: |
| 873 | body = [ast.Pass()] |
| 874 | if decorator_list is None: |
| 875 | decorator_list = [] |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 876 | return ast.ClassDef("myclass", bases, keywords, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 877 | body, decorator_list) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 878 | self.stmt(cls(bases=[ast.Name("x", ast.Store())]), |
| 879 | "must have Load context") |
| 880 | self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]), |
| 881 | "must have Load context") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 882 | self.stmt(cls(body=[]), "empty body on ClassDef") |
| 883 | self.stmt(cls(body=[None]), "None disallowed") |
| 884 | self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]), |
| 885 | "must have Load context") |
| 886 | |
| 887 | def test_delete(self): |
| 888 | self.stmt(ast.Delete([]), "empty targets on Delete") |
| 889 | self.stmt(ast.Delete([None]), "None disallowed") |
| 890 | self.stmt(ast.Delete([ast.Name("x", ast.Load())]), |
| 891 | "must have Del context") |
| 892 | |
| 893 | def test_assign(self): |
| 894 | self.stmt(ast.Assign([], ast.Num(3)), "empty targets on Assign") |
| 895 | self.stmt(ast.Assign([None], ast.Num(3)), "None disallowed") |
| 896 | self.stmt(ast.Assign([ast.Name("x", ast.Load())], ast.Num(3)), |
| 897 | "must have Store context") |
| 898 | self.stmt(ast.Assign([ast.Name("x", ast.Store())], |
| 899 | ast.Name("y", ast.Store())), |
| 900 | "must have Load context") |
| 901 | |
| 902 | def test_augassign(self): |
| 903 | aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(), |
| 904 | ast.Name("y", ast.Load())) |
| 905 | self.stmt(aug, "must have Store context") |
| 906 | aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(), |
| 907 | ast.Name("y", ast.Store())) |
| 908 | self.stmt(aug, "must have Load context") |
| 909 | |
| 910 | def test_for(self): |
| 911 | x = ast.Name("x", ast.Store()) |
| 912 | y = ast.Name("y", ast.Load()) |
| 913 | p = ast.Pass() |
| 914 | self.stmt(ast.For(x, y, [], []), "empty body on For") |
| 915 | self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []), |
| 916 | "must have Store context") |
| 917 | self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []), |
| 918 | "must have Load context") |
| 919 | e = ast.Expr(ast.Name("x", ast.Store())) |
| 920 | self.stmt(ast.For(x, y, [e], []), "must have Load context") |
| 921 | self.stmt(ast.For(x, y, [p], [e]), "must have Load context") |
| 922 | |
| 923 | def test_while(self): |
| 924 | self.stmt(ast.While(ast.Num(3), [], []), "empty body on While") |
| 925 | self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []), |
| 926 | "must have Load context") |
| 927 | self.stmt(ast.While(ast.Num(3), [ast.Pass()], |
| 928 | [ast.Expr(ast.Name("x", ast.Store()))]), |
| 929 | "must have Load context") |
| 930 | |
| 931 | def test_if(self): |
| 932 | self.stmt(ast.If(ast.Num(3), [], []), "empty body on If") |
| 933 | i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], []) |
| 934 | self.stmt(i, "must have Load context") |
| 935 | i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], []) |
| 936 | self.stmt(i, "must have Load context") |
| 937 | i = ast.If(ast.Num(3), [ast.Pass()], |
| 938 | [ast.Expr(ast.Name("x", ast.Store()))]) |
| 939 | self.stmt(i, "must have Load context") |
| 940 | |
| 941 | def test_with(self): |
| 942 | p = ast.Pass() |
| 943 | self.stmt(ast.With([], [p]), "empty items on With") |
| 944 | i = ast.withitem(ast.Num(3), None) |
| 945 | self.stmt(ast.With([i], []), "empty body on With") |
| 946 | i = ast.withitem(ast.Name("x", ast.Store()), None) |
| 947 | self.stmt(ast.With([i], [p]), "must have Load context") |
| 948 | i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load())) |
| 949 | self.stmt(ast.With([i], [p]), "must have Store context") |
| 950 | |
| 951 | def test_raise(self): |
| 952 | r = ast.Raise(None, ast.Num(3)) |
| 953 | self.stmt(r, "Raise with cause but no exception") |
| 954 | r = ast.Raise(ast.Name("x", ast.Store()), None) |
| 955 | self.stmt(r, "must have Load context") |
| 956 | r = ast.Raise(ast.Num(4), ast.Name("x", ast.Store())) |
| 957 | self.stmt(r, "must have Load context") |
| 958 | |
| 959 | def test_try(self): |
| 960 | p = ast.Pass() |
| 961 | t = ast.Try([], [], [], [p]) |
| 962 | self.stmt(t, "empty body on Try") |
| 963 | t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p]) |
| 964 | self.stmt(t, "must have Load context") |
| 965 | t = ast.Try([p], [], [], []) |
| 966 | self.stmt(t, "Try has neither except handlers nor finalbody") |
| 967 | t = ast.Try([p], [], [p], [p]) |
| 968 | self.stmt(t, "Try has orelse but no except handlers") |
| 969 | t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], []) |
| 970 | self.stmt(t, "empty body on ExceptHandler") |
| 971 | e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])] |
| 972 | self.stmt(ast.Try([p], e, [], []), "must have Load context") |
| 973 | e = [ast.ExceptHandler(None, "x", [p])] |
| 974 | t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p]) |
| 975 | self.stmt(t, "must have Load context") |
| 976 | t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))]) |
| 977 | self.stmt(t, "must have Load context") |
| 978 | |
| 979 | def test_assert(self): |
| 980 | self.stmt(ast.Assert(ast.Name("x", ast.Store()), None), |
| 981 | "must have Load context") |
| 982 | assrt = ast.Assert(ast.Name("x", ast.Load()), |
| 983 | ast.Name("y", ast.Store())) |
| 984 | self.stmt(assrt, "must have Load context") |
| 985 | |
| 986 | def test_import(self): |
| 987 | self.stmt(ast.Import([]), "empty names on Import") |
| 988 | |
| 989 | def test_importfrom(self): |
| 990 | imp = ast.ImportFrom(None, [ast.alias("x", None)], -42) |
Serhiy Storchaka | 7de2840 | 2016-06-27 23:40:43 +0300 | [diff] [blame] | 991 | self.stmt(imp, "Negative ImportFrom level") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 992 | self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom") |
| 993 | |
| 994 | def test_global(self): |
| 995 | self.stmt(ast.Global([]), "empty names on Global") |
| 996 | |
| 997 | def test_nonlocal(self): |
| 998 | self.stmt(ast.Nonlocal([]), "empty names on Nonlocal") |
| 999 | |
| 1000 | def test_expr(self): |
| 1001 | e = ast.Expr(ast.Name("x", ast.Store())) |
| 1002 | self.stmt(e, "must have Load context") |
| 1003 | |
| 1004 | def test_boolop(self): |
| 1005 | b = ast.BoolOp(ast.And(), []) |
| 1006 | self.expr(b, "less than 2 values") |
| 1007 | b = ast.BoolOp(ast.And(), [ast.Num(3)]) |
| 1008 | self.expr(b, "less than 2 values") |
| 1009 | b = ast.BoolOp(ast.And(), [ast.Num(4), None]) |
| 1010 | self.expr(b, "None disallowed") |
| 1011 | b = ast.BoolOp(ast.And(), [ast.Num(4), ast.Name("x", ast.Store())]) |
| 1012 | self.expr(b, "must have Load context") |
| 1013 | |
| 1014 | def test_unaryop(self): |
| 1015 | u = ast.UnaryOp(ast.Not(), ast.Name("x", ast.Store())) |
| 1016 | self.expr(u, "must have Load context") |
| 1017 | |
| 1018 | def test_lambda(self): |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1019 | a = ast.arguments([], [], None, [], [], None, []) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1020 | self.expr(ast.Lambda(a, ast.Name("x", ast.Store())), |
| 1021 | "must have Load context") |
| 1022 | def fac(args): |
| 1023 | return ast.Lambda(args, ast.Name("x", ast.Load())) |
| 1024 | self._check_arguments(fac, self.expr) |
| 1025 | |
| 1026 | def test_ifexp(self): |
| 1027 | l = ast.Name("x", ast.Load()) |
| 1028 | s = ast.Name("y", ast.Store()) |
| 1029 | for args in (s, l, l), (l, s, l), (l, l, s): |
Benjamin Peterson | 71ce897 | 2011-08-09 16:17:12 -0500 | [diff] [blame] | 1030 | self.expr(ast.IfExp(*args), "must have Load context") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1031 | |
| 1032 | def test_dict(self): |
| 1033 | d = ast.Dict([], [ast.Name("x", ast.Load())]) |
| 1034 | self.expr(d, "same number of keys as values") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1035 | d = ast.Dict([ast.Name("x", ast.Load())], [None]) |
| 1036 | self.expr(d, "None disallowed") |
| 1037 | |
| 1038 | def test_set(self): |
| 1039 | self.expr(ast.Set([None]), "None disallowed") |
| 1040 | s = ast.Set([ast.Name("x", ast.Store())]) |
| 1041 | self.expr(s, "must have Load context") |
| 1042 | |
| 1043 | def _check_comprehension(self, fac): |
| 1044 | self.expr(fac([]), "comprehension with no generators") |
| 1045 | g = ast.comprehension(ast.Name("x", ast.Load()), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1046 | ast.Name("x", ast.Load()), [], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1047 | self.expr(fac([g]), "must have Store context") |
| 1048 | g = ast.comprehension(ast.Name("x", ast.Store()), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1049 | ast.Name("x", ast.Store()), [], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1050 | self.expr(fac([g]), "must have Load context") |
| 1051 | x = ast.Name("x", ast.Store()) |
| 1052 | y = ast.Name("y", ast.Load()) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1053 | g = ast.comprehension(x, y, [None], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1054 | self.expr(fac([g]), "None disallowed") |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1055 | g = ast.comprehension(x, y, [ast.Name("x", ast.Store())], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1056 | self.expr(fac([g]), "must have Load context") |
| 1057 | |
| 1058 | def _simple_comp(self, fac): |
| 1059 | g = ast.comprehension(ast.Name("x", ast.Store()), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1060 | ast.Name("x", ast.Load()), [], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1061 | self.expr(fac(ast.Name("x", ast.Store()), [g]), |
| 1062 | "must have Load context") |
| 1063 | def wrap(gens): |
| 1064 | return fac(ast.Name("x", ast.Store()), gens) |
| 1065 | self._check_comprehension(wrap) |
| 1066 | |
| 1067 | def test_listcomp(self): |
| 1068 | self._simple_comp(ast.ListComp) |
| 1069 | |
| 1070 | def test_setcomp(self): |
| 1071 | self._simple_comp(ast.SetComp) |
| 1072 | |
| 1073 | def test_generatorexp(self): |
| 1074 | self._simple_comp(ast.GeneratorExp) |
| 1075 | |
| 1076 | def test_dictcomp(self): |
| 1077 | g = ast.comprehension(ast.Name("y", ast.Store()), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1078 | ast.Name("p", ast.Load()), [], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1079 | c = ast.DictComp(ast.Name("x", ast.Store()), |
| 1080 | ast.Name("y", ast.Load()), [g]) |
| 1081 | self.expr(c, "must have Load context") |
| 1082 | c = ast.DictComp(ast.Name("x", ast.Load()), |
| 1083 | ast.Name("y", ast.Store()), [g]) |
| 1084 | self.expr(c, "must have Load context") |
| 1085 | def factory(comps): |
| 1086 | k = ast.Name("x", ast.Load()) |
| 1087 | v = ast.Name("y", ast.Load()) |
| 1088 | return ast.DictComp(k, v, comps) |
| 1089 | self._check_comprehension(factory) |
| 1090 | |
| 1091 | def test_yield(self): |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1092 | self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load") |
| 1093 | self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1094 | |
| 1095 | def test_compare(self): |
| 1096 | left = ast.Name("x", ast.Load()) |
| 1097 | comp = ast.Compare(left, [ast.In()], []) |
| 1098 | self.expr(comp, "no comparators") |
| 1099 | comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)]) |
| 1100 | self.expr(comp, "different number of comparators and operands") |
| 1101 | comp = ast.Compare(ast.Num("blah"), [ast.In()], [left]) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1102 | self.expr(comp) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1103 | comp = ast.Compare(left, [ast.In()], [ast.Num("blah")]) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1104 | self.expr(comp) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1105 | |
| 1106 | def test_call(self): |
| 1107 | func = ast.Name("x", ast.Load()) |
| 1108 | args = [ast.Name("y", ast.Load())] |
| 1109 | keywords = [ast.keyword("w", ast.Name("z", ast.Load()))] |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1110 | call = ast.Call(ast.Name("x", ast.Store()), args, keywords) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1111 | self.expr(call, "must have Load context") |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1112 | call = ast.Call(func, [None], keywords) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1113 | self.expr(call, "None disallowed") |
| 1114 | bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))] |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1115 | call = ast.Call(func, args, bad_keywords) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1116 | self.expr(call, "must have Load context") |
| 1117 | |
| 1118 | def test_num(self): |
| 1119 | class subint(int): |
| 1120 | pass |
| 1121 | class subfloat(float): |
| 1122 | pass |
| 1123 | class subcomplex(complex): |
| 1124 | pass |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1125 | for obj in "0", "hello": |
| 1126 | self.expr(ast.Num(obj)) |
| 1127 | for obj in subint(), subfloat(), subcomplex(): |
| 1128 | self.expr(ast.Num(obj), "invalid type", exc=TypeError) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1129 | |
| 1130 | def test_attribute(self): |
| 1131 | attr = ast.Attribute(ast.Name("x", ast.Store()), "y", ast.Load()) |
| 1132 | self.expr(attr, "must have Load context") |
| 1133 | |
| 1134 | def test_subscript(self): |
| 1135 | sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)), |
| 1136 | ast.Load()) |
| 1137 | self.expr(sub, "must have Load context") |
| 1138 | x = ast.Name("x", ast.Load()) |
| 1139 | sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())), |
| 1140 | ast.Load()) |
| 1141 | self.expr(sub, "must have Load context") |
| 1142 | s = ast.Name("x", ast.Store()) |
| 1143 | for args in (s, None, None), (None, s, None), (None, None, s): |
| 1144 | sl = ast.Slice(*args) |
| 1145 | self.expr(ast.Subscript(x, sl, ast.Load()), |
| 1146 | "must have Load context") |
| 1147 | sl = ast.ExtSlice([]) |
| 1148 | self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice") |
| 1149 | sl = ast.ExtSlice([ast.Index(s)]) |
| 1150 | self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context") |
| 1151 | |
| 1152 | def test_starred(self): |
| 1153 | left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())], |
| 1154 | ast.Store()) |
| 1155 | assign = ast.Assign([left], ast.Num(4)) |
| 1156 | self.stmt(assign, "must have Store context") |
| 1157 | |
| 1158 | def _sequence(self, fac): |
| 1159 | self.expr(fac([None], ast.Load()), "None disallowed") |
| 1160 | self.expr(fac([ast.Name("x", ast.Store())], ast.Load()), |
| 1161 | "must have Load context") |
| 1162 | |
| 1163 | def test_list(self): |
| 1164 | self._sequence(ast.List) |
| 1165 | |
| 1166 | def test_tuple(self): |
| 1167 | self._sequence(ast.Tuple) |
| 1168 | |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1169 | def test_nameconstant(self): |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1170 | self.expr(ast.NameConstant(4)) |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1171 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1172 | def test_stdlib_validates(self): |
| 1173 | stdlib = os.path.dirname(ast.__file__) |
| 1174 | tests = [fn for fn in os.listdir(stdlib) if fn.endswith(".py")] |
| 1175 | tests.extend(["test/test_grammar.py", "test/test_unpack_ex.py"]) |
| 1176 | for module in tests: |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 1177 | with self.subTest(module): |
| 1178 | fn = os.path.join(stdlib, module) |
| 1179 | with open(fn, "r", encoding="utf-8") as fp: |
| 1180 | source = fp.read() |
| 1181 | mod = ast.parse(source, fn) |
| 1182 | compile(mod, fn, "exec") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 1183 | |
| 1184 | |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1185 | class ConstantTests(unittest.TestCase): |
| 1186 | """Tests on the ast.Constant node type.""" |
| 1187 | |
| 1188 | def compile_constant(self, value): |
| 1189 | tree = ast.parse("x = 123") |
| 1190 | |
| 1191 | node = tree.body[0].value |
| 1192 | new_node = ast.Constant(value=value) |
| 1193 | ast.copy_location(new_node, node) |
| 1194 | tree.body[0].value = new_node |
| 1195 | |
| 1196 | code = compile(tree, "<string>", "exec") |
| 1197 | |
| 1198 | ns = {} |
| 1199 | exec(code, ns) |
| 1200 | return ns['x'] |
| 1201 | |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 1202 | def test_validation(self): |
| 1203 | with self.assertRaises(TypeError) as cm: |
| 1204 | self.compile_constant([1, 2, 3]) |
| 1205 | self.assertEqual(str(cm.exception), |
| 1206 | "got an invalid type in Constant: list") |
| 1207 | |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1208 | def test_singletons(self): |
| 1209 | for const in (None, False, True, Ellipsis, b'', frozenset()): |
| 1210 | with self.subTest(const=const): |
| 1211 | value = self.compile_constant(const) |
| 1212 | self.assertIs(value, const) |
| 1213 | |
| 1214 | def test_values(self): |
| 1215 | nested_tuple = (1,) |
| 1216 | nested_frozenset = frozenset({1}) |
| 1217 | for level in range(3): |
| 1218 | nested_tuple = (nested_tuple, 2) |
| 1219 | nested_frozenset = frozenset({nested_frozenset, 2}) |
| 1220 | values = (123, 123.0, 123j, |
| 1221 | "unicode", b'bytes', |
| 1222 | tuple("tuple"), frozenset("frozenset"), |
| 1223 | nested_tuple, nested_frozenset) |
| 1224 | for value in values: |
| 1225 | with self.subTest(value=value): |
| 1226 | result = self.compile_constant(value) |
| 1227 | self.assertEqual(result, value) |
| 1228 | |
| 1229 | def test_assign_to_constant(self): |
| 1230 | tree = ast.parse("x = 1") |
| 1231 | |
| 1232 | target = tree.body[0].targets[0] |
| 1233 | new_target = ast.Constant(value=1) |
| 1234 | ast.copy_location(new_target, target) |
| 1235 | tree.body[0].targets[0] = new_target |
| 1236 | |
| 1237 | with self.assertRaises(ValueError) as cm: |
| 1238 | compile(tree, "string", "exec") |
| 1239 | self.assertEqual(str(cm.exception), |
| 1240 | "expression which can't be assigned " |
| 1241 | "to in Store context") |
| 1242 | |
| 1243 | def test_get_docstring(self): |
| 1244 | tree = ast.parse("'docstring'\nx = 1") |
| 1245 | self.assertEqual(ast.get_docstring(tree), 'docstring') |
| 1246 | |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1247 | def get_load_const(self, tree): |
| 1248 | # Compile to bytecode, disassemble and get parameter of LOAD_CONST |
| 1249 | # instructions |
| 1250 | co = compile(tree, '<string>', 'exec') |
| 1251 | consts = [] |
| 1252 | for instr in dis.get_instructions(co): |
| 1253 | if instr.opname == 'LOAD_CONST': |
| 1254 | consts.append(instr.argval) |
| 1255 | return consts |
| 1256 | |
| 1257 | @support.cpython_only |
| 1258 | def test_load_const(self): |
| 1259 | consts = [None, |
| 1260 | True, False, |
| 1261 | 124, |
| 1262 | 2.0, |
| 1263 | 3j, |
| 1264 | "unicode", |
| 1265 | b'bytes', |
| 1266 | (1, 2, 3)] |
| 1267 | |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame] | 1268 | code = '\n'.join(['x={!r}'.format(const) for const in consts]) |
| 1269 | code += '\nx = ...' |
| 1270 | consts.extend((Ellipsis, None)) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1271 | |
| 1272 | tree = ast.parse(code) |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame] | 1273 | self.assertEqual(self.get_load_const(tree), |
| 1274 | consts) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1275 | |
| 1276 | # Replace expression nodes with constants |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame] | 1277 | for assign, const in zip(tree.body, consts): |
| 1278 | assert isinstance(assign, ast.Assign), ast.dump(assign) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1279 | new_node = ast.Constant(value=const) |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame] | 1280 | ast.copy_location(new_node, assign.value) |
| 1281 | assign.value = new_node |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1282 | |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame] | 1283 | self.assertEqual(self.get_load_const(tree), |
| 1284 | consts) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1285 | |
| 1286 | def test_literal_eval(self): |
| 1287 | tree = ast.parse("1 + 2") |
| 1288 | binop = tree.body[0].value |
| 1289 | |
| 1290 | new_left = ast.Constant(value=10) |
| 1291 | ast.copy_location(new_left, binop.left) |
| 1292 | binop.left = new_left |
| 1293 | |
Serhiy Storchaka | d8ac4d1 | 2018-01-04 11:15:39 +0200 | [diff] [blame] | 1294 | new_right = ast.Constant(value=20j) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1295 | ast.copy_location(new_right, binop.right) |
| 1296 | binop.right = new_right |
| 1297 | |
Serhiy Storchaka | d8ac4d1 | 2018-01-04 11:15:39 +0200 | [diff] [blame] | 1298 | self.assertEqual(ast.literal_eval(binop), 10+20j) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1299 | |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1300 | def test_string_kind(self): |
| 1301 | c = ast.parse('"x"', mode='eval').body |
| 1302 | self.assertEqual(c.value, "x") |
| 1303 | self.assertEqual(c.kind, None) |
| 1304 | |
| 1305 | c = ast.parse('u"x"', mode='eval').body |
| 1306 | self.assertEqual(c.value, "x") |
| 1307 | self.assertEqual(c.kind, "u") |
| 1308 | |
| 1309 | c = ast.parse('r"x"', mode='eval').body |
| 1310 | self.assertEqual(c.value, "x") |
| 1311 | self.assertEqual(c.kind, None) |
| 1312 | |
| 1313 | c = ast.parse('b"x"', mode='eval').body |
| 1314 | self.assertEqual(c.value, b"x") |
| 1315 | self.assertEqual(c.kind, None) |
| 1316 | |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1317 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1318 | class EndPositionTests(unittest.TestCase): |
| 1319 | """Tests for end position of AST nodes. |
| 1320 | |
| 1321 | Testing end positions of nodes requires a bit of extra care |
| 1322 | because of how LL parsers work. |
| 1323 | """ |
| 1324 | def _check_end_pos(self, ast_node, end_lineno, end_col_offset): |
| 1325 | self.assertEqual(ast_node.end_lineno, end_lineno) |
| 1326 | self.assertEqual(ast_node.end_col_offset, end_col_offset) |
| 1327 | |
| 1328 | def _check_content(self, source, ast_node, content): |
| 1329 | self.assertEqual(ast.get_source_segment(source, ast_node), content) |
| 1330 | |
| 1331 | def _parse_value(self, s): |
| 1332 | # Use duck-typing to support both single expression |
| 1333 | # and a right hand side of an assignment statement. |
| 1334 | return ast.parse(s).body[0].value |
| 1335 | |
| 1336 | def test_lambda(self): |
| 1337 | s = 'lambda x, *y: None' |
| 1338 | lam = self._parse_value(s) |
| 1339 | self._check_content(s, lam.body, 'None') |
| 1340 | self._check_content(s, lam.args.args[0], 'x') |
| 1341 | self._check_content(s, lam.args.vararg, 'y') |
| 1342 | |
| 1343 | def test_func_def(self): |
| 1344 | s = dedent(''' |
| 1345 | def func(x: int, |
| 1346 | *args: str, |
| 1347 | z: float = 0, |
| 1348 | **kwargs: Any) -> bool: |
| 1349 | return True |
| 1350 | ''').strip() |
| 1351 | fdef = ast.parse(s).body[0] |
| 1352 | self._check_end_pos(fdef, 5, 15) |
| 1353 | self._check_content(s, fdef.body[0], 'return True') |
| 1354 | self._check_content(s, fdef.args.args[0], 'x: int') |
| 1355 | self._check_content(s, fdef.args.args[0].annotation, 'int') |
| 1356 | self._check_content(s, fdef.args.kwarg, 'kwargs: Any') |
| 1357 | self._check_content(s, fdef.args.kwarg.annotation, 'Any') |
| 1358 | |
| 1359 | def test_call(self): |
| 1360 | s = 'func(x, y=2, **kw)' |
| 1361 | call = self._parse_value(s) |
| 1362 | self._check_content(s, call.func, 'func') |
| 1363 | self._check_content(s, call.keywords[0].value, '2') |
| 1364 | self._check_content(s, call.keywords[1].value, 'kw') |
| 1365 | |
| 1366 | def test_call_noargs(self): |
| 1367 | s = 'x[0]()' |
| 1368 | call = self._parse_value(s) |
| 1369 | self._check_content(s, call.func, 'x[0]') |
| 1370 | self._check_end_pos(call, 1, 6) |
| 1371 | |
| 1372 | def test_class_def(self): |
| 1373 | s = dedent(''' |
| 1374 | class C(A, B): |
| 1375 | x: int = 0 |
| 1376 | ''').strip() |
| 1377 | cdef = ast.parse(s).body[0] |
| 1378 | self._check_end_pos(cdef, 2, 14) |
| 1379 | self._check_content(s, cdef.bases[1], 'B') |
| 1380 | self._check_content(s, cdef.body[0], 'x: int = 0') |
| 1381 | |
| 1382 | def test_class_kw(self): |
| 1383 | s = 'class S(metaclass=abc.ABCMeta): pass' |
| 1384 | cdef = ast.parse(s).body[0] |
| 1385 | self._check_content(s, cdef.keywords[0].value, 'abc.ABCMeta') |
| 1386 | |
| 1387 | def test_multi_line_str(self): |
| 1388 | s = dedent(''' |
| 1389 | x = """Some multi-line text. |
| 1390 | |
| 1391 | It goes on starting from same indent.""" |
| 1392 | ''').strip() |
| 1393 | assign = ast.parse(s).body[0] |
| 1394 | self._check_end_pos(assign, 3, 40) |
| 1395 | self._check_end_pos(assign.value, 3, 40) |
| 1396 | |
| 1397 | def test_continued_str(self): |
| 1398 | s = dedent(''' |
| 1399 | x = "first part" \\ |
| 1400 | "second part" |
| 1401 | ''').strip() |
| 1402 | assign = ast.parse(s).body[0] |
| 1403 | self._check_end_pos(assign, 2, 13) |
| 1404 | self._check_end_pos(assign.value, 2, 13) |
| 1405 | |
| 1406 | def test_suites(self): |
| 1407 | # We intentionally put these into the same string to check |
| 1408 | # that empty lines are not part of the suite. |
| 1409 | s = dedent(''' |
| 1410 | while True: |
| 1411 | pass |
| 1412 | |
| 1413 | if one(): |
| 1414 | x = None |
| 1415 | elif other(): |
| 1416 | y = None |
| 1417 | else: |
| 1418 | z = None |
| 1419 | |
| 1420 | for x, y in stuff: |
| 1421 | assert True |
| 1422 | |
| 1423 | try: |
| 1424 | raise RuntimeError |
| 1425 | except TypeError as e: |
| 1426 | pass |
| 1427 | |
| 1428 | pass |
| 1429 | ''').strip() |
| 1430 | mod = ast.parse(s) |
| 1431 | while_loop = mod.body[0] |
| 1432 | if_stmt = mod.body[1] |
| 1433 | for_loop = mod.body[2] |
| 1434 | try_stmt = mod.body[3] |
| 1435 | pass_stmt = mod.body[4] |
| 1436 | |
| 1437 | self._check_end_pos(while_loop, 2, 8) |
| 1438 | self._check_end_pos(if_stmt, 9, 12) |
| 1439 | self._check_end_pos(for_loop, 12, 15) |
| 1440 | self._check_end_pos(try_stmt, 17, 8) |
| 1441 | self._check_end_pos(pass_stmt, 19, 4) |
| 1442 | |
| 1443 | self._check_content(s, while_loop.test, 'True') |
| 1444 | self._check_content(s, if_stmt.body[0], 'x = None') |
| 1445 | self._check_content(s, if_stmt.orelse[0].test, 'other()') |
| 1446 | self._check_content(s, for_loop.target, 'x, y') |
| 1447 | self._check_content(s, try_stmt.body[0], 'raise RuntimeError') |
| 1448 | self._check_content(s, try_stmt.handlers[0].type, 'TypeError') |
| 1449 | |
| 1450 | def test_fstring(self): |
| 1451 | s = 'x = f"abc {x + y} abc"' |
| 1452 | fstr = self._parse_value(s) |
| 1453 | binop = fstr.values[1].value |
| 1454 | self._check_content(s, binop, 'x + y') |
| 1455 | |
| 1456 | def test_fstring_multi_line(self): |
| 1457 | s = dedent(''' |
| 1458 | f"""Some multi-line text. |
| 1459 | { |
| 1460 | arg_one |
| 1461 | + |
| 1462 | arg_two |
| 1463 | } |
| 1464 | It goes on...""" |
| 1465 | ''').strip() |
| 1466 | fstr = self._parse_value(s) |
| 1467 | binop = fstr.values[1].value |
| 1468 | self._check_end_pos(binop, 5, 7) |
| 1469 | self._check_content(s, binop.left, 'arg_one') |
| 1470 | self._check_content(s, binop.right, 'arg_two') |
| 1471 | |
| 1472 | def test_import_from_multi_line(self): |
| 1473 | s = dedent(''' |
| 1474 | from x.y.z import ( |
| 1475 | a, b, c as c |
| 1476 | ) |
| 1477 | ''').strip() |
| 1478 | imp = ast.parse(s).body[0] |
| 1479 | self._check_end_pos(imp, 3, 1) |
| 1480 | |
| 1481 | def test_slices(self): |
| 1482 | s1 = 'f()[1, 2] [0]' |
| 1483 | s2 = 'x[ a.b: c.d]' |
| 1484 | sm = dedent(''' |
| 1485 | x[ a.b: f () , |
| 1486 | g () : c.d |
| 1487 | ] |
| 1488 | ''').strip() |
| 1489 | i1, i2, im = map(self._parse_value, (s1, s2, sm)) |
| 1490 | self._check_content(s1, i1.value, 'f()[1, 2]') |
| 1491 | self._check_content(s1, i1.value.slice.value, '1, 2') |
| 1492 | self._check_content(s2, i2.slice.lower, 'a.b') |
| 1493 | self._check_content(s2, i2.slice.upper, 'c.d') |
| 1494 | self._check_content(sm, im.slice.dims[0].upper, 'f ()') |
| 1495 | self._check_content(sm, im.slice.dims[1].lower, 'g ()') |
| 1496 | self._check_end_pos(im, 3, 3) |
| 1497 | |
| 1498 | def test_binop(self): |
| 1499 | s = dedent(''' |
| 1500 | (1 * 2 + (3 ) + |
| 1501 | 4 |
| 1502 | ) |
| 1503 | ''').strip() |
| 1504 | binop = self._parse_value(s) |
| 1505 | self._check_end_pos(binop, 2, 6) |
| 1506 | self._check_content(s, binop.right, '4') |
| 1507 | self._check_content(s, binop.left, '1 * 2 + (3 )') |
| 1508 | self._check_content(s, binop.left.right, '3') |
| 1509 | |
| 1510 | def test_boolop(self): |
| 1511 | s = dedent(''' |
| 1512 | if (one_condition and |
| 1513 | (other_condition or yet_another_one)): |
| 1514 | pass |
| 1515 | ''').strip() |
| 1516 | bop = ast.parse(s).body[0].test |
| 1517 | self._check_end_pos(bop, 2, 44) |
| 1518 | self._check_content(s, bop.values[1], |
| 1519 | 'other_condition or yet_another_one') |
| 1520 | |
| 1521 | def test_tuples(self): |
| 1522 | s1 = 'x = () ;' |
| 1523 | s2 = 'x = 1 , ;' |
| 1524 | s3 = 'x = (1 , 2 ) ;' |
| 1525 | sm = dedent(''' |
| 1526 | x = ( |
| 1527 | a, b, |
| 1528 | ) |
| 1529 | ''').strip() |
| 1530 | t1, t2, t3, tm = map(self._parse_value, (s1, s2, s3, sm)) |
| 1531 | self._check_content(s1, t1, '()') |
| 1532 | self._check_content(s2, t2, '1 ,') |
| 1533 | self._check_content(s3, t3, '(1 , 2 )') |
| 1534 | self._check_end_pos(tm, 3, 1) |
| 1535 | |
| 1536 | def test_attribute_spaces(self): |
| 1537 | s = 'func(x. y .z)' |
| 1538 | call = self._parse_value(s) |
| 1539 | self._check_content(s, call, s) |
| 1540 | self._check_content(s, call.args[0], 'x. y .z') |
| 1541 | |
| 1542 | def test_displays(self): |
| 1543 | s1 = '[{}, {1, }, {1, 2,} ]' |
| 1544 | s2 = '{a: b, f (): g () ,}' |
| 1545 | c1 = self._parse_value(s1) |
| 1546 | c2 = self._parse_value(s2) |
| 1547 | self._check_content(s1, c1.elts[0], '{}') |
| 1548 | self._check_content(s1, c1.elts[1], '{1, }') |
| 1549 | self._check_content(s1, c1.elts[2], '{1, 2,}') |
| 1550 | self._check_content(s2, c2.keys[1], 'f ()') |
| 1551 | self._check_content(s2, c2.values[1], 'g ()') |
| 1552 | |
| 1553 | def test_comprehensions(self): |
| 1554 | s = dedent(''' |
| 1555 | x = [{x for x, y in stuff |
| 1556 | if cond.x} for stuff in things] |
| 1557 | ''').strip() |
| 1558 | cmp = self._parse_value(s) |
| 1559 | self._check_end_pos(cmp, 2, 37) |
| 1560 | self._check_content(s, cmp.generators[0].iter, 'things') |
| 1561 | self._check_content(s, cmp.elt.generators[0].iter, 'stuff') |
| 1562 | self._check_content(s, cmp.elt.generators[0].ifs[0], 'cond.x') |
| 1563 | self._check_content(s, cmp.elt.generators[0].target, 'x, y') |
| 1564 | |
| 1565 | def test_yield_await(self): |
| 1566 | s = dedent(''' |
| 1567 | async def f(): |
| 1568 | yield x |
| 1569 | await y |
| 1570 | ''').strip() |
| 1571 | fdef = ast.parse(s).body[0] |
| 1572 | self._check_content(s, fdef.body[0].value, 'yield x') |
| 1573 | self._check_content(s, fdef.body[1].value, 'await y') |
| 1574 | |
| 1575 | def test_source_segment_multi(self): |
| 1576 | s_orig = dedent(''' |
| 1577 | x = ( |
| 1578 | a, b, |
| 1579 | ) + () |
| 1580 | ''').strip() |
| 1581 | s_tuple = dedent(''' |
| 1582 | ( |
| 1583 | a, b, |
| 1584 | ) |
| 1585 | ''').strip() |
| 1586 | binop = self._parse_value(s_orig) |
| 1587 | self.assertEqual(ast.get_source_segment(s_orig, binop.left), s_tuple) |
| 1588 | |
| 1589 | def test_source_segment_padded(self): |
| 1590 | s_orig = dedent(''' |
| 1591 | class C: |
| 1592 | def fun(self) -> None: |
| 1593 | "ЖЖЖЖЖ" |
| 1594 | ''').strip() |
| 1595 | s_method = ' def fun(self) -> None:\n' \ |
| 1596 | ' "ЖЖЖЖЖ"' |
| 1597 | cdef = ast.parse(s_orig).body[0] |
| 1598 | self.assertEqual(ast.get_source_segment(s_orig, cdef.body[0], padded=True), |
| 1599 | s_method) |
| 1600 | |
| 1601 | def test_source_segment_endings(self): |
| 1602 | s = 'v = 1\r\nw = 1\nx = 1\n\ry = 1\rz = 1\r\n' |
| 1603 | v, w, x, y, z = ast.parse(s).body |
| 1604 | self._check_content(s, v, 'v = 1') |
| 1605 | self._check_content(s, w, 'w = 1') |
| 1606 | self._check_content(s, x, 'x = 1') |
| 1607 | self._check_content(s, y, 'y = 1') |
| 1608 | self._check_content(s, z, 'z = 1') |
| 1609 | |
| 1610 | def test_source_segment_tabs(self): |
| 1611 | s = dedent(''' |
| 1612 | class C: |
| 1613 | \t\f def fun(self) -> None: |
| 1614 | \t\f pass |
| 1615 | ''').strip() |
| 1616 | s_method = ' \t\f def fun(self) -> None:\n' \ |
| 1617 | ' \t\f pass' |
| 1618 | |
| 1619 | cdef = ast.parse(s).body[0] |
| 1620 | self.assertEqual(ast.get_source_segment(s, cdef.body[0], padded=True), s_method) |
| 1621 | |
| 1622 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 1623 | def main(): |
| 1624 | if __name__ != '__main__': |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1625 | return |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 1626 | if sys.argv[1:] == ['-g']: |
| 1627 | for statements, kind in ((exec_tests, "exec"), (single_tests, "single"), |
| 1628 | (eval_tests, "eval")): |
| 1629 | print(kind+"_results = [") |
Victor Stinner | f089196 | 2016-02-08 17:15:21 +0100 | [diff] [blame] | 1630 | for statement in statements: |
| 1631 | tree = ast.parse(statement, "?", kind) |
| 1632 | print("%r," % (to_tuple(tree),)) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 1633 | print("]") |
| 1634 | print("main()") |
| 1635 | raise SystemExit |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 1636 | unittest.main() |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1637 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1638 | #### EVERYTHING BELOW IS GENERATED BY python Lib/test/test_ast.py -g ##### |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1639 | exec_results = [ |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1640 | ('Module', [('Expr', (1, 0), ('Constant', (1, 0), None, None))], []), |
| 1641 | ('Module', [('Expr', (1, 0), ('Constant', (1, 0), 'module docstring', None))], []), |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1642 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (1, 9))], [], None, None)], []), |
| 1643 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Expr', (1, 9), ('Constant', (1, 9), 'function docstring', None))], [], None, None)], []), |
| 1644 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None, None)], [], None, [], [], None, []), [('Pass', (1, 10))], [], None, None)], []), |
| 1645 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None, None)], [], None, [], [], None, [('Constant', (1, 8), 0, None)]), [('Pass', (1, 12))], [], None, None)], []), |
| 1646 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], [], ('arg', (1, 7), 'args', None, None), [], [], None, []), [('Pass', (1, 14))], [], None, None)], []), |
| 1647 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], [], None, [], [], ('arg', (1, 8), 'kwargs', None, None), []), [('Pass', (1, 17))], [], None, None)], []), |
| 1648 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None, None), ('arg', (1, 9), 'b', None, None), ('arg', (1, 14), 'c', None, None), ('arg', (1, 22), 'd', None, None), ('arg', (1, 28), 'e', None, None)], [], ('arg', (1, 35), 'args', None, None), [('arg', (1, 41), 'f', None, None)], [('Constant', (1, 43), 42, None)], ('arg', (1, 49), 'kwargs', None, None), [('Constant', (1, 11), 1, None), ('Constant', (1, 16), None, None), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Expr', (1, 58), ('Constant', (1, 58), 'doc for f()', None))], [], None, None)], []), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1649 | ('Module', [('ClassDef', (1, 0), 'C', [], [], [('Pass', (1, 8))], [])], []), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1650 | ('Module', [('ClassDef', (1, 0), 'C', [], [], [('Expr', (1, 9), ('Constant', (1, 9), 'docstring for class C', None))], [])], []), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1651 | ('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], [('Pass', (1, 17))], [])], []), |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1652 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Return', (1, 8), ('Constant', (1, 15), 1, None))], [], None, None)], []), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1653 | ('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])], []), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1654 | ('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Constant', (1, 4), 1, None), None)], []), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1655 | ('Module', [('Assign', (1, 0), [('Tuple', (1, 0), [('Name', (1, 0), 'a', ('Store',)), ('Name', (1, 2), 'b', ('Store',))], ('Store',))], ('Name', (1, 6), 'c', ('Load',)), None)], []), |
| 1656 | ('Module', [('Assign', (1, 0), [('Tuple', (1, 0), [('Name', (1, 1), 'a', ('Store',)), ('Name', (1, 3), 'b', ('Store',))], ('Store',))], ('Name', (1, 8), 'c', ('Load',)), None)], []), |
| 1657 | ('Module', [('Assign', (1, 0), [('List', (1, 0), [('Name', (1, 1), 'a', ('Store',)), ('Name', (1, 3), 'b', ('Store',))], ('Store',))], ('Name', (1, 8), 'c', ('Load',)), None)], []), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1658 | ('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Constant', (1, 5), 1, None))], []), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1659 | ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [], None)], []), |
| 1660 | ('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])], []), |
| 1661 | ('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])], []), |
| 1662 | ('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))], None)], []), |
| 1663 | ('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',))), ('withitem', ('Name', (1, 13), 'z', ('Load',)), ('Name', (1, 18), 'q', ('Store',)))], [('Pass', (1, 21))], None)], []), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1664 | ('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Constant', (1, 16), 'string', None)], []), None)], []), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1665 | ('Module', [('Try', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [], [])], []), |
| 1666 | ('Module', [('Try', (1, 0), [('Pass', (2, 2))], [], [], [('Pass', (4, 2))])], []), |
| 1667 | ('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)], []), |
| 1668 | ('Module', [('Import', (1, 0), [('alias', 'sys', None)])], []), |
| 1669 | ('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)], []), |
| 1670 | ('Module', [('Global', (1, 0), ['v'])], []), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1671 | ('Module', [('Expr', (1, 0), ('Constant', (1, 0), 1, None))], []), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1672 | ('Module', [('Pass', (1, 0))], []), |
| 1673 | ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Break', (1, 11))], [], None)], []), |
| 1674 | ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Continue', (1, 11))], [], None)], []), |
| 1675 | ('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))], [], None)], []), |
| 1676 | ('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 5), 'a', ('Store',)), ('Name', (1, 7), 'b', ('Store',))], ('Store',)), ('Name', (1, 13), 'c', ('Load',)), [('Pass', (1, 16))], [], None)], []), |
| 1677 | ('Module', [('For', (1, 0), ('List', (1, 4), [('Name', (1, 5), 'a', ('Store',)), ('Name', (1, 7), 'b', ('Store',))], ('Store',)), ('Name', (1, 13), 'c', ('Load',)), [('Pass', (1, 16))], [], None)], []), |
| 1678 | ('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 0), ('Tuple', (2, 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',)), [], 0)]))], []), |
| 1679 | ('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Name', (1, 11), 'w', ('Store',)), ('Name', (1, 16), 'x', ('Load',)), [], 0), ('comprehension', ('Name', (1, 22), 'm', ('Store',)), ('Name', (1, 27), 'p', ('Load',)), [('Name', (1, 32), 'g', ('Load',))], 0)]))], []), |
| 1680 | ('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('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',)), [], 0)]))], []), |
| 1681 | ('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))], 0)]))], []), |
| 1682 | ('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('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',)), [], 0)]))], []), |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1683 | ('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Expr', (2, 1), ('Constant', (2, 1), 'async function', None)), ('Expr', (3, 1), ('Await', (3, 1), ('Call', (3, 7), ('Name', (3, 7), 'something', ('Load',)), [], [])))], [], None, None)], []), |
| 1684 | ('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('AsyncFor', (2, 1), ('Name', (2, 11), 'e', ('Store',)), ('Name', (2, 16), 'i', ('Load',)), [('Expr', (2, 19), ('Constant', (2, 19), 1, None))], [('Expr', (3, 7), ('Constant', (3, 7), 2, None))], None)], [], None, None)], []), |
| 1685 | ('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('AsyncWith', (2, 1), [('withitem', ('Name', (2, 12), 'a', ('Load',)), ('Name', (2, 17), 'b', ('Store',)))], [('Expr', (2, 20), ('Constant', (2, 20), 1, None))], None)], [], None, None)], []), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1686 | ('Module', [('Expr', (1, 0), ('Dict', (1, 0), [None, ('Constant', (1, 10), 2, None)], [('Dict', (1, 3), [('Constant', (1, 4), 1, None)], [('Constant', (1, 6), 2, None)]), ('Constant', (1, 12), 3, None)]))], []), |
| 1687 | ('Module', [('Expr', (1, 0), ('Set', (1, 0), [('Starred', (1, 1), ('Set', (1, 2), [('Constant', (1, 3), 1, None), ('Constant', (1, 6), 2, None)]), ('Load',)), ('Constant', (1, 10), 3, None)]))], []), |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1688 | ('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Expr', (2, 1), ('ListComp', (2, 1), ('Name', (2, 2), 'i', ('Load',)), [('comprehension', ('Name', (2, 14), 'b', ('Store',)), ('Name', (2, 19), 'c', ('Load',)), [], 1)]))], [], None, None)], []), |
| 1689 | ('Module', [('FunctionDef', (3, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (3, 9))], [('Name', (1, 1), 'deco1', ('Load',)), ('Call', (2, 0), ('Name', (2, 1), 'deco2', ('Load',)), [], [])], None, None)], []), |
| 1690 | ('Module', [('AsyncFunctionDef', (3, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (3, 15))], [('Name', (1, 1), 'deco1', ('Load',)), ('Call', (2, 0), ('Name', (2, 1), 'deco2', ('Load',)), [], [])], None, None)], []), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1691 | ('Module', [('ClassDef', (3, 0), 'C', [], [], [('Pass', (3, 9))], [('Name', (1, 1), 'deco1', ('Load',)), ('Call', (2, 0), ('Name', (2, 1), 'deco2', ('Load',)), [], [])])], []), |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1692 | ('Module', [('FunctionDef', (2, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (2, 9))], [('Call', (1, 1), ('Name', (1, 1), 'deco', ('Load',)), [('GeneratorExp', (1, 5), ('Name', (1, 6), 'a', ('Load',)), [('comprehension', ('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 17), 'b', ('Load',)), [], 0)])], [])], None, None)], []), |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 1693 | ('Module', [('Expr', (1, 0), ('NamedExpr', (1, 1), ('Name', (1, 1), 'a', ('Store',)), ('Constant', (1, 6), 1, None)))], []), |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1694 | ] |
| 1695 | single_results = [ |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1696 | ('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Constant', (1, 0), 1, None), ('Add',), ('Constant', (1, 2), 2, None)))]), |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1697 | ] |
| 1698 | eval_results = [ |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1699 | ('Expression', ('Constant', (1, 0), None, None)), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1700 | ('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])), |
| 1701 | ('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))), |
| 1702 | ('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))), |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1703 | ('Expression', ('Lambda', (1, 0), ('arguments', [], [], None, [], [], None, []), ('Constant', (1, 7), None, None))), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1704 | ('Expression', ('Dict', (1, 0), [('Constant', (1, 2), 1, None)], [('Constant', (1, 4), 2, None)])), |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 1705 | ('Expression', ('Dict', (1, 0), [], [])), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1706 | ('Expression', ('Set', (1, 0), [('Constant', (1, 1), None, None)])), |
| 1707 | ('Expression', ('Dict', (1, 0), [('Constant', (2, 6), 1, None)], [('Constant', (4, 10), 2, None)])), |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1708 | ('Expression', ('ListComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])), |
| 1709 | ('Expression', ('GeneratorExp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])), |
| 1710 | ('Expression', ('ListComp', (1, 0), ('Tuple', (1, 1), [('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',)), [], 0)])), |
| 1711 | ('Expression', ('ListComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])), |
| 1712 | ('Expression', ('ListComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])), |
| 1713 | ('Expression', ('SetComp', (1, 0), ('Tuple', (1, 1), [('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',)), [], 0)])), |
| 1714 | ('Expression', ('SetComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])), |
| 1715 | ('Expression', ('SetComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])), |
| 1716 | ('Expression', ('GeneratorExp', (1, 0), ('Tuple', (1, 1), [('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',)), [], 0)])), |
| 1717 | ('Expression', ('GeneratorExp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])), |
| 1718 | ('Expression', ('GeneratorExp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1719 | ('Expression', ('Compare', (1, 0), ('Constant', (1, 0), 1, None), [('Lt',), ('Lt',)], [('Constant', (1, 4), 2, None), ('Constant', (1, 8), 3, None)])), |
| 1720 | ('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Constant', (1, 2), 1, None), ('Constant', (1, 4), 2, None), ('Starred', (1, 10), ('Name', (1, 11), 'd', ('Load',)), ('Load',))], [('keyword', 'c', ('Constant', (1, 8), 3, None)), ('keyword', None, ('Name', (1, 15), 'e', ('Load',)))])), |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1721 | ('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('GeneratorExp', (1, 1), ('Name', (1, 2), 'a', ('Load',)), [('comprehension', ('Name', (1, 8), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Load',)), [], 0)])], [])), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1722 | ('Expression', ('Constant', (1, 0), 10, None)), |
| 1723 | ('Expression', ('Constant', (1, 0), 'string', None)), |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1724 | ('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))), |
| 1725 | ('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1726 | ('Expression', ('Name', (1, 0), 'v', ('Load',))), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1727 | ('Expression', ('List', (1, 0), [('Constant', (1, 1), 1, None), ('Constant', (1, 3), 2, None), ('Constant', (1, 5), 3, None)], ('Load',))), |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 1728 | ('Expression', ('List', (1, 0), [], ('Load',))), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1729 | ('Expression', ('Tuple', (1, 0), [('Constant', (1, 0), 1, None), ('Constant', (1, 2), 2, None), ('Constant', (1, 4), 3, None)], ('Load',))), |
| 1730 | ('Expression', ('Tuple', (1, 0), [('Constant', (1, 1), 1, None), ('Constant', (1, 3), 2, None), ('Constant', (1, 5), 3, None)], ('Load',))), |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 1731 | ('Expression', ('Tuple', (1, 0), [], ('Load',))), |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 1732 | ('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', ('Constant', (1, 12), 1, None), ('Constant', (1, 14), 2, None), None), ('Load',))], [])), |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1733 | ] |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 1734 | main() |