Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 92e9f29

Browse files
committed
add extra tests to verify that co_varnames is being set up properly
also normalize checks for syntax errors and delete commented out definition of verify.
1 parent a6ebc48 commit 92e9f29

2 files changed

Lines changed: 23 additions & 24 deletions

File tree

Lib/test/output/test_grammar

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ extended print_stmt
2424
1 2 3
2525
1 1 1
2626
hello world
27+
SyntaxError expected for "print ,"
28+
SyntaxError expected for "print >> x,"
2729
del_stmt
2830
pass_stmt
2931
flow_stmt
@@ -56,6 +58,6 @@ classdef
5658
[(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]
5759
[(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), (5, 'Banana'), (5, 'Coconut')]
5860
[0, 0, 0]
59-
good: got a SyntaxError as expected
60-
good: got a SyntaxError as expected
61+
SyntaxError expected for "[i, s for i in nums for s in strs]"
62+
SyntaxError expected for "[x if y]"
6163
[('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), ('Macdonalds', 'Cheeseburger')]

Lib/test/test_grammar.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44
from test_support import *
55

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+
614
print '1. Parser'
715

816
print '1.1 Tokens'
@@ -83,9 +91,6 @@
8391

8492
print '1.1.3 String literals'
8593

86-
##def verify(s):
87-
## if not s: raise TestFailed, 'see traceback'
88-
8994
x = ''; y = ""; verify(len(x) == 0 and x == y)
9095
x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39)
9196
x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34)
@@ -154,12 +159,20 @@ def f1(): pass
154159
def f2(one_argument): pass
155160
def f3(two, arguments): pass
156161
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'))
157168
def a1(one_arg,): pass
158169
def a2(two, args,): pass
159170
def v0(*rest): pass
160171
def v1(a, *rest): pass
161172
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,)))
163176
def d01(a=1): pass
164177
d01()
165178
d01(1)
@@ -302,13 +315,6 @@ def tellme(file=None):
302315
driver()
303316

304317
# 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
312318
check_syntax('print ,')
313319
check_syntax('print >> x,')
314320

@@ -618,17 +624,8 @@ def test_in_func(l):
618624

619625
print test_in_func(nums)
620626

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]")
632629

633630
suppliers = [
634631
(1, "Boeing"),

0 commit comments

Comments
 (0)