Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
py3_test_grammar.GrammarTests Class Reference

List of all members.

Public Member Functions

def testEvalInput
def testFuncdef
def testLambdef
def testSimpleStmt
 stmt: simple_stmt | compound_stmt Tested below
def testExprStmt
 small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt Tested below
def testDelStmt
def testPassStmt
def testBreakStmt
def testContinueStmt
def test_break_continue_loop
def testReturn
def testYield
def testRaise
def testImport
def testGlobal
def testNonlocal
def testAssert
def testIf
 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef Tested below
def testWhile
def testFor
def testTry
def testSuite
def testTest
def testComparison
def testBinaryMaskOps
def testShiftOps
def testAdditiveOps
def testMultiplicativeOps
def testUnaryOps
def testSelectors
def testAtoms
def testClassdef
 exprlist: expr (',' expr)* [','] testlist: test (',' test)* [','] These have been exercised enough above
def testDictcomps
def testListcomps
def testGenexps
def testComprehensionSpecials
def test_with_statement
def testIfElseExpr

Public Attributes

 max
 sofar

Detailed Description

Definition at line 134 of file py3_test_grammar.py.


Member Function Documentation

Definition at line 425 of file py3_test_grammar.py.

00425 
00426     def test_break_continue_loop(self):
00427         # This test warrants an explanation. It is a test specifically for SF bugs
00428         # #463359 and #462937. The bug is that a 'break' statement executed or
00429         # exception raised inside a try/except inside a loop, *after* a continue
00430         # statement has been executed in that loop, will cause the wrong number of
00431         # arguments to be popped off the stack and the instruction pointer reset to
00432         # a very small number (usually 0.) Because of this, the following test
00433         # *must* written as a function, and the tracking vars *must* be function
00434         # arguments with default values. Otherwise, the test will loop and loop.
00435 
00436         def test_inner(extra_burning_oil = 1, count=0):
00437             big_hippo = 2
00438             while big_hippo:
00439                 count += 1
00440                 try:
00441                     if extra_burning_oil and big_hippo == 1:
00442                         extra_burning_oil -= 1
00443                         break
00444                     big_hippo -= 1
00445                     continue
00446                 except:
00447                     raise
00448             if count > 2 or big_hippo != 1:
00449                 self.fail("continue then break in try/except in loop broken!")
00450         test_inner()

Here is the call graph for this function:

Definition at line 871 of file py3_test_grammar.py.

00871 
00872     def test_with_statement(self):
00873         class manager(object):
00874             def __enter__(self):
00875                 return (1, 2)
00876             def __exit__(self, *args):
00877                 pass
00878 
00879         with manager():
00880             pass
00881         with manager() as x:
00882             pass
00883         with manager() as (x, y):
00884             pass
00885         with manager(), manager():
00886             pass
00887         with manager() as x, manager() as y:
00888             pass
00889         with manager() as x, manager():
00890             pass

Here is the call graph for this function:

Definition at line 644 of file py3_test_grammar.py.

00644 
00645     def testAdditiveOps(self):
00646         x = 1
00647         x = 1 + 1
00648         x = 1 - 1 - 1
00649         x = 1 - 1 + 1 - 1 + 1

Definition at line 496 of file py3_test_grammar.py.

00496 
00497     def testAssert(self):
00498         # assert_stmt: 'assert' test [',' test]
00499         assert 1
00500         assert 1, 1
00501         assert lambda x:x
00502         assert 1, lambda x:x+1
00503         try:
00504             assert 0, "msg"
00505         except AssertionError as e:
00506             self.assertEquals(e.args[0], "msg")
00507         else:
00508             if __debug__:
00509                 self.fail("AssertionError not raised by assert 0")

Here is the call graph for this function:

Definition at line 693 of file py3_test_grammar.py.

00693 
00694     def testAtoms(self):
00695         ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
00696         ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
00697 
00698         x = (1)
00699         x = (1 or 2 or 3)
00700         x = (1 or 2 or 3, 2, 3)
00701 
00702         x = []
00703         x = [1]
00704         x = [1 or 2 or 3]
00705         x = [1 or 2 or 3, 2, 3]
00706         x = []
00707 
00708         x = {}
00709         x = {'one': 1}
00710         x = {'one': 1,}
00711         x = {'one' or 'two': 1 or 2}
00712         x = {'one': 1, 'two': 2}
00713         x = {'one': 1, 'two': 2,}
00714         x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
00715 
00716         x = {'one'}
00717         x = {'one', 1,}
00718         x = {'one', 'two', 'three'}
00719         x = {2, 3, 4,}
00720 
00721         x = x
00722         x = 'x'
00723         x = 123

