Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1 | import ast |
| 2 | import dis |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 3 | import os |
| 4 | import sys |
| 5 | import unittest |
Benjamin Peterson | 9ed3743 | 2012-07-08 11:13:36 -0700 | [diff] [blame] | 6 | import weakref |
| 7 | |
| 8 | from test import support |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 9 | |
| 10 | def to_tuple(t): |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 11 | if t is None or isinstance(t, (str, int, complex)): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 12 | return t |
| 13 | elif isinstance(t, list): |
| 14 | return [to_tuple(e) for e in t] |
| 15 | result = [t.__class__.__name__] |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 16 | if hasattr(t, 'lineno') and hasattr(t, 'col_offset'): |
| 17 | result.append((t.lineno, t.col_offset)) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 18 | if t._fields is None: |
| 19 | return tuple(result) |
| 20 | for f in t._fields: |
| 21 | result.append(to_tuple(getattr(t, f))) |
| 22 | return tuple(result) |
| 23 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 24 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 25 | # These tests are compiled through "exec" |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 26 | # There should be at least one test per statement |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 27 | exec_tests = [ |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 28 | # None |
| 29 | "None", |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 30 | # Module docstring |
| 31 | "'module docstring'", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 32 | # FunctionDef |
| 33 | "def f(): pass", |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 34 | # FunctionDef with docstring |
| 35 | "def f(): 'function docstring'", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 36 | # FunctionDef with arg |
| 37 | "def f(a): pass", |
| 38 | # FunctionDef with arg and default value |
| 39 | "def f(a=0): pass", |
| 40 | # FunctionDef with varargs |
| 41 | "def f(*args): pass", |
| 42 | # FunctionDef with kwargs |
| 43 | "def f(**kwargs): pass", |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 44 | # FunctionDef with all kind of args and docstring |
| 45 | "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] | 46 | # ClassDef |
| 47 | "class C:pass", |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 48 | # ClassDef with docstring |
| 49 | "class C: 'docstring for class C'", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 50 | # ClassDef, new style class |
| 51 | "class C(object): pass", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 52 | # Return |
| 53 | "def f():return 1", |
| 54 | # Delete |
| 55 | "del v", |
| 56 | # Assign |
| 57 | "v = 1", |
| 58 | # AugAssign |
| 59 | "v += 1", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 60 | # For |
| 61 | "for v in v:pass", |
| 62 | # While |
| 63 | "while v:pass", |
| 64 | # If |
| 65 | "if v:pass", |
Benjamin Peterson | aeabd5f | 2011-05-27 15:02:03 -0500 | [diff] [blame] | 66 | # With |
| 67 | "with x as y: pass", |
| 68 | "with x as y, z as q: pass", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 69 | # Raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 70 | "raise Exception('string')", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 71 | # TryExcept |
| 72 | "try:\n pass\nexcept Exception:\n pass", |
| 73 | # TryFinally |
| 74 | "try:\n pass\nfinally:\n pass", |
| 75 | # Assert |
| 76 | "assert v", |
| 77 | # Import |
| 78 | "import sys", |
| 79 | # ImportFrom |
| 80 | "from sys import v", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 81 | # Global |
| 82 | "global v", |
| 83 | # Expr |
| 84 | "1", |
| 85 | # Pass, |
| 86 | "pass", |
| 87 | # Break |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 88 | "for v in v:break", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 89 | # Continue |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 90 | "for v in v:continue", |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 91 | # for statements with naked tuples (see http://bugs.python.org/issue6704) |
| 92 | "for a,b in c: pass", |
| 93 | "[(a,b) for a,b in c]", |
| 94 | "((a,b) for a,b in c)", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 95 | "((a,b) for (a,b) in c)", |
| 96 | # Multiline generator expression (test for .lineno & .col_offset) |
| 97 | """( |
| 98 | ( |
| 99 | Aa |
| 100 | , |
| 101 | Bb |
| 102 | ) |
| 103 | for |
| 104 | Aa |
| 105 | , |
| 106 | Bb in Cc |
| 107 | )""", |
| 108 | # dictcomp |
| 109 | "{a : b for w in x for m in p if g}", |
| 110 | # dictcomp with naked tuple |
| 111 | "{a : b for v,w in x}", |
| 112 | # setcomp |
| 113 | "{r for l in x if g}", |
| 114 | # setcomp with naked tuple |
| 115 | "{r for l,m in x}", |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 116 | # AsyncFunctionDef |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 117 | "async def f():\n 'async function'\n await something()", |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 118 | # AsyncFor |
| 119 | "async def f():\n async for e in i: 1\n else: 2", |
| 120 | # AsyncWith |
| 121 | "async def f():\n async with a as b: 1", |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 122 | # PEP 448: Additional Unpacking Generalizations |
| 123 | "{**{1:2}, 2:3}", |
| 124 | "{*{1, 2}, 3}", |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 125 | # Asynchronous comprehensions |
| 126 | "async def f():\n [i async for b in c]", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 127 | ] |
| 128 | |
| 129 | # These are compiled through "single" |
| 130 | # because of overlap with "eval", it just tests what |
| 131 | # can't be tested with "eval" |
| 132 | single_tests = [ |
| 133 | "1+2" |
| 134 | ] |
| 135 | |
| 136 | # These are compiled through "eval" |
| 137 | # It should test all expressions |
| 138 | eval_tests = [ |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 139 | # None |
| 140 | "None", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 141 | # BoolOp |
| 142 | "a and b", |
| 143 | # BinOp |
| 144 | "a + b", |
| 145 | # UnaryOp |
| 146 | "not v", |
| 147 | # Lambda |
| 148 | "lambda:None", |
| 149 | # Dict |
| 150 | "{ 1:2 }", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 151 | # Empty dict |
| 152 | "{}", |
| 153 | # Set |
| 154 | "{None,}", |
| 155 | # Multiline dict (test for .lineno & .col_offset) |
| 156 | """{ |
| 157 | 1 |
| 158 | : |
| 159 | 2 |
| 160 | }""", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 161 | # ListComp |
| 162 | "[a for b in c if d]", |
| 163 | # GeneratorExp |
| 164 | "(a for b in c if d)", |
| 165 | # Yield - yield expressions can't work outside a function |
| 166 | # |
| 167 | # Compare |
| 168 | "1 < 2 < 3", |
| 169 | # Call |
| 170 | "f(1,2,c=3,*d,**e)", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 171 | # Num |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 172 | "10", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 173 | # Str |
| 174 | "'string'", |
| 175 | # Attribute |
| 176 | "a.b", |
| 177 | # Subscript |
| 178 | "a[b:c]", |
| 179 | # Name |
| 180 | "v", |
| 181 | # List |
| 182 | "[1,2,3]", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 183 | # Empty list |
| 184 | "[]", |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 185 | # Tuple |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 186 | "1,2,3", |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 187 | # Tuple |
| 188 | "(1,2,3)", |
| 189 | # Empty tuple |
| 190 | "()", |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 191 | # Combination |
| 192 | "a.b.c.d(a.b[1:2])", |
| 193 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 194 | ] |
| 195 | |
| 196 | # TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension |
| 197 | # excepthandler, arguments, keywords, alias |
| 198 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 199 | class AST_Tests(unittest.TestCase): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 200 | |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 201 | def _assertTrueorder(self, ast_node, parent_pos): |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 202 | 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] | 203 | return |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 204 | if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)): |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 205 | node_pos = (ast_node.lineno, ast_node.col_offset) |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 206 | self.assertTrue(node_pos >= parent_pos) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 207 | parent_pos = (ast_node.lineno, ast_node.col_offset) |
| 208 | for name in ast_node._fields: |
| 209 | value = getattr(ast_node, name) |
| 210 | if isinstance(value, list): |
| 211 | for child in value: |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 212 | self._assertTrueorder(child, parent_pos) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 213 | elif value is not None: |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 214 | self._assertTrueorder(value, parent_pos) |
Tim Peters | 5ddfe41 | 2006-03-01 23:02:57 +0000 | [diff] [blame] | 215 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 216 | def test_AST_objects(self): |
| 217 | x = ast.AST() |
| 218 | self.assertEqual(x._fields, ()) |
Benjamin Peterson | 7e0dbfb | 2012-03-12 09:46:44 -0700 | [diff] [blame] | 219 | x.foobar = 42 |
| 220 | self.assertEqual(x.foobar, 42) |
| 221 | self.assertEqual(x.__dict__["foobar"], 42) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 222 | |
| 223 | with self.assertRaises(AttributeError): |
| 224 | x.vararg |
| 225 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 226 | with self.assertRaises(TypeError): |
| 227 | # "_ast.AST constructor takes 0 positional arguments" |
| 228 | ast.AST(2) |
| 229 | |
Benjamin Peterson | 9ed3743 | 2012-07-08 11:13:36 -0700 | [diff] [blame] | 230 | def test_AST_garbage_collection(self): |
| 231 | class X: |
| 232 | pass |
| 233 | a = ast.AST() |
| 234 | a.x = X() |
| 235 | a.x.a = a |
| 236 | ref = weakref.ref(a.x) |
| 237 | del a |
| 238 | support.gc_collect() |
| 239 | self.assertIsNone(ref()) |
| 240 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 241 | def test_snippets(self): |
| 242 | for input, output, kind in ((exec_tests, exec_results, "exec"), |
| 243 | (single_tests, single_results, "single"), |
| 244 | (eval_tests, eval_results, "eval")): |
| 245 | for i, o in zip(input, output): |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 246 | with self.subTest(action="parsing", input=i): |
| 247 | ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST) |
| 248 | self.assertEqual(to_tuple(ast_tree), o) |
| 249 | self._assertTrueorder(ast_tree, (0, 0)) |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 250 | with self.subTest(action="compiling", input=i, kind=kind): |
| 251 | compile(ast_tree, "?", kind) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 252 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 253 | def test_slice(self): |
| 254 | slc = ast.parse("x[::]").body[0].value.slice |
| 255 | self.assertIsNone(slc.upper) |
| 256 | self.assertIsNone(slc.lower) |
| 257 | self.assertIsNone(slc.step) |
| 258 | |
| 259 | def test_from_import(self): |
| 260 | im = ast.parse("from . import y").body[0] |
| 261 | self.assertIsNone(im.module) |
| 262 | |
Benjamin Peterson | a4e4e35 | 2012-03-22 08:19:04 -0400 | [diff] [blame] | 263 | def test_non_interned_future_from_ast(self): |
| 264 | mod = ast.parse("from __future__ import division") |
| 265 | self.assertIsInstance(mod.body[0], ast.ImportFrom) |
| 266 | mod.body[0].module = " __future__ ".strip() |
| 267 | compile(mod, "<test>", "exec") |
| 268 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 269 | def test_base_classes(self): |
| 270 | self.assertTrue(issubclass(ast.For, ast.stmt)) |
| 271 | self.assertTrue(issubclass(ast.Name, ast.expr)) |
| 272 | self.assertTrue(issubclass(ast.stmt, ast.AST)) |
| 273 | self.assertTrue(issubclass(ast.expr, ast.AST)) |
| 274 | self.assertTrue(issubclass(ast.comprehension, ast.AST)) |
| 275 | self.assertTrue(issubclass(ast.Gt, ast.AST)) |
| 276 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 277 | def test_field_attr_existence(self): |
| 278 | for name, item in ast.__dict__.items(): |
| 279 | if isinstance(item, type) and name != 'AST' and name[0].isupper(): |
| 280 | x = item() |
| 281 | if isinstance(x, ast.AST): |
| 282 | self.assertEqual(type(x._fields), tuple) |
| 283 | |
| 284 | def test_arguments(self): |
| 285 | x = ast.arguments() |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 286 | self.assertEqual(x._fields, ('args', 'vararg', 'kwonlyargs', |
| 287 | 'kw_defaults', 'kwarg', 'defaults')) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 288 | |
| 289 | with self.assertRaises(AttributeError): |
| 290 | x.vararg |
| 291 | |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 292 | x = ast.arguments(*range(1, 7)) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 293 | self.assertEqual(x.vararg, 2) |
| 294 | |
| 295 | def test_field_attr_writable(self): |
| 296 | x = ast.Num() |
| 297 | # We can assign to _fields |
| 298 | x._fields = 666 |
| 299 | self.assertEqual(x._fields, 666) |
| 300 | |
| 301 | def test_classattrs(self): |
| 302 | x = ast.Num() |
| 303 | self.assertEqual(x._fields, ('n',)) |
| 304 | |
| 305 | with self.assertRaises(AttributeError): |
| 306 | x.n |
| 307 | |
| 308 | x = ast.Num(42) |
| 309 | self.assertEqual(x.n, 42) |
| 310 | |
| 311 | with self.assertRaises(AttributeError): |
| 312 | x.lineno |
| 313 | |
| 314 | with self.assertRaises(AttributeError): |
| 315 | x.foobar |
| 316 | |
| 317 | x = ast.Num(lineno=2) |
| 318 | self.assertEqual(x.lineno, 2) |
| 319 | |
| 320 | x = ast.Num(42, lineno=0) |
| 321 | self.assertEqual(x.lineno, 0) |
| 322 | self.assertEqual(x._fields, ('n',)) |
| 323 | self.assertEqual(x.n, 42) |
| 324 | |
| 325 | self.assertRaises(TypeError, ast.Num, 1, 2) |
| 326 | self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0) |
| 327 | |
| 328 | def test_module(self): |
| 329 | body = [ast.Num(42)] |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 330 | x = ast.Module(body) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 331 | self.assertEqual(x.body, body) |
| 332 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 333 | def test_nodeclasses(self): |
Florent Xicluna | 992d9e0 | 2011-11-11 19:35:42 +0100 | [diff] [blame] | 334 | # Zero arguments constructor explicitly allowed |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 335 | x = ast.BinOp() |
| 336 | self.assertEqual(x._fields, ('left', 'op', 'right')) |
| 337 | |
| 338 | # Random attribute allowed too |
| 339 | x.foobarbaz = 5 |
| 340 | self.assertEqual(x.foobarbaz, 5) |
| 341 | |
| 342 | n1 = ast.Num(1) |
| 343 | n3 = ast.Num(3) |
| 344 | addop = ast.Add() |
| 345 | x = ast.BinOp(n1, addop, n3) |
| 346 | self.assertEqual(x.left, n1) |
| 347 | self.assertEqual(x.op, addop) |
| 348 | self.assertEqual(x.right, n3) |
Benjamin Peterson | 68b543a | 2011-06-27 17:51:18 -0500 | [diff] [blame] | 349 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 350 | x = ast.BinOp(1, 2, 3) |
| 351 | self.assertEqual(x.left, 1) |
| 352 | self.assertEqual(x.op, 2) |
| 353 | self.assertEqual(x.right, 3) |
| 354 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 355 | x = ast.BinOp(1, 2, 3, lineno=0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 356 | self.assertEqual(x.left, 1) |
| 357 | self.assertEqual(x.op, 2) |
| 358 | self.assertEqual(x.right, 3) |
| 359 | self.assertEqual(x.lineno, 0) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 360 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 361 | # node raises exception when given too many arguments |
| 362 | self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4) |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 363 | # node raises exception when given too many arguments |
| 364 | self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 365 | |
| 366 | # can set attributes through kwargs too |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 367 | x = ast.BinOp(left=1, op=2, right=3, lineno=0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 368 | self.assertEqual(x.left, 1) |
| 369 | self.assertEqual(x.op, 2) |
| 370 | self.assertEqual(x.right, 3) |
| 371 | self.assertEqual(x.lineno, 0) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 372 | |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 373 | # Random kwargs also allowed |
| 374 | x = ast.BinOp(1, 2, 3, foobarbaz=42) |
| 375 | self.assertEqual(x.foobarbaz, 42) |
| 376 | |
| 377 | def test_no_fields(self): |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 378 | # this used to fail because Sub._fields was None |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 379 | x = ast.Sub() |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 380 | self.assertEqual(x._fields, ()) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 381 | |
| 382 | def test_pickling(self): |
| 383 | import pickle |
| 384 | mods = [pickle] |
| 385 | try: |
| 386 | import cPickle |
| 387 | mods.append(cPickle) |
| 388 | except ImportError: |
| 389 | pass |
| 390 | protocols = [0, 1, 2] |
| 391 | for mod in mods: |
| 392 | for protocol in protocols: |
| 393 | for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests): |
| 394 | ast2 = mod.loads(mod.dumps(ast, protocol)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 395 | self.assertEqual(to_tuple(ast2), to_tuple(ast)) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 396 | |
Benjamin Peterson | 5b06681 | 2010-11-20 01:38:49 +0000 | [diff] [blame] | 397 | def test_invalid_sum(self): |
| 398 | pos = dict(lineno=2, col_offset=3) |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 399 | m = ast.Module([ast.Expr(ast.expr(**pos), **pos)]) |
Benjamin Peterson | 5b06681 | 2010-11-20 01:38:49 +0000 | [diff] [blame] | 400 | with self.assertRaises(TypeError) as cm: |
| 401 | compile(m, "<test>", "exec") |
| 402 | self.assertIn("but got <_ast.expr", str(cm.exception)) |
| 403 | |
Benjamin Peterson | 2193d2b | 2011-07-22 10:50:23 -0500 | [diff] [blame] | 404 | def test_invalid_identitifer(self): |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 405 | m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))]) |
Benjamin Peterson | 2193d2b | 2011-07-22 10:50:23 -0500 | [diff] [blame] | 406 | ast.fix_missing_locations(m) |
| 407 | with self.assertRaises(TypeError) as cm: |
| 408 | compile(m, "<test>", "exec") |
| 409 | self.assertIn("identifier must be of type str", str(cm.exception)) |
| 410 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 411 | def test_empty_yield_from(self): |
| 412 | # Issue 16546: yield from value is not optional. |
| 413 | empty_yield_from = ast.parse("def f():\n yield from g()") |
| 414 | empty_yield_from.body[0].body[0].value.value = None |
| 415 | with self.assertRaises(ValueError) as cm: |
| 416 | compile(empty_yield_from, "<test>", "exec") |
| 417 | self.assertIn("field value is required", str(cm.exception)) |
| 418 | |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 419 | @support.cpython_only |
| 420 | def test_issue31592(self): |
| 421 | # There shouldn't be an assertion failure in case of a bad |
| 422 | # unicodedata.normalize(). |
| 423 | import unicodedata |
| 424 | def bad_normalize(*args): |
| 425 | return None |
| 426 | with support.swap_attr(unicodedata, 'normalize', bad_normalize): |
| 427 | self.assertRaises(TypeError, ast.parse, '\u03D5') |
| 428 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 429 | |
| 430 | class ASTHelpers_Test(unittest.TestCase): |
| 431 | |
| 432 | def test_parse(self): |
| 433 | a = ast.parse('foo(1 + 1)') |
| 434 | b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST) |
| 435 | self.assertEqual(ast.dump(a), ast.dump(b)) |
| 436 | |
Benjamin Peterson | 2e2c903 | 2012-09-02 14:23:15 -0400 | [diff] [blame] | 437 | def test_parse_in_error(self): |
| 438 | try: |
| 439 | 1/0 |
| 440 | except Exception: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 441 | with self.assertRaises(SyntaxError) as e: |
| 442 | ast.literal_eval(r"'\U'") |
| 443 | self.assertIsNotNone(e.exception.__context__) |
Benjamin Peterson | 2e2c903 | 2012-09-02 14:23:15 -0400 | [diff] [blame] | 444 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 445 | def test_dump(self): |
| 446 | node = ast.parse('spam(eggs, "and cheese")') |
| 447 | self.assertEqual(ast.dump(node), |
| 448 | "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), " |
| 449 | "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], " |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 450 | "keywords=[]))])" |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 451 | ) |
| 452 | self.assertEqual(ast.dump(node, annotate_fields=False), |
| 453 | "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), " |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 454 | "Str('and cheese')], []))])" |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 455 | ) |
| 456 | self.assertEqual(ast.dump(node, include_attributes=True), |
| 457 | "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), " |
| 458 | "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), " |
| 459 | "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 460 | "col_offset=11)], keywords=[], " |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 461 | "lineno=1, col_offset=0), lineno=1, col_offset=0)])" |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 462 | ) |
| 463 | |
| 464 | def test_copy_location(self): |
| 465 | src = ast.parse('1 + 1', mode='eval') |
| 466 | src.body.right = ast.copy_location(ast.Num(2), src.body.right) |
| 467 | self.assertEqual(ast.dump(src, include_attributes=True), |
| 468 | 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), ' |
| 469 | 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, ' |
| 470 | 'col_offset=0))' |
| 471 | ) |
| 472 | |
| 473 | def test_fix_missing_locations(self): |
| 474 | src = ast.parse('write("spam")') |
| 475 | src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()), |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 476 | [ast.Str('eggs')], []))) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 477 | self.assertEqual(src, ast.fix_missing_locations(src)) |
| 478 | self.assertEqual(ast.dump(src, include_attributes=True), |
| 479 | "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), " |
| 480 | "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 481 | "col_offset=6)], keywords=[], " |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 482 | "lineno=1, col_offset=0), lineno=1, col_offset=0), " |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 483 | "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, " |
| 484 | "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 485 | "keywords=[], lineno=1, " |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 486 | "col_offset=0), lineno=1, col_offset=0)])" |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 487 | ) |
| 488 | |
| 489 | def test_increment_lineno(self): |
| 490 | src = ast.parse('1 + 1', mode='eval') |
| 491 | self.assertEqual(ast.increment_lineno(src, n=3), src) |
| 492 | self.assertEqual(ast.dump(src, include_attributes=True), |
| 493 | 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' |
| 494 | 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' |
| 495 | 'col_offset=0))' |
| 496 | ) |
Georg Brandl | 619e7ba | 2011-01-09 07:38:51 +0000 | [diff] [blame] | 497 | # issue10869: do not increment lineno of root twice |
Georg Brandl | efb6902 | 2011-01-09 07:50:48 +0000 | [diff] [blame] | 498 | src = ast.parse('1 + 1', mode='eval') |
Georg Brandl | 619e7ba | 2011-01-09 07:38:51 +0000 | [diff] [blame] | 499 | self.assertEqual(ast.increment_lineno(src.body, n=3), src.body) |
| 500 | self.assertEqual(ast.dump(src, include_attributes=True), |
| 501 | 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' |
| 502 | 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' |
| 503 | 'col_offset=0))' |
| 504 | ) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 505 | |
| 506 | def test_iter_fields(self): |
| 507 | node = ast.parse('foo()', mode='eval') |
| 508 | d = dict(ast.iter_fields(node.body)) |
| 509 | self.assertEqual(d.pop('func').id, 'foo') |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 510 | self.assertEqual(d, {'keywords': [], 'args': []}) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 511 | |
| 512 | def test_iter_child_nodes(self): |
| 513 | node = ast.parse("spam(23, 42, eggs='leek')", mode='eval') |
| 514 | self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4) |
| 515 | iterator = ast.iter_child_nodes(node.body) |
| 516 | self.assertEqual(next(iterator).id, 'spam') |
| 517 | self.assertEqual(next(iterator).n, 23) |
| 518 | self.assertEqual(next(iterator).n, 42) |
| 519 | self.assertEqual(ast.dump(next(iterator)), |
| 520 | "keyword(arg='eggs', value=Str(s='leek'))" |
| 521 | ) |
| 522 | |
| 523 | def test_get_docstring(self): |
| 524 | node = ast.parse('def foo():\n """line one\n line two"""') |
| 525 | self.assertEqual(ast.get_docstring(node.body[0]), |
| 526 | 'line one\nline two') |
| 527 | |
Yury Selivanov | 2f07a66 | 2015-07-23 08:54:35 +0300 | [diff] [blame] | 528 | node = ast.parse('async def foo():\n """spam\n ham"""') |
| 529 | self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham') |
Matthias Bussonnier | 41cea70 | 2017-02-23 22:44:19 -0800 | [diff] [blame] | 530 | self.assertIsNone(ast.get_docstring(ast.parse(''))) |
Yury Selivanov | 2f07a66 | 2015-07-23 08:54:35 +0300 | [diff] [blame] | 531 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 532 | def test_literal_eval(self): |
| 533 | self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3]) |
| 534 | self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42}) |
| 535 | self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None)) |
Benjamin Peterson | 3e74289 | 2010-07-11 12:59:24 +0000 | [diff] [blame] | 536 | self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3}) |
Benjamin Peterson | 5ef96e5 | 2010-07-11 23:06:06 +0000 | [diff] [blame] | 537 | self.assertEqual(ast.literal_eval('b"hi"'), b"hi") |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 538 | self.assertRaises(ValueError, ast.literal_eval, 'foo()') |
Serhiy Storchaka | d8ac4d1 | 2018-01-04 11:15:39 +0200 | [diff] [blame] | 539 | self.assertEqual(ast.literal_eval('6'), 6) |
| 540 | self.assertEqual(ast.literal_eval('+6'), 6) |
Raymond Hettinger | bc95973 | 2010-10-08 00:47:45 +0000 | [diff] [blame] | 541 | self.assertEqual(ast.literal_eval('-6'), -6) |
Raymond Hettinger | bc95973 | 2010-10-08 00:47:45 +0000 | [diff] [blame] | 542 | self.assertEqual(ast.literal_eval('3.25'), 3.25) |
Serhiy Storchaka | d8ac4d1 | 2018-01-04 11:15:39 +0200 | [diff] [blame] | 543 | self.assertEqual(ast.literal_eval('+3.25'), 3.25) |
| 544 | self.assertEqual(ast.literal_eval('-3.25'), -3.25) |
| 545 | self.assertEqual(repr(ast.literal_eval('-0.0')), '-0.0') |
| 546 | self.assertRaises(ValueError, ast.literal_eval, '++6') |
| 547 | self.assertRaises(ValueError, ast.literal_eval, '+True') |
| 548 | self.assertRaises(ValueError, ast.literal_eval, '2+3') |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 549 | |
Serhiy Storchaka | d8ac4d1 | 2018-01-04 11:15:39 +0200 | [diff] [blame] | 550 | def test_literal_eval_complex(self): |
| 551 | # Issue #4907 |
| 552 | self.assertEqual(ast.literal_eval('6j'), 6j) |
| 553 | self.assertEqual(ast.literal_eval('-6j'), -6j) |
| 554 | self.assertEqual(ast.literal_eval('6.75j'), 6.75j) |
| 555 | self.assertEqual(ast.literal_eval('-6.75j'), -6.75j) |
| 556 | self.assertEqual(ast.literal_eval('3+6j'), 3+6j) |
| 557 | self.assertEqual(ast.literal_eval('-3+6j'), -3+6j) |
| 558 | self.assertEqual(ast.literal_eval('3-6j'), 3-6j) |
| 559 | self.assertEqual(ast.literal_eval('-3-6j'), -3-6j) |
| 560 | self.assertEqual(ast.literal_eval('3.25+6.75j'), 3.25+6.75j) |
| 561 | self.assertEqual(ast.literal_eval('-3.25+6.75j'), -3.25+6.75j) |
| 562 | self.assertEqual(ast.literal_eval('3.25-6.75j'), 3.25-6.75j) |
| 563 | self.assertEqual(ast.literal_eval('-3.25-6.75j'), -3.25-6.75j) |
| 564 | self.assertEqual(ast.literal_eval('(3+6j)'), 3+6j) |
| 565 | self.assertRaises(ValueError, ast.literal_eval, '-6j+3') |
| 566 | self.assertRaises(ValueError, ast.literal_eval, '-6j+3j') |
| 567 | self.assertRaises(ValueError, ast.literal_eval, '3+-6j') |
| 568 | self.assertRaises(ValueError, ast.literal_eval, '3+(0+6j)') |
| 569 | self.assertRaises(ValueError, ast.literal_eval, '-(3+6j)') |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 570 | |
Amaury Forgeot d'Arc | 58e8761 | 2011-11-22 21:51:55 +0100 | [diff] [blame] | 571 | def test_bad_integer(self): |
| 572 | # issue13436: Bad error message with invalid numeric values |
| 573 | body = [ast.ImportFrom(module='time', |
| 574 | names=[ast.alias(name='sleep')], |
| 575 | level=None, |
| 576 | lineno=None, col_offset=None)] |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 577 | mod = ast.Module(body) |
Amaury Forgeot d'Arc | 58e8761 | 2011-11-22 21:51:55 +0100 | [diff] [blame] | 578 | with self.assertRaises(ValueError) as cm: |
| 579 | compile(mod, 'test', 'exec') |
| 580 | self.assertIn("invalid integer value: None", str(cm.exception)) |
| 581 | |
Berker Peksag | 0a5bd51 | 2016-04-29 19:50:02 +0300 | [diff] [blame] | 582 | def test_level_as_none(self): |
| 583 | body = [ast.ImportFrom(module='time', |
| 584 | names=[ast.alias(name='sleep')], |
| 585 | level=None, |
| 586 | lineno=0, col_offset=0)] |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 587 | mod = ast.Module(body) |
Berker Peksag | 0a5bd51 | 2016-04-29 19:50:02 +0300 | [diff] [blame] | 588 | code = compile(mod, 'test', 'exec') |
| 589 | ns = {} |
| 590 | exec(code, ns) |
| 591 | self.assertIn('sleep', ns) |
| 592 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 593 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 594 | class ASTValidatorTests(unittest.TestCase): |
| 595 | |
| 596 | def mod(self, mod, msg=None, mode="exec", *, exc=ValueError): |
| 597 | mod.lineno = mod.col_offset = 0 |
| 598 | ast.fix_missing_locations(mod) |
| 599 | with self.assertRaises(exc) as cm: |
| 600 | compile(mod, "<test>", mode) |
| 601 | if msg is not None: |
| 602 | self.assertIn(msg, str(cm.exception)) |
| 603 | |
| 604 | def expr(self, node, msg=None, *, exc=ValueError): |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 605 | mod = ast.Module([ast.Expr(node)]) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 606 | self.mod(mod, msg, exc=exc) |
| 607 | |
| 608 | def stmt(self, stmt, msg=None): |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 609 | mod = ast.Module([stmt]) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 610 | self.mod(mod, msg) |
| 611 | |
| 612 | def test_module(self): |
| 613 | m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))]) |
| 614 | self.mod(m, "must have Load context", "single") |
| 615 | m = ast.Expression(ast.Name("x", ast.Store())) |
| 616 | self.mod(m, "must have Load context", "eval") |
| 617 | |
| 618 | def _check_arguments(self, fac, check): |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 619 | def arguments(args=None, vararg=None, |
| 620 | kwonlyargs=None, kwarg=None, |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 621 | defaults=None, kw_defaults=None): |
| 622 | if args is None: |
| 623 | args = [] |
| 624 | if kwonlyargs is None: |
| 625 | kwonlyargs = [] |
| 626 | if defaults is None: |
| 627 | defaults = [] |
| 628 | if kw_defaults is None: |
| 629 | kw_defaults = [] |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 630 | args = ast.arguments(args, vararg, kwonlyargs, kw_defaults, |
| 631 | kwarg, defaults) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 632 | return fac(args) |
| 633 | args = [ast.arg("x", ast.Name("x", ast.Store()))] |
| 634 | check(arguments(args=args), "must have Load context") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 635 | check(arguments(kwonlyargs=args), "must have Load context") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 636 | check(arguments(defaults=[ast.Num(3)]), |
| 637 | "more positional defaults than args") |
| 638 | check(arguments(kw_defaults=[ast.Num(4)]), |
| 639 | "length of kwonlyargs is not the same as kw_defaults") |
| 640 | args = [ast.arg("x", ast.Name("x", ast.Load()))] |
| 641 | check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]), |
| 642 | "must have Load context") |
| 643 | args = [ast.arg("a", ast.Name("x", ast.Load())), |
| 644 | ast.arg("b", ast.Name("y", ast.Load()))] |
| 645 | check(arguments(kwonlyargs=args, |
| 646 | kw_defaults=[None, ast.Name("x", ast.Store())]), |
| 647 | "must have Load context") |
| 648 | |
| 649 | def test_funcdef(self): |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 650 | a = ast.arguments([], None, [], [], None, []) |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 651 | f = ast.FunctionDef("x", a, [], [], None) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 652 | self.stmt(f, "empty body on FunctionDef") |
| 653 | f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())], |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 654 | None) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 655 | self.stmt(f, "must have Load context") |
| 656 | f = ast.FunctionDef("x", a, [ast.Pass()], [], |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 657 | ast.Name("x", ast.Store())) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 658 | self.stmt(f, "must have Load context") |
| 659 | def fac(args): |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 660 | return ast.FunctionDef("x", args, [ast.Pass()], [], None) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 661 | self._check_arguments(fac, self.stmt) |
| 662 | |
| 663 | def test_classdef(self): |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 664 | def cls(bases=None, keywords=None, body=None, decorator_list=None): |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 665 | if bases is None: |
| 666 | bases = [] |
| 667 | if keywords is None: |
| 668 | keywords = [] |
| 669 | if body is None: |
| 670 | body = [ast.Pass()] |
| 671 | if decorator_list is None: |
| 672 | decorator_list = [] |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 673 | return ast.ClassDef("myclass", bases, keywords, |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 674 | body, decorator_list) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 675 | self.stmt(cls(bases=[ast.Name("x", ast.Store())]), |
| 676 | "must have Load context") |
| 677 | self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]), |
| 678 | "must have Load context") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 679 | self.stmt(cls(body=[]), "empty body on ClassDef") |
| 680 | self.stmt(cls(body=[None]), "None disallowed") |
| 681 | self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]), |
| 682 | "must have Load context") |
| 683 | |
| 684 | def test_delete(self): |
| 685 | self.stmt(ast.Delete([]), "empty targets on Delete") |
| 686 | self.stmt(ast.Delete([None]), "None disallowed") |
| 687 | self.stmt(ast.Delete([ast.Name("x", ast.Load())]), |
| 688 | "must have Del context") |
| 689 | |
| 690 | def test_assign(self): |
| 691 | self.stmt(ast.Assign([], ast.Num(3)), "empty targets on Assign") |
| 692 | self.stmt(ast.Assign([None], ast.Num(3)), "None disallowed") |
| 693 | self.stmt(ast.Assign([ast.Name("x", ast.Load())], ast.Num(3)), |
| 694 | "must have Store context") |
| 695 | self.stmt(ast.Assign([ast.Name("x", ast.Store())], |
| 696 | ast.Name("y", ast.Store())), |
| 697 | "must have Load context") |
| 698 | |
| 699 | def test_augassign(self): |
| 700 | aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(), |
| 701 | ast.Name("y", ast.Load())) |
| 702 | self.stmt(aug, "must have Store context") |
| 703 | aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(), |
| 704 | ast.Name("y", ast.Store())) |
| 705 | self.stmt(aug, "must have Load context") |
| 706 | |
| 707 | def test_for(self): |
| 708 | x = ast.Name("x", ast.Store()) |
| 709 | y = ast.Name("y", ast.Load()) |
| 710 | p = ast.Pass() |
| 711 | self.stmt(ast.For(x, y, [], []), "empty body on For") |
| 712 | self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []), |
| 713 | "must have Store context") |
| 714 | self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []), |
| 715 | "must have Load context") |
| 716 | e = ast.Expr(ast.Name("x", ast.Store())) |
| 717 | self.stmt(ast.For(x, y, [e], []), "must have Load context") |
| 718 | self.stmt(ast.For(x, y, [p], [e]), "must have Load context") |
| 719 | |
| 720 | def test_while(self): |
| 721 | self.stmt(ast.While(ast.Num(3), [], []), "empty body on While") |
| 722 | self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []), |
| 723 | "must have Load context") |
| 724 | self.stmt(ast.While(ast.Num(3), [ast.Pass()], |
| 725 | [ast.Expr(ast.Name("x", ast.Store()))]), |
| 726 | "must have Load context") |
| 727 | |
| 728 | def test_if(self): |
| 729 | self.stmt(ast.If(ast.Num(3), [], []), "empty body on If") |
| 730 | i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], []) |
| 731 | self.stmt(i, "must have Load context") |
| 732 | i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], []) |
| 733 | self.stmt(i, "must have Load context") |
| 734 | i = ast.If(ast.Num(3), [ast.Pass()], |
| 735 | [ast.Expr(ast.Name("x", ast.Store()))]) |
| 736 | self.stmt(i, "must have Load context") |
| 737 | |
| 738 | def test_with(self): |
| 739 | p = ast.Pass() |
| 740 | self.stmt(ast.With([], [p]), "empty items on With") |
| 741 | i = ast.withitem(ast.Num(3), None) |
| 742 | self.stmt(ast.With([i], []), "empty body on With") |
| 743 | i = ast.withitem(ast.Name("x", ast.Store()), None) |
| 744 | self.stmt(ast.With([i], [p]), "must have Load context") |
| 745 | i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load())) |
| 746 | self.stmt(ast.With([i], [p]), "must have Store context") |
| 747 | |
| 748 | def test_raise(self): |
| 749 | r = ast.Raise(None, ast.Num(3)) |
| 750 | self.stmt(r, "Raise with cause but no exception") |
| 751 | r = ast.Raise(ast.Name("x", ast.Store()), None) |
| 752 | self.stmt(r, "must have Load context") |
| 753 | r = ast.Raise(ast.Num(4), ast.Name("x", ast.Store())) |
| 754 | self.stmt(r, "must have Load context") |
| 755 | |
| 756 | def test_try(self): |
| 757 | p = ast.Pass() |
| 758 | t = ast.Try([], [], [], [p]) |
| 759 | self.stmt(t, "empty body on Try") |
| 760 | t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p]) |
| 761 | self.stmt(t, "must have Load context") |
| 762 | t = ast.Try([p], [], [], []) |
| 763 | self.stmt(t, "Try has neither except handlers nor finalbody") |
| 764 | t = ast.Try([p], [], [p], [p]) |
| 765 | self.stmt(t, "Try has orelse but no except handlers") |
| 766 | t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], []) |
| 767 | self.stmt(t, "empty body on ExceptHandler") |
| 768 | e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])] |
| 769 | self.stmt(ast.Try([p], e, [], []), "must have Load context") |
| 770 | e = [ast.ExceptHandler(None, "x", [p])] |
| 771 | t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p]) |
| 772 | self.stmt(t, "must have Load context") |
| 773 | t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))]) |
| 774 | self.stmt(t, "must have Load context") |
| 775 | |
| 776 | def test_assert(self): |
| 777 | self.stmt(ast.Assert(ast.Name("x", ast.Store()), None), |
| 778 | "must have Load context") |
| 779 | assrt = ast.Assert(ast.Name("x", ast.Load()), |
| 780 | ast.Name("y", ast.Store())) |
| 781 | self.stmt(assrt, "must have Load context") |
| 782 | |
| 783 | def test_import(self): |
| 784 | self.stmt(ast.Import([]), "empty names on Import") |
| 785 | |
| 786 | def test_importfrom(self): |
| 787 | imp = ast.ImportFrom(None, [ast.alias("x", None)], -42) |
Serhiy Storchaka | 7de2840 | 2016-06-27 23:40:43 +0300 | [diff] [blame] | 788 | self.stmt(imp, "Negative ImportFrom level") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 789 | self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom") |
| 790 | |
| 791 | def test_global(self): |
| 792 | self.stmt(ast.Global([]), "empty names on Global") |
| 793 | |
| 794 | def test_nonlocal(self): |
| 795 | self.stmt(ast.Nonlocal([]), "empty names on Nonlocal") |
| 796 | |
| 797 | def test_expr(self): |
| 798 | e = ast.Expr(ast.Name("x", ast.Store())) |
| 799 | self.stmt(e, "must have Load context") |
| 800 | |
| 801 | def test_boolop(self): |
| 802 | b = ast.BoolOp(ast.And(), []) |
| 803 | self.expr(b, "less than 2 values") |
| 804 | b = ast.BoolOp(ast.And(), [ast.Num(3)]) |
| 805 | self.expr(b, "less than 2 values") |
| 806 | b = ast.BoolOp(ast.And(), [ast.Num(4), None]) |
| 807 | self.expr(b, "None disallowed") |
| 808 | b = ast.BoolOp(ast.And(), [ast.Num(4), ast.Name("x", ast.Store())]) |
| 809 | self.expr(b, "must have Load context") |
| 810 | |
| 811 | def test_unaryop(self): |
| 812 | u = ast.UnaryOp(ast.Not(), ast.Name("x", ast.Store())) |
| 813 | self.expr(u, "must have Load context") |
| 814 | |
| 815 | def test_lambda(self): |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 816 | a = ast.arguments([], None, [], [], None, []) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 817 | self.expr(ast.Lambda(a, ast.Name("x", ast.Store())), |
| 818 | "must have Load context") |
| 819 | def fac(args): |
| 820 | return ast.Lambda(args, ast.Name("x", ast.Load())) |
| 821 | self._check_arguments(fac, self.expr) |
| 822 | |
| 823 | def test_ifexp(self): |
| 824 | l = ast.Name("x", ast.Load()) |
| 825 | s = ast.Name("y", ast.Store()) |
| 826 | for args in (s, l, l), (l, s, l), (l, l, s): |
Benjamin Peterson | 71ce897 | 2011-08-09 16:17:12 -0500 | [diff] [blame] | 827 | self.expr(ast.IfExp(*args), "must have Load context") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 828 | |
| 829 | def test_dict(self): |
| 830 | d = ast.Dict([], [ast.Name("x", ast.Load())]) |
| 831 | self.expr(d, "same number of keys as values") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 832 | d = ast.Dict([ast.Name("x", ast.Load())], [None]) |
| 833 | self.expr(d, "None disallowed") |
| 834 | |
| 835 | def test_set(self): |
| 836 | self.expr(ast.Set([None]), "None disallowed") |
| 837 | s = ast.Set([ast.Name("x", ast.Store())]) |
| 838 | self.expr(s, "must have Load context") |
| 839 | |
| 840 | def _check_comprehension(self, fac): |
| 841 | self.expr(fac([]), "comprehension with no generators") |
| 842 | g = ast.comprehension(ast.Name("x", ast.Load()), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 843 | ast.Name("x", ast.Load()), [], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 844 | self.expr(fac([g]), "must have Store context") |
| 845 | g = ast.comprehension(ast.Name("x", ast.Store()), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 846 | ast.Name("x", ast.Store()), [], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 847 | self.expr(fac([g]), "must have Load context") |
| 848 | x = ast.Name("x", ast.Store()) |
| 849 | y = ast.Name("y", ast.Load()) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 850 | g = ast.comprehension(x, y, [None], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 851 | self.expr(fac([g]), "None disallowed") |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 852 | g = ast.comprehension(x, y, [ast.Name("x", ast.Store())], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 853 | self.expr(fac([g]), "must have Load context") |
| 854 | |
| 855 | def _simple_comp(self, fac): |
| 856 | g = ast.comprehension(ast.Name("x", ast.Store()), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 857 | ast.Name("x", ast.Load()), [], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 858 | self.expr(fac(ast.Name("x", ast.Store()), [g]), |
| 859 | "must have Load context") |
| 860 | def wrap(gens): |
| 861 | return fac(ast.Name("x", ast.Store()), gens) |
| 862 | self._check_comprehension(wrap) |
| 863 | |
| 864 | def test_listcomp(self): |
| 865 | self._simple_comp(ast.ListComp) |
| 866 | |
| 867 | def test_setcomp(self): |
| 868 | self._simple_comp(ast.SetComp) |
| 869 | |
| 870 | def test_generatorexp(self): |
| 871 | self._simple_comp(ast.GeneratorExp) |
| 872 | |
| 873 | def test_dictcomp(self): |
| 874 | g = ast.comprehension(ast.Name("y", ast.Store()), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 875 | ast.Name("p", ast.Load()), [], 0) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 876 | c = ast.DictComp(ast.Name("x", ast.Store()), |
| 877 | ast.Name("y", ast.Load()), [g]) |
| 878 | self.expr(c, "must have Load context") |
| 879 | c = ast.DictComp(ast.Name("x", ast.Load()), |
| 880 | ast.Name("y", ast.Store()), [g]) |
| 881 | self.expr(c, "must have Load context") |
| 882 | def factory(comps): |
| 883 | k = ast.Name("x", ast.Load()) |
| 884 | v = ast.Name("y", ast.Load()) |
| 885 | return ast.DictComp(k, v, comps) |
| 886 | self._check_comprehension(factory) |
| 887 | |
| 888 | def test_yield(self): |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 889 | self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load") |
| 890 | self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load") |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 891 | |
| 892 | def test_compare(self): |
| 893 | left = ast.Name("x", ast.Load()) |
| 894 | comp = ast.Compare(left, [ast.In()], []) |
| 895 | self.expr(comp, "no comparators") |
| 896 | comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)]) |
| 897 | self.expr(comp, "different number of comparators and operands") |
| 898 | comp = ast.Compare(ast.Num("blah"), [ast.In()], [left]) |
| 899 | self.expr(comp, "non-numeric", exc=TypeError) |
| 900 | comp = ast.Compare(left, [ast.In()], [ast.Num("blah")]) |
| 901 | self.expr(comp, "non-numeric", exc=TypeError) |
| 902 | |
| 903 | def test_call(self): |
| 904 | func = ast.Name("x", ast.Load()) |
| 905 | args = [ast.Name("y", ast.Load())] |
| 906 | keywords = [ast.keyword("w", ast.Name("z", ast.Load()))] |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 907 | call = ast.Call(ast.Name("x", ast.Store()), args, keywords) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 908 | self.expr(call, "must have Load context") |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 909 | call = ast.Call(func, [None], keywords) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 910 | self.expr(call, "None disallowed") |
| 911 | bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))] |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 912 | call = ast.Call(func, args, bad_keywords) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 913 | self.expr(call, "must have Load context") |
| 914 | |
| 915 | def test_num(self): |
| 916 | class subint(int): |
| 917 | pass |
| 918 | class subfloat(float): |
| 919 | pass |
| 920 | class subcomplex(complex): |
| 921 | pass |
| 922 | for obj in "0", "hello", subint(), subfloat(), subcomplex(): |
| 923 | self.expr(ast.Num(obj), "non-numeric", exc=TypeError) |
| 924 | |
| 925 | def test_attribute(self): |
| 926 | attr = ast.Attribute(ast.Name("x", ast.Store()), "y", ast.Load()) |
| 927 | self.expr(attr, "must have Load context") |
| 928 | |
| 929 | def test_subscript(self): |
| 930 | sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)), |
| 931 | ast.Load()) |
| 932 | self.expr(sub, "must have Load context") |
| 933 | x = ast.Name("x", ast.Load()) |
| 934 | sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())), |
| 935 | ast.Load()) |
| 936 | self.expr(sub, "must have Load context") |
| 937 | s = ast.Name("x", ast.Store()) |
| 938 | for args in (s, None, None), (None, s, None), (None, None, s): |
| 939 | sl = ast.Slice(*args) |
| 940 | self.expr(ast.Subscript(x, sl, ast.Load()), |
| 941 | "must have Load context") |
| 942 | sl = ast.ExtSlice([]) |
| 943 | self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice") |
| 944 | sl = ast.ExtSlice([ast.Index(s)]) |
| 945 | self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context") |
| 946 | |
| 947 | def test_starred(self): |
| 948 | left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())], |
| 949 | ast.Store()) |
| 950 | assign = ast.Assign([left], ast.Num(4)) |
| 951 | self.stmt(assign, "must have Store context") |
| 952 | |
| 953 | def _sequence(self, fac): |
| 954 | self.expr(fac([None], ast.Load()), "None disallowed") |
| 955 | self.expr(fac([ast.Name("x", ast.Store())], ast.Load()), |
| 956 | "must have Load context") |
| 957 | |
| 958 | def test_list(self): |
| 959 | self._sequence(ast.List) |
| 960 | |
| 961 | def test_tuple(self): |
| 962 | self._sequence(ast.Tuple) |
| 963 | |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 964 | def test_nameconstant(self): |
| 965 | self.expr(ast.NameConstant(4), "singleton must be True, False, or None") |
| 966 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 967 | def test_stdlib_validates(self): |
| 968 | stdlib = os.path.dirname(ast.__file__) |
| 969 | tests = [fn for fn in os.listdir(stdlib) if fn.endswith(".py")] |
| 970 | tests.extend(["test/test_grammar.py", "test/test_unpack_ex.py"]) |
| 971 | for module in tests: |
| 972 | fn = os.path.join(stdlib, module) |
| 973 | with open(fn, "r", encoding="utf-8") as fp: |
| 974 | source = fp.read() |
Victor Stinner | d502a07 | 2013-03-22 00:06:20 +0100 | [diff] [blame] | 975 | mod = ast.parse(source, fn) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 976 | compile(mod, fn, "exec") |
| 977 | |
| 978 | |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 979 | class ConstantTests(unittest.TestCase): |
| 980 | """Tests on the ast.Constant node type.""" |
| 981 | |
| 982 | def compile_constant(self, value): |
| 983 | tree = ast.parse("x = 123") |
| 984 | |
| 985 | node = tree.body[0].value |
| 986 | new_node = ast.Constant(value=value) |
| 987 | ast.copy_location(new_node, node) |
| 988 | tree.body[0].value = new_node |
| 989 | |
| 990 | code = compile(tree, "<string>", "exec") |
| 991 | |
| 992 | ns = {} |
| 993 | exec(code, ns) |
| 994 | return ns['x'] |
| 995 | |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 996 | def test_validation(self): |
| 997 | with self.assertRaises(TypeError) as cm: |
| 998 | self.compile_constant([1, 2, 3]) |
| 999 | self.assertEqual(str(cm.exception), |
| 1000 | "got an invalid type in Constant: list") |
| 1001 | |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1002 | def test_singletons(self): |
| 1003 | for const in (None, False, True, Ellipsis, b'', frozenset()): |
| 1004 | with self.subTest(const=const): |
| 1005 | value = self.compile_constant(const) |
| 1006 | self.assertIs(value, const) |
| 1007 | |
| 1008 | def test_values(self): |
| 1009 | nested_tuple = (1,) |
| 1010 | nested_frozenset = frozenset({1}) |
| 1011 | for level in range(3): |
| 1012 | nested_tuple = (nested_tuple, 2) |
| 1013 | nested_frozenset = frozenset({nested_frozenset, 2}) |
| 1014 | values = (123, 123.0, 123j, |
| 1015 | "unicode", b'bytes', |
| 1016 | tuple("tuple"), frozenset("frozenset"), |
| 1017 | nested_tuple, nested_frozenset) |
| 1018 | for value in values: |
| 1019 | with self.subTest(value=value): |
| 1020 | result = self.compile_constant(value) |
| 1021 | self.assertEqual(result, value) |
| 1022 | |
| 1023 | def test_assign_to_constant(self): |
| 1024 | tree = ast.parse("x = 1") |
| 1025 | |
| 1026 | target = tree.body[0].targets[0] |
| 1027 | new_target = ast.Constant(value=1) |
| 1028 | ast.copy_location(new_target, target) |
| 1029 | tree.body[0].targets[0] = new_target |
| 1030 | |
| 1031 | with self.assertRaises(ValueError) as cm: |
| 1032 | compile(tree, "string", "exec") |
| 1033 | self.assertEqual(str(cm.exception), |
| 1034 | "expression which can't be assigned " |
| 1035 | "to in Store context") |
| 1036 | |
| 1037 | def test_get_docstring(self): |
| 1038 | tree = ast.parse("'docstring'\nx = 1") |
| 1039 | self.assertEqual(ast.get_docstring(tree), 'docstring') |
| 1040 | |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1041 | def get_load_const(self, tree): |
| 1042 | # Compile to bytecode, disassemble and get parameter of LOAD_CONST |
| 1043 | # instructions |
| 1044 | co = compile(tree, '<string>', 'exec') |
| 1045 | consts = [] |
| 1046 | for instr in dis.get_instructions(co): |
| 1047 | if instr.opname == 'LOAD_CONST': |
| 1048 | consts.append(instr.argval) |
| 1049 | return consts |
| 1050 | |
| 1051 | @support.cpython_only |
| 1052 | def test_load_const(self): |
| 1053 | consts = [None, |
| 1054 | True, False, |
| 1055 | 124, |
| 1056 | 2.0, |
| 1057 | 3j, |
| 1058 | "unicode", |
| 1059 | b'bytes', |
| 1060 | (1, 2, 3)] |
| 1061 | |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame] | 1062 | code = '\n'.join(['x={!r}'.format(const) for const in consts]) |
| 1063 | code += '\nx = ...' |
| 1064 | consts.extend((Ellipsis, None)) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1065 | |
| 1066 | tree = ast.parse(code) |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame] | 1067 | self.assertEqual(self.get_load_const(tree), |
| 1068 | consts) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1069 | |
| 1070 | # Replace expression nodes with constants |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame] | 1071 | for assign, const in zip(tree.body, consts): |
| 1072 | assert isinstance(assign, ast.Assign), ast.dump(assign) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1073 | new_node = ast.Constant(value=const) |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame] | 1074 | ast.copy_location(new_node, assign.value) |
| 1075 | assign.value = new_node |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1076 | |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame] | 1077 | self.assertEqual(self.get_load_const(tree), |
| 1078 | consts) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1079 | |
| 1080 | def test_literal_eval(self): |
| 1081 | tree = ast.parse("1 + 2") |
| 1082 | binop = tree.body[0].value |
| 1083 | |
| 1084 | new_left = ast.Constant(value=10) |
| 1085 | ast.copy_location(new_left, binop.left) |
| 1086 | binop.left = new_left |
| 1087 | |
Serhiy Storchaka | d8ac4d1 | 2018-01-04 11:15:39 +0200 | [diff] [blame] | 1088 | new_right = ast.Constant(value=20j) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1089 | ast.copy_location(new_right, binop.right) |
| 1090 | binop.right = new_right |
| 1091 | |
Serhiy Storchaka | d8ac4d1 | 2018-01-04 11:15:39 +0200 | [diff] [blame] | 1092 | self.assertEqual(ast.literal_eval(binop), 10+20j) |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1093 | |
| 1094 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 1095 | def main(): |
| 1096 | if __name__ != '__main__': |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1097 | return |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 1098 | if sys.argv[1:] == ['-g']: |
| 1099 | for statements, kind in ((exec_tests, "exec"), (single_tests, "single"), |
| 1100 | (eval_tests, "eval")): |
| 1101 | print(kind+"_results = [") |
Victor Stinner | f089196 | 2016-02-08 17:15:21 +0100 | [diff] [blame] | 1102 | for statement in statements: |
| 1103 | tree = ast.parse(statement, "?", kind) |
| 1104 | print("%r," % (to_tuple(tree),)) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 1105 | print("]") |
| 1106 | print("main()") |
| 1107 | raise SystemExit |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 1108 | unittest.main() |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1109 | |
| 1110 | #### EVERYTHING BELOW IS GENERATED ##### |
| 1111 | exec_results = [ |
Serhiy Storchaka | 2641ee5 | 2018-05-29 10:49:10 +0300 | [diff] [blame^] | 1112 | ('Module', [('Expr', (1, 0), ('NameConstant', (1, 0), None))]), |
| 1113 | ('Module', [('Expr', (1, 0), ('Str', (1, 0), 'module docstring'))]), |
| 1114 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (1, 9))], [], None)]), |
| 1115 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (1, 9), ('Str', (1, 9), 'function docstring'))], [], None)]), |
| 1116 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, []), [('Pass', (1, 10))], [], None)]), |
| 1117 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [], None)]), |
| 1118 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], ('arg', (1, 7), 'args', None), [], [], None, []), [('Pass', (1, 14))], [], None)]), |
| 1119 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], ('arg', (1, 8), 'kwargs', None), []), [('Pass', (1, 17))], [], None)]), |
| 1120 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None), ('arg', (1, 9), 'b', None), ('arg', (1, 14), 'c', None), ('arg', (1, 22), 'd', None), ('arg', (1, 28), 'e', None)], ('arg', (1, 35), 'args', None), [('arg', (1, 41), 'f', None)], [('Num', (1, 43), 42)], ('arg', (1, 49), 'kwargs', None), [('Num', (1, 11), 1), ('NameConstant', (1, 16), None), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Expr', (1, 58), ('Str', (1, 58), 'doc for f()'))], [], None)]), |
| 1121 | ('Module', [('ClassDef', (1, 0), 'C', [], [], [('Pass', (1, 8))], [])]), |
| 1122 | ('Module', [('ClassDef', (1, 0), 'C', [], [], [('Expr', (1, 9), ('Str', (1, 9), 'docstring for class C'))], [])]), |
| 1123 | ('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], [('Pass', (1, 17))], [])]), |
| 1124 | ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]), |
| 1125 | ('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]), |
| 1126 | ('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]), |
| 1127 | ('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]), |
| 1128 | ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]), |
| 1129 | ('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]), |
| 1130 | ('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]), |
| 1131 | ('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))])]), |
| 1132 | ('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))])]), |
| 1133 | ('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], []), None)]), |
| 1134 | ('Module', [('Try', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [], [])]), |
| 1135 | ('Module', [('Try', (1, 0), [('Pass', (2, 2))], [], [], [('Pass', (4, 2))])]), |
| 1136 | ('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]), |
| 1137 | ('Module', [('Import', (1, 0), [('alias', 'sys', None)])]), |
| 1138 | ('Module', [('ImportFrom', (1, 0), 'sys', [('alias', 'v', None)], 0)]), |
| 1139 | ('Module', [('Global', (1, 0), ['v'])]), |
| 1140 | ('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]), |
| 1141 | ('Module', [('Pass', (1, 0))]), |
| 1142 | ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Break', (1, 11))], [])]), |
| 1143 | ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Continue', (1, 11))], [])]), |
| 1144 | ('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))], [])]), |
| 1145 | ('Module', [('Expr', (1, 0), ('ListComp', (1, 1), ('Tuple', (1, 2), [('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)]))]), |
| 1146 | ('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('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)]))]), |
| 1147 | ('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 12), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)]))]), |
| 1148 | ('Module', [('Expr', (1, 0), ('GeneratorExp', (2, 4), ('Tuple', (3, 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)]))]), |
| 1149 | ('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)]))]), |
| 1150 | ('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)]))]), |
| 1151 | ('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)]))]), |
| 1152 | ('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)]))]), |
| 1153 | ('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Str', (2, 1), 'async function')), ('Expr', (3, 1), ('Await', (3, 1), ('Call', (3, 7), ('Name', (3, 7), 'something', ('Load',)), [], [])))], [], None)]), |
| 1154 | ('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncFor', (2, 7), ('Name', (2, 11), 'e', ('Store',)), ('Name', (2, 16), 'i', ('Load',)), [('Expr', (2, 19), ('Num', (2, 19), 1))], [('Expr', (3, 7), ('Num', (3, 7), 2))])], [], None)]), |
| 1155 | ('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncWith', (2, 7), [('withitem', ('Name', (2, 12), 'a', ('Load',)), ('Name', (2, 17), 'b', ('Store',)))], [('Expr', (2, 20), ('Num', (2, 20), 1))])], [], None)]), |
| 1156 | ('Module', [('Expr', (1, 0), ('Dict', (1, 0), [None, ('Num', (1, 10), 2)], [('Dict', (1, 3), [('Num', (1, 4), 1)], [('Num', (1, 6), 2)]), ('Num', (1, 12), 3)]))]), |
| 1157 | ('Module', [('Expr', (1, 0), ('Set', (1, 0), [('Starred', (1, 1), ('Set', (1, 2), [('Num', (1, 3), 1), ('Num', (1, 6), 2)]), ('Load',)), ('Num', (1, 10), 3)]))]), |
| 1158 | ('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('ListComp', (2, 2), ('Name', (2, 2), 'i', ('Load',)), [('comprehension', ('Name', (2, 14), 'b', ('Store',)), ('Name', (2, 19), 'c', ('Load',)), [], 1)]))], [], None)]), |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1159 | ] |
| 1160 | single_results = [ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1161 | ('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]), |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1162 | ] |
| 1163 | eval_results = [ |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1164 | ('Expression', ('NameConstant', (1, 0), None)), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1165 | ('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])), |
| 1166 | ('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))), |
| 1167 | ('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))), |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1168 | ('Expression', ('Lambda', (1, 0), ('arguments', [], None, [], [], None, []), ('NameConstant', (1, 7), None))), |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 1169 | ('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])), |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 1170 | ('Expression', ('Dict', (1, 0), [], [])), |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 1171 | ('Expression', ('Set', (1, 0), [('NameConstant', (1, 1), None)])), |
| 1172 | ('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1173 | ('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])), |
| 1174 | ('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1175 | ('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])), |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1176 | ('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2), ('Starred', (1, 10), ('Name', (1, 11), 'd', ('Load',)), ('Load',))], [('keyword', 'c', ('Num', (1, 8), 3)), ('keyword', None, ('Name', (1, 15), 'e', ('Load',)))])), |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1177 | ('Expression', ('Num', (1, 0), 10)), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1178 | ('Expression', ('Str', (1, 0), 'string')), |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1179 | ('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))), |
| 1180 | ('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] | 1181 | ('Expression', ('Name', (1, 0), 'v', ('Load',))), |
| 1182 | ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))), |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 1183 | ('Expression', ('List', (1, 0), [], ('Load',))), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1184 | ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))), |
Benjamin Peterson | 6ccfe85 | 2011-06-27 17:46:06 -0500 | [diff] [blame] | 1185 | ('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))), |
| 1186 | ('Expression', ('Tuple', (1, 0), [], ('Load',))), |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1187 | ('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', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [])), |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1188 | ] |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 1189 | main() |