blob: dca904cb63d4b5889ad6523b2040247dc27232e0 [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Fred Drake58422e52001-06-04 03:56:24 +00002import unittest
Martin v. Löwis66e26632008-03-18 13:16:05 +00003import sys
Barry Warsaw04f357c2002-07-23 19:04:11 +00004from test import test_support
Fred Drake79ca79d2000-08-21 22:30:53 +00005
6#
7# First, we test that we can generate trees from valid source fragments,
8# and that these valid trees are indeed allowed by the tree-loading side
9# of the parser module.
10#
11
Fred Drake58422e52001-06-04 03:56:24 +000012class RoundtripLegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000013
Fred Drake58422e52001-06-04 03:56:24 +000014 def roundtrip(self, f, s):
15 st1 = f(s)
16 t = st1.totuple()
17 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +000018 st2 = parser.sequence2st(t)
Anthony Baxterc2a5a632004-08-02 06:10:11 +000019 except parser.ParserError, why:
20 self.fail("could not roundtrip %r: %s" % (s, why))
Fred Drake79ca79d2000-08-21 22:30:53 +000021
Fred Drake58422e52001-06-04 03:56:24 +000022 self.assertEquals(t, st2.totuple(),
23 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000024
Fred Drake58422e52001-06-04 03:56:24 +000025 def check_expr(self, s):
26 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000027
Benjamin Petersondcee09d2008-10-31 02:16:05 +000028 def test_flags_passed(self):
29 # The unicode literals flags has to be passed from the paser to AST
30 # generation.
31 suite = parser.suite("from __future__ import unicode_literals; x = ''")
32 code = suite.compile()
33 scope = {}
34 exec code in scope
Ezio Melottib0f5adc2010-01-24 16:58:36 +000035 self.assertIsInstance(scope["x"], unicode)
Benjamin Petersondcee09d2008-10-31 02:16:05 +000036
Fred Drake58422e52001-06-04 03:56:24 +000037 def check_suite(self, s):
38 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000039
Fred Drakecf580c72001-07-17 03:01:29 +000040 def test_yield_statement(self):
Tim Peters496563a2002-04-01 00:28:59 +000041 self.check_suite("def f(): yield 1")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000042 self.check_suite("def f(): yield")
43 self.check_suite("def f(): x += yield")
44 self.check_suite("def f(): x = yield 1")
45 self.check_suite("def f(): x = y = yield 1")
46 self.check_suite("def f(): x = yield")
47 self.check_suite("def f(): x = y = yield")
48 self.check_suite("def f(): 1 + (yield)*2")
49 self.check_suite("def f(): (yield 1)*2")
Tim Peters496563a2002-04-01 00:28:59 +000050 self.check_suite("def f(): return; yield 1")
51 self.check_suite("def f(): yield 1; return")
52 self.check_suite("def f():\n"
Fred Drakecf580c72001-07-17 03:01:29 +000053 " for x in range(30):\n"
54 " yield x\n")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000055 self.check_suite("def f():\n"
56 " if (yield):\n"
57 " yield x\n")
Fred Drakecf580c72001-07-17 03:01:29 +000058
Fred Drake58422e52001-06-04 03:56:24 +000059 def test_expressions(self):
60 self.check_expr("foo(1)")
Alexandre Vassalottiee936a22010-01-09 23:35:54 +000061 self.check_expr("{1:1}")
62 self.check_expr("{1:1, 2:2, 3:3}")
63 self.check_expr("{1:1, 2:2, 3:3,}")
64 self.check_expr("{1}")
65 self.check_expr("{1, 2, 3}")
66 self.check_expr("{1, 2, 3,}")
67 self.check_expr("[]")
68 self.check_expr("[1]")
Fred Drake58422e52001-06-04 03:56:24 +000069 self.check_expr("[1, 2, 3]")
Alexandre Vassalottiee936a22010-01-09 23:35:54 +000070 self.check_expr("[1, 2, 3,]")
71 self.check_expr("()")
72 self.check_expr("(1,)")
73 self.check_expr("(1, 2, 3)")
74 self.check_expr("(1, 2, 3,)")
Fred Drake58422e52001-06-04 03:56:24 +000075 self.check_expr("[x**3 for x in range(20)]")
76 self.check_expr("[x**3 for x in range(20) if x % 3]")
Neal Norwitzd3a91622006-04-12 05:27:46 +000077 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
Alexandre Vassalottib6465472010-01-11 22:36:12 +000078 self.check_expr("[x+y for x in range(30) for y in range(20) if x % 2 if y % 3]")
79 #self.check_expr("[x for x in lambda: True, lambda: False if x()]")
Neal Norwitzd3a91622006-04-12 05:27:46 +000080 self.check_expr("list(x**3 for x in range(20))")
81 self.check_expr("list(x**3 for x in range(20) if x % 3)")
82 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Alexandre Vassalottib6465472010-01-11 22:36:12 +000083 self.check_expr("list(x+y for x in range(30) for y in range(20) if x % 2 if y % 3)")
84 self.check_expr("{x**3 for x in range(30)}")
85 self.check_expr("{x**3 for x in range(30) if x % 3}")
86 self.check_expr("{x**3 for x in range(30) if x % 2 if x % 3}")
87 self.check_expr("{x+y for x in range(30) for y in range(20) if x % 2 if y % 3}")
88 self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30))}")
89 self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30)) if x % 3}")
90 self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30)) if x % 3 if y % 3}")
91 self.check_expr("{x:y for x in range(30) for y in range(20) if x % 2 if y % 3}")
Fred Drake58422e52001-06-04 03:56:24 +000092 self.check_expr("foo(*args)")
93 self.check_expr("foo(*args, **kw)")
94 self.check_expr("foo(**kw)")
95 self.check_expr("foo(key=value)")
96 self.check_expr("foo(key=value, *args)")
97 self.check_expr("foo(key=value, *args, **kw)")
98 self.check_expr("foo(key=value, **kw)")
99 self.check_expr("foo(a, b, c, *args)")
100 self.check_expr("foo(a, b, c, *args, **kw)")
101 self.check_expr("foo(a, b, c, **kw)")
Benjamin Petersonbd6a05f2008-08-19 22:06:11 +0000102 self.check_expr("foo(a, *args, keyword=23)")
Fred Drake58422e52001-06-04 03:56:24 +0000103 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000104 self.check_expr("foo - bar")
105 self.check_expr("foo * bar")
106 self.check_expr("foo / bar")
107 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +0000108 self.check_expr("lambda: 0")
109 self.check_expr("lambda x: 0")
110 self.check_expr("lambda *y: 0")
111 self.check_expr("lambda *y, **z: 0")
112 self.check_expr("lambda **z: 0")
113 self.check_expr("lambda x, y: 0")
114 self.check_expr("lambda foo=bar: 0")
115 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
116 self.check_expr("lambda foo=bar, **z: 0")
117 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
118 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
119 self.check_expr("lambda x, *y, **z: 0")
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000120 self.check_expr("lambda x: 5 if x else 2")
Raymond Hettinger354433a2004-05-19 08:20:33 +0000121 self.check_expr("(x for x in range(10))")
122 self.check_expr("foo(x for x in range(10))")
Fred Drake79ca79d2000-08-21 22:30:53 +0000123
Fred Drake58422e52001-06-04 03:56:24 +0000124 def test_print(self):
125 self.check_suite("print")
126 self.check_suite("print 1")
127 self.check_suite("print 1,")
128 self.check_suite("print >>fp")
129 self.check_suite("print >>fp, 1")
130 self.check_suite("print >>fp, 1,")
Fred Drake79ca79d2000-08-21 22:30:53 +0000131
Fred Drake58422e52001-06-04 03:56:24 +0000132 def test_simple_expression(self):
133 # expr_stmt
134 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +0000135
Fred Drake58422e52001-06-04 03:56:24 +0000136 def test_simple_assignments(self):
137 self.check_suite("a = b")
138 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +0000139
Fred Drake58422e52001-06-04 03:56:24 +0000140 def test_simple_augmented_assignments(self):
141 self.check_suite("a += b")
142 self.check_suite("a -= b")
143 self.check_suite("a *= b")
144 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000145 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000146 self.check_suite("a %= b")
147 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")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000153
Fred Drake58422e52001-06-04 03:56:24 +0000154 def test_function_defs(self):
155 self.check_suite("def f(): pass")
156 self.check_suite("def f(*args): pass")
157 self.check_suite("def f(*args, **kw): pass")
158 self.check_suite("def f(**kw): pass")
159 self.check_suite("def f(foo=bar): pass")
160 self.check_suite("def f(foo=bar, *args): pass")
161 self.check_suite("def f(foo=bar, *args, **kw): pass")
162 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000163
Fred Drake58422e52001-06-04 03:56:24 +0000164 self.check_suite("def f(a, b): pass")
165 self.check_suite("def f(a, b, *args): pass")
166 self.check_suite("def f(a, b, *args, **kw): pass")
167 self.check_suite("def f(a, b, **kw): pass")
168 self.check_suite("def f(a, b, foo=bar): pass")
169 self.check_suite("def f(a, b, foo=bar, *args): pass")
170 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
171 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000172
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000173 self.check_suite("@staticmethod\n"
174 "def f(): pass")
175 self.check_suite("@staticmethod\n"
176 "@funcattrs(x, y)\n"
177 "def f(): pass")
178 self.check_suite("@funcattrs()\n"
179 "def f(): pass")
180
Brett Cannonf4189912005-04-09 02:30:16 +0000181 def test_class_defs(self):
182 self.check_suite("class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000183
Fred Drake58422e52001-06-04 03:56:24 +0000184 def test_import_from_statement(self):
185 self.check_suite("from sys.path import *")
186 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000187 self.check_suite("from sys.path import (dirname)")
188 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000189 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000190 self.check_suite("from sys.path import (dirname as my_dirname)")
191 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000192 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000193 self.check_suite("from sys.path import (dirname, basename)")
194 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000195 self.check_suite(
196 "from sys.path import dirname as my_dirname, basename")
197 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000198 "from sys.path import (dirname as my_dirname, basename)")
199 self.check_suite(
200 "from sys.path import (dirname as my_dirname, basename,)")
201 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000202 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000203 self.check_suite(
204 "from sys.path import (dirname, basename as my_basename)")
205 self.check_suite(
206 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Peterson6624a9f2008-11-03 15:14:51 +0000207 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000208
Fred Drake58422e52001-06-04 03:56:24 +0000209 def test_basic_import_statement(self):
210 self.check_suite("import sys")
211 self.check_suite("import sys as system")
212 self.check_suite("import sys, math")
213 self.check_suite("import sys as system, math")
214 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000215
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000216 def test_pep263(self):
217 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
218 "pass\n")
219
220 def test_assert(self):
221 self.check_suite("assert alo < ahi and blo < bhi\n")
222
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +0000223 def test_with(self):
224 self.check_suite("with open('x'): pass\n")
225 self.check_suite("with open('x') as f: pass\n")
Georg Brandl944f6842009-05-25 21:02:56 +0000226 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +0000227
Georg Brandlfe879e82008-12-05 12:09:41 +0000228 def test_try_stmt(self):
229 self.check_suite("try: pass\nexcept: pass\n")
230 self.check_suite("try: pass\nfinally: pass\n")
231 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
232 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
233 "finally: pass\n")
234 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
235 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
236 "finally: pass\n")
237
Mark Dickinson070f0ab2010-06-30 16:27:57 +0000238 def test_except_clause(self):
239 self.check_suite("try: pass\nexcept: pass\n")
240 self.check_suite("try: pass\nexcept A: pass\n")
241 self.check_suite("try: pass\nexcept A, e: pass\n")
242 self.check_suite("try: pass\nexcept A as e: pass\n")
243
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000244 def test_position(self):
245 # An absolutely minimal test of position information. Better
246 # tests would be a big project.
Benjamin Petersona4a04d12009-12-06 21:24:30 +0000247 code = "def f(x):\n return x + 1"
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000248 st1 = parser.suite(code)
249 st2 = st1.totuple(line_info=1, col_info=1)
250
251 def walk(tree):
252 node_type = tree[0]
253 next = tree[1]
254 if isinstance(next, tuple):
255 for elt in tree[1:]:
256 for x in walk(elt):
257 yield x
258 else:
259 yield tree
Tim Peters147f9ae2006-08-25 22:05:39 +0000260
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000261 terminals = list(walk(st2))
262 self.assertEqual([
263 (1, 'def', 1, 0),
264 (1, 'f', 1, 4),
265 (7, '(', 1, 5),
266 (1, 'x', 1, 6),
267 (8, ')', 1, 7),
268 (11, ':', 1, 8),
269 (4, '', 1, 9),
270 (5, '', 2, -1),
271 (1, 'return', 2, 4),
272 (1, 'x', 2, 11),
273 (14, '+', 2, 13),
274 (2, '1', 2, 15),
275 (4, '', 2, 16),
Benjamin Petersona4a04d12009-12-06 21:24:30 +0000276 (6, '', 2, -1),
277 (4, '', 2, -1),
278 (0, '', 2, -1)],
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000279 terminals)
280
281
Fred Drake79ca79d2000-08-21 22:30:53 +0000282#
283# Second, we take *invalid* trees and make sure we get ParserError
284# rejections for them.
285#
286
Fred Drake58422e52001-06-04 03:56:24 +0000287class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000288
Fred Drake58422e52001-06-04 03:56:24 +0000289 def check_bad_tree(self, tree, label):
290 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000291 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000292 except parser.ParserError:
293 pass
294 else:
295 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000296
Fred Drake58422e52001-06-04 03:56:24 +0000297 def test_junk(self):
298 # not even remotely valid:
299 self.check_bad_tree((1, 2, 3), "<junk>")
300
Fred Drakecf580c72001-07-17 03:01:29 +0000301 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000302 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000303 tree = \
304 (257,
305 (264,
306 (285,
307 (259,
308 (1, 'def'),
309 (1, 'f'),
310 (260, (7, '('), (8, ')')),
311 (11, ':'),
312 (291,
313 (4, ''),
314 (5, ''),
315 (264,
316 (265,
317 (266,
318 (272,
319 (275,
320 (1, 'return'),
321 (313,
322 (292,
323 (293,
324 (294,
325 (295,
326 (297,
327 (298,
328 (299,
329 (300,
330 (301,
331 (302, (303, (304, (305, (2, '1')))))))))))))))))),
332 (264,
333 (265,
334 (266,
335 (272,
336 (276,
337 (1, 'yield'),
338 (313,
339 (292,
340 (293,
341 (294,
342 (295,
343 (297,
344 (298,
345 (299,
346 (300,
347 (301,
348 (302,
349 (303, (304, (305, (2, '1')))))))))))))))))),
350 (4, ''))),
351 (6, ''))))),
352 (4, ''),
353 (0, ''))))
354 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
355
356 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000357 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000358 tree = \
359 (257,
360 (264,
361 (265,
362 (266,
363 (278,
364 (1, 'from'),
365 (281, (1, '__future__')),
366 (1, 'import'),
367 (279, (1, 'generators')))),
368 (4, ''))),
369 (264,
370 (285,
371 (259,
372 (1, 'def'),
373 (1, 'f'),
374 (260, (7, '('), (8, ')')),
375 (11, ':'),
376 (291,
377 (4, ''),
378 (5, ''),
379 (264,
380 (265,
381 (266,
382 (272,
383 (275,
384 (1, 'return'),
385 (313,
386 (292,
387 (293,
388 (294,
389 (295,
390 (297,
391 (298,
392 (299,
393 (300,
394 (301,
395 (302, (303, (304, (305, (2, '1')))))))))))))))))),
396 (264,
397 (265,
398 (266,
399 (272,
400 (276,
401 (1, 'yield'),
402 (313,
403 (292,
404 (293,
405 (294,
406 (295,
407 (297,
408 (298,
409 (299,
410 (300,
411 (301,
412 (302,
413 (303, (304, (305, (2, '1')))))))))))))))))),
414 (4, ''))),
415 (6, ''))))),
416 (4, ''),
417 (0, ''))))
418 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
419
Fred Drake58422e52001-06-04 03:56:24 +0000420 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000421 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000422 tree = \
423 (257,
424 (264,
425 (265,
426 (266,
427 (268,
428 (1, 'print'),
429 (35, '>>'),
430 (290,
431 (291,
432 (292,
433 (293,
434 (295,
435 (296,
436 (297,
437 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
438 (12, ','))),
439 (4, ''))),
440 (0, ''))
441 self.check_bad_tree(tree, "print >>fp,")
442
443 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000444 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000445 tree = \
446 (258,
447 (311,
448 (290,
449 (291,
450 (292,
451 (293,
452 (295,
453 (296,
454 (297,
455 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
456 (12, ','),
457 (12, ','),
458 (290,
459 (291,
460 (292,
461 (293,
462 (295,
463 (296,
464 (297,
465 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
466 (4, ''),
467 (0, ''))
468 self.check_bad_tree(tree, "a,,c")
469
470 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000471 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000472 tree = \
473 (257,
474 (264,
475 (265,
476 (266,
477 (267,
478 (312,
479 (291,
480 (292,
481 (293,
482 (294,
483 (296,
484 (297,
485 (298,
486 (299,
487 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
488 (268, (37, '$=')),
489 (312,
490 (291,
491 (292,
492 (293,
493 (294,
494 (296,
495 (297,
496 (298,
497 (299,
498 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
499 (4, ''))),
500 (0, ''))
501 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000502
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000503 def test_malformed_global(self):
504 #doesn't have global keyword in ast
505 tree = (257,
506 (264,
507 (265,
508 (266,
509 (282, (1, 'foo'))), (4, ''))),
510 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000511 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000512 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000513
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514
515class CompileTestCase(unittest.TestCase):
516
517 # These tests are very minimal. :-(
518
519 def test_compile_expr(self):
520 st = parser.expr('2 + 3')
521 code = parser.compilest(st)
522 self.assertEquals(eval(code), 5)
523
524 def test_compile_suite(self):
525 st = parser.suite('x = 2; y = x + 3')
526 code = parser.compilest(st)
527 globs = {}
528 exec code in globs
529 self.assertEquals(globs['y'], 5)
530
531 def test_compile_error(self):
532 st = parser.suite('1 = 3 + 4')
533 self.assertRaises(SyntaxError, parser.compilest, st)
534
Guido van Rossumb6ac23c2007-07-18 17:19:14 +0000535 def test_compile_badunicode(self):
536 st = parser.suite('a = u"\U12345678"')
537 self.assertRaises(SyntaxError, parser.compilest, st)
538 st = parser.suite('a = u"\u1"')
539 self.assertRaises(SyntaxError, parser.compilest, st)
540
Facundo Batistafc2d0102008-02-23 12:01:13 +0000541class ParserStackLimitTestCase(unittest.TestCase):
542 """try to push the parser to/over it's limits.
543 see http://bugs.python.org/issue1881 for a discussion
544 """
545 def _nested_expression(self, level):
546 return "["*level+"]"*level
547
548 def test_deeply_nested_list(self):
549 e = self._nested_expression(99)
550 st = parser.expr(e)
551 st.compile()
552
553 def test_trigger_memory_error(self):
554 e = self._nested_expression(100)
Martin v. Löwis66e26632008-03-18 13:16:05 +0000555 print >>sys.stderr, "Expecting 's_push: parser stack overflow' in next line"
Facundo Batistafc2d0102008-02-23 12:01:13 +0000556 self.assertRaises(MemoryError, parser.expr, e)
557
Fred Drake2e2be372001-09-20 21:33:42 +0000558def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000559 test_support.run_unittest(
560 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561 IllegalSyntaxTestCase,
562 CompileTestCase,
Facundo Batistafc2d0102008-02-23 12:01:13 +0000563 ParserStackLimitTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000564 )
Fred Drake2e2be372001-09-20 21:33:42 +0000565
566
567if __name__ == "__main__":
568 test_main()