~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/test/test_grammar.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Python test set -- part 1, grammar.
 
2
# This just tests whether the parser accepts them all.
 
3
 
 
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
 
9
# regrtest.py.
 
10
 
 
11
from test.support import run_unittest, check_syntax_error
 
12
import unittest
 
13
import sys
 
14
# testing import *
 
15
from sys import *
 
16
 
 
17
class TokenTests(unittest.TestCase):
 
18
 
 
19
    def testBackslash(self):
 
20
        # Backslash means line continuation:
 
21
        x = 1 \
 
22
        + 1
 
23
        self.assertEquals(x, 2, 'backslash for line continuation')
 
24
 
 
25
        # Backslash does not means continuation in comments :\
 
26
        x = 0
 
27
        self.assertEquals(x, 0, 'backslash ending comment')
 
28
 
 
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)
 
40
            # XXX -2147483648
 
41
            self.assert_(0o37777777777 > 0)
 
42
            self.assert_(0xffffffff > 0)
 
43
            self.assert_(0b1111111111111111111111111111111 > 0)
 
44
            for s in ('2147483648', '0o40000000000', '0x100000000',
 
45
                      '0b10000000000000000000000000000000'):
 
46
                try:
 
47
                    x = eval(s)
 
48
                except OverflowError:
 
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':
 
58
                try:
 
59
                    x = eval(s)
 
60
                except OverflowError:
 
61
                    self.fail("OverflowError on huge integer literal %r" % s)
 
62
        else:
 
63
            self.fail('Weird maxsize value %r' % maxsize)
 
64
 
 
65
    def testLongIntegers(self):
 
66
        x = 0
 
67
        x = 0xffffffffffffffff
 
68
        x = 0Xffffffffffffffff
 
69
        x = 0o77777777777777777
 
70
        x = 0O77777777777777777
 
71
        x = 123456789012345678901234567890
 
72
        x = 0b100000000000000000000000000000000000000000000000000000000000000000000
 
73
        x = 0B111111111111111111111111111111111111111111111111111111111111111111111
 
74
 
 
75
    def testFloats(self):
 
76
        x = 3.14
 
77
        x = 314.
 
78
        x = 0.314
 
79
        # XXX x = 000.314
 
80
        x = .314
 
81
        x = 3e14
 
82
        x = 3E14
 
83
        x = 3e-14
 
84
        x = 3e+14
 
85
        x = 3.e14
 
86
        x = .3e14
 
87
        x = 3.1e4
 
88
 
 
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)
 
99
        x = """
 
100
The "quick"
 
101
brown fox
 
102
jumps over
 
103
the 'lazy' dog.
 
104
"""
 
105
        y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
 
106
        self.assertEquals(x, y)
 
107
        y = '''
 
108
The "quick"
 
109
brown fox
 
110
jumps over
 
111
the 'lazy' dog.
 
112
'''
 
113
        self.assertEquals(x, y)
 
114
        y = "\n\
 
115
The \"quick\"\n\
 
116
brown fox\n\
 
117
jumps over\n\
 
118
the 'lazy' dog.\n\
 
119
"
 
120
        self.assertEquals(x, y)
 
121
        y = '\n\
 
122
The \"quick\"\n\
 
123
brown fox\n\
 
124
jumps over\n\
 
125
the \'lazy\' dog.\n\
 
126
'
 
127
        self.assertEquals(x, y)
 
128
 
 
129
    def testEllipsis(self):
 
130
        x = ...
 
131
        self.assert_(x is Ellipsis)
 
132
        self.assertRaises(SyntaxError, eval, ".. .")
 
133
 
 
134
class GrammarTests(unittest.TestCase):
 
135
 
 
136
    # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
 
137
    # XXX can't test in a script -- this rule is only used when interactive
 
138
 
 
139
    # file_input: (NEWLINE | stmt)* ENDMARKER
 
140
    # Being tested as this very moment this very module
 
141
 
 
142
    # expr_input: testlist NEWLINE
 
143
    # XXX Hard to test -- used only in calls to input()
 
144
 
 
145
    def testEvalInput(self):
 
146
        # testlist ENDMARKER
 
147
        x = eval('1, 0 or 1')
 
148
 
 
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])* [','])
 