Definition at line 634 of file py3_test_grammar.py.

00634 
00635     def testBinaryMaskOps(self):
00636         x = 1 & 1
00637         x = 1 ^ 1
00638         x = 1 | 1

Definition at line 395 of file py3_test_grammar.py.

00395 
00396     def testBreakStmt(self):
00397         # 'break'
00398         while 1: break

exprlist: expr (',' expr)* [','] testlist: test (',' test)* [','] These have been exercised enough above

Definition at line 728 of file py3_test_grammar.py.

00728 
00729     def testClassdef(self):
00730         # 'class' NAME ['(' [testlist] ')'] ':' suite
00731         class B: pass
00732         class B2(): pass
00733         class C1(B): pass
00734         class C2(B): pass
00735         class D(C1, C2, B): pass
00736         class C:
00737             def meth1(self): pass
00738             def meth2(self, arg): pass
00739             def meth3(self, a1, a2): pass
00740 
00741         # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
00742         # decorators: decorator+
00743         # decorated: decorators (classdef | funcdef)
00744         def class_decorator(x): return x
00745         @class_decorator
00746         class G: pass

Definition at line 617 of file py3_test_grammar.py.

00617 
00618     def testComparison(self):
00619         ### comparison: expr (comp_op expr)*
00620         ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
00621         if 1: pass
00622         x = (1 == 1)
00623         if 1 == 1: pass
00624         if 1 != 1: pass
00625         if 1 < 1: pass
00626         if 1 > 1: pass
00627         if 1 <= 1: pass
00628         if 1 >= 1: pass
00629         if 1 is 1: pass
00630         if 1 is not 1: pass
00631         if 1 in (): pass
00632         if 1 not in (): pass
00633         if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass

Definition at line 852 of file py3_test_grammar.py.

00852 
00853     def testComprehensionSpecials(self):
00854         # test for outmost iterable precomputation
00855         x = 10; g = (i for i in range(x)); x = 5
00856         self.assertEqual(len(list(g)), 10)
00857 
00858         # This should hold, since we're only precomputing outmost iterable.
00859         x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
00860         x = 5; t = True;
00861         self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
00862 
00863         # Grammar allows multiple adjacent 'if's in listcomps and genexps,
00864         # even though it's silly. Make sure it works (ifelse broke this.)
00865         self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
00866         self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
00867 
00868         # verify unpacking single element tuples in listcomp/genexp.
00869         self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
00870         self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])

Here is the call graph for this function:

Definition at line 399 of file py3_test_grammar.py.

00399 
00400     def testContinueStmt(self):
00401         # 'continue'
00402         i = 1
00403         while i: i = 0; continue
00404 
00405         msg = ""
00406         while not msg:
00407             msg = "ok"
00408             try:
00409                 continue
00410                 msg = "continue failed to continue inside try"
00411             except:
00412                 msg = "continue inside try called except block"
00413         if msg != "ok":
00414             self.fail(msg)
00415 
00416         msg = ""
00417         while not msg:
00418             msg = "finally block not called"
00419             try:
00420                 continue
00421             finally:
00422                 msg = "ok"
00423         if msg != "ok":
00424             self.fail(msg)

Here is the call graph for this function:

Definition at line 379 of file py3_test_grammar.py.

00379 
00380     def testDelStmt(self):
00381         # 'del' exprlist
00382         abc = [1,2,3]
00383         x, y, z = abc
00384         xyz = x, y, z
00385 
00386         del abc
00387         del x, y, (z, xyz)

Definition at line 747 of file py3_test_grammar.py.

00747 
00748     def testDictcomps(self):
00749         # dictorsetmaker: ( (test ':' test (comp_for |
00750         #                                   (',' test ':' test)* [','])) |
00751         #                   (test (comp_for | (',' test)* [','])) )
00752         nums = [1, 2, 3]
00753         self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})

