Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_grammar.py

    r2 r391  
    22# This just tests whether the parser accepts them all.
    33
    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.test_support import run_unittest, check_syntax_error
     4from test.test_support import run_unittest, check_syntax_error, \
     5                              check_py3k_warnings
    126import unittest
    137import sys
     
    159from sys import *
    1610
     11
    1712class TokenTests(unittest.TestCase):
    1813
     
    2116        x = 1 \
    2217        + 1
    23         self.assertEquals(x, 2, 'backslash for line continuation')
     18        self.assertEqual(x, 2, 'backslash for line continuation')
    2419
    2520        # Backslash does not means continuation in comments :\
    2621        x = 0
    27         self.assertEquals(x, 0, 'backslash ending comment')
     22        self.assertEqual(x, 0, 'backslash ending comment')
    2823
    2924    def testPlainIntegers(self):
    30         self.assertEquals(0xff, 255)
    31         self.assertEquals(0377, 255)
    32         self.assertEquals(2147483647, 017777777777)
     25        self.assertEqual(0xff, 255)
     26        self.assertEqual(0377, 255)
     27        self.assertEqual(2147483647, 017777777777)
    3328        # "0x" is not a valid literal
    3429        self.assertRaises(SyntaxError, eval, "0x")
    3530        from sys import maxint
    3631        if maxint == 2147483647:
    37             self.assertEquals(-2147483647-1, -020000000000)
     32            self.assertEqual(-2147483647-1, -020000000000)
    3833            # XXX -2147483648
    39             self.assert_(037777777777 > 0)
    40             self.assert_(0xffffffff > 0)
     34            self.assertTrue(037777777777 > 0)
     35            self.assertTrue(0xffffffff > 0)
    4136            for s in '2147483648', '040000000000', '0x100000000':
    4237                try:
     
    4540                    self.fail("OverflowError on huge integer literal %r" % s)
    4641        elif maxint == 9223372036854775807:
    47             self.assertEquals(-9223372036854775807-1, -01000000000000000000000)
    48             self.assert_(01777777777777777777777 > 0)
    49             self.assert_(0xffffffffffffffff > 0)
     42            self.assertEqual(-9223372036854775807-1, -01000000000000000000000)
     43            self.assertTrue(01777777777777777777777 > 0)
     44            self.assertTrue(0xffffffffffffffff > 0)
    5045            for s in '9223372036854775808', '02000000000000000000000', \
    5146                     '0x10000000000000000':
     
    8277
    8378    def testStringLiterals(self):
    84         x = ''; y = ""; self.assert_(len(x) == 0 and x == y)
    85         x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39)
    86         x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34)
     79        x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
     80        x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
     81        x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)
    8782        x = "doesn't \"shrink\" does it"
    8883        y = 'doesn\'t "shrink" does it'
    89         self.assert_(len(x) == 24 and x == y)
     84        self.assertTrue(len(x) == 24 and x == y)
    9085        x = "does \"shrink\" doesn't it"
    9186        y = 'does "shrink" doesn\'t it'
    92         self.assert_(len(x) == 24 and x == y)
     87        self.assertTrue(len(x) == 24 and x == y)
    9388        x = """
    9489The "quick"
     
    9893"""
    9994        y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
    100         self.assertEquals(x, y)
     95        self.assertEqual(x, y)
    10196        y = '''
    10297The "quick"
     
    105100the 'lazy' dog.
    106101'''
    107         self.assertEquals(x, y)
     102        self.assertEqual(x, y)
    108103        y = "\n\
    109104The \"quick\"\n\
     
    112107the 'lazy' dog.\n\
    113108"
    114         self.assertEquals(x, y)
     109        self.assertEqual(x, y)
    115110        y = '\n\
    116111The \"quick\"\n\
     
    119114the \'lazy\' dog.\n\
    120115'
    121         self.assertEquals(x, y)
     116        self.assertEqual(x, y)
    122117
    123118
     
    153148        def f2(one_argument): pass
    154149        def f3(two, arguments): pass
    155         def f4(two, (compound, (argument, list))): pass
    156         def f5((compound, first), two): pass
    157         self.assertEquals(f2.func_code.co_varnames, ('one_argument',))
    158         self.assertEquals(f3.func_code.co_varnames, ('two', 'arguments'))
     150        # Silence Py3k warning
     151        exec('def f4(two, (compound, (argument, list))): pass')
     152        exec('def f5((compound, first), two): pass')
     153        self.assertEqual(f2.func_code.co_varnames, ('one_argument',))
     154        self.assertEqual(f3.func_code.co_varnames, ('two', 'arguments'))
    159155        if sys.platform.startswith('java'):
    160             self.assertEquals(f4.func_code.co_varnames,
     156            self.assertEqual(f4.func_code.co_varnames,
    161157                   ('two', '(compound, (argument, list))', 'compound', 'argument',
    162158                                'list',))
    163             self.assertEquals(f5.func_code.co_varnames,
     159            self.assertEqual(f5.func_code.co_varnames,
    164160                   ('(compound, first)', 'two', 'compound', 'first'))
    165161        else:
    166             self.assertEquals(f4.func_code.co_varnames,
     162            self.assertEqual(f4.func_code.co_varnames,
    167163                  ('two', '.1', 'compound', 'argument',  'list'))
    168             self.assertEquals(f5.func_code.co_varnames,
     164            self.assertEqual(f5.func_code.co_varnames,
    169165                  ('.0', 'two', 'compound', 'first'))
    170166        def a1(one_arg,): pass
     
    173169        def v1(a, *rest): pass
    174170        def v2(a, b, *rest): pass
    175         def v3(a, (b, c), *rest): return a, b, c, rest
     171        # Silence Py3k warning
     172        exec('def v3(a, (b, c), *rest): return a, b, c, rest')
    176173
    177174        f1()
     
    202199        # thus, the names nested inside tuples must appear after these names.
    203200        if sys.platform.startswith('java'):
    204             self.assertEquals(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))
     201            self.assertEqual(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))
    205202        else:
    206             self.assertEquals(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
    207         self.assertEquals(v3(1, (2, 3), 4), (1, 2, 3, (4,)))
     203            self.assertEqual(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
     204        self.assertEqual(v3(1, (2, 3), 4), (1, 2, 3, (4,)))
    208205        def d01(a=1): pass
    209206        d01()
     
    278275        d22v(1, 2, *(3, 4, 5))
    279276        d22v(1, *(2, 3), **{'d': 4})
    280         def d31v((x)): pass
     277        # Silence Py3k warning
     278        exec('def d31v((x)): pass')
     279        exec('def d32v((x,)): pass')
    281280        d31v(1)
    282         def d32v((x,)): pass
    283281        d32v((1,))
    284282
     
    286284        def f(*args, **kwargs):
    287285            return args, kwargs
    288         self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
     286        self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
    289287                                                    {'x':2, 'y':5}))
    290288        self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
     
    298296        ### lambdef: 'lambda' [varargslist] ':' test
    299297        l1 = lambda : 0
    300         self.assertEquals(l1(), 0)
     298        self.assertEqual(l1(), 0)
    301299        l2 = lambda : a[d] # XXX just testing the expression
    302300        l3 = lambda : [2 < x for x in [-1, 3, 0L]]
    303         self.assertEquals(l3(), [0, 1, 0])
     301        self.assertEqual(l3(), [0, 1, 0])
    304302        l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
    305         self.assertEquals(l4(), 1)
     303        self.assertEqual(l4(), 1)
    306304        l5 = lambda x, y, z=2: x + y + z
    307         self.assertEquals(l5(1, 2), 5)
    308         self.assertEquals(l5(1, 2, 3), 6)
     305        self.assertEqual(l5(1, 2), 5)
     306        self.assertEqual(l5(1, 2, 3), 6)
    309307        check_syntax_error(self, "lambda x: x = 2")
    310308        check_syntax_error(self, "lambda (None,): None")
     
    317315        x = 1; pass; del x
    318316        def foo():
    319             # verify statments that end with semi-colons
     317            # verify statements that end with semi-colons
    320318            x = 1; pass; del x;
    321319        foo()
     
    475473                except:
    476474                    raise
    477             if count > 2 or big_hippo <> 1:
     475            if count > 2 or big_hippo != 1:
    478476                self.fail("continue then break in try/except in loop broken!")
    479477        test_inner()
     
    537535        g = {}
    538536        exec 'z = 1' in g
    539         if g.has_key('__builtins__'): del g['__builtins__']
     537        if '__builtins__' in g: del g['__builtins__']
    540538        if g != {'z': 1}: self.fail('exec \'z = 1\' in g')
    541539        g = {}
    542540        l = {}
    543541
    544         import warnings
    545         warnings.filterwarnings("ignore", "global statement", module="<string>")
    546542        exec 'global a; a = 1; b = 2' in g, l
    547         if g.has_key('__builtins__'): del g['__builtins__']
    548         if l.has_key('__builtins__'): del l['__builtins__']
     543        if '__builtins__' in g: del g['__builtins__']
     544        if '__builtins__' in l: del l['__builtins__']
    549545        if (g, l) != ({'a':1}, {'b':2}):
    550546            self.fail('exec ... in g (%s), l (%s)' %(g,l))
    551547
    552548    def testAssert(self):
    553         # assert_stmt: 'assert' test [',' test]
     549        # assertTruestmt: 'assert' test [',' test]
    554550        assert 1
    555551        assert 1, 1
    556552        assert lambda x:x
    557553        assert 1, lambda x:x+1
     554
     555        try:
     556            assert True
     557        except AssertionError as e:
     558            self.fail("'assert True' should not have raised an AssertionError")
     559
     560        try:
     561            assert True, 'this should always pass'
     562        except AssertionError as e:
     563            self.fail("'assert True, msg' should not have "
     564                      "raised an AssertionError")
     565
     566    # these tests fail if python is run with -O, so check __debug__
     567    @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
     568    def testAssert2(self):
    558569        try:
    559570            assert 0, "msg"
    560571        except AssertionError, e:
    561             self.assertEquals(e.args[0], "msg")
     572            self.assertEqual(e.args[0], "msg")
    562573        else:
    563             if __debug__:
    564                 self.fail("AssertionError not raised by assert 0")
     574            self.fail("AssertionError not raised by assert 0")
     575
     576        try:
     577            assert False
     578        except AssertionError as e:
     579            self.assertEqual(len(e.args), 0)
     580        else:
     581            self.fail("AssertionError not raised by 'assert False'")
     582
    565583
    566584    ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
     
    593611        else:
    594612            x = 2
    595         self.assertEquals(x, 2)
     613        self.assertEqual(x, 2)
    596614
    597615    def testFor(self):
     
    678696        if 1 == 1: pass
    679697        if 1 != 1: pass
    680         if 1 <> 1: pass
    681698        if 1 < 1: pass
    682699        if 1 > 1: pass
     
    687704        if 1 in (): pass
    688705        if 1 not in (): pass
    689         if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
     706        if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
     707        # Silence Py3k warning
     708        if eval('1 <> 1'): pass
     709        if eval('1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1'): pass
    690710
    691711    def testBinaryMaskOps(self):
     
    746766        L = list(d)
    747767        L.sort()
    748         self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
     768        self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
    749769
    750770    def testAtoms(self):
    751771        ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
    752         ### dictmaker: test ':' test (',' test ':' test)* [',']
     772        ### dictorsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
    753773
    754774        x = (1)
     
    770790        x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
    771791
    772         x = `x`
    773         x = `1 or 2 or 3`
    774         self.assertEqual(`1,2`, '(1, 2)')
     792        x = {'one'}
     793        x = {'one', 1,}
     794        x = {'one', 'two', 'three'}
     795        x = {2, 3, 4,}
     796
     797        # Silence Py3k warning
     798        x = eval('`x`')
     799        x = eval('`1 or 2 or 3`')
     800        self.assertEqual(eval('`1,2`'), '(1, 2)')
    775801
    776802        x = x
     
    804830        self.assertEqual(G.decorated, True)
    805831
     832    def testDictcomps(self):
     833        # dictorsetmaker: ( (test ':' test (comp_for |
     834        #                                   (',' test ':' test)* [','])) |
     835        #                   (test (comp_for | (',' test)* [','])) )
     836        nums = [1, 2, 3]
     837        self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
     838
    806839    def testListcomps(self):
    807840        # list comprehension tests
     
    920953        self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
    921954        self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
     955
     956    def test_with_statement(self):
     957        class manager(object):
     958            def __enter__(self):
     959                return (1, 2)
     960            def __exit__(self, *args):
     961                pass
     962
     963        with manager():
     964            pass
     965        with manager() as x:
     966            pass
     967        with manager() as (x, y):
     968            pass
     969        with manager(), manager():
     970            pass
     971        with manager() as x, manager() as y:
     972            pass
     973        with manager() as x, manager():
     974            pass
    922975
    923976    def testIfElseExpr(self):
     
    9471000        self.assertEqual((6 < 4 if 0 else 2), 2)
    9481001
     1002    def test_paren_evaluation(self):
     1003        self.assertEqual(16 // (4 // 2), 8)
     1004        self.assertEqual((16 // 4) // 2, 2)
     1005        self.assertEqual(16 // 4 // 2, 2)
     1006        self.assertTrue(False is (2 is 3))
     1007        self.assertFalse((False is 2) is 3)
     1008        self.assertFalse(False is 2 is 3)
     1009
    9491010
    9501011def test_main():
    951     run_unittest(TokenTests, GrammarTests)
     1012    with check_py3k_warnings(
     1013            ("backquote not supported", SyntaxWarning),
     1014            ("tuple parameter unpacking has been removed", SyntaxWarning),
     1015            ("parenthesized argument names are invalid", SyntaxWarning),
     1016            ("classic int division", DeprecationWarning),
     1017            (".+ not supported in 3.x", DeprecationWarning)):
     1018        run_unittest(TokenTests, GrammarTests)
    9521019
    9531020if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.