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_compiler.py

    r2 r391  
    2424
    2525        next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
    26         libdir = os.path.dirname(unittest.__file__)
     26        # warning: if 'os' or 'test_support' are moved in some other dir,
     27        # they should be changed here.
     28        libdir = os.path.dirname(os.__file__)
    2729        testdir = os.path.dirname(test.test_support.__file__)
    2830
    29         for dir in [libdir, testdir]:
    30             for basename in os.listdir(dir):
     31        for dir in [testdir]:
     32            for basename in "test_os.py",:
    3133                # Print still working message since this test can be really slow
    3234                if next_time <= time.time():
     
    7678    def testTryExceptFinally(self):
    7779        # Test that except and finally clauses in one try stmt are recognized
    78         c = compiler.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1",
     80        c = compiler.compile("try:\n 1//0\nexcept:\n e = 1\nfinally:\n f = 1",
    7981                             "<string>", "exec")
    8082        dct = {}
    8183        exec c in dct
    82         self.assertEquals(dct.get('e'), 1)
    83         self.assertEquals(dct.get('f'), 1)
     84        self.assertEqual(dct.get('e'), 1)
     85        self.assertEqual(dct.get('f'), 1)
    8486
    8587    def testDefaultArgs(self):
     
    8890    def testDocstrings(self):
    8991        c = compiler.compile('"doc"', '<string>', 'exec')
    90         self.assert_('__doc__' in c.co_names)
     92        self.assertIn('__doc__', c.co_names)
    9193        c = compiler.compile('def f():\n "doc"', '<string>', 'exec')
    9294        g = {}
    9395        exec c in g
    94         self.assertEquals(g['f'].__doc__, "doc")
     96        self.assertEqual(g['f'].__doc__, "doc")
    9597
    9698    def testLineNo(self):
     
    111113    def _check_lineno(self, node):
    112114        if not node.__class__ in NOLINENO:
    113             self.assert_(isinstance(node.lineno, int),
     115            self.assertIsInstance(node.lineno, int,
    114116                "lineno=%s on %s" % (node.lineno, node.__class__))
    115             self.assert_(node.lineno > 0,
     117            self.assertTrue(node.lineno > 0,
    116118                "lineno=%s on %s" % (node.lineno, node.__class__))
    117119        for child in node.getChildNodes():
     
    119121
    120122    def testFlatten(self):
    121         self.assertEquals(flatten([1, [2]]), [1, 2])
    122         self.assertEquals(flatten((1, (2,))), [1, 2])
     123        self.assertEqual(flatten([1, [2]]), [1, 2])
     124        self.assertEqual(flatten((1, (2,))), [1, 2])
    123125
    124126    def testNestedScope(self):
     
    132134        dct = {}
    133135        exec c in dct
    134         self.assertEquals(dct.get('result'), 3)
     136        self.assertEqual(dct.get('result'), 3)
    135137
    136138    def testGenExp(self):
     
    139141                             '<string>',
    140142                             'eval')
    141         self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)])
     143        self.assertEqual(eval(c), [(0, 3), (1, 3), (2, 3)])
     144
     145    def testSetLiteral(self):
     146        c = compiler.compile('{1, 2, 3}', '<string>', 'eval')
     147        self.assertEqual(eval(c), {1,2,3})
     148        c = compiler.compile('{1, 2, 3,}', '<string>', 'eval')
     149        self.assertEqual(eval(c), {1,2,3})
     150
     151    def testDictLiteral(self):
     152        c = compiler.compile('{1:2, 2:3, 3:4}', '<string>', 'eval')
     153        self.assertEqual(eval(c), {1:2, 2:3, 3:4})
     154        c = compiler.compile('{1:2, 2:3, 3:4,}', '<string>', 'eval')
     155        self.assertEqual(eval(c), {1:2, 2:3, 3:4})
     156
     157    def testSetComp(self):
     158        c = compiler.compile('{x for x in range(1, 4)}', '<string>', 'eval')
     159        self.assertEqual(eval(c), {1, 2, 3})
     160        c = compiler.compile('{x * y for x in range(3) if x != 0'
     161                             '       for y in range(4) if y != 0}',
     162                             '<string>',
     163                             'eval')
     164        self.assertEqual(eval(c), {1, 2, 3, 4, 6})
     165
     166    def testDictComp(self):
     167        c = compiler.compile('{x:x+1 for x in range(1, 4)}', '<string>', 'eval')
     168        self.assertEqual(eval(c), {1:2, 2:3, 3:4})
     169        c = compiler.compile('{(x, y) : y for x in range(2) if x != 0'
     170                             '            for y in range(3) if y != 0}',
     171                             '<string>',
     172                             'eval')
     173        self.assertEqual(eval(c), {(1, 2): 2, (1, 1): 1})
    142174
    143175    def testWith(self):
     
    152184        dct = {'TrivialContext': TrivialContext}
    153185        exec c in dct
    154         self.assertEquals(dct.get('result'), 1)
     186        self.assertEqual(dct.get('result'), 1)
    155187
    156188    def testWithAss(self):
     
    164196        dct = {'TrivialContext': TrivialContext}
    165197        exec c in dct
    166         self.assertEquals(dct.get('result'), 1)
    167 
     198        self.assertEqual(dct.get('result'), 1)
     199
     200    def testWithMult(self):
     201        events = []
     202        class Ctx:
     203            def __init__(self, n):
     204                self.n = n
     205            def __enter__(self):
     206                events.append(self.n)
     207            def __exit__(self, *args):
     208                pass
     209        c = compiler.compile('from __future__ import with_statement\n'
     210                             'def f():\n'
     211                             '    with Ctx(1) as tc, Ctx(2) as tc2:\n'
     212                             '        return 1\n'
     213                             'result = f()',
     214                             '<string>',
     215                             'exec' )
     216        dct = {'Ctx': Ctx}
     217        exec c in dct
     218        self.assertEqual(dct.get('result'), 1)
     219        self.assertEqual(events, [1, 2])
     220
     221    def testGlobal(self):
     222        code = compiler.compile('global x\nx=1', '<string>', 'exec')
     223        d1 = {'__builtins__': {}}
     224        d2 = {}
     225        exec code in d1, d2
     226        # x should be in the globals dict
     227        self.assertEqual(d1.get('x'), 1)
    168228
    169229    def testPrintFunction(self):
     
    175235        dct = {'output': StringIO()}
    176236        exec c in dct
    177         self.assertEquals(dct['output'].getvalue(), 'a**b++')
     237        self.assertEqual(dct['output'].getvalue(), 'a**b++')
    178238
    179239    def _testErrEnc(self, src, text, offset):
     
    181241            compile(src, "", "exec")
    182242        except SyntaxError, e:
    183             self.assertEquals(e.offset, offset)
    184             self.assertEquals(e.text, text)
     243            self.assertEqual(e.offset, offset)
     244            self.assertEqual(e.text, text)
    185245
    186246    def testSourceCodeEncodingsError(self):
     
    221281d = {'a': 2}
    222282d = {}
     283d = {x: y for x, y in zip(range(5), range(5,10))}
     284s = {x for x in range(10)}
     285s = {1}
    223286t = ()
    224287t = (1, 2)
     
    248311def test_main():
    249312    global TEST_ALL
    250     TEST_ALL = test.test_support.is_resource_enabled("compiler")
     313    TEST_ALL = test.test_support.is_resource_enabled("cpu")
    251314    test.test_support.run_unittest(CompilerTest)
    252315
Note: See TracChangeset for help on using the changeset viewer.