4 from test
import test_support
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.
12 class RoundtripLegalSyntaxTestCase(unittest
.TestCase
):
14 def roundtrip(self
, f
, s
):
18 st2
= parser
.sequence2st(t
)
19 except parser
.ParserError
, why
:
20 self
.fail("could not roundtrip %r: %s" % (s
, why
))
22 self
.assertEquals(t
, st2
.totuple(),
23 "could not re-generate syntax tree")
25 def check_expr(self
, s
):
26 self
.roundtrip(parser
.expr
, s
)
28 def test_flags_passed(self
):
29 # The unicode literals flags has to be passed from the paser to AST
31 suite
= parser
.suite("from __future__ import unicode_literals; x = ''")
32 code
= suite
.compile()
35 self
.assertIsInstance(scope
["x"], unicode)
37 def check_suite(self
, s
):
38 self
.roundtrip(parser
.suite
, s
)
40 def test_yield_statement(self
):
41 self
.check_suite("def f(): yield 1")
42 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")
50 self
.check_suite("def f(): return; yield 1")
51 self
.check_suite("def f(): yield 1; return")
52 self
.check_suite("def f():\n"
53 " for x in range(30):\n"
55 self
.check_suite("def f():\n"
59 def test_expressions(self
):
60 self
.check_expr("foo(1)")
61 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,}")
68 self
.check_expr("[1]")
69 self
.check_expr("[1, 2, 3]")
70 self
.check_expr("[1, 2, 3,]")
72 self
.check_expr("(1,)")
73 self
.check_expr("(1, 2, 3)")
74 self
.check_expr("(1, 2, 3,)")
75 self
.check_expr("[x**3 for x in range(20)]")
76 self
.check_expr("[x**3 for x in range(20) if x % 3]")
77 self
.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
78 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()]")
80 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)")
83 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}")
92 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)")
102 self
.check_expr("foo(a, *args, keyword=23)")
103 self
.check_expr("foo + bar")
104 self
.check_expr("foo - bar")
105 self
.check_expr("foo * bar")
106 self
.check_expr("foo / bar")
107 self
.check_expr("foo // bar")
108 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")
120 self
.check_expr("lambda x: 5 if x else 2")
121 self
.check_expr("(x for x in range(10))")
122 self
.check_expr("foo(x for x in range(10))")
124 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,")
132 def test_simple_expression(self
):
134 self
.check_suite("a")
136 def test_simple_assignments(self
):
137 self
.check_suite("a = b")
138 self
.check_suite("a = b = c = d = e")
140 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")
145 self
.check_suite("a //= b")
146 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")
154 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")
164 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")
173 self
.check_suite("@staticmethod\n"
175 self
.check_suite("@staticmethod\n"
178 self
.check_suite("@funcattrs()\n"
181 def test_class_defs(self
):
182 self
.check_suite("class foo():pass")
184 def test_import_from_statement(self
):
185 self
.check_suite("from sys.path import *")
186 self
.check_suite("from sys.path import dirname")
187 self
.check_suite("from sys.path import (dirname)")
188 self
.check_suite("from sys.path import (dirname,)")
189 self
.check_suite("from sys.path import dirname as my_dirname")
190 self
.check_suite("from sys.path import (dirname as my_dirname)")
191 self
.check_suite("from sys.path import (dirname as my_dirname,)")
192 self
.check_suite("from sys.path import dirname, basename")
193 self
.check_suite("from sys.path import (dirname, basename)")
194 self
.check_suite("from sys.path import (dirname, basename,)")
196 "from sys.path import dirname as my_dirname, basename")
198 "from sys.path import (dirname as my_dirname, basename)")
200 "from sys.path import (dirname as my_dirname, basename,)")
202 "from sys.path import dirname, basename as my_basename")
204 "from sys.path import (dirname, basename as my_basename)")
206 "from sys.path import (dirname, basename as my_basename,)")
207 self
.check_suite("from .bogus import x")
209 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")
216 def test_pep263(self
):
217 self
.check_suite("# -*- coding: iso-8859-1 -*-\n"
220 def test_assert(self
):
221 self
.check_suite("assert alo < ahi and blo < bhi\n")
224 self
.check_suite("with open('x'): pass\n")
225 self
.check_suite("with open('x') as f: pass\n")
226 self
.check_suite("with open('x') as f, open('y') as g: pass\n")
228 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"
234 self
.check_suite("try: pass\nexcept: pass\nelse: pass\n")
235 self
.check_suite("try: pass\nexcept: pass\nelse: pass\n"
238 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")
244 def test_position(self
):
245 # An absolutely minimal test of position information. Better
246 # tests would be a big project.
247 code
= "def f(x):\n return x + 1"
248 st1
= parser
.suite(code
)
249 st2
= st1
.totuple(line_info
=1, col_info
=1)
254 if isinstance(next
, tuple):
261 terminals
= list(walk(st2
))
283 # Second, we take *invalid* trees and make sure we get ParserError
284 # rejections for them.
287 class IllegalSyntaxTestCase(unittest
.TestCase
):
289 def check_bad_tree(self
, tree
, label
):
291 parser
.sequence2st(tree
)
292 except parser
.ParserError
:
295 self
.fail("did not detect invalid tree for %r" % label
)
298 # not even remotely valid:
299 self
.check_bad_tree((1, 2, 3), "<junk>")
301 def test_illegal_yield_1(self
):
302 # Illegal yield statement: def f(): return 1; yield 1
310 (260, (7, '('), (8, ')')),
331 (302, (303, (304, (305, (2, '1')))))))))))))))))),
349 (303, (304, (305, (2, '1')))))))))))))))))),
354 self
.check_bad_tree(tree
, "def f():\n return 1\n yield 1")
356 def test_illegal_yield_2(self
):
357 # Illegal return in generator: def f(): return 1; yield 1
365 (281, (1, '__future__')),
367 (279, (1, 'generators')))),
374 (260, (7, '('), (8, ')')),
395 (302, (303, (304, (305, (2, '1')))))))))))))))))),
413 (303, (304, (305, (2, '1')))))))))))))))))),
418 self
.check_bad_tree(tree
, "def f():\n return 1\n yield 1")
420 def test_print_chevron_comma(self
):
421 # Illegal input: print >>fp,
437 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
441 self
.check_bad_tree(tree
, "print >>fp,")
443 def test_a_comma_comma_c(self
):
444 # Illegal input: a,,c
455 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
465 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
468 self
.check_bad_tree(tree
, "a,,c")
470 def test_illegal_operator(self
):
471 # Illegal input: a $= b
487 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
498 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
501 self
.check_bad_tree(tree
, "a $= b")
503 def test_malformed_global(self
):
504 #doesn't have global keyword in ast
509 (282, (1, 'foo'))), (4, ''))),
512 self
.check_bad_tree(tree
, "malformed global ast")
515 class CompileTestCase(unittest
.TestCase
):
517 # These tests are very minimal. :-(
519 def test_compile_expr(self
):
520 st
= parser
.expr('2 + 3')
521 code
= parser
.compilest(st
)
522 self
.assertEquals(eval(code
), 5)
524 def test_compile_suite(self
):
525 st
= parser
.suite('x = 2; y = x + 3')
526 code
= parser
.compilest(st
)
529 self
.assertEquals(globs
['y'], 5)
531 def test_compile_error(self
):
532 st
= parser
.suite('1 = 3 + 4')
533 self
.assertRaises(SyntaxError, parser
.compilest
, st
)
535 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
)
541 class ParserStackLimitTestCase(unittest
.TestCase
):
542 """try to push the parser to/over it's limits.
543 see http://bugs.python.org/issue1881 for a discussion
545 def _nested_expression(self
, level
):
546 return "["*level
+"]"*level
548 def test_deeply_nested_list(self
):
549 e
= self
._nested
_expression
(99)
553 def test_trigger_memory_error(self
):
554 e
= self
._nested
_expression
(100)
555 print >>sys
.stderr
, "Expecting 's_push: parser stack overflow' in next line"
556 self
.assertRaises(MemoryError, parser
.expr
, e
)
559 test_support
.run_unittest(
560 RoundtripLegalSyntaxTestCase
,
561 IllegalSyntaxTestCase
,
563 ParserStackLimitTestCase
,
567 if __name__
== "__main__":