1
# Python test set -- part 1, grammar.
2
# This just tests whether the parser accepts them all.
4
# NOTE: When you run this test as a script from the command line, you
5
# get warnings about certain hex/oct constants. Since those are
6
# issued by the parser, you can't suppress them by adding a
7
# filterwarnings() call to this module. Therefore, to shut up the
8
# regression test, the filterwarnings() call has been added to
11
from test.support import run_unittest, check_syntax_error
17
class TokenTests(unittest.TestCase):
19
def testBackslash(self):
20
# Backslash means line continuation:
23
self.assertEquals(x, 2, 'backslash for line continuation')
25
# Backslash does not means continuation in comments :\
27
self.assertEquals(x, 0, 'backslash ending comment')
29
def testPlainIntegers(self):
30
self.assertEquals(type(000), type(0))
31
self.assertEquals(0xff, 255)
32
self.assertEquals(0o377, 255)
33
self.assertEquals(2147483647, 0o17777777777)
34
self.assertEquals(0b1001, 9)
35
# "0x" is not a valid literal
36
self.assertRaises(SyntaxError, eval, "0x")
37
from sys import maxsize
38
if maxsize == 2147483647:
39
self.assertEquals(-2147483647-1, -0o20000000000)
41
self.assert_(0o37777777777 > 0)
42
self.assert_(0xffffffff > 0)
43
self.assert_(0b1111111111111111111111111111111 > 0)
44
for s in ('2147483648', '0o40000000000', '0x100000000',
45
'0b10000000000000000000000000000000'):
49
self.fail("OverflowError on huge integer literal %r" % s)
50
elif maxsize == 9223372036854775807:
51
self.assertEquals(-9223372036854775807-1, -0o1000000000000000000000)
52
self.assert_(0o1777777777777777777777 > 0)
53
self.assert_(0xffffffffffffffff > 0)
54
self.assert_(0b11111111111111111111111111111111111111111111111111111111111111 > 0)
55
for s in '9223372036854775808', '0o2000000000000000000000', \
56
'0x10000000000000000', \
57
'0b100000000000000000000000000000000000000000000000000000000000000':
61
self.fail("OverflowError on huge integer literal %r" % s)
63
self.fail('Weird maxsize value %r' % maxsize)
65
def testLongIntegers(self):
67
x = 0xffffffffffffffff
68
x = 0Xffffffffffffffff
69
x = 0o77777777777777777
70
x = 0O77777777777777777
71
x = 123456789012345678901234567890
72
x = 0b100000000000000000000000000000000000000000000000000000000000000000000
73
x = 0B111111111111111111111111111111111111111111111111111111111111111111111
89
def testStringLiterals(self):
90
x = ''; y = ""; self.assert_(len(x) == 0 and x == y)
91
x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39)
92
x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34)
93
x = "doesn't \"shrink\" does it"
94
y = 'doesn\'t "shrink" does it'
95
self.assert_(len(x) == 24 and x == y)
96
x = "does \"shrink\" doesn't it"
97
y = 'does "shrink" doesn\'t it'
98
self.assert_(len(x) == 24 and x == y)
105
y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
106
self.assertEquals(x, y)
113
self.assertEquals(x, y)
120
self.assertEquals(x, y)
127
self.assertEquals(x, y)
129
def testEllipsis(self):
131
self.assert_(x is Ellipsis)
132
self.assertRaises(SyntaxError, eval, ".. .")
134
class GrammarTests(unittest.TestCase):
136
# single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
137
# XXX can't test in a script -- this rule is only used when interactive
139
# file_input: (NEWLINE | stmt)* ENDMARKER
140
# Being tested as this very moment this very module
142
# expr_input: testlist NEWLINE
143
# XXX Hard to test -- used only in calls to input()
145
def testEvalInput(self):
147
x = eval('1, 0 or 1')
149
def testFuncdef(self):
150
### [decorators] 'def' NAME parameters ['->' test] ':' suite
151
### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
152
### decorators: decorator+
153
### parameters: '(' [typedargslist] ')'
154
### typedargslist: ((tfpdef ['=' test] ',')*
155
### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
156
### | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
157
### tfpdef: NAME [':' test]
158
### varargslist: ((vfpdef ['=' test] ',')*
159
### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
160
### | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
166
def f2(one_argument): pass
167
def f3(two, arguments): pass
168
self.assertEquals(f2.__code__.co_varnames, ('one_argument',))
169
self.assertEquals(f3.__code__.co_varnames, ('two', 'arguments'))
170
def a1(one_arg,): pass
171
def a2(two, args,): pass
173
def v1(a, *rest): pass
174
def v2(a, b, *rest): pass
185
v0(1,2,3,4,5,6,7,8,9,0)
190
v1(1,2,3,4,5,6,7,8,9,0)
194
v2(1,2,3,4,5,6,7,8,9,0)
201
def d11(a, b=1): pass
205
def d21(a, b, c=1): pass
212
def d02(a=1, b=2): pass
219
d02(**{'a': 1, 'b': 2})
220
def d12(a, b=1, c=2): pass
224
def d22(a, b, c=1, d=2): pass
228
def d01v(a=1, *rest): pass
235
def d11v(a, b=1, *rest): pass
239
def d21v(a, b, c=1, *rest): pass
244
d21v(1, 2, **{'c': 3})
245
def d02v(a=1, b=2, *rest): pass
251
d02v(**{'a': 1, 'b': 2})
252
def d12v(a, b=1, c=2, *rest): pass
258
d12v(1, 2, *(3, 4, 5))
259
d12v(1, *(2,), **{'c': 3})
260
def d22v(a, b, c=1, d=2, *rest): pass
266
d22v(1, 2, *(3, 4, 5))
267
d22v(1, *(2, 3), **{'d': 4})
269
# keyword argument type tests
271
str('x', **{b'foo':1 })
275
self.fail('Bytes should not work as keyword argument names')
276
# keyword only argument tests
277
def pos0key1(*, key): return key
279
def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2
280
pos2key2(1, 2, k1=100)
281
pos2key2(1, 2, k1=100, k2=200)
282
pos2key2(1, 2, k2=100, k1=200)
283
def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg
284
pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
285
pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
287
# keyword arguments after *arglist
288
def f(*args, **kwargs):
290
self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
292
self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
293
self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
295
# argument annotation tests
296
def f(x) -> list: pass
297
self.assertEquals(f.__annotations__, {'return': list})
299
self.assertEquals(f.__annotations__, {'x': int})
301
self.assertEquals(f.__annotations__, {'x': str})
302
def f(**x:float): pass
303
self.assertEquals(f.__annotations__, {'x': float})
304
def f(x, y:1+2): pass
305
self.assertEquals(f.__annotations__, {'y': 3})
306
def f(a, b:1, c:2, d): pass
307
self.assertEquals(f.__annotations__, {'b': 1, 'c': 2})
308
def f(a, b:1, c:2, d, e:3=4, f=5, *g:6): pass
309
self.assertEquals(f.__annotations__,
310
{'b': 1, 'c': 2, 'e': 3, 'g': 6})
311
def f(a, b:1, c:2, d, e:3=4, f=5, *g:6, h:7, i=8, j:9=10,
313
self.assertEquals(f.__annotations__,
314
{'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
315
'k': 11, 'return': 12})
316
# Check for SF Bug #1697248 - mixing decorators and a return annotation
317
def null(x): return x
319
def f(x) -> list: pass
320
self.assertEquals(f.__annotations__, {'return': list})
322
# test MAKE_CLOSURE with a variety of oparg's
324
def f(): return closure
325
def f(x=1): return closure
326
def f(*, k=1): return closure
327
def f() -> int: return closure
329
# Check ast errors in *args and *kwargs
330
check_syntax_error(self, "f(*g(1=2))")
331
check_syntax_error(self, "f(**g(1=2))")
333
def testLambdef(self):
334
### lambdef: 'lambda' [varargslist] ':' test
336
self.assertEquals(l1(), 0)
337
l2 = lambda : a[d] # XXX just testing the expression
338
l3 = lambda : [2 < x for x in [-1, 3, 0]]
339
self.assertEquals(l3(), [0, 1, 0])
340
l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
341
self.assertEquals(l4(), 1)
342
l5 = lambda x, y, z=2: x + y + z
343
self.assertEquals(l5(1, 2), 5)
344
self.assertEquals(l5(1, 2, 3), 6)
345
check_syntax_error(self, "lambda x: x = 2")
346
check_syntax_error(self, "lambda (None,): None")
347
l6 = lambda x, y, *, k=20: x+y+k
348
self.assertEquals(l6(1,2), 1+2+20)
349
self.assertEquals(l6(1,2,k=10), 1+2+10)
352
### stmt: simple_stmt | compound_stmt
355
def testSimpleStmt(self):
356
### simple_stmt: small_stmt (';' small_stmt)* [';']
359
# verify statments that end with semi-colons
363
### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
366
def testExprStmt(self):
367
# (exprlist '=')* exprlist
374
abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
376
check_syntax_error(self, "x + 1 = 1")
377
check_syntax_error(self, "a + 1 = b + 2")
379
def testDelStmt(self):
388
def testPassStmt(self):
392
# flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
395
def testBreakStmt(self):
399
def testContinueStmt(self):
402
while i: i = 0; continue
409
msg = "continue failed to continue inside try"
411
msg = "continue inside try called except block"
417
msg = "finally block not called"
425
def test_break_continue_loop(self):
426
# This test warrants an explanation. It is a test specifically for SF bugs
427
# #463359 and #462937. The bug is that a 'break' statement executed or
428
# exception raised inside a try/except inside a loop, *after* a continue
429
# statement has been executed in that loop, will cause the wrong number of
430
# arguments to be popped off the stack and the instruction pointer reset to
431
# a very small number (usually 0.) Because of this, the following test
432
# *must* written as a function, and the tracking vars *must* be function
433
# arguments with default values. Otherwise, the test will loop and loop.
435
def test_inner(extra_burning_oil = 1, count=0):
440
if extra_burning_oil and big_hippo == 1:
441
extra_burning_oil -= 1
447
if count > 2 or big_hippo != 1:
448
self.fail("continue then break in try/except in loop broken!")
451
def testReturn(self):
452
# 'return' [testlist]
457
check_syntax_error(self, "class foo:return 1")
460
check_syntax_error(self, "class foo:yield 1")
463
# 'raise' test [',' test]
464
try: raise RuntimeError('just testing')
465
except RuntimeError: pass
466
try: raise KeyboardInterrupt
467
except KeyboardInterrupt: pass
469
def testImport(self):
470
# 'import' dotted_as_names
473
# 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
474
from time import time
475
from time import (time)
476
# not testable inside a function, but already done at top of the module
478
from sys import path, argv
479
from sys import (path, argv)
480
from sys import (path, argv,)
482
def testGlobal(self):
483
# 'global' NAME (',' NAME)*
486
global one, two, three, four, five, six, seven, eight, nine, ten
488
def testNonlocal(self):
489
# 'nonlocal' NAME (',' NAME)*
496
def testAssert(self):
497
# assert_stmt: 'assert' test [',' test]
501
assert 1, lambda x:x+1
504
except AssertionError as e:
505
self.assertEquals(e.args[0], "msg")
508
self.fail("AssertionError not raised by assert 0")
510
### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
514
# 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
527
# 'while' test ':' suite ['else' ':' suite]
532
# Issue1920: "while 0" is optimized away,
533
# ensure that the "else" clause is still present.
539
self.assertEquals(x, 2)
542
# 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
543
for i in 1, 2, 3: pass
544
for i, j, k in (): pass
547
def __init__(self, max):
550
def __len__(self): return len(self.sofar)
551
def __getitem__(self, i):
552
if not 0 <= i < self.max: raise IndexError
555
self.sofar.append(n*n)
559
for x in Squares(10): n = n+x
561
self.fail('for over growing sequence')
564
for x, in [(1,), (2,), (3,)]:
566
self.assertEqual(result, [1, 2, 3])
569
### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
570
### | 'try' ':' suite 'finally' ':' suite
571
### except_clause: 'except' [expr ['as' expr]]
574
except ZeroDivisionError:
579
except EOFError: pass
580
except TypeError as msg: pass
581
except RuntimeError as msg: pass
585
except (EOFError, TypeError, ZeroDivisionError): pass
587
except (EOFError, TypeError, ZeroDivisionError) as msg: pass
592
# simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
607
### and_test ('or' and_test)*
608
### and_test: not_test ('and' not_test)*
609
### not_test: 'not' not_test | comparison
613
if not not not 1: pass
614
if not 1 and 1 and 1: pass
615
if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
617
def testComparison(self):
618
### comparison: expr (comp_op expr)*
619
### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
632
if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
634
def testBinaryMaskOps(self):
639
def testShiftOps(self):
644
def testAdditiveOps(self):
648
x = 1 - 1 + 1 - 1 + 1
650
def testMultiplicativeOps(self):
656
def testUnaryOps(self):
660
x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
661
x = -1*1/1 + 1*1 - ---1*1
663
def testSelectors(self):
664
### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
665
### subscript: expr | [expr] ':' [expr]
670
x = sys.modules['time'].time()
681
# A rough test of SF bug 1333982. http://python.org/sf/1333982
682
# The testing here is fairly incomplete.
683
# Test cases should include: commas with 1 and 2 colons
690
L.sort(key=lambda x: x if isinstance(x, tuple) else ())
691
self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
694
### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
695
### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
699
x = (1 or 2 or 3, 2, 3)
704
x = [1 or 2 or 3, 2, 3]
710
x = {'one' or 'two': 1 or 2}
711
x = {'one': 1, 'two': 2}
712
x = {'one': 1, 'two': 2,}
713
x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
717
x = {'one', 'two', 'three'}
724
### exprlist: expr (',' expr)* [',']
725
### testlist: test (',' test)* [',']
726
# These have been exercised enough above
728
def testClassdef(self):
729
# 'class' NAME ['(' [testlist] ')'] ':' suite
734
class D(C1, C2, B): pass
736
def meth1(self): pass
737
def meth2(self, arg): pass
738
def meth3(self, a1, a2): pass
740
# decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
741
# decorators: decorator+
742
# decorated: decorators (classdef | funcdef)
743
def class_decorator(x): return x
747
def testDictcomps(self):
748
# dictorsetmaker: ( (test ':' test (comp_for |
749
# (',' test ':' test)* [','])) |
750
# (test (comp_for | (',' test)* [','])) )
752
self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
754
def testListcomps(self):
755
# list comprehension tests
756
nums = [1, 2, 3, 4, 5]
757
strs = ["Apple", "Banana", "Coconut"]
758
spcs = [" Apple", " Banana ", "Coco nut "]
760
self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])
761
self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
762
self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
763
self.assertEqual([(i, s) for i in nums for s in strs],
764
[(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
765
(2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
766
(3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
767
(4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
768
(5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
769
self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
770
[(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
771
(3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
772
(5, 'Banana'), (5, 'Coconut')])
773
self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
774
[[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
777
return [0 < x < 3 for x in l if x > 2]
779
self.assertEqual(test_in_func(nums), [False, False, False])
781
def test_nested_front():
782
self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
783
[[1, 2], [3, 4], [5, 6]])
787
check_syntax_error(self, "[i, s for i in nums for s in strs]")
788
check_syntax_error(self, "[x if y]")
803
(1, 10), (1, 20), (2, 20), (3, 30)
808
for (sno, sname) in suppliers
809
for (pno, pname) in parts
810
for (sp_sno, sp_pno) in suppart
811
if sno == sp_sno and pno == sp_pno
814
self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
815
('Macdonalds', 'Cheeseburger')])
817
def testGenexps(self):
818
# generator expression tests
819
g = ([x for x in range(10)] for x in range(1))
820
self.assertEqual(next(g), [x for x in range(10)])
823
self.fail('should produce StopIteration exception')
824
except StopIteration:
831
self.fail('should produce TypeError')
835
self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
836
self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
838
a = [x for x in range(10)]
839
b = (x for x in (y for y in a))
840
self.assertEqual(sum(b), sum([x for x in range(10)]))
842
self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
843
self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
844
self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
845
self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
846
self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
847
self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)]))
848
self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
849
check_syntax_error(self, "foo(x for x in range(10), 100)")
850
check_syntax_error(self, "foo(100, x for x in range(10))")
852
def testComprehensionSpecials(self):
853
# test for outmost iterable precomputation
854
x = 10; g = (i for i in range(x)); x = 5
855
self.assertEqual(len(list(g)), 10)
857
# This should hold, since we're only precomputing outmost iterable.
858
x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
860
self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
862
# Grammar allows multiple adjacent 'if's in listcomps and genexps,
863
# even though it's silly. Make sure it works (ifelse broke this.)
864
self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
865
self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
867
# verify unpacking single element tuples in listcomp/genexp.
868
self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
869
self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
871
def testIfElseExpr(self):
872
# Test ifelse expressions in various cases
873
def _checkeval(msg, ret):
874
"helper to check that evaluation of expressions is done correctly"
878
# the next line is not allowed anymore
879
#self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
880
self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
881
self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True])
882
self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
883
self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
884
self.assertEqual((5 and 6 if 0 else 1), 1)
885
self.assertEqual(((5 and 6) if 0 else 1), 1)
886
self.assertEqual((5 and (6 if 1 else 1)), 6)
887
self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
888
self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
889
self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
890
self.assertEqual((not 5 if 1 else 1), False)
891
self.assertEqual((not 5 if 0 else 1), 1)
892
self.assertEqual((6 + 1 if 1 else 2), 7)
893
self.assertEqual((6 - 1 if 1 else 2), 5)
894
self.assertEqual((6 * 2 if 1 else 4), 12)
895
self.assertEqual((6 / 2 if 1 else 3), 3)
896
self.assertEqual((6 < 4 if 0 else 2), 2)
900
run_unittest(TokenTests, GrammarTests)
902
if __name__ == '__main__':