Here is the call graph for this function:

Definition at line 145 of file py3_test_grammar.py.

00145 
00146     def testEvalInput(self):
00147         # testlist ENDMARKER
00148         x = eval('1, 0 or 1')

small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt Tested below

Definition at line 366 of file py3_test_grammar.py.

00366 
00367     def testExprStmt(self):
00368         # (exprlist '=')* exprlist
00369         1
00370         1, 2, 3
00371         x = 1
00372         x = 1, 2, 3
00373         x = y = z = 1, 2, 3
00374         x, y, z = 1, 2, 3
00375         abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
00376 
00377         check_syntax_error(self, "x + 1 = 1")
00378         check_syntax_error(self, "a + 1 = b + 2")

Here is the call graph for this function:

Definition at line 541 of file py3_test_grammar.py.

00541 
00542     def testFor(self):
00543         # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
00544         for i in 1, 2, 3: pass
00545         for i, j, k in (): pass
00546         else: pass
00547         class Squares:
00548             def __init__(self, max):
00549                 self.max = max
00550                 self.sofar = []
00551             def __len__(self): return len(self.sofar)
00552             def __getitem__(self, i):
00553                 if not 0 <= i < self.max: raise IndexError
00554                 n = len(self.sofar)
00555                 while n <= i:
00556                     self.sofar.append(n*n)
00557                     n = n+1
00558                 return self.sofar[i]
00559         n = 0
00560         for x in Squares(10): n = n+x
00561         if n != 285:
00562             self.fail('for over growing sequence')
00563 
00564         result = []
00565         for x, in [(1,), (2,), (3,)]:
00566             result.append(x)
00567         self.assertEqual(result, [1, 2, 3])

Here is the call graph for this function:

Definition at line 149 of file py3_test_grammar.py.

