blob: 94b6113a0c3b03df513ca43cabebfd95f56c85b8 [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Benjamin Petersoneeed0c72008-11-03 15:18:30 +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 Peterson084ce7a2008-10-31 02:26:20 +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)")
62 self.check_expr("[1, 2, 3]")
63 self.check_expr("[x**3 for x in range(20)]")
64 self.check_expr("[x**3 for x in range(20) if x % 3]")
Neal Norwitzd3a91622006-04-12 05:27:46 +000065 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
66 self.check_expr("list(x**3 for x in range(20))")
67 self.check_expr("list(x**3 for x in range(20) if x % 3)")
68 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Fred Drake58422e52001-06-04 03:56:24 +000069 self.check_expr("foo(*args)")
70 self.check_expr("foo(*args, **kw)")
71 self.check_expr("foo(**kw)")
72 self.check_expr("foo(key=value)")
73 self.check_expr("foo(key=value, *args)")
74 self.check_expr("foo(key=value, *args, **kw)")
75 self.check_expr("foo(key=value, **kw)")
76 self.check_expr("foo(a, b, c, *args)")
77 self.check_expr("foo(a, b, c, *args, **kw)")
78 self.check_expr("foo(a, b, c, **kw)")
Benjamin Petersonbd6a05f2008-08-19 22:06:11 +000079 self.check_expr("foo(a, *args, keyword=23)")
Fred Drake58422e52001-06-04 03:56:24 +000080 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +000081 self.check_expr("foo - bar")
82 self.check_expr("foo * bar")
83 self.check_expr("foo / bar")
84 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +000085 self.check_expr("lambda: 0")
86 self.check_expr("lambda x: 0")
87 self.check_expr("lambda *y: 0")
88 self.check_expr("lambda *y, **z: 0")
89 self.check_expr("lambda **z: 0")
90 self.check_expr("lambda x, y: 0")
91 self.check_expr("lambda foo=bar: 0")
92 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
93 self.check_expr("lambda foo=bar, **z: 0")
94 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
95 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
96 self.check_expr("lambda x, *y, **z: 0")
Raymond Hettinger354433a2004-05-19 08:20:33 +000097 self.check_expr("(x for x in range(10))")
98 self.check_expr("foo(x for x in range(10))")
Fred Drake79ca79d2000-08-21 22:30:53 +000099
Fred Drake58422e52001-06-04 03:56:24 +0000100 def test_print(self):
101 self.check_suite("print")
102 self.check_suite("print 1")
103 self.check_suite("print 1,")
104 self.check_suite("print >>fp")
105 self.check_suite("print >>fp, 1")
106 self.check_suite("print >>fp, 1,")
Fred Drake79ca79d2000-08-21 22:30:53 +0000107
Fred Drake58422e52001-06-04 03:56:24 +0000108 def test_simple_expression(self):
109 # expr_stmt
110 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +0000111
Fred Drake58422e52001-06-04 03:56:24 +0000112 def test_simple_assignments(self):
113 self.check_suite("a = b")
114 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +0000115
Fred Drake58422e52001-06-04 03:56:24 +0000116 def test_simple_augmented_assignments(self):
117 self.check_suite("a += b")
118 self.check_suite("a -= b")
119 self.check_suite("a *= b")
120 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000121 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000122 self.check_suite("a %= b")
123 self.check_suite("a &= b")
124 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")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000129
Fred Drake58422e52001-06-04 03:56:24 +0000130 def test_function_defs(self):
131 self.check_suite("def f(): pass")
132 self.check_suite("def f(*args): pass")
133 self.check_suite("def f(*args, **kw): pass")
134 self.check_suite("def f(**kw): pass")
135 self.check_suite("def f(foo=bar): pass")
136 self.check_suite("def f(foo=bar, *args): pass")
137 self.check_suite("def f(foo=bar, *args, **kw): pass")
138 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000139
Fred Drake58422e52001-06-04 03:56:24 +0000140 self.check_suite("def f(a, b): pass")
141 self.check_suite("def f(a, b, *args): pass")
142 self.check_suite("def f(a, b, *args, **kw): pass")
143 self.check_suite("def f(a, b, **kw): pass")
144 self.check_suite("def f(a, b, foo=bar): pass")
145 self.check_suite("def f(a, b, foo=bar, *args): pass")
146 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
147 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000148
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000149 self.check_suite("@staticmethod\n"
150 "def f(): pass")
151 self.check_suite("@staticmethod\n"
152 "@funcattrs(x, y)\n"
153 "def f(): pass")
154 self.check_suite("@funcattrs()\n"
155 "def f(): pass")
156
Brett Cannonf4189912005-04-09 02:30:16 +0000157 def test_class_defs(self):
158 self.check_suite("class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000159
Fred Drake58422e52001-06-04 03:56:24 +0000160 def test_import_from_statement(self):
161 self.check_suite("from sys.path import *")
162 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000163 self.check_suite("from sys.path import (dirname)")
164 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000165 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000166 self.check_suite("from sys.path import (dirname as my_dirname)")
167 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000168 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000169 self.check_suite("from sys.path import (dirname, basename)")
170 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000171 self.check_suite(
172 "from sys.path import dirname as my_dirname, basename")
173 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000174 "from sys.path import (dirname as my_dirname, basename)")
175 self.check_suite(
176 "from sys.path import (dirname as my_dirname, basename,)")
177 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000178 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000179 self.check_suite(
180 "from sys.path import (dirname, basename as my_basename)")
181 self.check_suite(
182 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Petersoneeed0c72008-11-03 15:18:30 +0000183 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000184
Fred Drake58422e52001-06-04 03:56:24 +0000185 def test_basic_import_statement(self):
186 self.check_suite("import sys")
187 self.check_suite("import sys as system")
188 self.check_suite("import sys, math")
189 self.check_suite("import sys as system, math")
190 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000191
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000192 def test_pep263(self):
193 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
194 "pass\n")
195
196 def test_assert(self):
197 self.check_suite("assert alo < ahi and blo < bhi\n")
198
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000199 def test_position(self):
200 # An absolutely minimal test of position information. Better
201 # tests would be a big project.
202 code = "def f(x):\n return x + 1\n"
203 st1 = parser.suite(code)
204 st2 = st1.totuple(line_info=1, col_info=1)
205
206 def walk(tree):
207 node_type = tree[0]
208 next = tree[1]
209 if isinstance(next, tuple):
210 for elt in tree[1:]:
211 for x in walk(elt):
212 yield x
213 else:
214 yield tree
Tim Peters147f9ae2006-08-25 22:05:39 +0000215
Jeremy Hylton60e96f62006-08-22 20:46:00 +0000216 terminals = list(walk(st2))
217 self.assertEqual([
218 (1, 'def', 1, 0),
219 (1, 'f', 1, 4),
220 (7, '(', 1, 5),
221 (1, 'x', 1, 6),
222 (8, ')', 1, 7),
223 (11, ':', 1, 8),
224 (4, '', 1, 9),
225 (5, '', 2, -1),
226 (1, 'return', 2, 4),
227 (1, 'x', 2, 11),
228 (14, '+', 2, 13),
229 (2, '1', 2, 15),
230 (4, '', 2, 16),
231 (6, '', 2, -1),
232 (4, '', 2, -1),
233 (0, '', 2, -1)],
234 terminals)
235
236
Fred Drake79ca79d2000-08-21 22:30:53 +0000237#
238# Second, we take *invalid* trees and make sure we get ParserError
239# rejections for them.
240#
241
Fred Drake58422e52001-06-04 03:56:24 +0000242class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000243
Fred Drake58422e52001-06-04 03:56:24 +0000244 def check_bad_tree(self, tree, label):
245 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000246 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000247 except parser.ParserError:
248 pass
249 else:
250 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000251
Fred Drake58422e52001-06-04 03:56:24 +0000252 def test_junk(self):
253 # not even remotely valid:
254 self.check_bad_tree((1, 2, 3), "<junk>")
255
Fred Drakecf580c72001-07-17 03:01:29 +0000256 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000257 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000258 tree = \
259 (257,
260 (264,
261 (285,
262 (259,
263 (1, 'def'),
264 (1, 'f'),
265 (260, (7, '('), (8, ')')),
266 (11, ':'),
267 (291,
268 (4, ''),
269 (5, ''),
270 (264,
271 (265,
272 (266,
273 (272,
274 (275,
275 (1, 'return'),
276 (313,
277 (292,
278 (293,
279 (294,
280 (295,
281 (297,
282 (298,
283 (299,
284 (300,
285 (301,
286 (302, (303, (304, (305, (2, '1')))))))))))))))))),
287 (264,
288 (265,
289 (266,
290 (272,
291 (276,
292 (1, 'yield'),
293 (313,
294 (292,
295 (293,
296 (294,
297 (295,
298 (297,
299 (298,
300 (299,
301 (300,
302 (301,
303 (302,
304 (303, (304, (305, (2, '1')))))))))))))))))),
305 (4, ''))),
306 (6, ''))))),
307 (4, ''),
308 (0, ''))))
309 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
310
311 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000312 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000313 tree = \
314 (257,
315 (264,
316 (265,
317 (266,
318 (278,
319 (1, 'from'),
320 (281, (1, '__future__')),
321 (1, 'import'),
322 (279, (1, 'generators')))),
323 (4, ''))),
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
Fred Drake58422e52001-06-04 03:56:24 +0000375 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000376 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000377 tree = \
378 (257,
379 (264,
380 (265,
381 (266,
382 (268,
383 (1, 'print'),
384 (35, '>>'),
385 (290,
386 (291,
387 (292,
388 (293,
389 (295,
390 (296,
391 (297,
392 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
393 (12, ','))),
394 (4, ''))),
395 (0, ''))
396 self.check_bad_tree(tree, "print >>fp,")
397
398 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000399 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000400 tree = \
401 (258,
402 (311,
403 (290,
404 (291,
405 (292,
406 (293,
407 (295,
408 (296,
409 (297,
410 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
411 (12, ','),
412 (12, ','),
413 (290,
414 (291,
415 (292,
416 (293,
417 (295,
418 (296,
419 (297,
420 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
421 (4, ''),
422 (0, ''))
423 self.check_bad_tree(tree, "a,,c")
424
425 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000426 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000427 tree = \
428 (257,
429 (264,
430 (265,
431 (266,
432 (267,
433 (312,
434 (291,
435 (292,
436 (293,
437 (294,
438 (296,
439 (297,
440 (298,
441 (299,
442 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
443 (268, (37, '$=')),
444 (312,
445 (291,
446 (292,
447 (293,
448 (294,
449 (296,
450 (297,
451 (298,
452 (299,
453 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
454 (4, ''))),
455 (0, ''))
456 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000457
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000458 def test_malformed_global(self):
459 #doesn't have global keyword in ast
460 tree = (257,
461 (264,
462 (265,
463 (266,
464 (282, (1, 'foo'))), (4, ''))),
465 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000466 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000467 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000468
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469
470class CompileTestCase(unittest.TestCase):
471
472 # These tests are very minimal. :-(
473
474 def test_compile_expr(self):
475 st = parser.expr('2 + 3')
476 code = parser.compilest(st)
477 self.assertEquals(eval(code), 5)
478
479 def test_compile_suite(self):
480 st = parser.suite('x = 2; y = x + 3')
481 code = parser.compilest(st)
482 globs = {}
483 exec code in globs
484 self.assertEquals(globs['y'], 5)
485
486 def test_compile_error(self):
487 st = parser.suite('1 = 3 + 4')
488 self.assertRaises(SyntaxError, parser.compilest, st)
489
Guido van Rossumb6ac23c2007-07-18 17:19:14 +0000490 def test_compile_badunicode(self):
491 st = parser.suite('a = u"\U12345678"')
492 self.assertRaises(SyntaxError, parser.compilest, st)
493 st = parser.suite('a = u"\u1"')
494 self.assertRaises(SyntaxError, parser.compilest, st)
495
Facundo Batistafc2d0102008-02-23 12:01:13 +0000496class ParserStackLimitTestCase(unittest.TestCase):
497 """try to push the parser to/over it's limits.
498 see http://bugs.python.org/issue1881 for a discussion
499 """
500 def _nested_expression(self, level):
501 return "["*level+"]"*level
502
503 def test_deeply_nested_list(self):
504 e = self._nested_expression(99)
505 st = parser.expr(e)
506 st.compile()
507
508 def test_trigger_memory_error(self):
509 e = self._nested_expression(100)
Martin v. Löwis66e26632008-03-18 13:16:05 +0000510 print >>sys.stderr, "Expecting 's_push: parser stack overflow' in next line"
Facundo Batistafc2d0102008-02-23 12:01:13 +0000511 self.assertRaises(MemoryError, parser.expr, e)
512
Fred Drake2e2be372001-09-20 21:33:42 +0000513def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000514 test_support.run_unittest(
515 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516 IllegalSyntaxTestCase,
517 CompileTestCase,
Facundo Batistafc2d0102008-02-23 12:01:13 +0000518 ParserStackLimitTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000519 )
Fred Drake2e2be372001-09-20 21:33:42 +0000520
521
522if __name__ == "__main__":
523 test_main()