blob: 7082273b8f6bd11cb5befab6851b28e86679732d [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):
353 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)')
Mark Dickinson11c1dee2012-05-07 16:34:34 +0100356
357 def test_set_comprehensions(self):
358 self.check_expr('{x for x in seq}')
359 self.check_expr('{f(x) for x in seq}')
360 self.check_expr('{f(x) for x in seq if condition(x)}')
361
362 def test_dict_comprehensions(self):
363 self.check_expr('{x:x for x in seq}')
364 self.check_expr('{x**2:x[3] for x in seq if condition(x)}')
365 self.check_expr('{x:x for x in seq1 for y in seq2 if condition(x, y)}')
366
Thomas Wouters89f507f2006-12-13 04:49:30 +0000367
Fred Drake79ca79d2000-08-21 22:30:53 +0000368#
369# Second, we take *invalid* trees and make sure we get ParserError
370# rejections for them.
371#
372
Fred Drake58422e52001-06-04 03:56:24 +0000373class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000374
Fred Drake58422e52001-06-04 03:56:24 +0000375 def check_bad_tree(self, tree, label):
376 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000377 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000378 except parser.ParserError:
379 pass
380 else:
381 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000382
Fred Drake58422e52001-06-04 03:56:24 +0000383 def test_junk(self):
384 # not even remotely valid:
385 self.check_bad_tree((1, 2, 3), "<junk>")
386
Fred Drakecf580c72001-07-17 03:01:29 +0000387 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000388 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000389 tree = \
390 (257,
391 (264,
392 (285,
393 (259,
394 (1, 'def'),
395 (1, 'f'),
396 (260, (7, '('), (8, ')')),
397 (11, ':'),
398 (291,
399 (4, ''),
400 (5, ''),
401 (264,
402 (265,
403 (266,
404 (272,
405 (275,
406 (1, 'return'),
407 (313,
408 (292,
409 (293,
410 (294,
411 (295,
412 (297,
413 (298,
414 (299,
415 (300,
416 (301,
417 (302, (303, (304, (305, (2, '1')))))))))))))))))),
418 (264,
419 (265,
420 (266,
421 (272,
422 (276,
423 (1, 'yield'),
424 (313,
425 (292,
426 (293,
427 (294,
428 (295,
429 (297,
430 (298,
431 (299,
432 (300,
433 (301,
434 (302,
435 (303, (304, (305, (2, '1')))))))))))))))))),
436 (4, ''))),
437 (6, ''))))),
438 (4, ''),
439 (0, ''))))
440 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
441
442 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000443 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000444 tree = \
445 (257,
446 (264,
447 (265,
448 (266,
449 (278,
450 (1, 'from'),
451 (281, (1, '__future__')),
452 (1, 'import'),
453 (279, (1, 'generators')))),
454 (4, ''))),
455 (264,
456 (285,
457 (259,
458 (1, 'def'),
459 (1, 'f'),
460 (260, (7, '('), (8, ')')),
461 (11, ':'),
462 (291,
463 (4, ''),
464 (5, ''),
465 (264,
466 (265,
467 (266,
468 (272,
469 (275,
470 (1, 'return'),
471 (313,
472 (292,
473 (293,
474 (294,
475 (295,
476 (297,
477 (298,
478 (299,
479 (300,
480 (301,
481 (302, (303, (304, (305, (2, '1')))))))))))))))))),
482 (264,
483 (265,
484 (266,
485 (272,
486 (276,
487 (1, 'yield'),
488 (313,
489 (292,
490 (293,
491 (294,
492 (295,
493 (297,
494 (298,
495 (299,
496 (300,
497 (301,
498 (302,
499 (303, (304, (305, (2, '1')))))))))))))))))),
500 (4, ''))),
501 (6, ''))))),
502 (4, ''),
503 (0, ''))))
504 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
505
Fred Drake58422e52001-06-04 03:56:24 +0000506 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000507 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000508 tree = \
509 (258,
510 (311,
511 (290,
512 (291,
513 (292,
514 (293,
515 (295,
516 (296,
517 (297,
518 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
519 (12, ','),
520 (12, ','),
521 (290,
522 (291,
523 (292,
524 (293,
525 (295,
526 (296,
527 (297,
528 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
529 (4, ''),
530 (0, ''))
531 self.check_bad_tree(tree, "a,,c")
532
533 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000534 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000535 tree = \
536 (257,
537 (264,
538 (265,
539 (266,
540 (267,
541 (312,
542 (291,
543 (292,
544 (293,
545 (294,
546 (296,
547 (297,
548 (298,
549 (299,
550 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
551 (268, (37, '$=')),
552 (312,
553 (291,
554 (292,
555 (293,
556 (294,
557 (296,
558 (297,
559 (298,
560 (299,
561 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
562 (4, ''))),
563 (0, ''))
564 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000565
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000566 def test_malformed_global(self):
567 #doesn't have global keyword in ast
568 tree = (257,
569 (264,
570 (265,
571 (266,
572 (282, (1, 'foo'))), (4, ''))),
573 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000574 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000575 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000576
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000577 def test_missing_import_source(self):
Mark Dickinson3445b482010-07-04 18:15:26 +0000578 # from import fred
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000579 tree = \
580 (257,
Mark Dickinson3445b482010-07-04 18:15:26 +0000581 (268,
582 (269,
583 (270,
584 (282,
585 (284, (1, 'from'), (1, 'import'),
586 (287, (285, (1, 'fred')))))),
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000587 (4, ''))),
588 (4, ''), (0, ''))
Mark Dickinson3445b482010-07-04 18:15:26 +0000589 self.check_bad_tree(tree, "from import fred")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000590
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591
592class CompileTestCase(unittest.TestCase):
593
594 # These tests are very minimal. :-(
595
596 def test_compile_expr(self):
597 st = parser.expr('2 + 3')
598 code = parser.compilest(st)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000599 self.assertEqual(eval(code), 5)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
601 def test_compile_suite(self):
602 st = parser.suite('x = 2; y = x + 3')
603 code = parser.compilest(st)
604 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000605 exec(code, globs)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000606 self.assertEqual(globs['y'], 5)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607
608 def test_compile_error(self):
609 st = parser.suite('1 = 3 + 4')
610 self.assertRaises(SyntaxError, parser.compilest, st)
611
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000612 def test_compile_badunicode(self):
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000613 st = parser.suite('a = "\\U12345678"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000614 self.assertRaises(SyntaxError, parser.compilest, st)
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000615 st = parser.suite('a = "\\u1"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000616 self.assertRaises(SyntaxError, parser.compilest, st)
617
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000618 def test_issue_9011(self):
619 # Issue 9011: compilation of an unary minus expression changed
620 # the meaning of the ST, so that a second compilation produced
621 # incorrect results.
622 st = parser.expr('-3')
623 code1 = parser.compilest(st)
624 self.assertEqual(eval(code1), -3)
625 code2 = parser.compilest(st)
626 self.assertEqual(eval(code2), -3)
627
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000628class ParserStackLimitTestCase(unittest.TestCase):
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000629 """try to push the parser to/over its limits.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000630 see http://bugs.python.org/issue1881 for a discussion
631 """
632 def _nested_expression(self, level):
633 return "["*level+"]"*level
634
635 def test_deeply_nested_list(self):
636 # XXX used to be 99 levels in 2.x
637 e = self._nested_expression(93)
638 st = parser.expr(e)
639 st.compile()
640
641 def test_trigger_memory_error(self):
642 e = self._nested_expression(100)
Ezio Melotti39191842013-03-09 22:17:33 +0200643 rc, out, err = assert_python_failure('-c', e)
644 # parsing the expression will result in an error message
645 # followed by a MemoryError (see #11963)
Ezio Melottie7c32992013-03-10 03:25:45 +0200646 self.assertIn(b's_push: parser stack overflow', err)
647 self.assertIn(b'MemoryError', err)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000648
Mark Dickinson211c6252009-02-01 10:28:51 +0000649class STObjectTestCase(unittest.TestCase):
650 """Test operations on ST objects themselves"""
651
652 def test_comparisons(self):
653 # ST objects should support order and equality comparisons
654 st1 = parser.expr('2 + 3')
655 st2 = parser.suite('x = 2; y = x + 3')
656 st3 = parser.expr('list(x**3 for x in range(20))')
657 st1_copy = parser.expr('2 + 3')
658 st2_copy = parser.suite('x = 2; y = x + 3')
659 st3_copy = parser.expr('list(x**3 for x in range(20))')
660
661 # exercise fast path for object identity
Ezio Melottib3aedd42010-11-20 19:04:17 +0000662 self.assertEqual(st1 == st1, True)
663 self.assertEqual(st2 == st2, True)
664 self.assertEqual(st3 == st3, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000665 # slow path equality
666 self.assertEqual(st1, st1_copy)
667 self.assertEqual(st2, st2_copy)
668 self.assertEqual(st3, st3_copy)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000669 self.assertEqual(st1 == st2, False)
670 self.assertEqual(st1 == st3, False)
671 self.assertEqual(st2 == st3, False)
672 self.assertEqual(st1 != st1, False)
673 self.assertEqual(st2 != st2, False)
674 self.assertEqual(st3 != st3, False)
675 self.assertEqual(st1 != st1_copy, False)
676 self.assertEqual(st2 != st2_copy, False)
677 self.assertEqual(st3 != st3_copy, False)
678 self.assertEqual(st2 != st1, True)
679 self.assertEqual(st1 != st3, True)
680 self.assertEqual(st3 != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000681 # we don't particularly care what the ordering is; just that
682 # it's usable and self-consistent
Ezio Melottib3aedd42010-11-20 19:04:17 +0000683 self.assertEqual(st1 < st2, not (st2 <= st1))
684 self.assertEqual(st1 < st3, not (st3 <= st1))
685 self.assertEqual(st2 < st3, not (st3 <= st2))
686 self.assertEqual(st1 < st2, st2 > st1)
687 self.assertEqual(st1 < st3, st3 > st1)
688 self.assertEqual(st2 < st3, st3 > st2)
689 self.assertEqual(st1 <= st2, st2 >= st1)
690 self.assertEqual(st3 <= st1, st1 >= st3)
691 self.assertEqual(st2 <= st3, st3 >= st2)
Mark Dickinson211c6252009-02-01 10:28:51 +0000692 # transitivity
693 bottom = min(st1, st2, st3)
694 top = max(st1, st2, st3)
695 mid = sorted([st1, st2, st3])[1]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000696 self.assertTrue(bottom < mid)
697 self.assertTrue(bottom < top)
698 self.assertTrue(mid < top)
699 self.assertTrue(bottom <= mid)
700 self.assertTrue(bottom <= top)
701 self.assertTrue(mid <= top)
702 self.assertTrue(bottom <= bottom)
703 self.assertTrue(mid <= mid)
704 self.assertTrue(top <= top)
Mark Dickinson211c6252009-02-01 10:28:51 +0000705 # interaction with other types
Ezio Melottib3aedd42010-11-20 19:04:17 +0000706 self.assertEqual(st1 == 1588.602459, False)
707 self.assertEqual('spanish armada' != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000708 self.assertRaises(TypeError, operator.ge, st3, None)
709 self.assertRaises(TypeError, operator.le, False, st1)
710 self.assertRaises(TypeError, operator.lt, st1, 1815)
711 self.assertRaises(TypeError, operator.gt, b'waterloo', st2)
712
Jesus Ceae9c53182012-08-03 14:28:37 +0200713 check_sizeof = support.check_sizeof
714
715 @support.cpython_only
716 def test_sizeof(self):
717 def XXXROUNDUP(n):
718 if n <= 1:
719 return n
720 if n <= 128:
721 return (n + 3) & ~3
722 return 1 << (n - 1).bit_length()
723
724 basesize = support.calcobjsize('Pii')
725 nodesize = struct.calcsize('hP3iP0h')
726 def sizeofchildren(node):
727 if node is None:
728 return 0
729 res = 0
730 hasstr = len(node) > 1 and isinstance(node[-1], str)
731 if hasstr:
732 res += len(node[-1]) + 1
733 children = node[1:-1] if hasstr else node[1:]
734 if children:
735 res += XXXROUNDUP(len(children)) * nodesize
Jesus Ceae9c53182012-08-03 14:28:37 +0200736 for child in children:
737 res += sizeofchildren(child)
738 return res
739
740 def check_st_sizeof(st):
741 self.check_sizeof(st, basesize + nodesize +
742 sizeofchildren(st.totuple()))
743
744 check_st_sizeof(parser.expr('2 + 3'))
745 check_st_sizeof(parser.expr('2 + 3 + 4'))
746 check_st_sizeof(parser.suite('x = 2 + 3'))
747 check_st_sizeof(parser.suite(''))
748 check_st_sizeof(parser.suite('# -*- coding: utf-8 -*-'))
749 check_st_sizeof(parser.expr('[' + '2,' * 1000 + ']'))
750
Mark Dickinson211c6252009-02-01 10:28:51 +0000751
752 # XXX tests for pickling and unpickling of ST objects should go here
753
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500754class OtherParserCase(unittest.TestCase):
755
756 def test_two_args_to_expr(self):
757 # See bug #12264
758 with self.assertRaises(TypeError):
759 parser.expr("a", "b")
760
Fred Drake2e2be372001-09-20 21:33:42 +0000761if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500762 unittest.main()