blob: e2a42f9715200c8561c8aa82875ef51525899b32 [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Fred Drake58422e52001-06-04 03:56:24 +00002import unittest
Mark Dickinson211c6252009-02-01 10:28:51 +00003import operator
Jesus Ceae9c53182012-08-03 14:28:37 +02004import struct
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Berker Peksagce643912015-05-06 06:33:17 +03006from test.support.script_helper import assert_python_failure
Fred Drake79ca79d2000-08-21 22:30:53 +00007
8#
9# First, we test that we can generate trees from valid source fragments,
10# and that these valid trees are indeed allowed by the tree-loading side
11# of the parser module.
12#
13
Fred Drake58422e52001-06-04 03:56:24 +000014class RoundtripLegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000015
Fred Drake58422e52001-06-04 03:56:24 +000016 def roundtrip(self, f, s):
17 st1 = f(s)
18 t = st1.totuple()
19 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +000020 st2 = parser.sequence2st(t)
Guido van Rossumb940e112007-01-10 16:19:56 +000021 except parser.ParserError as why:
Anthony Baxterc2a5a632004-08-02 06:10:11 +000022 self.fail("could not roundtrip %r: %s" % (s, why))
Fred Drake79ca79d2000-08-21 22:30:53 +000023
Ezio Melottib3aedd42010-11-20 19:04:17 +000024 self.assertEqual(t, st2.totuple(),
25 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000026
Fred Drake58422e52001-06-04 03:56:24 +000027 def check_expr(self, s):
28 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000029
Benjamin Petersonf216c942008-10-31 02:28:05 +000030 def test_flags_passed(self):
31 # The unicode literals flags has to be passed from the paser to AST
32 # generation.
33 suite = parser.suite("from __future__ import unicode_literals; x = ''")
34 code = suite.compile()
35 scope = {}
36 exec(code, {}, scope)
Ezio Melottie9615932010-01-24 19:26:24 +000037 self.assertIsInstance(scope["x"], str)
Benjamin Petersonf216c942008-10-31 02:28:05 +000038
Fred Drake58422e52001-06-04 03:56:24 +000039 def check_suite(self, s):
40 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000041
Fred Drakecf580c72001-07-17 03:01:29 +000042 def test_yield_statement(self):
Tim Peters496563a2002-04-01 00:28:59 +000043 self.check_suite("def f(): yield 1")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000044 self.check_suite("def f(): yield")
45 self.check_suite("def f(): x += yield")
46 self.check_suite("def f(): x = yield 1")
47 self.check_suite("def f(): x = y = yield 1")
48 self.check_suite("def f(): x = yield")
49 self.check_suite("def f(): x = y = yield")
50 self.check_suite("def f(): 1 + (yield)*2")
51 self.check_suite("def f(): (yield 1)*2")
Tim Peters496563a2002-04-01 00:28:59 +000052 self.check_suite("def f(): return; yield 1")
53 self.check_suite("def f(): yield 1; return")
Nick Coghlan1f7ce622012-01-13 21:43:40 +100054 self.check_suite("def f(): yield from 1")
55 self.check_suite("def f(): x = yield from 1")
56 self.check_suite("def f(): f((yield from 1))")
57 self.check_suite("def f(): yield 1; return 1")
Tim Peters496563a2002-04-01 00:28:59 +000058 self.check_suite("def f():\n"
Fred Drakecf580c72001-07-17 03:01:29 +000059 " for x in range(30):\n"
60 " yield x\n")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000061 self.check_suite("def f():\n"
62 " if (yield):\n"
63 " yield x\n")
Fred Drakecf580c72001-07-17 03:01:29 +000064
Yury Selivanov75445082015-05-11 22:57:16 -040065 def test_await_statement(self):
66 self.check_suite("async def f():\n await smth()")
67 self.check_suite("async def f():\n foo = await smth()")
68 self.check_suite("async def f():\n foo, bar = await smth()")
69 self.check_suite("async def f():\n (await smth())")
70 self.check_suite("async def f():\n foo((await smth()))")
71 self.check_suite("async def f():\n await foo(); return 42")
72
73 def test_async_with_statement(self):
74 self.check_suite("async def f():\n async with 1: pass")
75 self.check_suite("async def f():\n async with a as b, c as d: pass")
76
77 def test_async_for_statement(self):
78 self.check_suite("async def f():\n async for i in (): pass")
79 self.check_suite("async def f():\n async for i, b in (): pass")
80
Mark Dickinson407b3bd2012-04-29 22:18:31 +010081 def test_nonlocal_statement(self):
82 self.check_suite("def f():\n"
83 " x = 0\n"
84 " def g():\n"
85 " nonlocal x\n")
86 self.check_suite("def f():\n"
87 " x = y = 0\n"
88 " def g():\n"
89 " nonlocal x, y\n")
90
Fred Drake58422e52001-06-04 03:56:24 +000091 def test_expressions(self):
92 self.check_expr("foo(1)")
93 self.check_expr("[1, 2, 3]")
94 self.check_expr("[x**3 for x in range(20)]")
95 self.check_expr("[x**3 for x in range(20) if x % 3]")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
97 self.check_expr("list(x**3 for x in range(20))")
98 self.check_expr("list(x**3 for x in range(20) if x % 3)")
99 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Fred Drake58422e52001-06-04 03:56:24 +0000100 self.check_expr("foo(*args)")
101 self.check_expr("foo(*args, **kw)")
102 self.check_expr("foo(**kw)")
103 self.check_expr("foo(key=value)")
104 self.check_expr("foo(key=value, *args)")
105 self.check_expr("foo(key=value, *args, **kw)")
106 self.check_expr("foo(key=value, **kw)")
107 self.check_expr("foo(a, b, c, *args)")
108 self.check_expr("foo(a, b, c, *args, **kw)")
109 self.check_expr("foo(a, b, c, **kw)")
Benjamin Peterson3938a902008-08-20 02:33:00 +0000110 self.check_expr("foo(a, *args, keyword=23)")
Fred Drake58422e52001-06-04 03:56:24 +0000111 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000112 self.check_expr("foo - bar")
113 self.check_expr("foo * bar")
114 self.check_expr("foo / bar")
115 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +0000116 self.check_expr("lambda: 0")
117 self.check_expr("lambda x: 0")
118 self.check_expr("lambda *y: 0")
119 self.check_expr("lambda *y, **z: 0")
120 self.check_expr("lambda **z: 0")
121 self.check_expr("lambda x, y: 0")
122 self.check_expr("lambda foo=bar: 0")
123 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
124 self.check_expr("lambda foo=bar, **z: 0")
125 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
126 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
127 self.check_expr("lambda x, *y, **z: 0")
Raymond Hettinger354433a2004-05-19 08:20:33 +0000128 self.check_expr("(x for x in range(10))")
129 self.check_expr("foo(x for x in range(10))")
Mark Dickinsonda029fb2012-05-07 17:24:04 +0100130 self.check_expr("...")
131 self.check_expr("a[...]")
Fred Drake79ca79d2000-08-21 22:30:53 +0000132
Fred Drake58422e52001-06-04 03:56:24 +0000133 def test_simple_expression(self):
134 # expr_stmt
135 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +0000136
Fred Drake58422e52001-06-04 03:56:24 +0000137 def test_simple_assignments(self):
138 self.check_suite("a = b")
139 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +0000140
Fred Drake58422e52001-06-04 03:56:24 +0000141 def test_simple_augmented_assignments(self):
142 self.check_suite("a += b")
143 self.check_suite("a -= b")
144 self.check_suite("a *= b")
145 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000146 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000147 self.check_suite("a %= b")
148 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")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000154
Fred Drake58422e52001-06-04 03:56:24 +0000155 def test_function_defs(self):
156 self.check_suite("def f(): pass")
157 self.check_suite("def f(*args): pass")
158 self.check_suite("def f(*args, **kw): pass")
159 self.check_suite("def f(**kw): pass")
160 self.check_suite("def f(foo=bar): pass")
161 self.check_suite("def f(foo=bar, *args): pass")
162 self.check_suite("def f(foo=bar, *args, **kw): pass")
163 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000164
Fred Drake58422e52001-06-04 03:56:24 +0000165 self.check_suite("def f(a, b): pass")
166 self.check_suite("def f(a, b, *args): pass")
167 self.check_suite("def f(a, b, *args, **kw): pass")
168 self.check_suite("def f(a, b, **kw): pass")
169 self.check_suite("def f(a, b, foo=bar): pass")
170 self.check_suite("def f(a, b, foo=bar, *args): pass")
171 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
172 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000173
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000174 self.check_suite("@staticmethod\n"
175 "def f(): pass")
176 self.check_suite("@staticmethod\n"
177 "@funcattrs(x, y)\n"
178 "def f(): pass")
179 self.check_suite("@funcattrs()\n"
180 "def f(): pass")
181
Mark Dickinsonea7e9f92012-04-29 18:34:40 +0100182 # keyword-only arguments
183 self.check_suite("def f(*, a): pass")
184 self.check_suite("def f(*, a = 5): pass")
185 self.check_suite("def f(*, a = 5, b): pass")
186 self.check_suite("def f(*, a, b = 5): pass")
187 self.check_suite("def f(*, a, b = 5, **kwds): pass")
188 self.check_suite("def f(*args, a): pass")
189 self.check_suite("def f(*args, a = 5): pass")
190 self.check_suite("def f(*args, a = 5, b): pass")
191 self.check_suite("def f(*args, a, b = 5): pass")
192 self.check_suite("def f(*args, a, b = 5, **kwds): pass")
193
194 # function annotations
195 self.check_suite("def f(a: int): pass")
196 self.check_suite("def f(a: int = 5): pass")
197 self.check_suite("def f(*args: list): pass")
198 self.check_suite("def f(**kwds: dict): pass")
199 self.check_suite("def f(*, a: int): pass")
200 self.check_suite("def f(*, a: int = 5): pass")
201 self.check_suite("def f() -> int: pass")
202
Brett Cannonf4189912005-04-09 02:30:16 +0000203 def test_class_defs(self):
204 self.check_suite("class foo():pass")
Guido van Rossumfc158e22007-11-15 19:17:28 +0000205 self.check_suite("class foo(object):pass")
Mark Dickinson2bd61a92010-07-04 16:37:31 +0000206 self.check_suite("@class_decorator\n"
207 "class foo():pass")
208 self.check_suite("@class_decorator(arg)\n"
209 "class foo():pass")
210 self.check_suite("@decorator1\n"
211 "@decorator2\n"
212 "class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000213
Fred Drake58422e52001-06-04 03:56:24 +0000214 def test_import_from_statement(self):
215 self.check_suite("from sys.path import *")
216 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000217 self.check_suite("from sys.path import (dirname)")
218 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000219 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000220 self.check_suite("from sys.path import (dirname as my_dirname)")
221 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000222 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000223 self.check_suite("from sys.path import (dirname, basename)")
224 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000225 self.check_suite(
226 "from sys.path import dirname as my_dirname, basename")
227 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000228 "from sys.path import (dirname as my_dirname, basename)")
229 self.check_suite(
230 "from sys.path import (dirname as my_dirname, basename,)")
231 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000232 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000233 self.check_suite(
234 "from sys.path import (dirname, basename as my_basename)")
235 self.check_suite(
236 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000237 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000238
Fred Drake58422e52001-06-04 03:56:24 +0000239 def test_basic_import_statement(self):
240 self.check_suite("import sys")
241 self.check_suite("import sys as system")
242 self.check_suite("import sys, math")
243 self.check_suite("import sys as system, math")
244 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000245
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000246 def test_relative_imports(self):
247 self.check_suite("from . import name")
248 self.check_suite("from .. import name")
Mark Dickinsonfeb3b752010-07-04 18:38:57 +0000249 # check all the way up to '....', since '...' is tokenized
250 # differently from '.' (it's an ellipsis token).
251 self.check_suite("from ... import name")
252 self.check_suite("from .... import name")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000253 self.check_suite("from .pkg import name")
254 self.check_suite("from ..pkg import name")
Mark Dickinsonfeb3b752010-07-04 18:38:57 +0000255 self.check_suite("from ...pkg import name")
256 self.check_suite("from ....pkg import name")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000257
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000258 def test_pep263(self):
259 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
260 "pass\n")
261
262 def test_assert(self):
263 self.check_suite("assert alo < ahi and blo < bhi\n")
264
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000265 def test_with(self):
266 self.check_suite("with open('x'): pass\n")
267 self.check_suite("with open('x') as f: pass\n")
Georg Brandl0c315622009-05-25 21:10:36 +0000268 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000269
Georg Brandleee31162008-12-07 15:15:22 +0000270 def test_try_stmt(self):
271 self.check_suite("try: pass\nexcept: pass\n")
272 self.check_suite("try: pass\nfinally: pass\n")
273 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
274 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
275 "finally: pass\n")
276 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
277 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
278 "finally: pass\n")
279
Thomas Wouters89f507f2006-12-13 04:49:30 +0000280 def test_position(self):
281 # An absolutely minimal test of position information. Better
282 # tests would be a big project.
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000283 code = "def f(x):\n return x + 1"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000284 st1 = parser.suite(code)
285 st2 = st1.totuple(line_info=1, col_info=1)
286
287 def walk(tree):
288 node_type = tree[0]
289 next = tree[1]
290 if isinstance(next, tuple):
291 for elt in tree[1:]:
292 for x in walk(elt):
293 yield x
294 else:
295 yield tree
296
297 terminals = list(walk(st2))
298 self.assertEqual([
299 (1, 'def', 1, 0),
300 (1, 'f', 1, 4),
301 (7, '(', 1, 5),
302 (1, 'x', 1, 6),
303 (8, ')', 1, 7),
304 (11, ':', 1, 8),
305 (4, '', 1, 9),
306 (5, '', 2, -1),
307 (1, 'return', 2, 4),
308 (1, 'x', 2, 11),
309 (14, '+', 2, 13),
310 (2, '1', 2, 15),
311 (4, '', 2, 16),
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000312 (6, '', 2, -1),
313 (4, '', 2, -1),
314 (0, '', 2, -1)],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000315 terminals)
316
Benjamin Peterson4905e802009-09-27 02:43:28 +0000317 def test_extended_unpacking(self):
318 self.check_suite("*a = y")
319 self.check_suite("x, *b, = m")
320 self.check_suite("[*a, *b] = y")
321 self.check_suite("for [*x, b] in x: pass")
322
Mark Dickinsoncf360b92012-05-07 12:01:27 +0100323 def test_raise_statement(self):
324 self.check_suite("raise\n")
325 self.check_suite("raise e\n")
326 self.check_suite("try:\n"
327 " suite\n"
328 "except Exception as e:\n"
329 " raise ValueError from e\n")
330
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400331 def test_list_displays(self):
332 self.check_expr('[]')
333 self.check_expr('[*{2}, 3, *[4]]')
334
Mark Dickinson11c1dee2012-05-07 16:34:34 +0100335 def test_set_displays(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400336 self.check_expr('{*{2}, 3, *[4]}')
Mark Dickinson11c1dee2012-05-07 16:34:34 +0100337 self.check_expr('{2}')
338 self.check_expr('{2,}')
339 self.check_expr('{2, 3}')
340 self.check_expr('{2, 3,}')
341
342 def test_dict_displays(self):
343 self.check_expr('{}')
344 self.check_expr('{a:b}')
345 self.check_expr('{a:b,}')
346 self.check_expr('{a:b, c:d}')
347 self.check_expr('{a:b, c:d,}')
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400348 self.check_expr('{**{}}')
349 self.check_expr('{**{}, 3:4, **{5:6, 7:8}}')
350
351 def test_argument_unpacking(self):
Yury Selivanov50a26142015-08-05 17:59:45 -0400352 self.check_expr("f(*a, **b)")
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400353 self.check_expr('f(a, *b, *c, *d)')
354 self.check_expr('f(**a, **b)')
355 self.check_expr('f(2, *a, *b, **b, **c, **d)')
Yury Selivanov50a26142015-08-05 17:59:45 -0400356 self.check_expr("f(*b, *() or () and (), **{} and {}, **() or {})")
Mark Dickinson11c1dee2012-05-07 16:34:34 +0100357
358 def test_set_comprehensions(self):
359 self.check_expr('{x for x in seq}')
360 self.check_expr('{f(x) for x in seq}')
361 self.check_expr('{f(x) for x in seq if condition(x)}')
362
363 def test_dict_comprehensions(self):
364 self.check_expr('{x:x for x in seq}')
365 self.check_expr('{x**2:x[3] for x in seq if condition(x)}')
366 self.check_expr('{x:x for x in seq1 for y in seq2 if condition(x, y)}')
367
Thomas Wouters89f507f2006-12-13 04:49:30 +0000368
Fred Drake79ca79d2000-08-21 22:30:53 +0000369#
370# Second, we take *invalid* trees and make sure we get ParserError
371# rejections for them.
372#
373
Fred Drake58422e52001-06-04 03:56:24 +0000374class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000375
Fred Drake58422e52001-06-04 03:56:24 +0000376 def check_bad_tree(self, tree, label):
377 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000378 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000379 except parser.ParserError:
380 pass
381 else:
382 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000383
Fred Drake58422e52001-06-04 03:56:24 +0000384 def test_junk(self):
385 # not even remotely valid:
386 self.check_bad_tree((1, 2, 3), "<junk>")
387
Fred Drakecf580c72001-07-17 03:01:29 +0000388 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000389 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000390 tree = \
391 (257,
392 (264,
393 (285,
394 (259,
395 (1, 'def'),
396 (1, 'f'),
397 (260, (7, '('), (8, ')')),
398 (11, ':'),
399 (291,
400 (4, ''),
401 (5, ''),
402 (264,
403 (265,
404 (266,
405 (272,
406 (275,
407 (1, 'return'),
408 (313,
409 (292,
410 (293,
411 (294,
412 (295,
413 (297,
414 (298,
415 (299,
416 (300,
417 (301,
418 (302, (303, (304, (305, (2, '1')))))))))))))))))),
419 (264,
420 (265,
421 (266,
422 (272,
423 (276,
424 (1, 'yield'),
425 (313,
426 (292,
427 (293,
428 (294,
429 (295,
430 (297,
431 (298,
432 (299,
433 (300,
434 (301,
435 (302,
436 (303, (304, (305, (2, '1')))))))))))))))))),
437 (4, ''))),
438 (6, ''))))),
439 (4, ''),
440 (0, ''))))
441 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
442
443 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000444 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000445 tree = \
446 (257,
447 (264,
448 (265,
449 (266,
450 (278,
451 (1, 'from'),
452 (281, (1, '__future__')),
453 (1, 'import'),
454 (279, (1, 'generators')))),
455 (4, ''))),
456 (264,
457 (285,
458 (259,
459 (1, 'def'),
460 (1, 'f'),
461 (260, (7, '('), (8, ')')),
462 (11, ':'),
463 (291,
464 (4, ''),
465 (5, ''),
466 (264,
467 (265,
468 (266,
469 (272,
470 (275,
471 (1, 'return'),
472 (313,
473 (292,
474 (293,
475 (294,
476 (295,
477 (297,
478 (298,
479 (299,
480 (300,
481 (301,
482 (302, (303, (304, (305, (2, '1')))))))))))))))))),
483 (264,
484 (265,
485 (266,
486 (272,
487 (276,
488 (1, 'yield'),
489 (313,
490 (292,
491 (293,
492 (294,
493 (295,
494 (297,
495 (298,
496 (299,
497 (300,
498 (301,
499 (302,
500 (303, (304, (305, (2, '1')))))))))))))))))),
501 (4, ''))),
502 (6, ''))))),
503 (4, ''),
504 (0, ''))))
505 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
506
Fred Drake58422e52001-06-04 03:56:24 +0000507 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000508 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000509 tree = \
510 (258,
511 (311,
512 (290,
513 (291,
514 (292,
515 (293,
516 (295,
517 (296,
518 (297,
519 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
520 (12, ','),
521 (12, ','),
522 (290,
523 (291,
524 (292,
525 (293,
526 (295,
527 (296,
528 (297,
529 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
530 (4, ''),
531 (0, ''))
532 self.check_bad_tree(tree, "a,,c")
533
534 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000535 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000536 tree = \
537 (257,
538 (264,
539 (265,
540 (266,
541 (267,
542 (312,
543 (291,
544 (292,
545 (293,
546 (294,
547 (296,
548 (297,
549 (298,
550 (299,
551 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
552 (268, (37, '$=')),
553 (312,
554 (291,
555 (292,
556 (293,
557 (294,
558 (296,
559 (297,
560 (298,
561 (299,
562 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
563 (4, ''))),
564 (0, ''))
565 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000566
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000567 def test_malformed_global(self):
568 #doesn't have global keyword in ast
569 tree = (257,
570 (264,
571 (265,
572 (266,
573 (282, (1, 'foo'))), (4, ''))),
574 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000575 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000576 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000577
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000578 def test_missing_import_source(self):
Mark Dickinson3445b482010-07-04 18:15:26 +0000579 # from import fred
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000580 tree = \
581 (257,
Mark Dickinson3445b482010-07-04 18:15:26 +0000582 (268,
583 (269,
584 (270,
585 (282,
586 (284, (1, 'from'), (1, 'import'),
587 (287, (285, (1, 'fred')))))),
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000588 (4, ''))),
589 (4, ''), (0, ''))
Mark Dickinson3445b482010-07-04 18:15:26 +0000590 self.check_bad_tree(tree, "from import fred")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592
593class CompileTestCase(unittest.TestCase):
594
595 # These tests are very minimal. :-(
596
597 def test_compile_expr(self):
598 st = parser.expr('2 + 3')
599 code = parser.compilest(st)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000600 self.assertEqual(eval(code), 5)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601
602 def test_compile_suite(self):
603 st = parser.suite('x = 2; y = x + 3')
604 code = parser.compilest(st)
605 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000606 exec(code, globs)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000607 self.assertEqual(globs['y'], 5)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608
609 def test_compile_error(self):
610 st = parser.suite('1 = 3 + 4')
611 self.assertRaises(SyntaxError, parser.compilest, st)
612
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000613 def test_compile_badunicode(self):
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000614 st = parser.suite('a = "\\U12345678"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000615 self.assertRaises(SyntaxError, parser.compilest, st)
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000616 st = parser.suite('a = "\\u1"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000617 self.assertRaises(SyntaxError, parser.compilest, st)
618
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000619 def test_issue_9011(self):
620 # Issue 9011: compilation of an unary minus expression changed
621 # the meaning of the ST, so that a second compilation produced
622 # incorrect results.
623 st = parser.expr('-3')
624 code1 = parser.compilest(st)
625 self.assertEqual(eval(code1), -3)
626 code2 = parser.compilest(st)
627 self.assertEqual(eval(code2), -3)
628
Serhiy Storchaka9305d832016-06-18 13:53:36 +0300629 def test_compile_filename(self):
630 st = parser.expr('a + 5')
631 code = parser.compilest(st)
632 self.assertEqual(code.co_filename, '<syntax-tree>')
633 code = st.compile()
634 self.assertEqual(code.co_filename, '<syntax-tree>')
Serhiy Storchakafebc3322016-08-06 23:29:29 +0300635 for filename in 'file.py', b'file.py':
Serhiy Storchaka9305d832016-06-18 13:53:36 +0300636 code = parser.compilest(st, filename)
637 self.assertEqual(code.co_filename, 'file.py')
638 code = st.compile(filename)
639 self.assertEqual(code.co_filename, 'file.py')
Serhiy Storchakafebc3322016-08-06 23:29:29 +0300640 for filename in bytearray(b'file.py'), memoryview(b'file.py'):
641 with self.assertWarns(DeprecationWarning):
642 code = parser.compilest(st, filename)
643 self.assertEqual(code.co_filename, 'file.py')
644 with self.assertWarns(DeprecationWarning):
645 code = st.compile(filename)
646 self.assertEqual(code.co_filename, 'file.py')
Serhiy Storchaka9305d832016-06-18 13:53:36 +0300647 self.assertRaises(TypeError, parser.compilest, st, list(b'file.py'))
648 self.assertRaises(TypeError, st.compile, list(b'file.py'))
649
650
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000651class ParserStackLimitTestCase(unittest.TestCase):
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000652 """try to push the parser to/over its limits.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000653 see http://bugs.python.org/issue1881 for a discussion
654 """
655 def _nested_expression(self, level):
656 return "["*level+"]"*level
657
658 def test_deeply_nested_list(self):
659 # XXX used to be 99 levels in 2.x
660 e = self._nested_expression(93)
661 st = parser.expr(e)
662 st.compile()
663
664 def test_trigger_memory_error(self):
665 e = self._nested_expression(100)
Ezio Melotti39191842013-03-09 22:17:33 +0200666 rc, out, err = assert_python_failure('-c', e)
667 # parsing the expression will result in an error message
668 # followed by a MemoryError (see #11963)
Ezio Melottie7c32992013-03-10 03:25:45 +0200669 self.assertIn(b's_push: parser stack overflow', err)
670 self.assertIn(b'MemoryError', err)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000671
Mark Dickinson211c6252009-02-01 10:28:51 +0000672class STObjectTestCase(unittest.TestCase):
673 """Test operations on ST objects themselves"""
674
675 def test_comparisons(self):
676 # ST objects should support order and equality comparisons
677 st1 = parser.expr('2 + 3')
678 st2 = parser.suite('x = 2; y = x + 3')
679 st3 = parser.expr('list(x**3 for x in range(20))')
680 st1_copy = parser.expr('2 + 3')
681 st2_copy = parser.suite('x = 2; y = x + 3')
682 st3_copy = parser.expr('list(x**3 for x in range(20))')
683
684 # exercise fast path for object identity
Ezio Melottib3aedd42010-11-20 19:04:17 +0000685 self.assertEqual(st1 == st1, True)
686 self.assertEqual(st2 == st2, True)
687 self.assertEqual(st3 == st3, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000688 # slow path equality
689 self.assertEqual(st1, st1_copy)
690 self.assertEqual(st2, st2_copy)
691 self.assertEqual(st3, st3_copy)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000692 self.assertEqual(st1 == st2, False)
693 self.assertEqual(st1 == st3, False)
694 self.assertEqual(st2 == st3, False)
695 self.assertEqual(st1 != st1, False)
696 self.assertEqual(st2 != st2, False)
697 self.assertEqual(st3 != st3, False)
698 self.assertEqual(st1 != st1_copy, False)
699 self.assertEqual(st2 != st2_copy, False)
700 self.assertEqual(st3 != st3_copy, False)
701 self.assertEqual(st2 != st1, True)
702 self.assertEqual(st1 != st3, True)
703 self.assertEqual(st3 != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000704 # we don't particularly care what the ordering is; just that
705 # it's usable and self-consistent
Ezio Melottib3aedd42010-11-20 19:04:17 +0000706 self.assertEqual(st1 < st2, not (st2 <= st1))
707 self.assertEqual(st1 < st3, not (st3 <= st1))
708 self.assertEqual(st2 < st3, not (st3 <= st2))
709 self.assertEqual(st1 < st2, st2 > st1)
710 self.assertEqual(st1 < st3, st3 > st1)
711 self.assertEqual(st2 < st3, st3 > st2)
712 self.assertEqual(st1 <= st2, st2 >= st1)
713 self.assertEqual(st3 <= st1, st1 >= st3)
714 self.assertEqual(st2 <= st3, st3 >= st2)
Mark Dickinson211c6252009-02-01 10:28:51 +0000715 # transitivity
716 bottom = min(st1, st2, st3)
717 top = max(st1, st2, st3)
718 mid = sorted([st1, st2, st3])[1]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000719 self.assertTrue(bottom < mid)
720 self.assertTrue(bottom < top)
721 self.assertTrue(mid < top)
722 self.assertTrue(bottom <= mid)
723 self.assertTrue(bottom <= top)
724 self.assertTrue(mid <= top)
725 self.assertTrue(bottom <= bottom)
726 self.assertTrue(mid <= mid)
727 self.assertTrue(top <= top)
Mark Dickinson211c6252009-02-01 10:28:51 +0000728 # interaction with other types
Ezio Melottib3aedd42010-11-20 19:04:17 +0000729 self.assertEqual(st1 == 1588.602459, False)
730 self.assertEqual('spanish armada' != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000731 self.assertRaises(TypeError, operator.ge, st3, None)
732 self.assertRaises(TypeError, operator.le, False, st1)
733 self.assertRaises(TypeError, operator.lt, st1, 1815)
734 self.assertRaises(TypeError, operator.gt, b'waterloo', st2)
735
Jesus Ceae9c53182012-08-03 14:28:37 +0200736 check_sizeof = support.check_sizeof
737
738 @support.cpython_only
739 def test_sizeof(self):
740 def XXXROUNDUP(n):
741 if n <= 1:
742 return n
743 if n <= 128:
744 return (n + 3) & ~3
745 return 1 << (n - 1).bit_length()
746
747 basesize = support.calcobjsize('Pii')
748 nodesize = struct.calcsize('hP3iP0h')
749 def sizeofchildren(node):
750 if node is None:
751 return 0
752 res = 0
753 hasstr = len(node) > 1 and isinstance(node[-1], str)
754 if hasstr:
755 res += len(node[-1]) + 1
756 children = node[1:-1] if hasstr else node[1:]
757 if children:
758 res += XXXROUNDUP(len(children)) * nodesize
Jesus Ceae9c53182012-08-03 14:28:37 +0200759 for child in children:
760 res += sizeofchildren(child)
761 return res
762
763 def check_st_sizeof(st):
764 self.check_sizeof(st, basesize + nodesize +
765 sizeofchildren(st.totuple()))
766
767 check_st_sizeof(parser.expr('2 + 3'))
768 check_st_sizeof(parser.expr('2 + 3 + 4'))
769 check_st_sizeof(parser.suite('x = 2 + 3'))
770 check_st_sizeof(parser.suite(''))
771 check_st_sizeof(parser.suite('# -*- coding: utf-8 -*-'))
772 check_st_sizeof(parser.expr('[' + '2,' * 1000 + ']'))
773
Mark Dickinson211c6252009-02-01 10:28:51 +0000774
775 # XXX tests for pickling and unpickling of ST objects should go here
776
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500777class OtherParserCase(unittest.TestCase):
778
779 def test_two_args_to_expr(self):
780 # See bug #12264
781 with self.assertRaises(TypeError):
782 parser.expr("a", "b")
783
Fred Drake2e2be372001-09-20 21:33:42 +0000784if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500785 unittest.main()