161
        ### vfpdef: NAME
 
162
        def f1(): pass
 
163
        f1()
 
164
        f1(*())
 
165
        f1(*(), **{})
 
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
 
172
        def v0(*rest): pass
 
173
        def v1(a, *rest): pass
 
174
        def v2(a, b, *rest): pass
 
175
 
 
176
        f1()
 
177
        f2(1)
 
178
        f2(1,)
 
179
        f3(1, 2)
 
180
        f3(1, 2,)
 
181
        v0()
 
182
        v0(1)
 
183
        v0(1,)
 
184
        v0(1,2)
 
185
        v0(1,2,3,4,5,6,7,8,9,0)
 
186
        v1(1)
 
187
        v1(1,)
 
188
        v1(1,2)
 
189
        v1(1,2,3)
 
190
        v1(1,2,3,4,5,6,7,8,9,0)
 
191
        v2(1,2)
 
192
        v2(1,2,3)
 
193
        v2(1,2,3,4)
 
194
        v2(1,2,3,4,5,6,7,8,9,0)
 
195
 
 
196
        def d01(a=1): pass
 
197
        d01()
 
198
        d01(1)
 
199
        d01(*(1,))
 
200
        d01(**{'a':2})
 
201
        def d11(a, b=1): pass
 
202
        d11(1)
 
203
        d11(1, 2)
 
204
        d11(1, **{'b':2})
 
205
        def d21(a, b, c=1): pass
 
206
        d21(1, 2)
 
207
        d21(1, 2, 3)
 
208
        d21(*(1, 2, 3))
 
209
        d21(1, *(2, 3))
 
210
        d21(1, 2, *(3,))
 
211
        d21(1, 2, **{'c':3})
 
212
        def d02(a=1, b=2): pass
 
213
        d02()
 
214
        d02(1)
 
215
        d02(1, 2)
 
216
        d02(*(1, 2))
 
217
        d02(1, *(2,))
 
218
        d02(1, **{'b':2})
 
219
        d02(**{'a': 1, 'b': 2})
 
220
        def d12(a, b=1, c=2): pass
 
221
        d12(1)
 
222
        d12(1, 2)
 
223
        d12(1, 2, 3)
 
224
        def d22(a, b, c=1, d=2): pass
 
225
        d22(1, 2)
 
226
        d22(1, 2, 3)
 
227
        d22(1, 2, 3, 4)
 
228
        def d01v(a=1, *rest): pass
 
229
        d01v()
 
230
        d01v(1)
 
231
        d01v(1, 2)
 
232
        d01v(*(1, 2, 3, 4))
 
233
        d01v(*(1,))
 
234
        d01v(**{'a':2})
 
235
        def d11v(a, b=1, *rest): pass
 
236
        d11v(1)
 
237
        d11v(1, 2)
 
238
        d11v(1, 2, 3)
 
239
        def d21v(a, b, c=1, *rest): pass
 
240
        d21v(1, 2)
 
241
        d21v(1, 2, 3)
 
242
        d21v(1, 2, 3, 4)
 
243
        d21v(*(1, 2, 3, 4))
 
244
        d21v(1, 2, **{'c': 3})
 
245
        def d02v(a=1, b=2, *rest): pass
 
246
        d02v()
 
247
        d02v(1)
 
248
        d02v(1, 2)
 
249
        d02v(1, 2, 3)
 
250
        d02v(1, *(2, 3, 4))
 
251
        d02v(**{'a': 1, 'b': 2})
 
252
        def d12v(a, b=1, c=2, *rest): pass
 
253
        d12v(1)
 
254
        d12v(1, 2)
 
255
        d12v(1, 2, 3)
 
256
        d12v(1, 2, 3, 4)
 
257
        d12v(*(1, 2, 3, 4))
 
258
        d12v(1, 2, *(3, 4, 5))
 
259
        d12v(1, *(2,), **{'c': 3})
 
260
        def d22v(a, b, c=1, d=2, *rest): pass
 
261
        d22v(1, 2)
 
262
        d22v(1, 2, 3)
 
263
        d22v(1, 2, 3, 4)
 
264
        d22v(1, 2, 3, 4, 5)
 
265
        d22v(*(1, 2, 3, 4))
 
