|
3 | 3 |
|
4 | 4 | from test_support import * |
5 | 5 |
|
| 6 | +def check_syntax(statement): |
| 7 | + try: |
| 8 | + compile(statement, '<string>', 'exec') |
| 9 | + except SyntaxError: |
| 10 | + print 'SyntaxError expected for "%s"' % statement |
| 11 | + else: |
| 12 | + print 'Missing SyntaxError: "%s"' % statement |
| 13 | + |
6 | 14 | print '1. Parser' |
7 | 15 |
|
8 | 16 | print '1.1 Tokens' |
|
83 | 91 |
|
84 | 92 | print '1.1.3 String literals' |
85 | 93 |
|
86 | | -##def verify(s): |
87 | | -## if not s: raise TestFailed, 'see traceback' |
88 | | - |
89 | 94 | x = ''; y = ""; verify(len(x) == 0 and x == y) |
90 | 95 | x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39) |
91 | 96 | x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34) |
@@ -154,12 +159,20 @@ def f1(): pass |
154 | 159 | def f2(one_argument): pass |
155 | 160 | def f3(two, arguments): pass |
156 | 161 | def f4(two, (compound, (argument, list))): pass |
| 162 | +def f5((compound, first), two): pass |
| 163 | +verify(f2.func_code.co_varnames == ('one_argument',)) |
| 164 | +verify(f3.func_code.co_varnames == ('two', 'arguments')) |
| 165 | +verify(f4.func_code.co_varnames == ('two', '.2', 'compound', 'argument', |
| 166 | + 'list')) |
| 167 | +verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first')) |
157 | 168 | def a1(one_arg,): pass |
158 | 169 | def a2(two, args,): pass |
159 | 170 | def v0(*rest): pass |
160 | 171 | def v1(a, *rest): pass |
161 | 172 | def v2(a, b, *rest): pass |
162 | | -def v3(a, (b, c), *rest): pass |
| 173 | +def v3(a, (b, c), *rest): return a, b, c, rest |
| 174 | +verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c')) |
| 175 | +verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,))) |
163 | 176 | def d01(a=1): pass |
164 | 177 | d01() |
165 | 178 | d01(1) |
@@ -302,13 +315,6 @@ def tellme(file=None): |
302 | 315 | driver() |
303 | 316 |
|
304 | 317 | # syntax errors |
305 | | -def check_syntax(statement): |
306 | | - try: |
307 | | - compile(statement, '<string>', 'exec') |
308 | | - except SyntaxError: |
309 | | - pass |
310 | | - else: |
311 | | - print 'Missing SyntaxError: "%s"' % statement |
312 | 318 | check_syntax('print ,') |
313 | 319 | check_syntax('print >> x,') |
314 | 320 |
|
@@ -618,17 +624,8 @@ def test_in_func(l): |
618 | 624 |
|
619 | 625 | print test_in_func(nums) |
620 | 626 |
|
621 | | -try: |
622 | | - eval("[i, s for i in nums for s in strs]") |
623 | | - print "FAIL: should have raised a SyntaxError!" |
624 | | -except SyntaxError: |
625 | | - print "good: got a SyntaxError as expected" |
626 | | - |
627 | | -try: |
628 | | - eval("[x if y]") |
629 | | - print "FAIL: should have raised a SyntaxError!" |
630 | | -except SyntaxError: |
631 | | - print "good: got a SyntaxError as expected" |
| 627 | +check_syntax("[i, s for i in nums for s in strs]") |
| 628 | +check_syntax("[x if y]") |
632 | 629 |
|
633 | 630 | suppliers = [ |
634 | 631 | (1, "Boeing"), |
|
0 commit comments