blob: 07a4c3287c9e7af6f41e0c8095f207003e05e862 [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
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000238 def test_position(self):
239 # An absolutely minimal test of position information. Better
240 # tests would be a big project.
Benjamin Petersona4a04d12009-12-06 21:24:30 +0000241 code = "def f(x):\n return x + 1"
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000242 st1 = parser.suite(code)
243 st2 = st1.totuple(line_info=1, col_info=1)
244
245 def walk(tree):
246 node_type = tree[0]
247 next = tree[1]
248 if isinstance(next, tuple):
249 for elt in tree[1:]:
250 for x in walk(elt):
251 yield x
252 else:
253 yield tree
Tim Peters147f9ae2006-08-25 22:05:39 +0000254
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000255 terminals = list(walk(st2))
256 self.assertEqual([
257 (1, 'def', 1, 0),
258 (1, 'f', 1, 4),
259 (7, '(', 1, 5),
260 (1, 'x', 1, 6),
261 (8, ')', 1, 7),
262 (11, ':', 1, 8),
263 (4, '', 1, 9),
264 (5, '', 2, -1),
265 (1, 'return', 2, 4),
266 (1, 'x', 2, 11),
267 (14, '+', 2, 13),
268 (2, '1', 2, 15),
269 (4, '', 2, 16),
Benjamin Petersona4a04d12009-12-06 21:24:30 +0000270 (6, '', 2, -1),
271 (4, '', 2, -1),
272 (0, '', 2, -1)],
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000273 terminals)
274
275
Fred Drake79ca79d2000-08-21 22:30:53 +0000276#
277# Second, we take *invalid* trees and make sure we get ParserError
278# rejections for them.
279#
280
Fred Drake58422e52001-06-04 03:56:24 +0000281class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000282
Fred Drake58422e52001-06-04 03:56:24 +0000283 def check_bad_tree(self, tree, label):
284 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000285 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000286 except parser.ParserError:
287 pass
288 else:
289 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000290
Fred Drake58422e52001-06-04 03:56:24 +0000291 def test_junk(self):
292 # not even remotely valid:
293 self.check_bad_tree((1, 2, 3), "<junk>")
294
Fred Drakecf580c72001-07-17 03:01:29 +0000295 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000296 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000297 tree = \
298 (257,
299 (264,
300 (285,
301 (259,
302 (1, 'def'),
303 (1, 'f'),
304 (260, (7, '('), (8, ')')),
305 (11, ':'),
306 (291,
307 (4, ''),
308 (5, ''),
309 (264,
310 (265,
311 (266,
312 (272,
313 (275,
314 (1, 'return'),
315 (313,
316 (292,
317 (293,
318 (294,
319 (295,
320 (297,
321 (298,
322 (299,
323 (300,
324 (301,
325 (302, (303, (304, (305, (2, '1')))))))))))))))))),
326 (264,
327 (265,
328 (266,
329 (272,
330 (276,
331 (1, 'yield'),
332 (313,
333 (292,
334 (293,
335 (294,
336 (295,
337 (297,
338 (298,
339 (299,
340 (300,
341 (301,
342 (302,
343 (303, (304, (305, (2, '1')))))))))))))))))),
344 (4, ''))),
345 (6, ''))))),
346 (4, ''),
347 (0, ''))))
348 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
349
350 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000351 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000352 tree = \
353 (257,
354 (264,
355 (265,
356 (266,
357 (278,
358 (1, 'from'),
359 (281, (1, '__future__')),
360 (1, 'import'),
361 (279, (1, 'generators')))),
362 (4, ''))),
363 (264,
364 (285,
365 (259,
366 (1, 'def'),
367 (1, 'f'),
368 (260, (7, '('), (8, ')')),
369 (11, ':'),
370 (291,
371 (4, ''),
372 (5, ''),
373 (264,
374 (265,
375 (266,
376 (272,
377 (275,
378 (1, 'return'),
379 (313,
380 (292,
381 (293,
382 (294,
383 (295,
384 (297,
385 (298,
386 (299,
387 (300,
388 (301,
389 (302, (303, (304, (305, (2, '1')))))))))))))))))),
390 (264,
391 (265,
392 (266,
393 (272,
394 (276,
395 (1, 'yield'),
396 (313,
397 (292,
398 (293,
399 (294,
400 (295,
401 (297,
402 (298,
403 (299,
404 (300,
405 (301,
406 (302,
407 (303, (304, (305, (2, '1')))))))))))))))))),
408 (4, ''))),
409 (6, ''))))),
410 (4, ''),
411 (0, ''))))
412 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
413
Fred Drake58422e52001-06-04 03:56:24 +0000414 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000415 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000416 tree = \
417 (257,
418 (264,
419 (265,
420 (266,
421 (268,
422 (1, 'print'),
423 (35, '>>'),
424 (290,
425 (291,
426 (292,
427 (293,
428 (295,
429 (296,
430 (297,
431 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
432 (12, ','))),
433 (4, ''))),
434 (0, ''))
435 self.check_bad_tree(tree, "print >>fp,")
436
437 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000438 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000439 tree = \
440 (258,
441 (311,
442 (290,
443 (291,
444 (292,
445 (293,
446 (295,
447 (296,
448 (297,
449 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
450 (12, ','),
451 (12, ','),
452 (290,
453 (291,
454 (292,
455 (293,
456 (295,
457 (296,
458 (297,
459 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
460 (4, ''),
461 (0, ''))
462 self.check_bad_tree(tree, "a,,c")
463
464 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000465 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000466 tree = \
467 (257,
468 (264,
469 (265,
470 (266,
471 (267,
472 (312,
473 (291,
474 (292,
475 (293,
476 (294,
477 (296,
478 (297,
479 (298,
480 (299,
481 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
482 (268, (37, '$=')),
483 (312,
484 (291,
485 (292,
486 (293,
487 (294,
488 (296,
489 (297,
490 (298,
491 (299,
492 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
493 (4, ''))),
494 (0, ''))
495 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000496
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000497 def test_malformed_global(self):
498 #doesn't have global keyword in ast
499 tree = (257,
500 (264,
501 (265,
502 (266,
503 (282, (1, 'foo'))), (4, ''))),
504 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000505 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000506 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000507
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508
509class CompileTestCase(unittest.TestCase):
510
511 # These tests are very minimal. :-(
512
513 def test_compile_expr(self):
514 st = parser.expr('2 + 3')
515 code = parser.compilest(st)
516 self.assertEquals(eval(code), 5)
517
518 def test_compile_suite(self):
519 st = parser.suite('x = 2; y = x + 3')
520 code = parser.compilest(st)
521 globs = {}
522 exec code in globs
523 self.assertEquals(globs['y'], 5)
524
525 def test_compile_error(self):
526 st = parser.suite('1 = 3 + 4')
527 self.assertRaises(SyntaxError, parser.compilest, st)
528
Guido van Rossumb6ac23c2007-07-18 17:19:14 +0000529 def test_compile_badunicode(self):
530 st = parser.suite('a = u"\U12345678"')
531 self.assertRaises(SyntaxError, parser.compilest, st)
532 st = parser.suite('a = u"\u1"')
533 self.assertRaises(SyntaxError, parser.compilest, st)
534
Facundo Batistafc2d0102008-02-23 12:01:13 +0000535class ParserStackLimitTestCase(unittest.TestCase):
536 """try to push the parser to/over it's limits.
537 see http://bugs.python.org/issue1881 for a discussion
538 """
539 def _nested_expression(self, level):
540 return "["*level+"]"*level
541
542 def test_deeply_nested_list(self):
543 e = self._nested_expression(99)
544 st = parser.expr(e)
545 st.compile()
546
547 def test_trigger_memory_error(self):
548 e = self._nested_expression(100)
Martin v. Löwis66e26632008-03-18 13:16:05 +0000549 print >>sys.stderr, "Expecting 's_push: parser stack overflow' in next line"
Facundo Batistafc2d0102008-02-23 12:01:13 +0000550 self.assertRaises(MemoryError, parser.expr, e)
551
Fred Drake2e2be372001-09-20 21:33:42 +0000552def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000553 test_support.run_unittest(
554 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 IllegalSyntaxTestCase,
556 CompileTestCase,
Facundo Batistafc2d0102008-02-23 12:01:13 +0000557 ParserStackLimitTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000558 )
Fred Drake2e2be372001-09-20 21:33:42 +0000559
560
561if __name__ == "__main__":
562 test_main()