00149 
00150     def testFuncdef(self):
00151         ### [decorators] 'def' NAME parameters ['->' test] ':' suite
00152         ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
00153         ### decorators: decorator+
00154         ### parameters: '(' [typedargslist] ')'
00155         ### typedargslist: ((tfpdef ['=' test] ',')*
00156         ###                ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
00157         ###                | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
00158         ### tfpdef: NAME [':' test]
00159         ### varargslist: ((vfpdef ['=' test] ',')*
00160         ###              ('*' [vfpdef] (',' vfpdef ['=' test])*  [',' '**' vfpdef] | '**' vfpdef)
00161         ###              | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
00162         ### vfpdef: NAME
00163         def f1(): pass
00164         f1()
00165         f1(*())
00166         f1(*(), **{})
00167         def f2(one_argument): pass
00168         def f3(two, arguments): pass
00169         self.assertEquals(f2.__code__.co_varnames, ('one_argument',))
00170         self.assertEquals(f3.__code__.co_varnames, ('two', 'arguments'))
00171         def a1(one_arg,): pass
00172         def a2(two, args,): pass
00173         def v0(*rest): pass
00174         def v1(a, *rest): pass
00175         def v2(a, b, *rest): pass
00176 
00177         f1()
00178         f2(1)
00179         f2(1,)
00180         f3(1, 2)
00181         f3(1, 2,)
00182         v0()
00183         v0(1)
00184         v0(1,)
00185         v0(1,2)
00186         v0(1,2,3,4,5,6,7,8,9,0)
00187         v1(1)
00188         v1(1,)
00189         v1(1,2)
00190         v1(1,2,3)
00191         v1(1,2,3,4,5,6,7,8,9,0)
00192         v2(1,2)
00193         v2(1,2,3)
00194         v2(1,2,3,4)
00195         v2(1,2,3,4,5,6,7,8,9,0)
00196 
00197         def d01(a=1): pass
00198         d01()
00199         d01(1)
00200         d01(*(1,))
00201         d01(**{'a':2})
00202         def d11(a, b=1): pass
00203         d11(1)
00204         d11(1, 2)
00205         d11(1, **{'b':2})
00206         def d21(a, b, c=1): pass
00207         d21(1, 2)
00208         d21(1, 2, 3)
00209         d21(*(1, 2, 3))
00210         d21(1, *(2, 3))
00211         d21(1, 2, *(3,))
00212         d21(1, 2, **{'c':3})
00213         def d02(a=1, b=2): pass
00214         d02()
00215         d02(1)
00216         d02(1, 2)
00217         d02(*(1, 2))
00218         d02(1, *(2,))
00219         d02(1, **{'b':2})
00220         d02(**{'a': 1, 'b': 2})
00221         def d12(a, b=1, c=2): pass
00222         d12(1)
00223         d12(1, 2)
00224         d12(1, 2, 3)
00225         def d22(a, b, c=1, d=2): pass
00226         d22(1, 2)
00227         d22(1, 2, 3)
00228         d22(1, 2, 3, 4)
00229         def d01v(a=1, *rest): pass
00230         d01v()
00231         d01v(1)
00232         d01v(1, 2)
00233         d01v(*(1, 2, 3, 4))
00234         d01v(*(1,))
00235         d01v(**{'a':2})
00236         def d11v(a, b=1, *rest): pass
00237         d11v(1)
00238         d11v(1, 2)
00239         d11v(1, 2, 3)
00240         def d21v(a, b, c=1, *rest): pass
00241         d21v(1, 2)
00242         d21v(1, 2, 3)
00243         d21v(1, 2, 3, 4)
00244         d21v(*(1, 2, 3, 4))
00245         d21v(1, 2, **{'c': 3})
00246         def d02v(a=1, b=2, *rest): pass
00247         d02v()
00248         d02v(1)
00249         d02v(1, 2)
00250         d02v(1, 2, 3)
00251         d02v(1, *(2, 3, 4))
00252         d02v(**{'a': 1, 'b': 2})
00253         def d12v(a, b=1, c=2, *rest): pass
00254         d12v(1)
00255         d12v(1, 2)
00256         d12v(1, 2, 3)
00257         d12v(1, 2, 3, 4)
00258         d12v(*(1, 2, 3, 4))
00259         d12v(1, 2, *(3, 4, 5))
00260         d12v(1, *(2,), **{'c': 3})
00261         def d22v(a, b, c=1, d=2, *rest): pass
00262         d22v(1, 2)
00263         d22v(1, 2, 3)
00264         d22v(1, 2, 3, 4)
00265         d22v(1, 2, 3, 4, 5)
00266         d22v(*(1, 2, 3, 4))
00267         d22v(1, 2, *(3, 4, 5))
00268         d22v(1, *(2, 3), **{'d': 4})
00269 
00270         # keyword argument type tests
00271         try:
00272             str('x', **{b'foo':1 })
00273         except TypeError:
00274             pass
00275         else:
00276             self.fail('Bytes should not work as keyword argument names')
00277         # keyword only argument tests
00278         def pos0key1(*, key): return key
00279         pos0key1(key=100)
00280         def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2
00281         pos2key2(1, 2, k1=100)
00282         pos2key2(1, 2, k1=100, k2=200)
00283         pos2key2(1, 2, k2=100, k1=200)
00284         def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg
00285         pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
00286         pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
00287 
00288         # keyword arguments after *arglist
00289         def f(*args, **kwargs):
00290             return args, kwargs
00291         self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
00292                                                     {'x':2, 'y':5}))
00293         self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
00294         self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
00295 
00296         # argument annotation tests
00297         def f(x) -> list: pass
00298         self.assertEquals(f.__annotations__, {'return': list})
00299         def f(x:int): pass
00300         self.assertEquals(f.__annotations__, {'x': int})
00301         def f(*x:str): pass
00302         self.assertEquals(f.__annotations__, {'x': str})
00303         def f(**x:float): pass
00304         self.assertEquals(f.__annotations__, {'x': float})
00305         def f(x, y:1+2): pass
00306         self.assertEquals(f.__annotations__, {'y': 3})
00307         def f(a, b:1, c:2, d): pass
00308         self.assertEquals(f.__annotations__, {'b': 1, 'c': 2})
00309         def f(a, b:1, c:2, d, e:3=4, f=5, *g:6): pass
00310         self.assertEquals(f.__annotations__,
00311                           {'b': 1, 'c': 2, 'e': 3, 'g': 6})
00312         def f(a, b:1, c:2, d, e:3=4, f=5, *g:6, h:7, i=8, j:9=10,
00313               **k:11) -> 12: pass
00314         self.assertEquals(f.__annotations__,
00315                           {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
00316                            'k': 11, 'return': 12})
00317         # Check for SF Bug #1697248 - mixing decorators and a return annotation
00318         def null(x): return x
00319         @null
00320         def f(x) -> list: pass
00321         self.assertEquals(f.__annotations__, {'return': list})
00322 
00323         # test MAKE_CLOSURE with a variety of oparg's
00324         closure = 1
00325         def f(): return closure
00326         def f(x=1): return closure
00327         def f(*, k=1): return closure
00328         def f() -> int: return closure
00329 
00330         # Check ast errors in *args and *kwargs
00331         check_syntax_error(self, "f(*g(1=2))")
00332         check_syntax_error(self, "f(**g(1=2))")