266
        d22v(1, 2, *(3, 4, 5))
 
267
        d22v(1, *(2, 3), **{'d': 4})
 
268
 
 
269
        # keyword argument type tests
 
270
        try:
 
271
            str('x', **{b'foo':1 })
 
272
        except TypeError:
 
273
            pass
 
274
        else:
 
275
            self.fail('Bytes should not work as keyword argument names')
 
276
        # keyword only argument tests
 
277
        def pos0key1(*, key): return key
 
278
        pos0key1(key=100)
 
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)
 
286
 
 
287
        # keyword arguments after *arglist
 
288
        def f(*args, **kwargs):
 
289
            return args, kwargs
 
290
        self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
 
291
                                                    {'x':2, 'y':5}))
 
292
        self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
 
293
        self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
 
294
 
 
295
        # argument annotation tests
 
296
        def f(x) -> list: pass
 
297
        self.assertEquals(f.__annotations__, {'return': list})
 
298
        def f(x:int): pass
 
299
        self.assertEquals(f.__annotations__, {'x': int})
 
300
        def f(*x:str): pass
 
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,
 
312
              **k:11) -> 12: pass
 
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
 
318
        @null
 
319
        def f(x) -> list: pass
 
320
        self.assertEquals(f.__annotations__, {'return': list})
 
321
 
 
322
        # test MAKE_CLOSURE with a variety of oparg's
 
323
        closure = 1
 
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
 
328
 
 
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))")
 
332
 
 
333
    def testLambdef(self):
 
334
        ### lambdef: 'lambda' [varargslist] ':' test
 
335
        l1 = lambda : 0
 
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)
 
350
 
 
351
 
 
352
    ### stmt: simple_stmt | compound_stmt
 
353
    # Tested below
 
354
 
 
355
    def testSimpleStmt(self):
 
356
        ### simple_stmt: small_stmt (';' small_stmt)* [';']
 
357
        x = 1; pass; del x
 
358
        def foo():
 
359
            # verify statments that end with semi-colons
 
360
            x = 1; pass; del x;
 
361
        foo()
 
362
 
 
363
    ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
 
364
    # Tested below
 
365
 
 
366
    def testExprStmt(self):
 
367
        # (exprlist '=')* exprlist
 
368
        1
 
369
        1, 2, 3
 
370
        x = 1
 
371
        x = 1, 2, 3
 
372
        x = y = z = 1, 2, 3
 
373
        x, y, z = 1, 2, 3
 
374
        abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
 
375
 
 
376
        check_syntax_error(self, "x + 1 = 1")
 
377
        check_syntax_error(self, "a + 1 = b + 2")
 
378
 
 
379
    def testDelStmt(self):
 
380
        # 'del' exprlist
 
381
        abc = [1,2,3]
 
382
        x, y, z = abc
 
383
        xyz = x, y, z
 
384
 
 
385
        del abc
 
386
        del x, y, (z, xyz)
 
387
 
 
388
    def testPassStmt(self):
 
389
        # 'pass'
 
390
        pass
 
391
 
 
392
    # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
 
393
    # Tested below
 
394
 
 
395
    def testBreakStmt(self):
 
396
        # 'break'
 
397
        while 1: break
 
398
 
 
399
    def testContinueStmt(self):
 
400
        # 'continue'
 
401
        i = 1
 
402
        while i: i = 0; continue
 
403
 
 
404
        msg = ""
 
405
        while not msg:
 
406
            msg = "ok"
 
407
            try:
 
408
                continue
 
409
                msg = "continue failed to continue inside try"
 
410
            except:
 
411
                msg = "continue inside try called except block"
 
412
        if msg != "ok":
 
413
            self.fail(msg)
 
414
 
 
415
        msg = ""
 
416
        while not msg:
 
417
            msg = "finally block not called"
 
418
            try:
 
419
                continue
 
420
            finally:
 
421
                msg = "ok"
 
422
        if msg != "ok":
 
423
            self.fail(msg)
 
424
 
 
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.
 
434
 
 
435
        def test_inner(extra_burning_oil = 1, count=0):
 
436
            big_hippo = 2
 
437
            while big_hippo:
 
438
                count += 1
 
439
                try:
 
