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