blob: 6ae99758d3c7c5a572e152f12ea23869a35f4b39 [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Benjamin Peterson6624a9f2008-11-03 15:14:51 +00002import os
Fred Drake58422e52001-06-04 03:56:24 +00003import unittest
Martin v. Löwis66e26632008-03-18 13:16:05 +00004import sys
Barry Warsaw04f357c2002-07-23 19:04:11 +00005from test import test_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)
Anthony Baxterc2a5a632004-08-02 06:10:11 +000020 except parser.ParserError, why:
21 self.fail("could not roundtrip %r: %s" % (s, why))
Fred Drake79ca79d2000-08-21 22:30:53 +000022
Fred Drake58422e52001-06-04 03:56:24 +000023 self.assertEquals(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 Petersondcee09d2008-10-31 02:16: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 in scope
36 self.assertTrue(isinstance(scope["x"], unicode))
37
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
Fred Drake58422e52001-06-04 03:56:24 +000060 def test_expressions(self):
61 self.check_expr("foo(1)")
Alexandre Vassalottiee936a22010-01-09 23:35:54 +000062 self.check_expr("{1:1}")
63 self.check_expr("{1:1, 2:2, 3:3}")
64 self.check_expr("{1:1, 2:2, 3:3,}")
65 self.check_expr("{1}")
66 self.check_expr("{1, 2, 3}")
67 self.check_expr("{1, 2, 3,}")
68 self.check_expr("[]")
69 self.check_expr("[1]")
Fred Drake58422e52001-06-04 03:56:24 +000070 self.check_expr("[1, 2, 3]")
Alexandre Vassalottiee936a22010-01-09 23:35:54 +000071 self.check_expr("[1, 2, 3,]")
72 self.check_expr("()")
73 self.check_expr("(1,)")
74 self.check_expr("(1, 2, 3)")
75 self.check_expr("(1, 2, 3,)")
Fred Drake58422e52001-06-04 03:56:24 +000076 self.check_expr("[x**3 for x in range(20)]")
77 self.check_expr("[x**3 for x in range(20) if x % 3]")
Neal Norwitzd3a91622006-04-12 05:27:46 +000078 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
Alexandre Vassalottib6465472010-01-11 22:36:12 +000079 self.check_expr("[x+y for x in range(30) for y in range(20) if x % 2 if y % 3]")
80 #self.check_expr("[x for x in lambda: True, lambda: False if x()]")
Neal Norwitzd3a91622006-04-12 05:27:46 +000081 self.check_expr("list(x**3 for x in range(20))")
82 self.check_expr("list(x**3 for x in range(20) if x % 3)")
83 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Alexandre Vassalottib6465472010-01-11 22:36:12 +000084 self.check_expr("list(x+y for x in range(30) for y in range(20) if x % 2 if y % 3)")
85 self.check_expr("{x**3 for x in range(30)}")
86 self.check_expr("{x**3 for x in range(30) if x % 3}")
87 self.check_expr("{x**3 for x in range(30) if x % 2 if x % 3}")
88 self.check_expr("{x+y for x in range(30) for y in range(20) if x % 2 if y % 3}")
89 self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30))}")
90 self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30)) if x % 3}")
91 self.check_expr("{x**3: y**2 for x, y in zip(range(30), range(30)) if x % 3 if y % 3}")
92 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 +000093 self.check_expr("foo(*args)")
94 self.check_expr("foo(*args, **kw)")
95 self.check_expr("foo(**kw)")
96 self.check_expr("foo(key=value)")
97 self.check_expr("foo(key=value, *args)")
98 self.check_expr("foo(key=value, *args, **kw)")
99 self.check_expr("foo(key=value, **kw)")
100 self.check_expr("foo(a, b, c, *args)")
101 self.check_expr("foo(a, b, c, *args, **kw)")
102 self.check_expr("foo(a, b, c, **kw)")
Benjamin Petersonbd6a05f2008-08-19 22:06:11 +0000103 self.check_expr("foo(a, *args, keyword=23)")
Fred Drake58422e52001-06-04 03:56:24 +0000104 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000105 self.check_expr("foo - bar")
106 self.check_expr("foo * bar")
107 self.check_expr("foo / bar")
108 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +0000109 self.check_expr("lambda: 0")
110 self.check_expr("lambda x: 0")
111 self.check_expr("lambda *y: 0")
112 self.check_expr("lambda *y, **z: 0")
113 self.check_expr("lambda **z: 0")
114 self.check_expr("lambda x, y: 0")
115 self.check_expr("lambda foo=bar: 0")
116 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
117 self.check_expr("lambda foo=bar, **z: 0")
118 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
119 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
120 self.check_expr("lambda x, *y, **z: 0")
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000121 self.check_expr("lambda x: 5 if x else 2")
Raymond Hettinger354433a2004-05-19 08:20:33 +0000122 self.check_expr("(x for x in range(10))")
123 self.check_expr("foo(x for x in range(10))")
Fred Drake79ca79d2000-08-21 22:30:53 +0000124
Fred Drake58422e52001-06-04 03:56:24 +0000125 def test_print(self):
126 self.check_suite("print")
127 self.check_suite("print 1")
128 self.check_suite("print 1,")
129 self.check_suite("print >>fp")
130 self.check_suite("print >>fp, 1")
131 self.check_suite("print >>fp, 1,")
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
Brett Cannonf4189912005-04-09 02:30:16 +0000182 def test_class_defs(self):
183 self.check_suite("class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000184
Fred Drake58422e52001-06-04 03:56:24 +0000185 def test_import_from_statement(self):
186 self.check_suite("from sys.path import *")
187 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000188 self.check_suite("from sys.path import (dirname)")
189 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000190 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000191 self.check_suite("from sys.path import (dirname as my_dirname)")
192 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000193 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000194 self.check_suite("from sys.path import (dirname, basename)")
195 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000196 self.check_suite(
197 "from sys.path import dirname as my_dirname, basename")
198 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000199 "from sys.path import (dirname as my_dirname, basename)")
200 self.check_suite(
201 "from sys.path import (dirname as my_dirname, basename,)")
202 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000203 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000204 self.check_suite(
205 "from sys.path import (dirname, basename as my_basename)")
206 self.check_suite(
207 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Peterson6624a9f2008-11-03 15:14:51 +0000208 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000209
Fred Drake58422e52001-06-04 03:56:24 +0000210 def test_basic_import_statement(self):
211 self.check_suite("import sys")
212 self.check_suite("import sys as system")
213 self.check_suite("import sys, math")
214 self.check_suite("import sys as system, math")
215 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000216
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000217 def test_pep263(self):
218 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
219 "pass\n")
220
221 def test_assert(self):
222 self.check_suite("assert alo < ahi and blo < bhi\n")
223
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +0000224 def test_with(self):
225 self.check_suite("with open('x'): pass\n")
226 self.check_suite("with open('x') as f: pass\n")
Georg Brandl944f6842009-05-25 21:02:56 +0000227 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +0000228
Georg Brandlfe879e82008-12-05 12:09:41 +0000229 def test_try_stmt(self):
230 self.check_suite("try: pass\nexcept: pass\n")
231 self.check_suite("try: pass\nfinally: pass\n")
232 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
233 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
234 "finally: pass\n")
235 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
236 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
237 "finally: pass\n")
238
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000239 def test_position(self):
240 # An absolutely minimal test of position information. Better
241 # tests would be a big project.
Benjamin Petersona4a04d12009-12-06 21:24:30 +0000242 code = "def f(x):\n return x + 1"
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000243 st1 = parser.suite(code)
244 st2 = st1.totuple(line_info=1, col_info=1)
245
246 def walk(tree):
247 node_type = tree[0]
248 next = tree[1]
249 if isinstance(next, tuple):
250 for elt in tree[1:]:
251 for x in walk(elt):
252 yield x
253 else:
254 yield tree
Tim Peters147f9ae2006-08-25 22:05:39 +0000255
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000256 terminals = list(walk(st2))
257 self.assertEqual([
258 (1, 'def', 1, 0),
259 (1, 'f', 1, 4),
260 (7, '(', 1, 5),
261 (1, 'x', 1, 6),
262 (8, ')', 1, 7),
263 (11, ':', 1, 8),
264 (4, '', 1, 9),
265 (5, '', 2, -1),
266 (1, 'return', 2, 4),
267 (1, 'x', 2, 11),
268 (14, '+', 2, 13),
269 (2, '1', 2, 15),
270 (4, '', 2, 16),
Benjamin Petersona4a04d12009-12-06 21:24:30 +0000271 (6, '', 2, -1),
272 (4, '', 2, -1),
273 (0, '', 2, -1)],
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000274 terminals)
275
276
Fred Drake79ca79d2000-08-21 22:30:53 +0000277#
278# Second, we take *invalid* trees and make sure we get ParserError
279# rejections for them.
280#
281
Fred Drake58422e52001-06-04 03:56:24 +0000282class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000283
Fred Drake58422e52001-06-04 03:56:24 +0000284 def check_bad_tree(self, tree, label):
285 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000286 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000287 except parser.ParserError:
288 pass
289 else:
290 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000291
Fred Drake58422e52001-06-04 03:56:24 +0000292 def test_junk(self):
293 # not even remotely valid:
294 self.check_bad_tree((1, 2, 3), "<junk>")
295
Fred Drakecf580c72001-07-17 03:01:29 +0000296 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000297 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000298 tree = \
299 (257,
300 (264,
301 (285,
302 (259,
303 (1, 'def'),
304 (1, 'f'),
305 (260, (7, '('), (8, ')')),
306 (11, ':'),
307 (291,
308 (4, ''),
309 (5, ''),
310 (264,
311 (265,
312 (266,
313 (272,
314 (275,
315 (1, 'return'),
316 (313,
317 (292,
318 (293,
319 (294,
320 (295,
321 (297,
322 (298,
323 (299,
324 (300,
325 (301,
326 (302, (303, (304, (305, (2, '1')))))))))))))))))),
327 (264,
328 (265,
329 (266,
330 (272,
331 (276,
332 (1, 'yield'),
333 (313,
334 (292,
335 (293,
336 (294,
337 (295,
338 (297,
339 (298,
340 (299,
341 (300,
342 (301,
343 (302,
344 (303, (304, (305, (2, '1')))))))))))))))))),
345 (4, ''))),
346 (6, ''))))),
347 (4, ''),
348 (0, ''))))
349 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
350
351 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000352 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000353 tree = \
354 (257,
355 (264,
356 (265,
357 (266,
358 (278,
359 (1, 'from'),
360 (281, (1, '__future__')),
361 (1, 'import'),
362 (279, (1, 'generators')))),
363 (4, ''))),
364 (264,
365 (285,
366 (259,
367 (1, 'def'),
368 (1, 'f'),
369 (260, (7, '('), (8, ')')),
370 (11, ':'),
371 (291,
372 (4, ''),
373 (5, ''),
374 (264,
375 (265,
376 (266,
377 (272,
378 (275,
379 (1, 'return'),
380 (313,
381 (292,
382 (293,
383 (294,
384 (295,
385 (297,
386 (298,
387 (299,
388 (300,
389 (301,
390 (302, (303, (304, (305, (2, '1')))))))))))))))))),
391 (264,
392 (265,
393 (266,
394 (272,
395 (276,
396 (1, 'yield'),
397 (313,
398 (292,
399 (293,
400 (294,
401 (295,
402 (297,
403 (298,
404 (299,
405 (300,
406 (301,
407 (302,
408 (303, (304, (305, (2, '1')))))))))))))))))),
409 (4, ''))),
410 (6, ''))))),
411 (4, ''),
412 (0, ''))))
413 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
414
Fred Drake58422e52001-06-04 03:56:24 +0000415 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000416 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000417 tree = \
418 (257,
419 (264,
420 (265,
421 (266,
422 (268,
423 (1, 'print'),
424 (35, '>>'),
425 (290,
426 (291,
427 (292,
428 (293,
429 (295,
430 (296,
431 (297,
432 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
433 (12, ','))),
434 (4, ''))),
435 (0, ''))
436 self.check_bad_tree(tree, "print >>fp,")
437
438 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000439 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000440 tree = \
441 (258,
442 (311,
443 (290,
444 (291,
445 (292,
446 (293,
447 (295,
448 (296,
449 (297,
450 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
451 (12, ','),
452 (12, ','),
453 (290,
454 (291,
455 (292,
456 (293,
457 (295,
458 (296,
459 (297,
460 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
461 (4, ''),
462 (0, ''))
463 self.check_bad_tree(tree, "a,,c")
464
465 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000466 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000467 tree = \
468 (257,
469 (264,
470 (265,
471 (266,
472 (267,
473 (312,
474 (291,
475 (292,
476 (293,
477 (294,
478 (296,
479 (297,
480 (298,
481 (299,
482 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
483 (268, (37, '$=')),
484 (312,
485 (291,
486 (292,
487 (293,
488 (294,
489 (296,
490 (297,
491 (298,
492 (299,
493 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
494 (4, ''))),
495 (0, ''))
496 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000497
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000498 def test_malformed_global(self):
499 #doesn't have global keyword in ast
500 tree = (257,
501 (264,
502 (265,
503 (266,
504 (282, (1, 'foo'))), (4, ''))),
505 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000506 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000507 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000508
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509
510class CompileTestCase(unittest.TestCase):
511
512 # These tests are very minimal. :-(
513
514 def test_compile_expr(self):
515 st = parser.expr('2 + 3')
516 code = parser.compilest(st)
517 self.assertEquals(eval(code), 5)
518
519 def test_compile_suite(self):
520 st = parser.suite('x = 2; y = x + 3')
521 code = parser.compilest(st)
522 globs = {}
523 exec code in globs
524 self.assertEquals(globs['y'], 5)
525
526 def test_compile_error(self):
527 st = parser.suite('1 = 3 + 4')
528 self.assertRaises(SyntaxError, parser.compilest, st)
529
Guido van Rossumb6ac23c2007-07-18 17:19:14 +0000530 def test_compile_badunicode(self):
531 st = parser.suite('a = u"\U12345678"')
532 self.assertRaises(SyntaxError, parser.compilest, st)
533 st = parser.suite('a = u"\u1"')
534 self.assertRaises(SyntaxError, parser.compilest, st)
535
Facundo Batistafc2d0102008-02-23 12:01:13 +0000536class ParserStackLimitTestCase(unittest.TestCase):
537 """try to push the parser to/over it's limits.
538 see http://bugs.python.org/issue1881 for a discussion
539 """
540 def _nested_expression(self, level):
541 return "["*level+"]"*level
542
543 def test_deeply_nested_list(self):
544 e = self._nested_expression(99)
545 st = parser.expr(e)
546 st.compile()
547
548 def test_trigger_memory_error(self):
549 e = self._nested_expression(100)
Martin v. Löwis66e26632008-03-18 13:16:05 +0000550 print >>sys.stderr, "Expecting 's_push: parser stack overflow' in next line"
Facundo Batistafc2d0102008-02-23 12:01:13 +0000551 self.assertRaises(MemoryError, parser.expr, e)
552
Fred Drake2e2be372001-09-20 21:33:42 +0000553def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000554 test_support.run_unittest(
555 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 IllegalSyntaxTestCase,
557 CompileTestCase,
Facundo Batistafc2d0102008-02-23 12:01:13 +0000558 ParserStackLimitTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000559 )
Fred Drake2e2be372001-09-20 21:33:42 +0000560
561
562if __name__ == "__main__":
563 test_main()