440
                    if extra_burning_oil and big_hippo == 1:
 
441
                        extra_burning_oil -= 1
 
442
                        break
 
443
                    big_hippo -= 1
 
444
                    continue
 
445
                except:
 
446
                    raise
 
447
            if count > 2 or big_hippo != 1:
 
448
                self.fail("continue then break in try/except in loop broken!")
 
449
        test_inner()
 
450
 
 
451
    def testReturn(self):
 
452
        # 'return' [testlist]
 
453
        def g1(): return
 
454
        def g2(): return 1
 
455
        g1()
 
456
        x = g2()
 
457
        check_syntax_error(self, "class foo:return 1")
 
458
 
 
459
    def testYield(self):
 
460
        check_syntax_error(self, "class foo:yield 1")
 
461
 
 
462
    def testRaise(self):
 
463
        # 'raise' test [',' test]
 
464
        try: raise RuntimeError('just testing')
 
465
        except RuntimeError: pass
 
466
        try: raise KeyboardInterrupt
 
467
        except KeyboardInterrupt: pass
 
468
 
 
469
    def testImport(self):
 
470
        # 'import' dotted_as_names
 
471
        import sys
 
472
        import time, sys
 
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
 
477
        # from sys import *
 
478
        from sys import path, argv
 
479
        from sys import (path, argv)
 
480
        from sys import (path, argv,)
 
481
 
 
482
    def testGlobal(self):
 
483
        # 'global' NAME (',' NAME)*
 
484
        global a
 
485
        global a, b
 
486
        global one, two, three, four, five, six, seven, eight, nine, ten
 
487
 
 
488
    def testNonlocal(self):
 
489
        # 'nonlocal' NAME (',' NAME)*
 
490
        x = 0
 
491
        y = 0
 
492
        def f():
 
493
            nonlocal x
 
494
            nonlocal x, y
 
495
 
 
496
    def testAssert(self):
 
497
        # assert_stmt: 'assert' test [',' test]
 
498
        assert 1
 
499
        assert 1, 1
 
500
        assert lambda x:x
 
501
        assert 1, lambda x:x+1
 
502
        try:
 
503
            assert 0, "msg"
 
504
        except AssertionError as e:
 
505
            self.assertEquals(e.args[0], "msg")
 
506
        else:
 
507
            if __debug__:
 
508
                self.fail("AssertionError not raised by assert 0")
 
509
 
 
510
    ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
 
511
    # Tested below
 
512
 
 
513
    def testIf(self):
 
514
        # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
 
515
        if 1: pass
 
516
        if 1: pass
 
517
        else: pass
 
518
        if 0: pass
 
519
        elif 0: pass
 
520
        if 0: pass
 
521
        elif 0: pass
 
522
        elif 0: pass
 
523
        elif 0: pass
 
524
        else: pass
 
525
 
 
526
    def testWhile(self):
 
527
        # 'while' test ':' suite ['else' ':' suite]
 
528
        while 0: pass
 
529
        while 0: pass
 
530
        else: pass
 
531
 
 
532
        # Issue1920: "while 0" is optimized away,
 
533
        # ensure that the "else" clause is still present.
 
534
        x = 0
 
535
        while 0:
 
536
            x = 1
 
537
        else:
 
538
            x = 2
 
539
        self.assertEquals(x, 2)
 
540
 
 
541
    def testFor(self):
 
542
        # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
 
543
        for i in 1, 2, 3: pass
 
544
        for i, j, k in (): pass
 
545
        else: pass
 
546
        class Squares:
 
547
            def __init__(self, max):
 
548
                self.max = max
 
549
                self.sofar = []
 
550
            def __len__(self): return len(self.sofar)
 
551
            def __getitem__(self, i):
 
552
                if not 0 <= i < self.max: raise IndexError
 
553
                n = len(self.sofar)
 
554
                while n <= i:
 
555
                    self.sofar.append(n*n)
 
556
                    n = n+1
 
557
                return self.sofar[i]
 
558
        n = 0
 
559
        for x in Squares(10): n = n+x
 
560
        if n != 285:
 
561
            self.fail('for over growing sequence')
 
562
 
 
563
        result = []
 
564
        for x, in [(1,), (2,), (3,)]:
 
