blob: ac3899baedb629626d376cbf5cdda2366732bc73 [file] [log] [blame]
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03001import copy
Fred Drake79ca79d2000-08-21 22:30:53 +00002import parser
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +03003import pickle
Fred Drake58422e52001-06-04 03:56:24 +00004import unittest
Mark Dickinson211c6252009-02-01 10:28:51 +00005import operator
Jesus Ceae9c53182012-08-03 14:28:37 +02006import struct
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Berker Peksagce643912015-05-06 06:33:17 +03008from test.support.script_helper import assert_python_failure
Fred Drake79ca79d2000-08-21 22:30:53 +00009
10#
11# First, we test that we can generate trees from valid source fragments,
12# and that these valid trees are indeed allowed by the tree-loading side
13# of the parser module.
14#
15
Fred Drake58422e52001-06-04 03:56:24 +000016class RoundtripLegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000017
Fred Drake58422e52001-06-04 03:56:24 +000018 def roundtrip(self, f, s):
19 st1 = f(s)
20 t = st1.totuple()
21 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +000022 st2 = parser.sequence2st(t)
Guido van Rossumb940e112007-01-10 16:19:56 +000023 except parser.ParserError as why:
Anthony Baxterc2a5a632004-08-02 06:10:11 +000024 self.fail("could not roundtrip %r: %s" % (s, why))
Fred Drake79ca79d2000-08-21 22:30:53 +000025
Ezio Melottib3aedd42010-11-20 19:04:17 +000026 self.assertEqual(t, st2.totuple(),
27 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000028
Fred Drake58422e52001-06-04 03:56:24 +000029 def check_expr(self, s):
30 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000031
Benjamin Petersonf216c942008-10-31 02:28:05 +000032 def test_flags_passed(self):
Mike53f7a7c2017-12-14 14:04:53 +030033 # The unicode literals flags has to be passed from the parser to AST
Benjamin Petersonf216c942008-10-31 02:28:05 +000034 # generation.
35 suite = parser.suite("from __future__ import unicode_literals; x = ''")
36 code = suite.compile()
37 scope = {}
38 exec(code, {}, scope)
Ezio Melottie9615932010-01-24 19:26:24 +000039 self.assertIsInstance(scope["x"], str)
Benjamin Petersonf216c942008-10-31 02:28:05 +000040
Fred Drake58422e52001-06-04 03:56:24 +000041 def check_suite(self, s):
42 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000043
Fred Drakecf580c72001-07-17 03:01:29 +000044 def test_yield_statement(self):
Tim Peters496563a2002-04-01 00:28:59 +000045 self.check_suite("def f(): yield 1")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000046 self.check_suite("def f(): yield")
47 self.check_suite("def f(): x += yield")
48 self.check_suite("def f(): x = yield 1")
49 self.check_suite("def f(): x = y = yield 1")
50 self.check_suite("def f(): x = yield")
51 self.check_suite("def f(): x = y = yield")
52 self.check_suite("def f(): 1 + (yield)*2")
53 self.check_suite("def f(): (yield 1)*2")
Tim Peters496563a2002-04-01 00:28:59 +000054 self.check_suite("def f(): return; yield 1")
55 self.check_suite("def f(): yield 1; return")
Nick Coghlan1f7ce622012-01-13 21:43:40 +100056 self.check_suite("def f(): yield from 1")
57 self.check_suite("def f(): x = yield from 1")
58 self.check_suite("def f(): f((yield from 1))")
59 self.check_suite("def f(): yield 1; return 1")
Tim Peters496563a2002-04-01 00:28:59 +000060 self.check_suite("def f():\n"
Fred Drakecf580c72001-07-17 03:01:29 +000061 " for x in range(30):\n"
62 " yield x\n")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000063 self.check_suite("def f():\n"
64 " if (yield):\n"
65 " yield x\n")
Fred Drakecf580c72001-07-17 03:01:29 +000066
Yury Selivanov75445082015-05-11 22:57:16 -040067 def test_await_statement(self):
68 self.check_suite("async def f():\n await smth()")
69 self.check_suite("async def f():\n foo = await smth()")
70 self.check_suite("async def f():\n foo, bar = await smth()")
71 self.check_suite("async def f():\n (await smth())")
72 self.check_suite("async def f():\n foo((await smth()))")
73 self.check_suite("async def f():\n await foo(); return 42")
74
75 def test_async_with_statement(self):
76 self.check_suite("async def f():\n async with 1: pass")
77 self.check_suite("async def f():\n async with a as b, c as d: pass")
78
79 def test_async_for_statement(self):
80 self.check_suite("async def f():\n async for i in (): pass")
81 self.check_suite("async def f():\n async for i, b in (): pass")
82
Mark Dickinson407b3bd2012-04-29 22:18:31 +010083 def test_nonlocal_statement(self):
84 self.check_suite("def f():\n"
85 " x = 0\n"
86 " def g():\n"
87 " nonlocal x\n")
88 self.check_suite("def f():\n"
89 " x = y = 0\n"
90 " def g():\n"
91 " nonlocal x, y\n")
92
Fred Drake58422e52001-06-04 03:56:24 +000093 def test_expressions(self):
94 self.check_expr("foo(1)")
95 self.check_expr("[1, 2, 3]")
96 self.check_expr("[x**3 for x in range(20)]")
97 self.check_expr("[x**3 for x in range(20) if x % 3]")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
99 self.check_expr("list(x**3 for x in range(20))")
100 self.check_expr("list(x**3 for x in range(20) if x % 3)")
101 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Fred Drake58422e52001-06-04 03:56:24 +0000102 self.check_expr("foo(*args)")
103 self.check_expr("foo(*args, **kw)")
104 self.check_expr("foo(**kw)")
105 self.check_expr("foo(key=value)")
106 self.check_expr("foo(key=value, *args)")
107 self.check_expr("foo(key=value, *args, **kw)")
108 self.check_expr("foo(key=value, **kw)")
109 self.check_expr("foo(a, b, c, *args)")
110 self.check_expr("foo(a, b, c, *args, **kw)")
111 self.check_expr("foo(a, b, c, **kw)")
Benjamin Peterson3938a902008-08-20 02:33:00 +0000112 self.check_expr("foo(a, *args, keyword=23)")
Fred Drake58422e52001-06-04 03:56:24 +0000113 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000114 self.check_expr("foo - bar")
115 self.check_expr("foo * bar")
116 self.check_expr("foo / bar")
117 self.check_expr("foo // bar")
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700118 self.check_expr("(foo := 1)")
Fred Drake58422e52001-06-04 03:56:24 +0000119 self.check_expr("lambda: 0")
120 self.check_expr("lambda x: 0")
121 self.check_expr("lambda *y: 0")
122 self.check_expr("lambda *y, **z: 0")
123 self.check_expr("lambda **z: 0")
124 self.check_expr("lambda x, y: 0")
125 self.check_expr("lambda foo=bar: 0")
126 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
127 self.check_expr("lambda foo=bar, **z: 0")
128 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
129 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
130 self.check_expr("lambda x, *y, **z: 0")
Raymond Hettinger354433a2004-05-19 08:20:33 +0000131 self.check_expr("(x for x in range(10))")
132 self.check_expr("foo(x for x in range(10))")
Mark Dickinsonda029fb2012-05-07 17:24:04 +0100133 self.check_expr("...")
134 self.check_expr("a[...]")
Fred Drake79ca79d2000-08-21 22:30:53 +0000135
Fred Drake58422e52001-06-04 03:56:24 +0000136 def test_simple_expression(self):
137 # expr_stmt
138 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +0000139
Fred Drake58422e52001-06-04 03:56:24 +0000140 def test_simple_assignments(self):
141 self.check_suite("a = b")
142 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +0000143
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700144 def test_var_annot(self):
145 self.check_suite("x: int = 5")
146 self.check_suite("y: List[T] = []; z: [list] = fun()")
147 self.check_suite("x: tuple = (1, 2)")
148 self.check_suite("d[f()]: int = 42")
149 self.check_suite("f(d[x]): str = 'abc'")
150 self.check_suite("x.y.z.w: complex = 42j")
151 self.check_suite("x: int")
152 self.check_suite("def f():\n"
153 " x: str\n"
154 " y: int = 5\n")
155 self.check_suite("class C:\n"
156 " x: str\n"
157 " y: int = 5\n")
158 self.check_suite("class C:\n"
159 " def __init__(self, x: int) -> None:\n"
160 " self.x: int = x\n")
161 # double check for nonsense
162 with self.assertRaises(SyntaxError):
163 exec("2+2: int", {}, {})
164 with self.assertRaises(SyntaxError):
165 exec("[]: int = 5", {}, {})
166 with self.assertRaises(SyntaxError):
167 exec("x, *y, z: int = range(5)", {}, {})
168 with self.assertRaises(SyntaxError):
169 exec("t: tuple = 1, 2", {}, {})
170 with self.assertRaises(SyntaxError):
171 exec("u = v: int", {}, {})
172 with self.assertRaises(SyntaxError):
173 exec("False: int", {}, {})
174 with self.assertRaises(SyntaxError):
175 exec("x.False: int", {}, {})
176 with self.assertRaises(SyntaxError):
177 exec("x.y,: int", {}, {})
178 with self.assertRaises(SyntaxError):
179 exec("[0]: int", {}, {})
180 with self.assertRaises(SyntaxError):
181 exec("f(): int", {}, {})
182
Fred Drake58422e52001-06-04 03:56:24 +0000183 def test_simple_augmented_assignments(self):
184 self.check_suite("a += b")
185 self.check_suite("a -= b")
186 self.check_suite("a *= b")
187 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000188 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000189 self.check_suite("a %= b")
190 self.check_suite("a &= b")
191 self.check_suite("a |= b")
192 self.check_suite("a ^= b")
193 self.check_suite("a <<= b")
194 self.check_suite("a >>= b")
195 self.check_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000196
Fred Drake58422e52001-06-04 03:56:24 +0000197 def test_function_defs(self):
198 self.check_suite("def f(): pass")
199 self.check_suite("def f(*args): pass")
200 self.check_suite("def f(*args, **kw): pass")
201 self.check_suite("def f(**kw): pass")
202 self.check_suite("def f(foo=bar): pass")
203 self.check_suite("def f(foo=bar, *args): pass")
204 self.check_suite("def f(foo=bar, *args, **kw): pass")
205 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000206
Fred Drake58422e52001-06-04 03:56:24 +0000207 self.check_suite("def f(a, b): pass")
208 self.check_suite("def f(a, b, *args): pass")
209 self.check_suite("def f(a, b, *args, **kw): pass")
210 self.check_suite("def f(a, b, **kw): pass")
211 self.check_suite("def f(a, b, foo=bar): pass")
212 self.check_suite("def f(a, b, foo=bar, *args): pass")
213 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
214 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000215
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000216 self.check_suite("@staticmethod\n"
217 "def f(): pass")
218 self.check_suite("@staticmethod\n"
219 "@funcattrs(x, y)\n"
220 "def f(): pass")
221 self.check_suite("@funcattrs()\n"
222 "def f(): pass")
223
Mark Dickinsonea7e9f92012-04-29 18:34:40 +0100224 # keyword-only arguments
225 self.check_suite("def f(*, a): pass")
226 self.check_suite("def f(*, a = 5): pass")
227 self.check_suite("def f(*, a = 5, b): pass")
228 self.check_suite("def f(*, a, b = 5): pass")
229 self.check_suite("def f(*, a, b = 5, **kwds): pass")
230 self.check_suite("def f(*args, a): pass")
231 self.check_suite("def f(*args, a = 5): pass")
232 self.check_suite("def f(*args, a = 5, b): pass")
233 self.check_suite("def f(*args, a, b = 5): pass")
234 self.check_suite("def f(*args, a, b = 5, **kwds): pass")
235
236 # function annotations
237 self.check_suite("def f(a: int): pass")
238 self.check_suite("def f(a: int = 5): pass")
239 self.check_suite("def f(*args: list): pass")
240 self.check_suite("def f(**kwds: dict): pass")
241 self.check_suite("def f(*, a: int): pass")
242 self.check_suite("def f(*, a: int = 5): pass")
243 self.check_suite("def f() -> int: pass")
244
Brett Cannonf4189912005-04-09 02:30:16 +0000245 def test_class_defs(self):
246 self.check_suite("class foo():pass")
Guido van Rossumfc158e22007-11-15 19:17:28 +0000247 self.check_suite("class foo(object):pass")
Mark Dickinson2bd61a92010-07-04 16:37:31 +0000248 self.check_suite("@class_decorator\n"
249 "class foo():pass")
250 self.check_suite("@class_decorator(arg)\n"
251 "class foo():pass")
252 self.check_suite("@decorator1\n"
253 "@decorator2\n"
254 "class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000255
Fred Drake58422e52001-06-04 03:56:24 +0000256 def test_import_from_statement(self):
257 self.check_suite("from sys.path import *")
258 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000259 self.check_suite("from sys.path import (dirname)")
260 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000261 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000262 self.check_suite("from sys.path import (dirname as my_dirname)")
263 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000264 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000265 self.check_suite("from sys.path import (dirname, basename)")
266 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000267 self.check_suite(
268 "from sys.path import dirname as my_dirname, basename")
269 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000270 "from sys.path import (dirname as my_dirname, basename)")
271 self.check_suite(
272 "from sys.path import (dirname as my_dirname, basename,)")
273 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000274 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000275 self.check_suite(
276 "from sys.path import (dirname, basename as my_basename)")
277 self.check_suite(
278 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000279 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000280
Fred Drake58422e52001-06-04 03:56:24 +0000281 def test_basic_import_statement(self):
282 self.check_suite("import sys")
283 self.check_suite("import sys as system")
284 self.check_suite("import sys, math")
285 self.check_suite("import sys as system, math")
286 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000287
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000288 def test_relative_imports(self):
289 self.check_suite("from . import name")
290 self.check_suite("from .. import name")
Mark Dickinsonfeb3b752010-07-04 18:38:57 +0000291 # check all the way up to '....', since '...' is tokenized
292 # differently from '.' (it's an ellipsis token).
293 self.check_suite("from ... import name")
294 self.check_suite("from .... import name")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000295 self.check_suite("from .pkg import name")
296 self.check_suite("from ..pkg import name")
Mark Dickinsonfeb3b752010-07-04 18:38:57 +0000297 self.check_suite("from ...pkg import name")
298 self.check_suite("from ....pkg import name")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000299
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000300 def test_pep263(self):
301 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
302 "pass\n")
303
304 def test_assert(self):
305 self.check_suite("assert alo < ahi and blo < bhi\n")
306
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000307 def test_with(self):
308 self.check_suite("with open('x'): pass\n")
309 self.check_suite("with open('x') as f: pass\n")
Georg Brandl0c315622009-05-25 21:10:36 +0000310 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000311
Georg Brandleee31162008-12-07 15:15:22 +0000312 def test_try_stmt(self):
313 self.check_suite("try: pass\nexcept: pass\n")
314 self.check_suite("try: pass\nfinally: pass\n")
315 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
316 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
317 "finally: pass\n")
318 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
319 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
320 "finally: pass\n")
321
Thomas Wouters89f507f2006-12-13 04:49:30 +0000322 def test_position(self):
323 # An absolutely minimal test of position information. Better
324 # tests would be a big project.
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000325 code = "def f(x):\n return x + 1"
Serhiy Storchakae5362ea2018-04-19 01:55:37 +0300326 st = parser.suite(code)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000327
328 def walk(tree):
329 node_type = tree[0]
330 next = tree[1]
Serhiy Storchakae5362ea2018-04-19 01:55:37 +0300331 if isinstance(next, (tuple, list)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000332 for elt in tree[1:]:
333 for x in walk(elt):
334 yield x
335 else:
336 yield tree
337
Serhiy Storchakae5362ea2018-04-19 01:55:37 +0300338 expected = [
Thomas Wouters89f507f2006-12-13 04:49:30 +0000339 (1, 'def', 1, 0),
340 (1, 'f', 1, 4),
341 (7, '(', 1, 5),
342 (1, 'x', 1, 6),
343 (8, ')', 1, 7),
344 (11, ':', 1, 8),
345 (4, '', 1, 9),
346 (5, '', 2, -1),
347 (1, 'return', 2, 4),
348 (1, 'x', 2, 11),
349 (14, '+', 2, 13),
350 (2, '1', 2, 15),
351 (4, '', 2, 16),
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000352 (6, '', 2, -1),
353 (4, '', 2, -1),
Serhiy Storchakae5362ea2018-04-19 01:55:37 +0300354 (0, '', 2, -1),
355 ]
356
357 self.assertEqual(list(walk(st.totuple(line_info=True, col_info=True))),
358 expected)
359 self.assertEqual(list(walk(st.totuple())),
360 [(t, n) for t, n, l, c in expected])
361 self.assertEqual(list(walk(st.totuple(line_info=True))),
362 [(t, n, l) for t, n, l, c in expected])
363 self.assertEqual(list(walk(st.totuple(col_info=True))),
364 [(t, n, c) for t, n, l, c in expected])
365 self.assertEqual(list(walk(st.tolist(line_info=True, col_info=True))),
366 [list(x) for x in expected])
367 self.assertEqual(list(walk(parser.st2tuple(st, line_info=True,
368 col_info=True))),
369 expected)
370 self.assertEqual(list(walk(parser.st2list(st, line_info=True,
371 col_info=True))),
372 [list(x) for x in expected])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000373
Benjamin Peterson4905e802009-09-27 02:43:28 +0000374 def test_extended_unpacking(self):
375 self.check_suite("*a = y")
376 self.check_suite("x, *b, = m")
377 self.check_suite("[*a, *b] = y")
378 self.check_suite("for [*x, b] in x: pass")
379
Mark Dickinsoncf360b92012-05-07 12:01:27 +0100380 def test_raise_statement(self):
381 self.check_suite("raise\n")
382 self.check_suite("raise e\n")
383 self.check_suite("try:\n"
384 " suite\n"
385 "except Exception as e:\n"
386 " raise ValueError from e\n")
387
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400388 def test_list_displays(self):
389 self.check_expr('[]')
390 self.check_expr('[*{2}, 3, *[4]]')
391
Mark Dickinson11c1dee2012-05-07 16:34:34 +0100392 def test_set_displays(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400393 self.check_expr('{*{2}, 3, *[4]}')
Mark Dickinson11c1dee2012-05-07 16:34:34 +0100394 self.check_expr('{2}')
395 self.check_expr('{2,}')
396 self.check_expr('{2, 3}')
397 self.check_expr('{2, 3,}')
398
399 def test_dict_displays(self):
400 self.check_expr('{}')
401 self.check_expr('{a:b}')
402 self.check_expr('{a:b,}')
403 self.check_expr('{a:b, c:d}')
404 self.check_expr('{a:b, c:d,}')
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400405 self.check_expr('{**{}}')
406 self.check_expr('{**{}, 3:4, **{5:6, 7:8}}')
407
408 def test_argument_unpacking(self):
Yury Selivanov50a26142015-08-05 17:59:45 -0400409 self.check_expr("f(*a, **b)")
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400410 self.check_expr('f(a, *b, *c, *d)')
411 self.check_expr('f(**a, **b)')
412 self.check_expr('f(2, *a, *b, **b, **c, **d)')
Yury Selivanov50a26142015-08-05 17:59:45 -0400413 self.check_expr("f(*b, *() or () and (), **{} and {}, **() or {})")
Mark Dickinson11c1dee2012-05-07 16:34:34 +0100414
415 def test_set_comprehensions(self):
416 self.check_expr('{x for x in seq}')
417 self.check_expr('{f(x) for x in seq}')
418 self.check_expr('{f(x) for x in seq if condition(x)}')
419
420 def test_dict_comprehensions(self):
421 self.check_expr('{x:x for x in seq}')
422 self.check_expr('{x**2:x[3] for x in seq if condition(x)}')
423 self.check_expr('{x:x for x in seq1 for y in seq2 if condition(x, y)}')
424
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700425 def test_named_expressions(self):
426 self.check_suite("(a := 1)")
427 self.check_suite("(a := a)")
428 self.check_suite("if (match := pattern.search(data)) is None: pass")
429 self.check_suite("[y := f(x), y**2, y**3]")
430 self.check_suite("filtered_data = [y for x in data if (y := f(x)) is None]")
431 self.check_suite("(y := f(x))")
432 self.check_suite("y0 = (y1 := f(x))")
433 self.check_suite("foo(x=(y := f(x)))")
434 self.check_suite("def foo(answer=(p := 42)): pass")
435 self.check_suite("def foo(answer: (p := 42) = 5): pass")
436 self.check_suite("lambda: (x := 1)")
437 self.check_suite("(x := lambda: 1)")
438 self.check_suite("(x := lambda: (y := 1))") # not in PEP
439 self.check_suite("lambda line: (m := re.match(pattern, line)) and m.group(1)")
440 self.check_suite("x = (y := 0)")
441 self.check_suite("(z:=(y:=(x:=0)))")
442 self.check_suite("(info := (name, phone, *rest))")
443 self.check_suite("(x:=1,2)")
444 self.check_suite("(total := total + tax)")
445 self.check_suite("len(lines := f.readlines())")
446 self.check_suite("foo(x := 3, cat='vector')")
447 self.check_suite("foo(cat=(category := 'vector'))")
448 self.check_suite("if any(len(longline := l) >= 100 for l in lines): print(longline)")
449 self.check_suite(
450 "if env_base := os.environ.get('PYTHONUSERBASE', None): return env_base"
451 )
452 self.check_suite(
453 "if self._is_special and (ans := self._check_nans(context=context)): return ans"
454 )
455 self.check_suite("foo(b := 2, a=1)")
456 self.check_suite("foo(b := 2, a=1)")
457 self.check_suite("foo((b := 2), a=1)")
458 self.check_suite("foo(c=(b := 2), a=1)")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000459
Fred Drake79ca79d2000-08-21 22:30:53 +0000460#
461# Second, we take *invalid* trees and make sure we get ParserError
462# rejections for them.
463#
464
Fred Drake58422e52001-06-04 03:56:24 +0000465class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000466
Fred Drake58422e52001-06-04 03:56:24 +0000467 def check_bad_tree(self, tree, label):
468 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000469 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000470 except parser.ParserError:
471 pass
472 else:
473 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000474
Fred Drake58422e52001-06-04 03:56:24 +0000475 def test_junk(self):
476 # not even remotely valid:
477 self.check_bad_tree((1, 2, 3), "<junk>")
478
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300479 def test_illegal_terminal(self):
480 tree = \
481 (257,
482 (269,
483 (270,
484 (271,
485 (277,
486 (1,))),
487 (4, ''))),
488 (4, ''),
489 (0, ''))
490 self.check_bad_tree(tree, "too small items in terminal node")
491 tree = \
492 (257,
493 (269,
494 (270,
495 (271,
496 (277,
497 (1, b'pass'))),
498 (4, ''))),
499 (4, ''),
500 (0, ''))
501 self.check_bad_tree(tree, "non-string second item in terminal node")
502 tree = \
503 (257,
504 (269,
505 (270,
506 (271,
507 (277,
508 (1, 'pass', '0', 0))),
509 (4, ''))),
510 (4, ''),
511 (0, ''))
512 self.check_bad_tree(tree, "non-integer third item in terminal node")
513 tree = \
514 (257,
515 (269,
516 (270,
517 (271,
518 (277,
519 (1, 'pass', 0, 0))),
520 (4, ''))),
521 (4, ''),
522 (0, ''))
523 self.check_bad_tree(tree, "too many items in terminal node")
524
Fred Drakecf580c72001-07-17 03:01:29 +0000525 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000526 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000527 tree = \
528 (257,
529 (264,
530 (285,
531 (259,
532 (1, 'def'),
533 (1, 'f'),
534 (260, (7, '('), (8, ')')),
535 (11, ':'),
536 (291,
537 (4, ''),
538 (5, ''),
539 (264,
540 (265,
541 (266,
542 (272,
543 (275,
544 (1, 'return'),
545 (313,
546 (292,
547 (293,
548 (294,
549 (295,
550 (297,
551 (298,
552 (299,
553 (300,
554 (301,
555 (302, (303, (304, (305, (2, '1')))))))))))))))))),
556 (264,
557 (265,
558 (266,
559 (272,
560 (276,
561 (1, 'yield'),
562 (313,
563 (292,
564 (293,
565 (294,
566 (295,
567 (297,
568 (298,
569 (299,
570 (300,
571 (301,
572 (302,
573 (303, (304, (305, (2, '1')))))))))))))))))),
574 (4, ''))),
575 (6, ''))))),
576 (4, ''),
577 (0, ''))))
578 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
579
580 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000581 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000582 tree = \
583 (257,
584 (264,
585 (265,
586 (266,
587 (278,
588 (1, 'from'),
589 (281, (1, '__future__')),
590 (1, 'import'),
591 (279, (1, 'generators')))),
592 (4, ''))),
593 (264,
594 (285,
595 (259,
596 (1, 'def'),
597 (1, 'f'),
598 (260, (7, '('), (8, ')')),
599 (11, ':'),
600 (291,
601 (4, ''),
602 (5, ''),
603 (264,
604 (265,
605 (266,
606 (272,
607 (275,
608 (1, 'return'),
609 (313,
610 (292,
611 (293,
612 (294,
613 (295,
614 (297,
615 (298,
616 (299,
617 (300,
618 (301,
619 (302, (303, (304, (305, (2, '1')))))))))))))))))),
620 (264,
621 (265,
622 (266,
623 (272,
624 (276,
625 (1, 'yield'),
626 (313,
627 (292,
628 (293,
629 (294,
630 (295,
631 (297,
632 (298,
633 (299,
634 (300,
635 (301,
636 (302,
637 (303, (304, (305, (2, '1')))))))))))))))))),
638 (4, ''))),
639 (6, ''))))),
640 (4, ''),
641 (0, ''))))
642 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
643
Fred Drake58422e52001-06-04 03:56:24 +0000644 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000645 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000646 tree = \
647 (258,
648 (311,
649 (290,
650 (291,
651 (292,
652 (293,
653 (295,
654 (296,
655 (297,
656 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
657 (12, ','),
658 (12, ','),
659 (290,
660 (291,
661 (292,
662 (293,
663 (295,
664 (296,
665 (297,
666 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
667 (4, ''),
668 (0, ''))
669 self.check_bad_tree(tree, "a,,c")
670
671 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000672 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000673 tree = \
674 (257,
675 (264,
676 (265,
677 (266,
678 (267,
679 (312,
680 (291,
681 (292,
682 (293,
683 (294,
684 (296,
685 (297,
686 (298,
687 (299,
688 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
689 (268, (37, '$=')),
690 (312,
691 (291,
692 (292,
693 (293,
694 (294,
695 (296,
696 (297,
697 (298,
698 (299,
699 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
700 (4, ''))),
701 (0, ''))
702 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000703
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000704 def test_malformed_global(self):
705 #doesn't have global keyword in ast
706 tree = (257,
707 (264,
708 (265,
709 (266,
710 (282, (1, 'foo'))), (4, ''))),
711 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000712 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000713 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000714
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000715 def test_missing_import_source(self):
Mark Dickinson3445b482010-07-04 18:15:26 +0000716 # from import fred
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000717 tree = \
718 (257,
Mark Dickinson3445b482010-07-04 18:15:26 +0000719 (268,
720 (269,
721 (270,
722 (282,
723 (284, (1, 'from'), (1, 'import'),
724 (287, (285, (1, 'fred')))))),
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000725 (4, ''))),
726 (4, ''), (0, ''))
Mark Dickinson3445b482010-07-04 18:15:26 +0000727 self.check_bad_tree(tree, "from import fred")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000728
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300729 def test_illegal_encoding(self):
730 # Illegal encoding declaration
731 tree = \
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700732 (341,
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300733 (257, (0, '')))
734 self.check_bad_tree(tree, "missed encoding")
735 tree = \
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700736 (341,
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300737 (257, (0, '')),
738 b'iso-8859-1')
739 self.check_bad_tree(tree, "non-string encoding")
740 tree = \
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700741 (341,
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300742 (257, (0, '')),
743 '\udcff')
744 with self.assertRaises(UnicodeEncodeError):
745 parser.sequence2st(tree)
746
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747
748class CompileTestCase(unittest.TestCase):
749
750 # These tests are very minimal. :-(
751
752 def test_compile_expr(self):
753 st = parser.expr('2 + 3')
754 code = parser.compilest(st)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000755 self.assertEqual(eval(code), 5)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756
757 def test_compile_suite(self):
758 st = parser.suite('x = 2; y = x + 3')
759 code = parser.compilest(st)
760 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000761 exec(code, globs)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000762 self.assertEqual(globs['y'], 5)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763
764 def test_compile_error(self):
765 st = parser.suite('1 = 3 + 4')
766 self.assertRaises(SyntaxError, parser.compilest, st)
767
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000768 def test_compile_badunicode(self):
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000769 st = parser.suite('a = "\\U12345678"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000770 self.assertRaises(SyntaxError, parser.compilest, st)
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000771 st = parser.suite('a = "\\u1"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000772 self.assertRaises(SyntaxError, parser.compilest, st)
773
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000774 def test_issue_9011(self):
775 # Issue 9011: compilation of an unary minus expression changed
776 # the meaning of the ST, so that a second compilation produced
777 # incorrect results.
778 st = parser.expr('-3')
779 code1 = parser.compilest(st)
780 self.assertEqual(eval(code1), -3)
781 code2 = parser.compilest(st)
782 self.assertEqual(eval(code2), -3)
783
Serhiy Storchaka9305d832016-06-18 13:53:36 +0300784 def test_compile_filename(self):
785 st = parser.expr('a + 5')
786 code = parser.compilest(st)
787 self.assertEqual(code.co_filename, '<syntax-tree>')
788 code = st.compile()
789 self.assertEqual(code.co_filename, '<syntax-tree>')
Serhiy Storchakafebc3322016-08-06 23:29:29 +0300790 for filename in 'file.py', b'file.py':
Serhiy Storchaka9305d832016-06-18 13:53:36 +0300791 code = parser.compilest(st, filename)
792 self.assertEqual(code.co_filename, 'file.py')
793 code = st.compile(filename)
794 self.assertEqual(code.co_filename, 'file.py')
Serhiy Storchakafebc3322016-08-06 23:29:29 +0300795 for filename in bytearray(b'file.py'), memoryview(b'file.py'):
796 with self.assertWarns(DeprecationWarning):
797 code = parser.compilest(st, filename)
798 self.assertEqual(code.co_filename, 'file.py')
799 with self.assertWarns(DeprecationWarning):
800 code = st.compile(filename)
801 self.assertEqual(code.co_filename, 'file.py')
Serhiy Storchaka9305d832016-06-18 13:53:36 +0300802 self.assertRaises(TypeError, parser.compilest, st, list(b'file.py'))
803 self.assertRaises(TypeError, st.compile, list(b'file.py'))
804
805
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000806class ParserStackLimitTestCase(unittest.TestCase):
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000807 """try to push the parser to/over its limits.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000808 see http://bugs.python.org/issue1881 for a discussion
809 """
810 def _nested_expression(self, level):
811 return "["*level+"]"*level
812
813 def test_deeply_nested_list(self):
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700814 # This has fluctuated between 99 levels in 2.x, down to 93 levels in
815 # 3.7.X and back up to 99 in 3.8.X. Related to MAXSTACK size in Parser.h
816 e = self._nested_expression(99)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000817 st = parser.expr(e)
818 st.compile()
819
820 def test_trigger_memory_error(self):
821 e = self._nested_expression(100)
Ezio Melotti39191842013-03-09 22:17:33 +0200822 rc, out, err = assert_python_failure('-c', e)
823 # parsing the expression will result in an error message
824 # followed by a MemoryError (see #11963)
Ezio Melottie7c32992013-03-10 03:25:45 +0200825 self.assertIn(b's_push: parser stack overflow', err)
826 self.assertIn(b'MemoryError', err)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000827
Mark Dickinson211c6252009-02-01 10:28:51 +0000828class STObjectTestCase(unittest.TestCase):
829 """Test operations on ST objects themselves"""
830
831 def test_comparisons(self):
832 # ST objects should support order and equality comparisons
833 st1 = parser.expr('2 + 3')
834 st2 = parser.suite('x = 2; y = x + 3')
835 st3 = parser.expr('list(x**3 for x in range(20))')
836 st1_copy = parser.expr('2 + 3')
837 st2_copy = parser.suite('x = 2; y = x + 3')
838 st3_copy = parser.expr('list(x**3 for x in range(20))')
839
840 # exercise fast path for object identity
Ezio Melottib3aedd42010-11-20 19:04:17 +0000841 self.assertEqual(st1 == st1, True)
842 self.assertEqual(st2 == st2, True)
843 self.assertEqual(st3 == st3, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000844 # slow path equality
845 self.assertEqual(st1, st1_copy)
846 self.assertEqual(st2, st2_copy)
847 self.assertEqual(st3, st3_copy)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000848 self.assertEqual(st1 == st2, False)
849 self.assertEqual(st1 == st3, False)
850 self.assertEqual(st2 == st3, False)
851 self.assertEqual(st1 != st1, False)
852 self.assertEqual(st2 != st2, False)
853 self.assertEqual(st3 != st3, False)
854 self.assertEqual(st1 != st1_copy, False)
855 self.assertEqual(st2 != st2_copy, False)
856 self.assertEqual(st3 != st3_copy, False)
857 self.assertEqual(st2 != st1, True)
858 self.assertEqual(st1 != st3, True)
859 self.assertEqual(st3 != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000860 # we don't particularly care what the ordering is; just that
861 # it's usable and self-consistent
Ezio Melottib3aedd42010-11-20 19:04:17 +0000862 self.assertEqual(st1 < st2, not (st2 <= st1))
863 self.assertEqual(st1 < st3, not (st3 <= st1))
864 self.assertEqual(st2 < st3, not (st3 <= st2))
865 self.assertEqual(st1 < st2, st2 > st1)
866 self.assertEqual(st1 < st3, st3 > st1)
867 self.assertEqual(st2 < st3, st3 > st2)
868 self.assertEqual(st1 <= st2, st2 >= st1)
869 self.assertEqual(st3 <= st1, st1 >= st3)
870 self.assertEqual(st2 <= st3, st3 >= st2)
Mark Dickinson211c6252009-02-01 10:28:51 +0000871 # transitivity
872 bottom = min(st1, st2, st3)
873 top = max(st1, st2, st3)
874 mid = sorted([st1, st2, st3])[1]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000875 self.assertTrue(bottom < mid)
876 self.assertTrue(bottom < top)
877 self.assertTrue(mid < top)
878 self.assertTrue(bottom <= mid)
879 self.assertTrue(bottom <= top)
880 self.assertTrue(mid <= top)
881 self.assertTrue(bottom <= bottom)
882 self.assertTrue(mid <= mid)
883 self.assertTrue(top <= top)
Mark Dickinson211c6252009-02-01 10:28:51 +0000884 # interaction with other types
Ezio Melottib3aedd42010-11-20 19:04:17 +0000885 self.assertEqual(st1 == 1588.602459, False)
886 self.assertEqual('spanish armada' != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000887 self.assertRaises(TypeError, operator.ge, st3, None)
888 self.assertRaises(TypeError, operator.le, False, st1)
889 self.assertRaises(TypeError, operator.lt, st1, 1815)
890 self.assertRaises(TypeError, operator.gt, b'waterloo', st2)
891
Serhiy Storchakaa79f4c22017-04-19 21:09:21 +0300892 def test_copy_pickle(self):
893 sts = [
894 parser.expr('2 + 3'),
895 parser.suite('x = 2; y = x + 3'),
896 parser.expr('list(x**3 for x in range(20))')
897 ]
898 for st in sts:
899 st_copy = copy.copy(st)
900 self.assertEqual(st_copy.totuple(), st.totuple())
901 st_copy = copy.deepcopy(st)
902 self.assertEqual(st_copy.totuple(), st.totuple())
903 for proto in range(pickle.HIGHEST_PROTOCOL+1):
904 st_copy = pickle.loads(pickle.dumps(st, proto))
905 self.assertEqual(st_copy.totuple(), st.totuple())
906
Jesus Ceae9c53182012-08-03 14:28:37 +0200907 check_sizeof = support.check_sizeof
908
909 @support.cpython_only
910 def test_sizeof(self):
911 def XXXROUNDUP(n):
912 if n <= 1:
913 return n
914 if n <= 128:
915 return (n + 3) & ~3
916 return 1 << (n - 1).bit_length()
917
918 basesize = support.calcobjsize('Pii')
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000919 nodesize = struct.calcsize('hP3iP0h2i')
Jesus Ceae9c53182012-08-03 14:28:37 +0200920 def sizeofchildren(node):
921 if node is None:
922 return 0
923 res = 0
924 hasstr = len(node) > 1 and isinstance(node[-1], str)
925 if hasstr:
926 res += len(node[-1]) + 1
927 children = node[1:-1] if hasstr else node[1:]
928 if children:
929 res += XXXROUNDUP(len(children)) * nodesize
Jesus Ceae9c53182012-08-03 14:28:37 +0200930 for child in children:
931 res += sizeofchildren(child)
932 return res
933
934 def check_st_sizeof(st):
935 self.check_sizeof(st, basesize + nodesize +
936 sizeofchildren(st.totuple()))
937
938 check_st_sizeof(parser.expr('2 + 3'))
939 check_st_sizeof(parser.expr('2 + 3 + 4'))
940 check_st_sizeof(parser.suite('x = 2 + 3'))
941 check_st_sizeof(parser.suite(''))
942 check_st_sizeof(parser.suite('# -*- coding: utf-8 -*-'))
943 check_st_sizeof(parser.expr('[' + '2,' * 1000 + ']'))
944
Mark Dickinson211c6252009-02-01 10:28:51 +0000945
946 # XXX tests for pickling and unpickling of ST objects should go here
947
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500948class OtherParserCase(unittest.TestCase):
949
950 def test_two_args_to_expr(self):
951 # See bug #12264
952 with self.assertRaises(TypeError):
953 parser.expr("a", "b")
954
Fred Drake2e2be372001-09-20 21:33:42 +0000955if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500956 unittest.main()