blob: 72692067fe67ac0627f0257967b2a6c9bd5a169e [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
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000629class ParserStackLimitTestCase(unittest.TestCase):
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000630 """try to push the parser to/over its limits.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000631 see http://bugs.python.org/issue1881 for a discussion
632 """
633 def _nested_expression(self, level):
634 return "["*level+"]"*level
635
636 def test_deeply_nested_list(self):
637 # XXX used to be 99 levels in 2.x
638 e = self._nested_expression(93)
639 st = parser.expr(e)
640 st.compile()
641
642 def test_trigger_memory_error(self):
643 e = self._nested_expression(100)
Ezio Melotti39191842013-03-09 22:17:33 +0200644 rc, out, err = assert_python_failure('-c', e)
645 # parsing the expression will result in an error message
646 # followed by a MemoryError (see #11963)
Ezio Melottie7c32992013-03-10 03:25:45 +0200647 self.assertIn(b's_push: parser stack overflow', err)
648 self.assertIn(b'MemoryError', err)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000649
Mark Dickinson211c6252009-02-01 10:28:51 +0000650class STObjectTestCase(unittest.TestCase):
651 """Test operations on ST objects themselves"""
652
653 def test_comparisons(self):
654 # ST objects should support order and equality comparisons
655 st1 = parser.expr('2 + 3')
656 st2 = parser.suite('x = 2; y = x + 3')
657 st3 = parser.expr('list(x**3 for x in range(20))')
658 st1_copy = parser.expr('2 + 3')
659 st2_copy = parser.suite('x = 2; y = x + 3')
660 st3_copy = parser.expr('list(x**3 for x in range(20))')
661
662 # exercise fast path for object identity
Ezio Melottib3aedd42010-11-20 19:04:17 +0000663 self.assertEqual(st1 == st1, True)
664 self.assertEqual(st2 == st2, True)
665 self.assertEqual(st3 == st3, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000666 # slow path equality
667 self.assertEqual(st1, st1_copy)
668 self.assertEqual(st2, st2_copy)
669 self.assertEqual(st3, st3_copy)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000670 self.assertEqual(st1 == st2, False)
671 self.assertEqual(st1 == st3, False)
672 self.assertEqual(st2 == st3, False)
673 self.assertEqual(st1 != st1, False)
674 self.assertEqual(st2 != st2, False)
675 self.assertEqual(st3 != st3, False)
676 self.assertEqual(st1 != st1_copy, False)
677 self.assertEqual(st2 != st2_copy, False)
678 self.assertEqual(st3 != st3_copy, False)
679 self.assertEqual(st2 != st1, True)
680 self.assertEqual(st1 != st3, True)
681 self.assertEqual(st3 != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000682 # we don't particularly care what the ordering is; just that
683 # it's usable and self-consistent
Ezio Melottib3aedd42010-11-20 19:04:17 +0000684 self.assertEqual(st1 < st2, not (st2 <= st1))
685 self.assertEqual(st1 < st3, not (st3 <= st1))
686 self.assertEqual(st2 < st3, not (st3 <= st2))
687 self.assertEqual(st1 < st2, st2 > st1)
688 self.assertEqual(st1 < st3, st3 > st1)
689 self.assertEqual(st2 < st3, st3 > st2)
690 self.assertEqual(st1 <= st2, st2 >= st1)
691 self.assertEqual(st3 <= st1, st1 >= st3)
692 self.assertEqual(st2 <= st3, st3 >= st2)
Mark Dickinson211c6252009-02-01 10:28:51 +0000693 # transitivity
694 bottom = min(st1, st2, st3)
695 top = max(st1, st2, st3)
696 mid = sorted([st1, st2, st3])[1]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000697 self.assertTrue(bottom < mid)
698 self.assertTrue(bottom < top)
699 self.assertTrue(mid < top)
700 self.assertTrue(bottom <= mid)
701 self.assertTrue(bottom <= top)
702 self.assertTrue(mid <= top)
703 self.assertTrue(bottom <= bottom)
704 self.assertTrue(mid <= mid)
705 self.assertTrue(top <= top)
Mark Dickinson211c6252009-02-01 10:28:51 +0000706 # interaction with other types
Ezio Melottib3aedd42010-11-20 19:04:17 +0000707 self.assertEqual(st1 == 1588.602459, False)
708 self.assertEqual('spanish armada' != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000709 self.assertRaises(TypeError, operator.ge, st3, None)
710 self.assertRaises(TypeError, operator.le, False, st1)
711 self.assertRaises(TypeError, operator.lt, st1, 1815)
712 self.assertRaises(TypeError, operator.gt, b'waterloo', st2)
713
Jesus Ceae9c53182012-08-03 14:28:37 +0200714 check_sizeof = support.check_sizeof
715
716 @support.cpython_only
717 def test_sizeof(self):
718 def XXXROUNDUP(n):
719 if n <= 1:
720 return n
721 if n <= 128:
722 return (n + 3) & ~3
723 return 1 << (n - 1).bit_length()
724
725 basesize = support.calcobjsize('Pii')
726 nodesize = struct.calcsize('hP3iP0h')
727 def sizeofchildren(node):
728 if node is None:
729 return 0
730 res = 0
731 hasstr = len(node) > 1 and isinstance(node[-1], str)
732 if hasstr:
733 res += len(node[-1]) + 1
734 children = node[1:-1] if hasstr else node[1:]
735 if children:
736 res += XXXROUNDUP(len(children)) * nodesize
Jesus Ceae9c53182012-08-03 14:28:37 +0200737 for child in children:
738 res += sizeofchildren(child)
739 return res
740
741 def check_st_sizeof(st):
742 self.check_sizeof(st, basesize + nodesize +
743 sizeofchildren(st.totuple()))
744
745 check_st_sizeof(parser.expr('2 + 3'))
746 check_st_sizeof(parser.expr('2 + 3 + 4'))
747 check_st_sizeof(parser.suite('x = 2 + 3'))
748 check_st_sizeof(parser.suite(''))
749 check_st_sizeof(parser.suite('# -*- coding: utf-8 -*-'))
750 check_st_sizeof(parser.expr('[' + '2,' * 1000 + ']'))
751
Mark Dickinson211c6252009-02-01 10:28:51 +0000752
753 # XXX tests for pickling and unpickling of ST objects should go here
754
Benjamin Petersonf719957d2011-06-04 22:06:42 -0500755class OtherParserCase(unittest.TestCase):
756
757 def test_two_args_to_expr(self):
758 # See bug #12264
759 with self.assertRaises(TypeError):
760 parser.expr("a", "b")
761
Fred Drake2e2be372001-09-20 21:33:42 +0000762if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500763 unittest.main()