565
            result.append(x)
 
566
        self.assertEqual(result, [1, 2, 3])
 
567
 
 
568
    def testTry(self):
 
569
        ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
 
570
        ###         | 'try' ':' suite 'finally' ':' suite
 
571
        ### except_clause: 'except' [expr ['as' expr]]
 
572
        try:
 
573
            1/0
 
574
        except ZeroDivisionError:
 
575
            pass
 
576
        else:
 
577
            pass
 
578
        try: 1/0
 
579
        except EOFError: pass
 
580
        except TypeError as msg: pass
 
581
        except RuntimeError as msg: pass
 
582
        except: pass
 
583
        else: pass
 
584
        try: 1/0
 
585
        except (EOFError, TypeError, ZeroDivisionError): pass
 
586
        try: 1/0
 
587
        except (EOFError, TypeError, ZeroDivisionError) as msg: pass
 
588
        try: pass
 
589
        finally: pass
 
590
 
 
591
    def testSuite(self):
 
592
        # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
 
593
        if 1: pass
 
594
        if 1:
 
595
            pass
 
596
        if 1:
 
597
            #
 
598
            #
 
599
            #
 
600
            pass
 
601
            pass
 
602
            #
 
603
            pass
 
604
            #
 
605
 
 
606
    def testTest(self):
 
607
        ### and_test ('or' and_test)*
 
608
        ### and_test: not_test ('and' not_test)*
 
609
        ### not_test: 'not' not_test | comparison
 
610
        if not 1: pass
 
611
        if 1 and 1: pass
 
612
        if 1 or 1: pass
 
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
 
616
 
 
617
    def testComparison(self):
 
618
        ### comparison: expr (comp_op expr)*
 
619
        ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
 
620
        if 1: pass
 
621
        x = (1 == 1)
 
622
        if 1 == 1: pass
 
623
        if 1 != 1: pass
 
624
        if 1 < 1: pass
 
625
        if 1 > 1: pass
 
626
        if 1 <= 1: pass
 
627
        if 1 >= 1: pass
 
628
        if 1 is 1: pass
 
629
        if 1 is not 1: pass
 
630
        if 1 in (): pass
 
631
        if 1 not in (): pass
 
632
        if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
 
633
 
 
634
    def testBinaryMaskOps(self):
 
635
        x = 1 & 1
 
636
        x = 1 ^ 1
 
637
        x = 1 | 1
 
638
 
 
639
    def testShiftOps(self):
 
640
        x = 1 << 1
 
641
        x = 1 >> 1
 
642
        x = 1 << 1 >> 1
 
643
 
 
644
    def testAdditiveOps(self):
 
645
        x = 1
 
646
        x = 1 + 1
 
647
        x = 1 - 1 - 1
 
648
        x = 1 - 1 + 1 - 1 + 1
 
649
 
 
650
    def testMultiplicativeOps(self):
 
651
        x = 1 * 1
 
652
        x = 1 / 1
 
653
        x = 1 % 1
 
654
        x = 1 / 1 * 1 % 1
 
655
 
 
656
    def testUnaryOps(self):
 
657
        x = +1
 
658
        x = -1
 
659
        x = ~1
 
660
        x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
 
661
        x = -1*1/1 + 1*1 - ---1*1
 
662
 
 
663
    def testSelectors(self):
 
664
        ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
 
665
        ### subscript: expr | [expr] ':' [expr]
 
666
 
 
667
        import sys, time
 
668
        c = sys.path[0]
 
669
        x = time.time()
 
670
        x = sys.modules['time'].time()
 
671
        a = '01234'
 
672
        c = a[0]
 
673
        c = a[-1]
 
674
        s = a[0:5]
 
675
        s = a[:5]
 
676
        s = a[0:]
 
677
        s = a[:]
 
678
        s = a[-5:]
 
679
        s = a[:-1]
 
680
        s = a[-4:-3]
 
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
 
684
        d = {}
 
685
        d[1] = 1
 
686
        d[1,] = 2
 
687
        d[1,2] = 3
 
688
        d[1,2,3] = 4
 
689
        L = list(d)
 
690
        L.sort(key=lambda x: x if isinstance(x, tuple) else ())
 
691
        self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
 