Here is the call graph for this function:

Definition at line 817 of file py3_test_grammar.py.

00817 
00818     def testGenexps(self):
00819         # generator expression tests
00820         g = ([x for x in range(10)] for x in range(1))
00821         self.assertEqual(next(g), [x for x in range(10)])
00822         try:
00823             next(g)
00824             self.fail('should produce StopIteration exception')
00825         except StopIteration:
00826             pass
00827 
00828         a = 1
00829         try:
00830             g = (a for d in a)
00831             next(g)
00832             self.fail('should produce TypeError')
00833         except TypeError:
00834             pass
00835 
00836         self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
00837         self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
00838 
00839         a = [x for x in range(10)]
00840         b = (x for x in (y for y in a))
00841         self.assertEqual(sum(b), sum([x for x in range(10)]))
00842 
00843         self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
00844         self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
00845         self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
00846         self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
00847         self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
00848         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)]))
00849         self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
00850         check_syntax_error(self, "foo(x for x in range(10), 100)")
00851         check_syntax_error(self, "foo(100, x for x in range(10))")

Here is the call graph for this function:

Definition at line 482 of file py3_test_grammar.py.

00482 
00483     def testGlobal(self):
00484         # 'global' NAME (',' NAME)*
00485         global a
00486         global a, b
00487         global one, two, three, four, five, six, seven, eight, nine, ten

compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef Tested below

Definition at line 513 of file py3_test_grammar.py.

00513 
00514     def testIf(self):
00515         # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
00516         if 1: pass
00517         if 1: pass
00518         else: pass
00519         if 0: pass
00520         elif 0: pass
00521         if 0: pass
00522         elif 0: pass
00523         elif 0: pass
00524         elif 0: pass
00525         else: pass

Definition at line 891 of file py3_test_grammar.py.

00891 
00892     def testIfElseExpr(self):
00893         # Test ifelse expressions in various cases
00894         def _checkeval(msg, ret):
00895             "helper to check that evaluation of expressions is done correctly"
00896             print(x)
00897             return ret
00898 
00899         # the next line is not allowed anymore
00900         #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
00901         self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
00902         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])
00903         self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
00904         self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
00905         self.assertEqual((5 and 6 if 0 else 1), 1)
00906         self.assertEqual(((5 and 6) if 0 else 1), 1)
00907         self.assertEqual((5 and (6 if 1 else 1)), 6)
00908         self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
00909         self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
00910         self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
00911         self.assertEqual((not 5 if 1 else 1), False)
00912         self.assertEqual((not 5 if 0 else 1), 1)
00913         self.assertEqual((6 + 1 if 1 else 2), 7)
00914         self.assertEqual((6 - 1 if 1 else 2), 5)
00915         self.assertEqual((6 * 2 if 1 else 4), 12)
00916         self.assertEqual((6 / 2 if 1 else 3), 3)
00917         self.assertEqual((6 < 4 if 0 else 2), 2)
00918 

Here is the call graph for this function:

Definition at line 469 of file py3_test_grammar.py.

00469 
00470     def testImport(self):
00471         # 'import' dotted_as_names
00472         import sys
00473         import time, sys
00474         # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
00475         from time import time
00476         from time import (time)
00477         # not testable inside a function, but already done at top of the module
00478         # from sys import *
00479         from sys import path, argv
00480         from sys import (path, argv)
00481         from sys import (path, argv,)

Definition at line 333 of file py3_test_grammar.py.

