blob: f6105fc8184326a59bb7a7ac34e604806133172c [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Fred Drake79ca79d2000-08-21 22:30:53 +00006
7#
8# First, we test that we can generate trees from valid source fragments,
9# and that these valid trees are indeed allowed by the tree-loading side
10# of the parser module.
11#
12
Fred Drake58422e52001-06-04 03:56:24 +000013class RoundtripLegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000014
Fred Drake58422e52001-06-04 03:56:24 +000015 def roundtrip(self, f, s):
16 st1 = f(s)
17 t = st1.totuple()
18 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +000019 st2 = parser.sequence2st(t)
Guido van Rossumb940e112007-01-10 16:19:56 +000020 except parser.ParserError as why:
Anthony Baxterc2a5a632004-08-02 06:10:11 +000021 self.fail("could not roundtrip %r: %s" % (s, why))
Fred Drake79ca79d2000-08-21 22:30:53 +000022
Ezio Melottib3aedd42010-11-20 19:04:17 +000023 self.assertEqual(t, st2.totuple(),
24 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000025
Fred Drake58422e52001-06-04 03:56:24 +000026 def check_expr(self, s):
27 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000028
Benjamin Petersonf216c942008-10-31 02:28:05 +000029 def test_flags_passed(self):
30 # The unicode literals flags has to be passed from the paser to AST
31 # generation.
32 suite = parser.suite("from __future__ import unicode_literals; x = ''")
33 code = suite.compile()
34 scope = {}
35 exec(code, {}, scope)
Ezio Melottie9615932010-01-24 19:26:24 +000036 self.assertIsInstance(scope["x"], str)
Benjamin Petersonf216c942008-10-31 02:28:05 +000037
Fred Drake58422e52001-06-04 03:56:24 +000038 def check_suite(self, s):
39 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000040
Fred Drakecf580c72001-07-17 03:01:29 +000041 def test_yield_statement(self):
Tim Peters496563a2002-04-01 00:28:59 +000042 self.check_suite("def f(): yield 1")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000043 self.check_suite("def f(): yield")
44 self.check_suite("def f(): x += yield")
45 self.check_suite("def f(): x = yield 1")
46 self.check_suite("def f(): x = y = yield 1")
47 self.check_suite("def f(): x = yield")
48 self.check_suite("def f(): x = y = yield")
49 self.check_suite("def f(): 1 + (yield)*2")
50 self.check_suite("def f(): (yield 1)*2")
Tim Peters496563a2002-04-01 00:28:59 +000051 self.check_suite("def f(): return; yield 1")
52 self.check_suite("def f(): yield 1; return")
53 self.check_suite("def f():\n"
Fred Drakecf580c72001-07-17 03:01:29 +000054 " for x in range(30):\n"
55 " yield x\n")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000056 self.check_suite("def f():\n"
57 " if (yield):\n"
58 " yield x\n")
Fred Drakecf580c72001-07-17 03:01:29 +000059
Mark Dickinson407b3bd2012-04-29 22:18:31 +010060 def test_nonlocal_statement(self):
61 self.check_suite("def f():\n"
62 " x = 0\n"
63 " def g():\n"
64 " nonlocal x\n")
65 self.check_suite("def f():\n"
66 " x = y = 0\n"
67 " def g():\n"
68 " nonlocal x, y\n")
69
Fred Drake58422e52001-06-04 03:56:24 +000070 def test_expressions(self):
71 self.check_expr("foo(1)")
72 self.check_expr("[1, 2, 3]")
73 self.check_expr("[x**3 for x in range(20)]")
74 self.check_expr("[x**3 for x in range(20) if x % 3]")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000075 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
76 self.check_expr("list(x**3 for x in range(20))")
77 self.check_expr("list(x**3 for x in range(20) if x % 3)")
78 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Fred Drake58422e52001-06-04 03:56:24 +000079 self.check_expr("foo(*args)")
80 self.check_expr("foo(*args, **kw)")
81 self.check_expr("foo(**kw)")
82 self.check_expr("foo(key=value)")
83 self.check_expr("foo(key=value, *args)")
84 self.check_expr("foo(key=value, *args, **kw)")
85 self.check_expr("foo(key=value, **kw)")
86 self.check_expr("foo(a, b, c, *args)")
87 self.check_expr("foo(a, b, c, *args, **kw)")
88 self.check_expr("foo(a, b, c, **kw)")
Benjamin Peterson3938a902008-08-20 02:33:00 +000089 self.check_expr("foo(a, *args, keyword=23)")
Fred Drake58422e52001-06-04 03:56:24 +000090 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +000091 self.check_expr("foo - bar")
92 self.check_expr("foo * bar")
93 self.check_expr("foo / bar")
94 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +000095 self.check_expr("lambda: 0")
96 self.check_expr("lambda x: 0")
97 self.check_expr("lambda *y: 0")
98 self.check_expr("lambda *y, **z: 0")
99 self.check_expr("lambda **z: 0")
100 self.check_expr("lambda x, y: 0")
101 self.check_expr("lambda foo=bar: 0")
102 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
103 self.check_expr("lambda foo=bar, **z: 0")
104 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
105 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
106 self.check_expr("lambda x, *y, **z: 0")
Raymond Hettinger354433a2004-05-19 08:20:33 +0000107 self.check_expr("(x for x in range(10))")
108 self.check_expr("foo(x for x in range(10))")
Fred Drake79ca79d2000-08-21 22:30:53 +0000109
Fred Drake58422e52001-06-04 03:56:24 +0000110 def test_simple_expression(self):
111 # expr_stmt
112 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +0000113
Fred Drake58422e52001-06-04 03:56:24 +0000114 def test_simple_assignments(self):
115 self.check_suite("a = b")
116 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +0000117
Fred Drake58422e52001-06-04 03:56:24 +0000118 def test_simple_augmented_assignments(self):
119 self.check_suite("a += b")
120 self.check_suite("a -= b")
121 self.check_suite("a *= b")
122 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000123 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000124 self.check_suite("a %= b")
125 self.check_suite("a &= b")
126 self.check_suite("a |= b")
127 self.check_suite("a ^= b")
128 self.check_suite("a <<= b")
129 self.check_suite("a >>= b")
130 self.check_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000131
Fred Drake58422e52001-06-04 03:56:24 +0000132 def test_function_defs(self):
133 self.check_suite("def f(): pass")
134 self.check_suite("def f(*args): pass")
135 self.check_suite("def f(*args, **kw): pass")
136 self.check_suite("def f(**kw): pass")
137 self.check_suite("def f(foo=bar): pass")
138 self.check_suite("def f(foo=bar, *args): pass")
139 self.check_suite("def f(foo=bar, *args, **kw): pass")
140 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000141
Fred Drake58422e52001-06-04 03:56:24 +0000142 self.check_suite("def f(a, b): pass")
143 self.check_suite("def f(a, b, *args): pass")
144 self.check_suite("def f(a, b, *args, **kw): pass")
145 self.check_suite("def f(a, b, **kw): pass")
146 self.check_suite("def f(a, b, foo=bar): pass")
147 self.check_suite("def f(a, b, foo=bar, *args): pass")
148 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
149 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000150
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000151 self.check_suite("@staticmethod\n"
152 "def f(): pass")
153 self.check_suite("@staticmethod\n"
154 "@funcattrs(x, y)\n"
155 "def f(): pass")
156 self.check_suite("@funcattrs()\n"
157 "def f(): pass")
158
Mark Dickinsonea7e9f92012-04-29 18:34:40 +0100159 # keyword-only arguments
160 self.check_suite("def f(*, a): pass")
161 self.check_suite("def f(*, a = 5): pass")
162 self.check_suite("def f(*, a = 5, b): pass")
163 self.check_suite("def f(*, a, b = 5): pass")
164 self.check_suite("def f(*, a, b = 5, **kwds): pass")
165 self.check_suite("def f(*args, a): pass")
166 self.check_suite("def f(*args, a = 5): pass")
167 self.check_suite("def f(*args, a = 5, b): pass")
168 self.check_suite("def f(*args, a, b = 5): pass")
169 self.check_suite("def f(*args, a, b = 5, **kwds): pass")
170
171 # function annotations
172 self.check_suite("def f(a: int): pass")
173 self.check_suite("def f(a: int = 5): pass")
174 self.check_suite("def f(*args: list): pass")
175 self.check_suite("def f(**kwds: dict): pass")
176 self.check_suite("def f(*, a: int): pass")
177 self.check_suite("def f(*, a: int = 5): pass")
178 self.check_suite("def f() -> int: pass")
179
Brett Cannonf4189912005-04-09 02:30:16 +0000180 def test_class_defs(self):
181 self.check_suite("class foo():pass")
Guido van Rossumfc158e22007-11-15 19:17:28 +0000182 self.check_suite("class foo(object):pass")
Mark Dickinson2bd61a92010-07-04 16:37:31 +0000183 self.check_suite("@class_decorator\n"
184 "class foo():pass")
185 self.check_suite("@class_decorator(arg)\n"
186 "class foo():pass")
187 self.check_suite("@decorator1\n"
188 "@decorator2\n"
189 "class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000190
Fred Drake58422e52001-06-04 03:56:24 +0000191 def test_import_from_statement(self):
192 self.check_suite("from sys.path import *")
193 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000194 self.check_suite("from sys.path import (dirname)")
195 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000196 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000197 self.check_suite("from sys.path import (dirname as my_dirname)")
198 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000199 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000200 self.check_suite("from sys.path import (dirname, basename)")
201 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000202 self.check_suite(
203 "from sys.path import dirname as my_dirname, basename")
204 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000205 "from sys.path import (dirname as my_dirname, basename)")
206 self.check_suite(
207 "from sys.path import (dirname as my_dirname, basename,)")
208 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000209 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000210 self.check_suite(
211 "from sys.path import (dirname, basename as my_basename)")
212 self.check_suite(
213 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000214 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000215
Fred Drake58422e52001-06-04 03:56:24 +0000216 def test_basic_import_statement(self):
217 self.check_suite("import sys")
218 self.check_suite("import sys as system")
219 self.check_suite("import sys, math")
220 self.check_suite("import sys as system, math")
221 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000222
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000223 def test_relative_imports(self):
224 self.check_suite("from . import name")
225 self.check_suite("from .. import name")
Mark Dickinsonfeb3b752010-07-04 18:38:57 +0000226 # check all the way up to '....', since '...' is tokenized
227 # differently from '.' (it's an ellipsis token).
228 self.check_suite("from ... import name")
229 self.check_suite("from .... import name")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000230 self.check_suite("from .pkg import name")
231 self.check_suite("from ..pkg import name")
Mark Dickinsonfeb3b752010-07-04 18:38:57 +0000232 self.check_suite("from ...pkg import name")
233 self.check_suite("from ....pkg import name")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000234
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000235 def test_pep263(self):
236 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
237 "pass\n")
238
239 def test_assert(self):
240 self.check_suite("assert alo < ahi and blo < bhi\n")
241
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000242 def test_with(self):
243 self.check_suite("with open('x'): pass\n")
244 self.check_suite("with open('x') as f: pass\n")
Georg Brandl0c315622009-05-25 21:10:36 +0000245 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000246
Georg Brandleee31162008-12-07 15:15:22 +0000247 def test_try_stmt(self):
248 self.check_suite("try: pass\nexcept: pass\n")
249 self.check_suite("try: pass\nfinally: pass\n")
250 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
251 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
252 "finally: pass\n")
253 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
254 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
255 "finally: pass\n")
256
Thomas Wouters89f507f2006-12-13 04:49:30 +0000257 def test_position(self):
258 # An absolutely minimal test of position information. Better
259 # tests would be a big project.
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000260 code = "def f(x):\n return x + 1"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000261 st1 = parser.suite(code)
262 st2 = st1.totuple(line_info=1, col_info=1)
263
264 def walk(tree):
265 node_type = tree[0]
266 next = tree[1]
267 if isinstance(next, tuple):
268 for elt in tree[1:]:
269 for x in walk(elt):
270 yield x
271 else:
272 yield tree
273
274 terminals = list(walk(st2))
275 self.assertEqual([
276 (1, 'def', 1, 0),
277 (1, 'f', 1, 4),
278 (7, '(', 1, 5),
279 (1, 'x', 1, 6),
280 (8, ')', 1, 7),
281 (11, ':', 1, 8),
282 (4, '', 1, 9),
283 (5, '', 2, -1),
284 (1, 'return', 2, 4),
285 (1, 'x', 2, 11),
286 (14, '+', 2, 13),
287 (2, '1', 2, 15),
288 (4, '', 2, 16),
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000289 (6, '', 2, -1),
290 (4, '', 2, -1),
291 (0, '', 2, -1)],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000292 terminals)
293
Benjamin Peterson4905e802009-09-27 02:43:28 +0000294 def test_extended_unpacking(self):
295 self.check_suite("*a = y")
296 self.check_suite("x, *b, = m")
297 self.check_suite("[*a, *b] = y")
298 self.check_suite("for [*x, b] in x: pass")
299
Thomas Wouters89f507f2006-12-13 04:49:30 +0000300
Fred Drake79ca79d2000-08-21 22:30:53 +0000301#
302# Second, we take *invalid* trees and make sure we get ParserError
303# rejections for them.
304#
305
Fred Drake58422e52001-06-04 03:56:24 +0000306class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000307
Fred Drake58422e52001-06-04 03:56:24 +0000308 def check_bad_tree(self, tree, label):
309 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000310 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000311 except parser.ParserError:
312 pass
313 else:
314 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000315
Fred Drake58422e52001-06-04 03:56:24 +0000316 def test_junk(self):
317 # not even remotely valid:
318 self.check_bad_tree((1, 2, 3), "<junk>")
319
Fred Drakecf580c72001-07-17 03:01:29 +0000320 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000321 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000322 tree = \
323 (257,
324 (264,
325 (285,
326 (259,
327 (1, 'def'),
328 (1, 'f'),
329 (260, (7, '('), (8, ')')),
330 (11, ':'),
331 (291,
332 (4, ''),
333 (5, ''),
334 (264,
335 (265,
336 (266,
337 (272,
338 (275,
339 (1, 'return'),
340 (313,
341 (292,
342 (293,
343 (294,
344 (295,
345 (297,
346 (298,
347 (299,
348 (300,
349 (301,
350 (302, (303, (304, (305, (2, '1')))))))))))))))))),
351 (264,
352 (265,
353 (266,
354 (272,
355 (276,
356 (1, 'yield'),
357 (313,
358 (292,
359 (293,
360 (294,
361 (295,
362 (297,
363 (298,
364 (299,
365 (300,
366 (301,
367 (302,
368 (303, (304, (305, (2, '1')))))))))))))))))),
369 (4, ''))),
370 (6, ''))))),
371 (4, ''),
372 (0, ''))))
373 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
374
375 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000376 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000377 tree = \
378 (257,
379 (264,
380 (265,
381 (266,
382 (278,
383 (1, 'from'),
384 (281, (1, '__future__')),
385 (1, 'import'),
386 (279, (1, 'generators')))),
387 (4, ''))),
388 (264,
389 (285,
390 (259,
391 (1, 'def'),
392 (1, 'f'),
393 (260, (7, '('), (8, ')')),
394 (11, ':'),
395 (291,
396 (4, ''),
397 (5, ''),
398 (264,
399 (265,
400 (266,
401 (272,
402 (275,
403 (1, 'return'),
404 (313,
405 (292,
406 (293,
407 (294,
408 (295,
409 (297,
410 (298,
411 (299,
412 (300,
413 (301,
414 (302, (303, (304, (305, (2, '1')))))))))))))))))),
415 (264,
416 (265,
417 (266,
418 (272,
419 (276,
420 (1, 'yield'),
421 (313,
422 (292,
423 (293,
424 (294,
425 (295,
426 (297,
427 (298,
428 (299,
429 (300,
430 (301,
431 (302,
432 (303, (304, (305, (2, '1')))))))))))))))))),
433 (4, ''))),
434 (6, ''))))),
435 (4, ''),
436 (0, ''))))
437 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
438
Fred Drake58422e52001-06-04 03:56:24 +0000439 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000440 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000441 tree = \
442 (258,
443 (311,
444 (290,
445 (291,
446 (292,
447 (293,
448 (295,
449 (296,
450 (297,
451 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
452 (12, ','),
453 (12, ','),
454 (290,
455 (291,
456 (292,
457 (293,
458 (295,
459 (296,
460 (297,
461 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
462 (4, ''),
463 (0, ''))
464 self.check_bad_tree(tree, "a,,c")
465
466 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000467 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000468 tree = \
469 (257,
470 (264,
471 (265,
472 (266,
473 (267,
474 (312,
475 (291,
476 (292,
477 (293,
478 (294,
479 (296,
480 (297,
481 (298,
482 (299,
483 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
484 (268, (37, '$=')),
485 (312,
486 (291,
487 (292,
488 (293,
489 (294,
490 (296,
491 (297,
492 (298,
493 (299,
494 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
495 (4, ''))),
496 (0, ''))
497 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000498
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000499 def test_malformed_global(self):
500 #doesn't have global keyword in ast
501 tree = (257,
502 (264,
503 (265,
504 (266,
505 (282, (1, 'foo'))), (4, ''))),
506 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000507 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000508 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000509
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000510 def test_missing_import_source(self):
Mark Dickinson3445b482010-07-04 18:15:26 +0000511 # from import fred
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000512 tree = \
513 (257,
Mark Dickinson3445b482010-07-04 18:15:26 +0000514 (268,
515 (269,
516 (270,
517 (282,
518 (284, (1, 'from'), (1, 'import'),
519 (287, (285, (1, 'fred')))))),
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000520 (4, ''))),
521 (4, ''), (0, ''))
Mark Dickinson3445b482010-07-04 18:15:26 +0000522 self.check_bad_tree(tree, "from import fred")
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000523
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524
525class CompileTestCase(unittest.TestCase):
526
527 # These tests are very minimal. :-(
528
529 def test_compile_expr(self):
530 st = parser.expr('2 + 3')
531 code = parser.compilest(st)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000532 self.assertEqual(eval(code), 5)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533
534 def test_compile_suite(self):
535 st = parser.suite('x = 2; y = x + 3')
536 code = parser.compilest(st)
537 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000538 exec(code, globs)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000539 self.assertEqual(globs['y'], 5)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540
541 def test_compile_error(self):
542 st = parser.suite('1 = 3 + 4')
543 self.assertRaises(SyntaxError, parser.compilest, st)
544
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000545 def test_compile_badunicode(self):
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000546 st = parser.suite('a = "\\U12345678"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000547 self.assertRaises(SyntaxError, parser.compilest, st)
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000548 st = parser.suite('a = "\\u1"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000549 self.assertRaises(SyntaxError, parser.compilest, st)
550
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000551 def test_issue_9011(self):
552 # Issue 9011: compilation of an unary minus expression changed
553 # the meaning of the ST, so that a second compilation produced
554 # incorrect results.
555 st = parser.expr('-3')
556 code1 = parser.compilest(st)
557 self.assertEqual(eval(code1), -3)
558 code2 = parser.compilest(st)
559 self.assertEqual(eval(code2), -3)
560
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000561class ParserStackLimitTestCase(unittest.TestCase):
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000562 """try to push the parser to/over its limits.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000563 see http://bugs.python.org/issue1881 for a discussion
564 """
565 def _nested_expression(self, level):
566 return "["*level+"]"*level
567
568 def test_deeply_nested_list(self):
569 # XXX used to be 99 levels in 2.x
570 e = self._nested_expression(93)
571 st = parser.expr(e)
572 st.compile()
573
574 def test_trigger_memory_error(self):
575 e = self._nested_expression(100)
Christian Heimesb186d002008-03-18 15:15:01 +0000576 print("Expecting 's_push: parser stack overflow' in next line",
577 file=sys.stderr)
Antoine Pitrou88909542009-06-29 13:54:42 +0000578 sys.stderr.flush()
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000579 self.assertRaises(MemoryError, parser.expr, e)
580
Mark Dickinson211c6252009-02-01 10:28:51 +0000581class STObjectTestCase(unittest.TestCase):
582 """Test operations on ST objects themselves"""
583
584 def test_comparisons(self):
585 # ST objects should support order and equality comparisons
586 st1 = parser.expr('2 + 3')
587 st2 = parser.suite('x = 2; y = x + 3')
588 st3 = parser.expr('list(x**3 for x in range(20))')
589 st1_copy = parser.expr('2 + 3')
590 st2_copy = parser.suite('x = 2; y = x + 3')
591 st3_copy = parser.expr('list(x**3 for x in range(20))')
592
593 # exercise fast path for object identity
Ezio Melottib3aedd42010-11-20 19:04:17 +0000594 self.assertEqual(st1 == st1, True)
595 self.assertEqual(st2 == st2, True)
596 self.assertEqual(st3 == st3, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000597 # slow path equality
598 self.assertEqual(st1, st1_copy)
599 self.assertEqual(st2, st2_copy)
600 self.assertEqual(st3, st3_copy)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000601 self.assertEqual(st1 == st2, False)
602 self.assertEqual(st1 == st3, False)
603 self.assertEqual(st2 == st3, False)
604 self.assertEqual(st1 != st1, False)
605 self.assertEqual(st2 != st2, False)
606 self.assertEqual(st3 != st3, False)
607 self.assertEqual(st1 != st1_copy, False)
608 self.assertEqual(st2 != st2_copy, False)
609 self.assertEqual(st3 != st3_copy, False)
610 self.assertEqual(st2 != st1, True)
611 self.assertEqual(st1 != st3, True)
612 self.assertEqual(st3 != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000613 # we don't particularly care what the ordering is; just that
614 # it's usable and self-consistent
Ezio Melottib3aedd42010-11-20 19:04:17 +0000615 self.assertEqual(st1 < st2, not (st2 <= st1))
616 self.assertEqual(st1 < st3, not (st3 <= st1))
617 self.assertEqual(st2 < st3, not (st3 <= st2))
618 self.assertEqual(st1 < st2, st2 > st1)
619 self.assertEqual(st1 < st3, st3 > st1)
620 self.assertEqual(st2 < st3, st3 > st2)
621 self.assertEqual(st1 <= st2, st2 >= st1)
622 self.assertEqual(st3 <= st1, st1 >= st3)
623 self.assertEqual(st2 <= st3, st3 >= st2)
Mark Dickinson211c6252009-02-01 10:28:51 +0000624 # transitivity
625 bottom = min(st1, st2, st3)
626 top = max(st1, st2, st3)
627 mid = sorted([st1, st2, st3])[1]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000628 self.assertTrue(bottom < mid)
629 self.assertTrue(bottom < top)
630 self.assertTrue(mid < top)
631 self.assertTrue(bottom <= mid)
632 self.assertTrue(bottom <= top)
633 self.assertTrue(mid <= top)
634 self.assertTrue(bottom <= bottom)
635 self.assertTrue(mid <= mid)
636 self.assertTrue(top <= top)
Mark Dickinson211c6252009-02-01 10:28:51 +0000637 # interaction with other types
Ezio Melottib3aedd42010-11-20 19:04:17 +0000638 self.assertEqual(st1 == 1588.602459, False)
639 self.assertEqual('spanish armada' != st2, True)
Mark Dickinson211c6252009-02-01 10:28:51 +0000640 self.assertRaises(TypeError, operator.ge, st3, None)
641 self.assertRaises(TypeError, operator.le, False, st1)
642 self.assertRaises(TypeError, operator.lt, st1, 1815)
643 self.assertRaises(TypeError, operator.gt, b'waterloo', st2)
644
645
646 # XXX tests for pickling and unpickling of ST objects should go here
647
648
Fred Drake2e2be372001-09-20 21:33:42 +0000649def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000650 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000651 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652 IllegalSyntaxTestCase,
653 CompileTestCase,
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000654 ParserStackLimitTestCase,
Mark Dickinson211c6252009-02-01 10:28:51 +0000655 STObjectTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000656 )
Fred Drake2e2be372001-09-20 21:33:42 +0000657
658
659if __name__ == "__main__":
660 test_main()