692
 
 
693
    def testAtoms(self):
 
694
        ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
 
695
        ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
 
696
 
 
697
        x = (1)
 
698
        x = (1 or 2 or 3)
 
699
        x = (1 or 2 or 3, 2, 3)
 
700
 
 
701
        x = []
 
702
        x = [1]
 
703
        x = [1 or 2 or 3]
 
704
        x = [1 or 2 or 3, 2, 3]
 
705
        x = []
 
706
 
 
707
        x = {}
 
708
        x = {'one': 1}
 
709
        x = {'one': 1,}
 
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}
 
714
 
 
715
        x = {'one'}
 
716
        x = {'one', 1,}
 
717
        x = {'one', 'two', 'three'}
 
718
        x = {2, 3, 4,}
 
719
 
 
720
        x = x
 
721
        x = 'x'
 
722
        x = 123
 
723
 
 
724
    ### exprlist: expr (',' expr)* [',']
 
725
    ### testlist: test (',' test)* [',']
 
726
    # These have been exercised enough above
 
727
 
 
728
    def testClassdef(self):
 
729
        # 'class' NAME ['(' [testlist] ')'] ':' suite
 
730
        class B: pass
 
731
        class B2(): pass
 
732
        class C1(B): pass
 
733
        class C2(B): pass
 
734
        class D(C1, C2, B): pass
 
735
        class C:
 
736
            def meth1(self): pass
 
737
            def meth2(self, arg): pass
 
738
            def meth3(self, a1, a2): pass
 
739
 
 
740
        # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
 
741
        # decorators: decorator+
 
742
        # decorated: decorators (classdef | funcdef)
 
743
        def class_decorator(x): return x
 
744
        @class_decorator
 
745
        class G: pass
 
746
 
 
747
    def testDictcomps(self):
 
748
        # dictorsetmaker: ( (test ':' test (comp_for |
 
749
        #                                   (',' test ':' test)* [','])) |
 
750
        #                   (test (comp_for | (',' test)* [','])) )
 
751
        nums = [1, 2, 3]
 
752
        self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
 
753
 
 
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  "]
 
759
 
 
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]])
 
775
 
 
776
        def test_in_func(l):
 
777
            return [0 < x < 3 for x in l if x > 2]
 
778
 
 
779
        self.assertEqual(test_in_func(nums), [False, False, False])
 
780
 
 
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]])
 
784
 
 
785
        test_nested_front()
 
786
 
 
787
        check_syntax_error(self, "[i, s for i in nums for s in strs]")
 
788
        check_syntax_error(self, "[x if y]")
 
789
 
 
790
        suppliers = [
 
791
          (1, "Boeing"),
 
792
          (2, "Ford"),
 
793
          (3, "Macdonalds")
 
794
        ]
 
795
 
 
796
        parts = [
 
797
          (10, "Airliner"),
 
798
          (20, "Engine"),
 
799
          (30, "Cheeseburger")
 
800
        ]
 
801
 
 
802
        suppart = [
 
803
          (1, 10), (1, 20), (2, 20), (3, 30)
 
804
        ]
 
805
 
 
806
        x = [
 
807
          (sname, pname)
 
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
 
812
        ]
 
813
 
 
814
        self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
 
815
                             ('Macdonalds', 'Cheeseburger')])
 
816
 
 
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)])
 
821
        try:
 
822
            next(g)
 
823
            self.fail('should produce StopIteration exception')
 
824
        except StopIteration:
 
825
            pass
 
826
 
 
827
        a = 1
 
828
        try:
 
829
            g = (a for d in a)
 
830
            next(g)
 
831
            self.fail('should produce TypeError')
 
832
        except TypeError:
 
833
            pass
 
834
 
 
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'])
 
837
 
 
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)]))
 
841
 
 
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))")
 
851
 
 
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)
 
856
 
 
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))
 
859
        x = 5; t = True;
 
860
        self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
 
861
 
 
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])
 
866
 
 
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])
 
870
 
 
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"
 
875
            print(x)
 
876
            return ret
 
877
 
 
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)
 
897
 
 
898
 
 
899
def test_main():
 
900
    run_unittest(TokenTests, GrammarTests)
 
901
 
 
902
if __name__ == '__main__':
 
903
    test_main()