00333 
00334     def testLambdef(self):
00335         ### lambdef: 'lambda' [varargslist] ':' test
00336         l1 = lambda : 0
00337         self.assertEquals(l1(), 0)
00338         l2 = lambda : a[d] # XXX just testing the expression
00339         l3 = lambda : [2 < x for x in [-1, 3, 0]]
00340         self.assertEquals(l3(), [0, 1, 0])
00341         l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
00342         self.assertEquals(l4(), 1)
00343         l5 = lambda x, y, z=2: x + y + z
00344         self.assertEquals(l5(1, 2), 5)
00345         self.assertEquals(l5(1, 2, 3), 6)
00346         check_syntax_error(self, "lambda x: x = 2")
00347         check_syntax_error(self, "lambda (None,): None")
00348         l6 = lambda x, y, *, k=20: x+y+k
00349         self.assertEquals(l6(1,2), 1+2+20)
00350         self.assertEquals(l6(1,2,k=10), 1+2+10)
00351 

Here is the call graph for this function:

Definition at line 754 of file py3_test_grammar.py.

00754 
00755     def testListcomps(self):
00756         # list comprehension tests
00757         nums = [1, 2, 3, 4, 5]
00758         strs = ["Apple", "Banana", "Coconut"]
00759         spcs = ["  Apple", " Banana ", "Coco  nut  "]
00760 
00761         self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco  nut'])
00762         self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
00763         self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
00764         self.assertEqual([(i, s) for i in nums for s in strs],
00765                          [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
00766                           (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
00767                           (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
00768                           (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
00769                           (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
00770         self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
00771                          [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
00772                           (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
00773                           (5, 'Banana'), (5, 'Coconut')])
00774         self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
00775                          [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
00776 
00777         def test_in_func(l):
00778             return [0 < x < 3 for x in l if x > 2]
00779 
00780         self.assertEqual(test_in_func(nums), [False, False, False])
00781 
00782         def test_nested_front():
00783             self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
00784                              [[1, 2], [3, 4], [5, 6]])
00785 
00786         test_nested_front()
00787 
00788         check_syntax_error(self, "[i, s for i in nums for s in strs]")
00789         check_syntax_error(self, "[x if y]")
00790 
00791         suppliers = [
00792           (1, "Boeing"),
00793           (2, "Ford"),
00794           (3, "Macdonalds")
00795         ]
00796 
00797         parts = [
00798           (10, "Airliner"),
00799           (20, "Engine"),
00800           (30, "Cheeseburger")
00801         ]
00802 
00803         suppart = [
00804           (1, 10), (1, 20), (2, 20), (3, 30)
00805         ]
00806 
00807         x = [
00808           (sname, pname)
00809             for (sno, sname) in suppliers
00810               for (pno, pname) in parts
00811                 for (sp_sno, sp_pno) in suppart
00812                   if sno == sp_sno and pno == sp_pno
00813         ]
00814 
00815         self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
00816                              ('Macdonalds', 'Cheeseburger')])

Here is the call graph for this function:

Definition at line 650 of file py3_test_grammar.py.

00650 
00651     def testMultiplicativeOps(self):
00652         x = 1 * 1
00653         x = 1 / 1
00654         x = 1 % 1
00655         x = 1 / 1 * 1 % 1

Definition at line 488 of file py3_test_grammar.py.

00488 
00489     def testNonlocal(self):
00490         # 'nonlocal' NAME (',' NAME)*
00491         x = 0
00492         y = 0
00493         def f():
00494             nonlocal x
00495             nonlocal x, y

Definition at line 388 of file py3_test_grammar.py.

00388 
00389     def testPassStmt(self):
00390         # 'pass'
00391         pass

Definition at line 462 of file py3_test_grammar.py.

00462 
00463     def testRaise(self):
00464         # 'raise' test [',' test]
00465         try: raise RuntimeError('just testing')
00466         except RuntimeError: pass
00467         try: raise KeyboardInterrupt
00468         except KeyboardInterrupt: pass

Definition at line 451 of file py3_test_grammar.py.

00451 
00452     def testReturn(self):
00453         # 'return' [testlist]
00454         def g1(): return
00455         def g2(): return 1
00456         g1()
00457         x = g2()
00458         check_syntax_error(self, "class foo:return 1")

Here is the call graph for this function:

Definition at line 663 of file py3_test_grammar.py.

00663 
00664     def testSelectors(self):
00665         ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
00666         ### subscript: expr | [expr] ':' [expr]
00667 
00668         import sys, time
00669         c = sys.path[0]
00670         x = time.time()
00671         x = sys.modules['time'].time()
00672         a = '01234'
00673         c = a[0]
00674         c = a[-1]
00675         s = a[0:5]
00676         s = a[:5]
00677         s = a[0:]
00678         s = a[:]
00679         s = a[-5:]
00680         s = a[:-1]
00681         s = a[-4:-3]
00682         # A rough test of SF bug 1333982.  http://python.org/sf/1333982
00683         # The testing here is fairly incomplete.
00684         # Test cases should include: commas with 1 and 2 colons
00685         d = {}
00686         d[1] = 1
00687         d[1,] = 2
00688         d[1,2] = 3
00689         d[1,2,3] = 4
00690         L = list(d)
00691         L.sort(key=lambda x: x if isinstance(x, tuple) else ())
00692         self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')

Definition at line 639 of file py3_test_grammar.py.

00639 
00640     def testShiftOps(self):
00641         x = 1 << 1
00642         x = 1 >> 1
00643         x = 1 << 1 >> 1

stmt: simple_stmt | compound_stmt Tested below

Definition at line 355 of file py3_test_grammar.py.

00355 
00356     def testSimpleStmt(self):
00357         ### simple_stmt: small_stmt (';' small_stmt)* [';']
00358         x = 1; pass; del x
00359         def foo():
00360             # verify statements that end with semi-colons
00361             x = 1; pass; del x;
00362         foo()

Definition at line 591 of file py3_test_grammar.py.

00591 
00592     def testSuite(self):
00593         # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
00594         if 1: pass
00595         if 1:
00596             pass
00597         if 1:
00598             #
00599             #
00600             #
00601             pass
00602             pass
00603             #
00604             pass
00605             #

Definition at line 606 of file py3_test_grammar.py.

00606 
00607     def testTest(self):
00608         ### and_test ('or' and_test)*
00609         ### and_test: not_test ('and' not_test)*
00610         ### not_test: 'not' not_test | comparison
00611         if not 1: pass
00612         if 1 and 1: pass
00613         if 1 or 1: pass
00614         if not not not 1: pass
00615         if not 1 and 1 and 1: pass
00616         if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass

Definition at line 568 of file py3_test_grammar.py.

00568 
00569     def testTry(self):
00570         ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
00571         ###         | 'try' ':' suite 'finally' ':' suite
00572         ### except_clause: 'except' [expr ['as' expr]]
00573         try:
00574             1/0
00575         except ZeroDivisionError:
00576             pass
00577         else:
00578             pass
00579         try: 1/0
00580         except EOFError: pass
00581         except TypeError as msg: pass
00582         except RuntimeError as msg: pass
00583         except: pass
00584         else: pass
00585         try: 1/0
00586         except (EOFError, TypeError, ZeroDivisionError): pass
00587         try: 1/0
00588         except (EOFError, TypeError, ZeroDivisionError) as msg: pass
00589         try: pass
00590         finally: pass

Definition at line 656 of file py3_test_grammar.py.

00656 
00657     def testUnaryOps(self):
00658         x = +1
00659         x = -1
00660         x = ~1
00661         x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
00662         x = -1*1/1 + 1*1 - ---1*1

Definition at line 526 of file py3_test_grammar.py.

00526 
00527     def testWhile(self):
00528         # 'while' test ':' suite ['else' ':' suite]
00529         while 0: pass
00530         while 0: pass
00531         else: pass
00532 
00533         # Issue1920: "while 0" is optimized away,
00534         # ensure that the "else" clause is still present.
00535         x = 0
00536         while 0:
00537             x = 1
00538         else:
00539             x = 2
00540         self.assertEquals(x, 2)

Definition at line 459 of file py3_test_grammar.py.

00459 
00460     def testYield(self):
00461         check_syntax_error(self, "class foo:yield 1")

Here is the call graph for this function:


Member Data Documentation

Definition at line 548 of file py3_test_grammar.py.

Definition at line 549 of file py3_test_grammar.py.


The documentation for this class was generated from the following file: