blob: a964877e3176403bd1be379bd5640d2fa1da6085 [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")
Mark Dickinsona7ee59b2010-07-04 16:23:54 +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")
190
Tim Peterse8906822005-04-20 17:45:13 +0000191
Fred Drake58422e52001-06-04 03:56:24 +0000192 def test_import_from_statement(self):
193 self.check_suite("from sys.path import *")
194 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000195 self.check_suite("from sys.path import (dirname)")
196 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000197 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000198 self.check_suite("from sys.path import (dirname as my_dirname)")
199 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000200 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000201 self.check_suite("from sys.path import (dirname, basename)")
202 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000203 self.check_suite(
204 "from sys.path import dirname as my_dirname, basename")
205 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000206 "from sys.path import (dirname as my_dirname, basename)")
207 self.check_suite(
208 "from sys.path import (dirname as my_dirname, basename,)")
209 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000210 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000211 self.check_suite(
212 "from sys.path import (dirname, basename as my_basename)")
213 self.check_suite(
214 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Peterson6624a9f2008-11-03 15:14:51 +0000215 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000216
Fred Drake58422e52001-06-04 03:56:24 +0000217 def test_basic_import_statement(self):
218 self.check_suite("import sys")
219 self.check_suite("import sys as system")
220 self.check_suite("import sys, math")
221 self.check_suite("import sys as system, math")
222 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000223
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000224 def test_pep263(self):
225 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
226 "pass\n")
227
228 def test_assert(self):
229 self.check_suite("assert alo < ahi and blo < bhi\n")
230
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +0000231 def test_with(self):
232 self.check_suite("with open('x'): pass\n")
233 self.check_suite("with open('x') as f: pass\n")
Georg Brandl944f6842009-05-25 21:02:56 +0000234 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson9dfe6a82008-11-24 04:09:03 +0000235
Georg Brandlfe879e82008-12-05 12:09:41 +0000236 def test_try_stmt(self):
237 self.check_suite("try: pass\nexcept: pass\n")
238 self.check_suite("try: pass\nfinally: pass\n")
239 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
240 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
241 "finally: pass\n")
242 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
243 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
244 "finally: pass\n")
245
Mark Dickinson070f0ab2010-06-30 16:27:57 +0000246 def test_except_clause(self):
247 self.check_suite("try: pass\nexcept: pass\n")
248 self.check_suite("try: pass\nexcept A: pass\n")
249 self.check_suite("try: pass\nexcept A, e: pass\n")
250 self.check_suite("try: pass\nexcept A as e: pass\n")
251
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000252 def test_position(self):
253 # An absolutely minimal test of position information. Better
254 # tests would be a big project.
Benjamin Petersona4a04d12009-12-06 21:24:30 +0000255 code = "def f(x):\n return x + 1"
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000256 st1 = parser.suite(code)
257 st2 = st1.totuple(line_info=1, col_info=1)
258
259 def walk(tree):
260 node_type = tree[0]
261 next = tree[1]
262 if isinstance(next, tuple):
263 for elt in tree[1:]:
264 for x in walk(elt):
265 yield x
266 else:
267 yield tree
Tim Peters147f9ae2006-08-25 22:05:39 +0000268
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000269 terminals = list(walk(st2))
270 self.assertEqual([
271 (1, 'def', 1, 0),
272 (1, 'f', 1, 4),
273 (7, '(', 1, 5),
274 (1, 'x', 1, 6),
275 (8, ')', 1, 7),
276 (11, ':', 1, 8),
277 (4, '', 1, 9),
278 (5, '', 2, -1),
279 (1, 'return', 2, 4),
280 (1, 'x', 2, 11),
281 (14, '+', 2, 13),
282 (2, '1', 2, 15),
283 (4, '', 2, 16),
Benjamin Petersona4a04d12009-12-06 21:24:30 +0000284 (6, '', 2, -1),
285 (4, '', 2, -1),
286 (0, '', 2, -1)],
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000287 terminals)
288
289
Fred Drake79ca79d2000-08-21 22:30:53 +0000290#
291# Second, we take *invalid* trees and make sure we get ParserError
292# rejections for them.
293#
294
Fred Drake58422e52001-06-04 03:56:24 +0000295class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000296
Fred Drake58422e52001-06-04 03:56:24 +0000297 def check_bad_tree(self, tree, label):
298 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000299 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000300 except parser.ParserError:
301 pass
302 else:
303 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000304
Fred Drake58422e52001-06-04 03:56:24 +0000305 def test_junk(self):
306 # not even remotely valid:
307 self.check_bad_tree((1, 2, 3), "<junk>")
308
Fred Drakecf580c72001-07-17 03:01:29 +0000309 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000310 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000311 tree = \
312 (257,
313 (264,
314 (285,
315 (259,
316 (1, 'def'),
317 (1, 'f'),
318 (260, (7, '('), (8, ')')),
319 (11, ':'),
320 (291,
321 (4, ''),
322 (5, ''),
323 (264,
324 (265,
325 (266,
326 (272,
327 (275,
328 (1, 'return'),
329 (313,
330 (292,
331 (293,
332 (294,
333 (295,
334 (297,
335 (298,
336 (299,
337 (300,
338 (301,
339 (302, (303, (304, (305, (2, '1')))))))))))))))))),
340 (264,
341 (265,
342 (266,
343 (272,
344 (276,
345 (1, 'yield'),
346 (313,
347 (292,
348 (293,
349 (294,
350 (295,
351 (297,
352 (298,
353 (299,
354 (300,
355 (301,
356 (302,
357 (303, (304, (305, (2, '1')))))))))))))))))),
358 (4, ''))),
359 (6, ''))))),
360 (4, ''),
361 (0, ''))))
362 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
363
364 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000365 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000366 tree = \
367 (257,
368 (264,
369 (265,
370 (266,
371 (278,
372 (1, 'from'),
373 (281, (1, '__future__')),
374 (1, 'import'),
375 (279, (1, 'generators')))),
376 (4, ''))),
377 (264,
378 (285,
379 (259,
380 (1, 'def'),
381 (1, 'f'),
382 (260, (7, '('), (8, ')')),
383 (11, ':'),
384 (291,
385 (4, ''),
386 (5, ''),
387 (264,
388 (265,
389 (266,
390 (272,
391 (275,
392 (1, 'return'),
393 (313,
394 (292,
395 (293,
396 (294,
397 (295,
398 (297,
399 (298,
400 (299,
401 (300,
402 (301,
403 (302, (303, (304, (305, (2, '1')))))))))))))))))),
404 (264,
405 (265,
406 (266,
407 (272,
408 (276,
409 (1, 'yield'),
410 (313,
411 (292,
412 (293,
413 (294,
414 (295,
415 (297,
416 (298,
417 (299,
418 (300,
419 (301,
420 (302,
421 (303, (304, (305, (2, '1')))))))))))))))))),
422 (4, ''))),
423 (6, ''))))),
424 (4, ''),
425 (0, ''))))
426 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
427
Fred Drake58422e52001-06-04 03:56:24 +0000428 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000429 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000430 tree = \
431 (257,
432 (264,
433 (265,
434 (266,
435 (268,
436 (1, 'print'),
437 (35, '>>'),
438 (290,
439 (291,
440 (292,
441 (293,
442 (295,
443 (296,
444 (297,
445 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
446 (12, ','))),
447 (4, ''))),
448 (0, ''))
449 self.check_bad_tree(tree, "print >>fp,")
450
451 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000452 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000453 tree = \
454 (258,
455 (311,
456 (290,
457 (291,
458 (292,
459 (293,
460 (295,
461 (296,
462 (297,
463 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
464 (12, ','),
465 (12, ','),
466 (290,
467 (291,
468 (292,
469 (293,
470 (295,
471 (296,
472 (297,
473 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
474 (4, ''),
475 (0, ''))
476 self.check_bad_tree(tree, "a,,c")
477
478 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000479 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000480 tree = \
481 (257,
482 (264,
483 (265,
484 (266,
485 (267,
486 (312,
487 (291,
488 (292,
489 (293,
490 (294,
491 (296,
492 (297,
493 (298,
494 (299,
495 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
496 (268, (37, '$=')),
497 (312,
498 (291,
499 (292,
500 (293,
501 (294,
502 (296,
503 (297,
504 (298,
505 (299,
506 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
507 (4, ''))),
508 (0, ''))
509 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000510
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000511 def test_malformed_global(self):
512 #doesn't have global keyword in ast
513 tree = (257,
514 (264,
515 (265,
516 (266,
517 (282, (1, 'foo'))), (4, ''))),
518 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000519 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000520 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000521
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522
523class CompileTestCase(unittest.TestCase):
524
525 # These tests are very minimal. :-(
526
527 def test_compile_expr(self):
528 st = parser.expr('2 + 3')
529 code = parser.compilest(st)
530 self.assertEquals(eval(code), 5)
531
532 def test_compile_suite(self):
533 st = parser.suite('x = 2; y = x + 3')
534 code = parser.compilest(st)
535 globs = {}
536 exec code in globs
537 self.assertEquals(globs['y'], 5)
538
539 def test_compile_error(self):
540 st = parser.suite('1 = 3 + 4')
541 self.assertRaises(SyntaxError, parser.compilest, st)
542
Guido van Rossumb6ac23c2007-07-18 17:19:14 +0000543 def test_compile_badunicode(self):
544 st = parser.suite('a = u"\U12345678"')
545 self.assertRaises(SyntaxError, parser.compilest, st)
546 st = parser.suite('a = u"\u1"')
547 self.assertRaises(SyntaxError, parser.compilest, st)
548
Facundo Batistafc2d0102008-02-23 12:01:13 +0000549class ParserStackLimitTestCase(unittest.TestCase):
550 """try to push the parser to/over it's limits.
551 see http://bugs.python.org/issue1881 for a discussion
552 """
553 def _nested_expression(self, level):
554 return "["*level+"]"*level
555
556 def test_deeply_nested_list(self):
557 e = self._nested_expression(99)
558 st = parser.expr(e)
559 st.compile()
560
561 def test_trigger_memory_error(self):
562 e = self._nested_expression(100)
Martin v. Löwis66e26632008-03-18 13:16:05 +0000563 print >>sys.stderr, "Expecting 's_push: parser stack overflow' in next line"
Facundo Batistafc2d0102008-02-23 12:01:13 +0000564 self.assertRaises(MemoryError, parser.expr, e)
565
Fred Drake2e2be372001-09-20 21:33:42 +0000566def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000567 test_support.run_unittest(
568 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569 IllegalSyntaxTestCase,
570 CompileTestCase,
Facundo Batistafc2d0102008-02-23 12:01:13 +0000571 ParserStackLimitTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000572 )
Fred Drake2e2be372001-09-20 21:33:42 +0000573
574
575if __name__ == "__main__":
576 test_main()