blob: ab6577f44d7e15303419ff2940cd1e83eb4364a6 [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Fred Drake58422e52001-06-04 03:56:24 +00002import unittest
Christian Heimesb186d002008-03-18 15:15:01 +00003import sys
Mark Dickinson211c6252009-02-01 10:28:51 +00004import operator
Jesus Ceae9c53182012-08-03 14:28:37 +02005import struct
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Berker Peksagce643912015-05-06 06:33:17 +03007from test.support.script_helper import assert_python_failure
Fred Drake79ca79d2000-08-21 22:30:53 +00008
9#
10# First, we test that we can generate trees from valid source fragments,
11# and that these valid trees are indeed allowed by the tree-loading side
12# of the parser module.
13#
14
Fred Drake58422e52001-06-04 03:56:24 +000015class RoundtripLegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000016
Fred Drake58422e52001-06-04 03:56:24 +000017 def roundtrip(self, f, s):
18 st1 = f(s)
19 t = st1.totuple()
20 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +000021 st2 = parser.sequence2st(t)
Guido van Rossumb940e112007-01-10 16:19:56 +000022 except parser.ParserError as why:
Anthony Baxterc2a5a632004-08-02 06:10:11 +000023 self.fail("could not roundtrip %r: %s" % (s, why))
Fred Drake79ca79d2000-08-21 22:30:53 +000024
Ezio Melottib3aedd42010-11-20 19:04:17 +000025 self.assertEqual(t, st2.totuple(),
26 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000027
Fred Drake58422e52001-06-04 03:56:24 +000028 def check_expr(self, s):
29 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000030
Benjamin Petersonf216c942008-10-31 02:28:05 +000031 def test_flags_passed(self):
32 # The unicode literals flags has to be passed from the paser to AST
33 # generation.
34 suite = parser.suite("from __future__ import unicode_literals; x = ''")
35 code = suite.compile()
36 scope = {}
37 exec(code, {}, scope)
Ezio Melottie9615932010-01-24 19:26:24 +000038 self.assertIsInstance(scope["x"], str)
Benjamin Petersonf216c942008-10-31 02:28:05 +000039
Fred Drake58422e52001-06-04 03:56:24 +000040 def check_suite(self, s):
41 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000042
Fred Drakecf580c72001-07-17 03:01:29 +000043 def test_yield_statement(self):
Tim Peters496563a2002-04-01 00:28:59 +000044 self.check_suite("def f(): yield 1")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000045 self.check_suite("def f(): yield")
46 self.check_suite("def f(): x += yield")
47 self.check_suite("def f(): x = yield 1")
48 self.check_suite("def f(): x = y = yield 1")
49 self.check_suite("def f(): x = yield")
50 self.check_suite("def f(): x = y = yield")
51 self.check_suite("def f(): 1 + (yield)*2")
52 self.check_suite("def f(): (yield 1)*2")
Tim Peters496563a2002-04-01 00:28:59 +000053 self.check_suite("def f(): return; yield 1")
54 self.check_suite("def f(): yield 1; return")
Nick Coghlan1f7ce622012-01-13 21:43:40 +100055 self.check_suite("def f(): yield from 1")
56 self.check_suite("def f(): x = yield from 1")
57 self.check_suite("def f(): f((yield from 1))")
58 self.check_suite("def f(): yield 1; return 1")
Tim Peters496563a2002-04-01 00:28:59 +000059 self.check_suite("def f():\n"
Fred Drakecf580c72001-07-17 03:01:29 +000060 " for x in range(30):\n"
61 " yield x\n")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000062 self.check_suite("def f():\n"
63 " if (yield):\n"
64 " yield x\n")
Fred Drakecf580c72001-07-17 03:01:29 +000065
Yury Selivanov75445082015-05-11 22:57:16 -040066 def test_await_statement(self):
67 self.check_suite("async def f():\n await smth()")
68 self.check_suite("async def f():\n foo = await smth()")
69 self.check_suite("async def f():\n foo, bar = await smth()")
70 self.check_suite("async def f():\n (await smth())")
71 self.check_suite("async def f():\n foo((await smth()))")
72 self.check_suite("async def f():\n await foo(); return 42")
73
74 def test_async_with_statement(self):
75 self.check_suite("async def f():\n async with 1: pass")
76 self.check_suite("async def f():\n async with a as b, c as d: pass")
77
78 def test_async_for_statement(self):
79 self.check_suite("async def f():\n async for i in (): pass")
80 self.check_suite("async def f():\n async for i, b in (): pass")
81
Mark Dickinson407b3bd2012-04-29 22:18:31 +010082 def test_nonlocal_statement(self):
83 self.check_suite("def f():\n"
84 " x = 0\n"
85 " def g():\n"
86 " nonlocal x\n")
87 self.check_suite("def f():\n"
88 " x = y = 0\n"
89 " def g():\n"
90 " nonlocal x, y\n")
91
Fred Drake58422e52001-06-04 03:56:24 +000092 def test_expressions(self):
93 self.check_expr("foo(1)")
94 self.check_expr("[1, 2, 3]")
95 self.check_expr("[x**3 for x in range(20)]")
96 self.check_expr("[x**3 for x in range(20) if x % 3]")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
98 self.check_expr("list(x**3 for x in range(20))")
99 self.check_expr("list(x**3 for x in range(20) if x % 3)")
100 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Fred Drake58422e52001-06-04 03:56:24 +0000101 self.check_expr("foo(*args)")
102 self.check_expr("foo(*args, **kw)")
103 self.check_expr("foo(**kw)")
104 self.check_expr("foo(key=value)")
105 self.check_expr("foo(key=value, *args)")
106 self.check_expr("foo(key=value, *args, **kw)")
107 self.check_expr("foo(key=value, **kw)")
108 self.check_expr("foo(a, b, c, *args)")
109 self.check_expr("foo(a, b, c, *args, **kw)")
110 self.check_expr("foo(a, b, c, **kw)")
Benjamin Peterson3938a902008-08-20 02:33:00 +0000111 self.check_expr("foo(a, *args, keyword=23)")
Fred Drake58422e52001-06-04 03:56:24 +0000112 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000113 self.check_expr("foo - bar")
114 self.check_expr("foo * bar")
115 self.check_expr("foo / bar")
116 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +0000117 self.check_expr("lambda: 0")
118 self.check_expr("lambda x: 0")
119 self.check_expr("lambda *y: 0")
120 self.check_expr("lambda *y, **z: 0")
121 self.check_expr("lambda **z: 0")
122 self.check_expr("lambda x, y: 0")
123 self.check_expr("lambda foo=bar: 0")
124 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
125 self.check_expr("lambda foo=bar, **z: 0")
126 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
127 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
128 self.check_expr("lambda x, *y, **z: 0")
Raymond Hettinger354433a2004-05-19 08:20:33 +0000129 self.check_expr("(x for x in range(10))")
130 self.check_expr("foo(x for x in range(10))")
Mark Dickinsonda029fb2012-05-07 17:24:04 +0100131 self.check_expr("...")
132 self.check_expr("a[...]")
Fred Drake79ca79d2000-08-21 22:30:53 +0000133
Fred Drake58422e52001-06-04 03:56:24 +0000134 def test_simple_expression(self):
135 # expr_stmt
136 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +0000137
Fred Drake58422e52001-06-04 03:56:24 +0000138 def test_simple_assignments(self):
139 self.check_suite("a = b")
140 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +0000141
Fred Drake58422e52001-06-04 03:56:24 +0000142 def test_simple_augmented_assignments(self):
143 self.check_suite("a += b")
144 self.check_suite("a -= b")
145 self.check_suite("a *= b")
146 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000147 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000148 self.check_suite("a %= b")
149 self.check_suite("a &= b")
150 self.check_suite("a |= b")
151 self.check_suite("a ^= b")
152 self.check_suite("a <<= b")
153 self.check_suite("a >>= b")
154 self.check_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000155
Fred Drake58422e52001-06-04 03:56:24 +0000156 def test_function_defs(self):
157 self.check_suite("def f(): pass")
158 self.check_suite("def f(*args): pass")
159 self.check_suite("def f(*args, **kw): pass")
160 self.check_suite("def f(**kw): pass")
161 self.check_suite("def f(foo=bar): pass")
162 self.check_suite("def f(foo=bar, *args): pass")
163 self.check_suite("def f(foo=bar, *args, **kw): pass")
164 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000165
Fred Drake58422e52001-06-04 03:56:24 +0000166 self.check_suite("def f(a, b): pass")
167 self.check_suite("def f(a, b, *args): pass")
168 self.check_suite("def f(a, b, *args, **kw): pass")
169 self.check_suite("def f(a, b, **kw): pass")
170 self.check_suite("def f(a, b, foo=bar): pass")
171 self.check_suite("def f(a, b, foo=bar, *args): pass")
172 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
173 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000174
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000175 self.check_suite("@staticmethod\n"
176 "def f(): pass")
177 self.check_suite("@staticmethod\n"
178 "@funcattrs(x, y)\n"
179 "def f(): pass")
180 self.check_suite("@funcattrs()\n"
181 "def f(): pass")
182
Mark Dickinsonea7e9f92012-04-29 18:34:40 +0100183 # keyword-only arguments
184 self.check_suite("def f(*, a): pass")
185 self.check_suite("def f(*, a = 5): pass")
186 self.check_suite("def f(*, a = 5, b): pass")
187 self.check_suite("def f(*, a, b = 5): pass")
188 self.check_suite("def f(*, a, b = 5, **kwds): pass")
189 self.check_suite("def f(*args, a): pass")
190 self.check_suite("def f(*args, a = 5): pass")
191 self.check_suite("def f(*args, a = 5, b): pass")
192 self.check_suite("def f(*args, a, b = 5): pass")
193 self.check_suite("def f(*args, a, b = 5, **kwds): pass")
194
195 # function annotations
196 self.check_suite("def f(a: int): pass")
197 self.check_suite("def f(a: int = 5): pass")
198 self.check_suite("def f(*args: list): pass")
199 self.check_suite("def f(**kwds: dict): pass")
200 self.check_suite("def f(*, a: int): pass")
201 self.check_suite("def f(*, a: int = 5): pass")
202 self.check_suite("def f() -> int: pass")
203
Brett Cannonf4189912005-04-09 02:30:16 +0000204 def test_class_defs(self):
205 self.check_suite("class foo():pass")
Guido van Rossumfc158e22007-11-15 19:17:28 +0000206 self.check_suite("class foo(object):pass")
Mark Dickinson2bd61a92010-07-04 16:37:31 +0000207 self.check_suite("@class_decorator\n"
208 "class foo():pass")
209 self.check_suite("@class_decorator(arg)\n"
210 "class foo():pass")
211 self.check_suite("@decorator1\n"
212 "@decorator2\n"
213 "class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000214
Fred Drake58422e52001-06-04 03:56:24 +0000215 def test_import_from_statement(self):
216 self.check_suite("from sys.path import *")
217 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000218 self.check_suite("from sys.path import (dirname)")
219 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000220 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000221 self.check_suite("from sys.path import (dirname as my_dirname)")
222 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000223 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000224 self.check_suite("from sys.path import (dirname, basename)")
225 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000226 self.check_suite(
227 "from sys.path import dirname as my_dirname, basename")
228 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000229 "from sys.path import (dirname as my_dirname, basename)")
230 self.check_suite(
231 "from sys.path import (dirname as my_dirname, basename,)")
232 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000233 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000234 self.check_suite(
235 "from sys.path import (dirname, basename as my_basename)")
236 self.check_suite(
237 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000238 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000239
Fred Drake58422e52001-06-04 03:56:24 +0000240 def test_basic_import_statement(self):
241 self.check_suite("import sys")
242 self.check_suite("import sys as system")
243 self.check_suite("import sys, math")
244 self.check_suite("import sys as system, math")
245 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000246
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000247 def test_relative_imports(self):
248 self.check_suite("from . import name")
249 self.check_suite("from .. import name")
Mark Dickinsonfeb3b752010-07-04 18:38:57 +0000250 # check all the way up to '....', since '...' is tokenized
251 # differently from '.' (it's an ellipsis token).
252 self.check_suite("from ... import name")
253 self.check_suite("from .... import name")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000254 self.check_suite("from .pkg import name")
255 self.check_suite("from ..pkg import name")
Mark Dickinsonfeb3b752010-07-04 18:38:57 +0000256 self.check_suite("from ...pkg import name")
257 self.check_suite("from ....pkg import name")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000258
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000259 def test_pep263(self):
260 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
261 "pass\n")
262
263 def test_assert(self):
264 self.check_suite("assert alo < ahi and blo < bhi\n")
265
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000266 def test_with(self):
267 self.check_suite("with open('x'): pass\n")
268 self.check_suite("with open('x') as f: pass\n")
Georg Brandl0c315622009-05-25 21:10:36 +0000269 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000270
Georg Brandleee31162008-12-07 15:15:22 +0000271 def test_try_stmt(self):
272 self.check_suite("try: pass\nexcept: pass\n")
273 self.check_suite("try: pass\nfinally: pass\n")
274 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
275 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
276 "finally: pass\n")
277 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
278 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
279 "finally: pass\n")
280
Thomas Wouters89f507f2006-12-13 04:49:30 +0000281 def test_position(self):
282 # An absolutely minimal test of position information. Better
283 # tests would be a big project.
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000284 code = "def f(x):\n return x + 1"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000285 st1 = parser.suite(code)
286 st2 = st1.totuple(line_info=1, col_info=1)
287
288 def walk(tree):
289 node_type = tree[0]
290 next = tree[1]
291 if isinstance(next, tuple):
292 for elt in tree[1:]:
293 for x in walk(elt):
294 yield x
295 else:
296 yield tree
297
298 terminals = list(walk(st2))
299 self.assertEqual([
300 (1, 'def', 1, 0),
301 (1, 'f', 1, 4),
302 (7, '(', 1, 5),
303 (1, 'x', 1, 6),
304 (8, ')', 1, 7),
305 (11, ':', 1, 8),
306 (4, '', 1, 9),
307 (5, '', 2, -1),
308 (1, 'return', 2, 4),
309 (1, 'x', 2, 11),
310 (14, '+', 2, 13),
311 (2, '1', 2, 15),
312 (4, '', 2, 16),
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000313 (6, '', 2, -1),
314 (4, '', 2, -1),
315 (0, '', 2, -1)],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000316 terminals)
317
Benjamin Peterson4905e802009-09-27 02:43:28 +0000318 def test_extended_unpacking(self):
319 self.check_suite("*a = y")
320 self.check_suite("x, *b, = m")
321 self.check_suite("[*a, *b] = y")
322 self.check_suite("for [*x, b] in x: pass")
323
Mark Dickinsoncf360b92012-05-07 12:01:27 +0100324 def test_raise_statement(self):
325 self.check_suite("raise\n")
326 self.check_suite("raise e\n")
327 self.check_suite("try:\n"
328 " suite\n"
329 "except Exception as e:\n"
330 " raise ValueError from e\n")
331
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400332 def test_list_displays(self):
333 self.check_expr('[]')
334 self.check_expr('[*{2}, 3, *[4]]')
335
Mark Dickinson11c1dee2012-05-07 16:34:34 +0100336 def test_set_displays(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400337 self.check_expr('{*{2}, 3, *[4]}')
Mark Dickinson11c1dee2012-05-07 16:34:34 +0100338 self.check_expr('{2}')
339 self.check_expr('{2,}')
340 self.check_expr('{2, 3}')
341 self.check_expr('{2, 3,}')
342
343 def test_dict_displays(self):
344 self.check_expr('{}')
345 self.check_expr('{a:b}')
346 self.check_expr('{a:b,}')
347 self.check_expr('{a:b, c:d}')
348 self.check_expr('{a:b, c:d,}')
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400349 self.check_expr('{**{}}')
350 self.check_expr('{**{}, 3:4, **{5:6, 7:8}}')
351
352 def test_argument_unpacking(self):
Yury Selivanov50a26142015-08-05 17:59:45 -0400353 self.check_expr("f(*a, **b)")
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400354 self.check_expr('f(a, *b, *c, *d)')
355 self.check_expr('f(**a, **b)')
356 self.check_expr('f(2, *a, *b, **b, **c, **d)')
Yury Selivanov50a26142015-08-05 17:59:45 -0400357 self.check_expr("f(*b, *() or () and (), **{} and {}, **() or {})")
Mark Dickinson11c1dee2012-05-07 16:34:34 +0100358
359 def test_set_comprehensions(self):
360 self.check_expr('{x for x in seq}')
361 self.check_expr('{f(x) for x in seq}')
362 self.check_expr('{f(x) for x in seq if condition(x)}')
363
364 def test_dict_comprehensions(self):
365 self.check_expr('{x:x for x in seq}')
366 self.check_expr('{x**2:x[3] for x in seq if condition(x)}')
367 self.check_expr('{x:x for x in seq1 for y in seq2 if condition(x, y)}')
368
Thomas Wouters89f507f2006-12-13 04:49:30 +0000369
Fred Drake79ca79d2000-08-21 22:30:53 +0000370#
371# Second, we take *invalid* trees and make sure we get ParserError
372# rejections for them.
373#
374
Fred Drake58422e52001-06-04 03:56:24 +0000375class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000376
Fred Drake58422e52001-06-04 03:56:24 +0000377 def check_bad_tree(self, tree, label):
378 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000379 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000380 except parser.ParserError:
381 pass
382 else:
383 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000384
Fred Drake58422e52001-06-04 03:56:24 +0000385 def test_junk(self):
386 # not even remotely valid:
387 self.check_bad_tree((1, 2, 3), "<junk>")
388
Fred Drakecf580c72001-07-17 03:01:29 +0000389 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000390 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000391 tree = \
392 (257,
393 (264,
394 (285,
395 (259,
396 (1, 'def'),
397 (1, 'f'),
398 (260, (7, '('), (8, ')')),
399 (11, ':'),
400 (291,
401 (4, ''),
402 (5, ''),
403 (264,
404 (265,
405 (266,
406 (272,
407 (275,
408 (1, 'return'),
409 (313,
410 (292,
411 (293,
412 (294,
413 (295,
414 (297,
415 (298,
416 (299,
417 (300,
418 (301,
419 (302, (303, (304, (305, (2, '1')))))))))))))))))),
420 (264,
421 (265,
422 (266,
423 (272,
424 (276,
425 (1, 'yield'),
426 (313,
427 (292,
428 (293,
429 (294,
430 (295,
431 (297,
432 (298,
433 (299,
434 (300,
435 (301,
436 (302,
437 (303, (304, (305, (2, '1')))))))))))))))))),
438 (4, ''))),
439 (6, ''))))),
440 (4, ''),
441 (0, ''))))
442 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
443
444 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000445 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000446 tree = \
447 (257,
448 (264,
449 (265,
450 (266,
451 (278,
452 (1, 'from'),
453 (281, (1, '__future__')),
454 (1, 'import'),
455 (279, (1, 'generators')))),
456 (4, ''))),
457 (264,
458 (285,
459 (259,
460 (1, 'def'),
461 (1, 'f'),
462 (260, (7, '('), (8, ')')),
463 (11, ':'),
464 (291,
465 (4, ''),
466 (5, ''),
467 (264,
468 (265,
469 (266,
470 (272,
471 (275,
472 (1, 'return'),
473 (313,
474 (292,
475 (293,
476 (294,
477 (295,
478 (297,
479 (298,
480 (299,
481 (300,
482 (301,
483 (302, (303, (304, (305, (2, '1')))))))))))))))))),
484 (264,
485 (265,
486 (266,
487 (272,
488 (276,
489 (1, 'yield'),
490 (313,
491 (292,
492 (293,
493 (294,
494 (295,
495 (297,
496 (298,
497 (299,
498 (300,
499 (301,
500 (302,
501 (303, (304, (305, (2, '1')))))))))))))))))),
502 (4, ''))),
503 (6, ''))))),
504 (4, ''),
505 (0, ''))))
506 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
507
Fred Drake58422e52001-06-04 03:56:24 +0000508 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000509 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000510 tree = \
511 (258,
512 (311,
513 (290,
514 (291,
515 (292,
516 (293,
517 (295,
518 (296,
519 (297,
520 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
521 (12, ','),
522 (12, ','),
523 (290,
524 (291,
525 (292,
526 (293,
527 (295,
528 (296,
529 (297,
530 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
531 (4, ''),
532 (0, ''))
533 self.check_bad_tree(tree, "a,,c")
534
535 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000536 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000537 tree = \
538 (257,
539 (264,
540 (265,
541 (266,
542 (267,
543 (312,
544 (291,
545 (292,
546 (293,
547 (294,
548 (296,
549 (297,
550 (298,
551 (299,
552 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
553 (268, (37, '$=')),
554 (312,
555 (291,
556 (292,
557 (293,
558 (294,
559 (296,
560 (297,
561 (298,
562 (299,
563 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
564 (4, ''))),
565 (0, ''))
566 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000567
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000568 def test_malformed_global(self):
569 #doesn't have global keyword in ast
570 tree = (257,
571 (264,
572 (265,
573 (266,
574 (282, (1, 'foo'))), (4, ''))),
575 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000576 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000577 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000578
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000579 def test_missing_import_source(self):
Mark Dickinson3445b482010-07-04 18:15:26 +0000580 # from import fred
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000581 tree = \
582 (257,
Mark Dickinson3445b482010-07-04 18:15:26 +0000583 (268,
584 (269,
585 (270,
586 (282,
587 (284, (1, 'from'), (1, 'import'),
588 (287, (285, (1, 'fred')))))),
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000589 (4, ''))),
590 (4, ''), (0, ''))
Mark Dickinson3445b482010-07-04 18:15:26 +0000591 self.check_bad_tree(tree, "from import fred")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000592
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593
594class CompileTestCase(unittest.TestCase):
595
596 # These tests are very minimal. :-(
597
598 def test_compile_expr(self):
599 st = parser.expr('2 + 3')
600 code = parser.compilest(st)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000601 self.assertEqual(eval(code), 5)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
603 def test_compile_suite(self):
604 st = parser.suite('x = 2; y = x + 3')
605 code = parser.compilest(st)
606 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000607 exec(code, globs)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000608 self.assertEqual(globs['y'], 5)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609
610 def test_compile_error(self):
611 st = parser.suite('1 = 3 + 4')
612 self.assertRaises(SyntaxError, parser.compilest, st)
613
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000614 def test_compile_badunicode(self):
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000615 st = parser.suite('a = "\\U12345678"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000616 self.assertRaises(SyntaxError, parser.compilest, st)
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000617 st = parser.suite('a = "\\u1"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000618 self.assertRaises(SyntaxError, parser.compilest, st)
619
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000620 def test_issue_9011(self):
621 # Issue 9011: compilation of an unary minus expression changed
622 # the meaning of the ST, so that a second compilation produced
623 # incorrect results.
624 st = parser.expr('-3')
625 code1 = parser.compilest(st)
626 self.assertEqual(eval(code1), -3)
627 code2 = parser.compilest(st)
628 self.assertEqual(eval(code2), -3)
629
Serhiy Storchaka9305d832016-06-18 13:53:36 +0300630 def test_compile_filename(self):
631 st = parser.expr('a + 5')
632 code = parser.compilest(st)
633 self.assertEqual(code.co_filename, '<syntax-tree>')
634 code = st.compile()
635 self.assertEqual(code.co_filename, '<syntax-tree>')
636 for filename in ('file.py', b'file.py',
637 bytearray(b'file.py'), memoryview(b'file.py')):
638 code = parser.compilest(st, filename)
639 self.assertEqual(code.co_filename, 'file.py')
640 code = st.compile(filename)
641 self.assertEqual(code.co_filename, 'file.py')
642 self.assertRaises(TypeError, parser.compilest, st, list(b'file.py'))
643 self.assertRaises(TypeError, st.compile, list(b'file.py'))
644
645
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000646class ParserStackLimitTestCase(unittest.TestCase):
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000647 """try to push the parser to/over its limits.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000648 see http://bugs.python.org/issue1881 for a discussion
649 """
650 def _nested_expression(self, level):
651 return "["*level+"]"*level
652
653 def test_deeply_nested_list(self):
654 # XXX used to be 99 levels in 2.x
655 e = self._nested_expression(93)
656 st = parser.expr(e)
657 st.compile()
658
659 def test_trigger_memory_error(self):
660 e = self._nested_expression(100)
Ezio Melotti39191842013-03-09 22:17:33 +0200661 rc, out, err = assert_python_failure('-c', e)
662 # parsing the expression will result in an error message
663 # followed by a MemoryError (see #11963)
Ezio Melottie7c32992013-03-10 03:25:45 +0200664 self.assertIn(b's_push: parser stack overflow', err)
665 self.assertIn(b'MemoryError', err)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000666
Mark Dickinson211c6252009-02-01 10:28:51 +0000667class STObjectTestCase(unittest.TestCase):
668 """Test operations on ST objects themselves"""
669
670 def test_comparisons(self):
671 # ST objects should support order and equality comparisons
672 st1 = parser.expr('2 + 3')
673 st2 = parser.suite('x = 2; y = x + 3')
674 st3 = parser.expr('list(x**3 for x in range(20))')
675 st1_copy = parser.expr('2 + 3')
676 st2_copy = parser.suite('x = 2; y = x + 3')
677 st3_copy = parser.expr('list(x**3 for x in range(20))')
678
679 # exercise fast path for object identity
Ezio Melottib3aedd42010-11-20 19:04:17 +0000680 self.assertEqual(st1 == st1, True)
681 self.assertEqual(st2 == st2, True)
682 self.assertEqual(st3 == st3, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000683 # slow path equality
684 self.assertEqual(st1, st1_copy)
685 self.assertEqual(st2, st2_copy)
686 self.assertEqual(st3, st3_copy)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000687 self.assertEqual(st1 == st2, False)
688 self.assertEqual(st1 == st3, False)
689 self.assertEqual(st2 == st3, False)
690 self.assertEqual(st1 != st1, False)
691 self.assertEqual(st2 != st2, False)
692 self.assertEqual(st3 != st3, False)
693 self.assertEqual(st1 != st1_copy, False)
694 self.assertEqual(st2 != st2_copy, False)
695 self.assertEqual(st3 != st3_copy, False)
696 self.assertEqual(st2 != st1, True)
697 self.assertEqual(st1 != st3, True)
698 self.assertEqual(st3 != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000699 # we don't particularly care what the ordering is; just that
700 # it's usable and self-consistent
Ezio Melottib3aedd42010-11-20 19:04:17 +0000701 self.assertEqual(st1 < st2, not (st2 <= st1))
702 self.assertEqual(st1 < st3, not (st3 <= st1))
703 self.assertEqual(st2 < st3, not (st3 <= st2))
704 self.assertEqual(st1 < st2, st2 > st1)
705 self.assertEqual(st1 < st3, st3 > st1)
706 self.assertEqual(st2 < st3, st3 > st2)
707 self.assertEqual(st1 <= st2, st2 >= st1)
708 self.assertEqual(st3 <= st1, st1 >= st3)
709 self.assertEqual(st2 <= st3, st3 >= st2)
Mark Dickinson211c6252009-02-01 10:28:51 +0000710 # transitivity
711 bottom = min(st1, st2, st3)
712 top = max(st1, st2, st3)
713 mid = sorted([st1, st2, st3])[1]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000714 self.assertTrue(bottom < mid)
715 self.assertTrue(bottom < top)
716 self.assertTrue(mid < top)
717 self.assertTrue(bottom <= mid)
718 self.assertTrue(bottom <= top)
719 self.assertTrue(mid <= top)
720 self.assertTrue(bottom <= bottom)
721 self.assertTrue(mid <= mid)
722 self.assertTrue(top <= top)
Mark Dickinson211c6252009-02-01 10:28:51 +0000723 # interaction with other types
Ezio Melottib3aedd42010-11-20 19:04:17 +0000724 self.assertEqual(st1 == 1588.602459, False)
725 self.assertEqual('spanish armada' != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000726 self.assertRaises(TypeError, operator.ge, st3, None)
727 self.assertRaises(TypeError, operator.le, False, st1)
728 self.assertRaises(TypeError, operator.lt, st1, 1815)
729 self.assertRaises(TypeError, operator.gt, b'waterloo', st2)
730
Jesus Ceae9c53182012-08-03 14:28:37 +0200731 check_sizeof = support.check_sizeof
732
733 @support.cpython_only
734 def test_sizeof(self):
735 def XXXROUNDUP(n):
736 if n <= 1:
737 return n
738 if n <= 128:
739 return (n + 3) & ~3
740 return 1 << (n - 1).bit_length()
741
742 basesize = support.calcobjsize('Pii')
743 nodesize = struct.calcsize('hP3iP0h')
744 def sizeofchildren(node):
745 if node is None:
746 return 0
747 res = 0
748 hasstr = len(node) > 1 and isinstance(node[-1], str)
749 if hasstr:
750 res += len(node[-1]) + 1
751 children = node[1:-1] if hasstr else node[1:]
752 if children:
753 res += XXXROUNDUP(len(children)) * nodesize
Jesus Ceae9c53182012-08-03 14:28:37 +0200754 for child in children:
755 res += sizeofchildren(child)
756 return res
757
758 def check_st_sizeof(st):
759 self.check_sizeof(st, basesize + nodesize +
760 sizeofchildren(st.totuple()))
761
762 check_st_sizeof(parser.expr('2 + 3'))
763 check_st_sizeof(parser.expr('2 + 3 + 4'))
764 check_st_sizeof(parser.suite('x = 2 + 3'))
765 check_st_sizeof(parser.suite(''))
766 check_st_sizeof(parser.suite('# -*- coding: utf-8 -*-'))
767 check_st_sizeof(parser.expr('[' + '2,' * 1000 + ']'))
768
Mark Dickinson211c6252009-02-01 10:28:51 +0000769
770 # XXX tests for pickling and unpickling of ST objects should go here
771
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500772class OtherParserCase(unittest.TestCase):
773
774 def test_two_args_to_expr(self):
775 # See bug #12264
776 with self.assertRaises(TypeError):
777 parser.expr("a", "b")
778
Fred Drake2e2be372001-09-20 21:33:42 +0000779if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500780 unittest.main()