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

    r2 r391  
    33import _ast
    44from test import test_support
     5import textwrap
    56
    67class TestSpecifics(unittest.TestCase):
     8
     9    def test_no_ending_newline(self):
     10        compile("hi", "<test>", "exec")
     11        compile("hi\r", "<test>", "exec")
     12
     13    def test_empty(self):
     14        compile("", "<test>", "exec")
     15
     16    def test_other_newlines(self):
     17        compile("\r\n", "<test>", "exec")
     18        compile("\r", "<test>", "exec")
     19        compile("hi\r\nstuff\r\ndef f():\n    pass\r", "<test>", "exec")
     20        compile("this_is\rreally_old_mac\rdef f():\n    pass", "<test>", "exec")
    721
    822    def test_debug_assignment(self):
     
    4761        except SyntaxError:
    4862            pass
     63
     64    def test_exec_functional_style(self):
     65        # Exec'ing a tuple of length 2 works.
     66        g = {'b': 2}
     67        exec("a = b + 1", g)
     68        self.assertEqual(g['a'], 3)
     69
     70        # As does exec'ing a tuple of length 3.
     71        l = {'b': 3}
     72        g = {'b': 5, 'c': 7}
     73        exec("a = b + c", g, l)
     74        self.assertNotIn('a', g)
     75        self.assertEqual(l['a'], 10)
     76
     77        # Tuples not of length 2 or 3 are invalid.
     78        with self.assertRaises(TypeError):
     79            exec("a = b + 1",)
     80
     81        with self.assertRaises(TypeError):
     82            exec("a = b + 1", {}, {}, {})
     83
     84        # Can't mix and match the two calling forms.
     85        g = {'a': 3, 'b': 4}
     86        l = {}
     87        with self.assertRaises(TypeError):
     88            exec("a = b + 1", g) in g
     89        with self.assertRaises(TypeError):
     90            exec("a = b + 1", g, l) in g, l
    4991
    5092    def test_exec_with_general_mapping_for_locals(self):
     
    130172    def test_complex_args(self):
    131173
     174        with test_support.check_py3k_warnings(
     175                ("tuple parameter unpacking has been removed", SyntaxWarning)):
     176            exec textwrap.dedent('''
    132177        def comp_args((a, b)):
    133178            return a,b
     
    147192        self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3))
    148193        self.assertEqual(comp_args(), (2, 3, 4))
     194        ''')
    149195
    150196    def test_argument_order(self):
     
    235281        else:
    236282            self.fail("How many bits *does* this machine have???")
    237         # Verify treatment of contant folding on -(sys.maxint+1)
     283        # Verify treatment of constant folding on -(sys.maxint+1)
    238284        # i.e. -2147483648 on 32 bit platforms.  Should return int, not long.
    239         self.assertTrue(isinstance(eval("%s" % (-sys.maxint - 1)), int))
    240         self.assertTrue(isinstance(eval("%s" % (-sys.maxint - 2)), long))
     285        self.assertIsInstance(eval("%s" % (-sys.maxint - 1)), int)
     286        self.assertIsInstance(eval("%s" % (-sys.maxint - 2)), long)
    241287
    242288    if sys.maxint == 9223372036854775807:
     
    253299            for variable in self.test_32_63_bit_values.func_code.co_consts:
    254300                if variable is not None:
    255                     self.assertTrue(isinstance(variable, int))
     301                    self.assertIsInstance(variable, int)
    256302
    257303    def test_sequence_unpacking_error(self):
     
    271317            'for None in range(10): pass',
    272318            'def f(None): pass',
     319            'import None',
     320            'import x as None',
     321            'from x import None',
     322            'from x import y as None'
    273323        ]
    274324        for stmt in stmts:
     
    276326            self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
    277327            self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
     328        # This is ok.
     329        compile("from None import x", "tmp", "exec")
     330        compile("from x import None as y", "tmp", "exec")
     331        compile("import None as x", "tmp", "exec")
    278332
    279333    def test_import(self):
     
    333387        self.assertNotEqual(id(f1.func_code), id(f2.func_code))
    334388
     389    def test_lambda_doc(self):
     390        l = lambda: "foo"
     391        self.assertIsNone(l.__doc__)
     392
    335393    def test_unicode_encoding(self):
    336394        code = u"# -*- coding: utf-8 -*-\npass\n"
     
    358416        self.assertEqual(d[1], 2)
    359417        del d[1]
    360         self.assertEqual(1 in d, False)
     418        self.assertNotIn(1, d)
    361419        # Tuple of indices
    362420        d[1, 1] = 1
     
    365423        self.assertEqual(d[1, 1], 2)
    366424        del d[1, 1]
    367         self.assertEqual((1, 1) in d, False)
     425        self.assertNotIn((1, 1), d)
    368426        # Simple slice
    369427        d[1:2] = 1
     
    372430        self.assertEqual(d[1:2], 2)
    373431        del d[1:2]
    374         self.assertEqual(slice(1, 2) in d, False)
     432        self.assertNotIn(slice(1, 2), d)
    375433        # Tuple of simple slices
    376434        d[1:2, 1:2] = 1
     
    379437        self.assertEqual(d[1:2, 1:2], 2)
    380438        del d[1:2, 1:2]
    381         self.assertEqual((slice(1, 2), slice(1, 2)) in d, False)
     439        self.assertNotIn((slice(1, 2), slice(1, 2)), d)
    382440        # Extended slice
    383441        d[1:2:3] = 1
     
    386444        self.assertEqual(d[1:2:3], 2)
    387445        del d[1:2:3]
    388         self.assertEqual(slice(1, 2, 3) in d, False)
     446        self.assertNotIn(slice(1, 2, 3), d)
    389447        # Tuple of extended slices
    390448        d[1:2:3, 1:2:3] = 1
     
    393451        self.assertEqual(d[1:2:3, 1:2:3], 2)
    394452        del d[1:2:3, 1:2:3]
    395         self.assertEqual((slice(1, 2, 3), slice(1, 2, 3)) in d, False)
     453        self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d)
    396454        # Ellipsis
    397455        d[...] = 1
     
    400458        self.assertEqual(d[...], 2)
    401459        del d[...]
    402         self.assertEqual(Ellipsis in d, False)
     460        self.assertNotIn(Ellipsis, d)
    403461        # Tuple of Ellipses
    404462        d[..., ...] = 1
     
    407465        self.assertEqual(d[..., ...], 2)
    408466        del d[..., ...]
    409         self.assertEqual((Ellipsis, Ellipsis) in d, False)
     467        self.assertNotIn((Ellipsis, Ellipsis), d)
    410468
    411469    def test_mangling(self):
     
    417475                import __package__.module
    418476
    419         self.assert_("_A__mangled" in A.f.func_code.co_varnames)
    420         self.assert_("__not_mangled__" in A.f.func_code.co_varnames)
    421         self.assert_("_A__mangled_mod" in A.f.func_code.co_varnames)
    422         self.assert_("__package__" in A.f.func_code.co_varnames)
     477        self.assertIn("_A__mangled", A.f.func_code.co_varnames)
     478        self.assertIn("__not_mangled__", A.f.func_code.co_varnames)
     479        self.assertIn("_A__mangled_mod", A.f.func_code.co_varnames)
     480        self.assertIn("__package__", A.f.func_code.co_varnames)
    423481
    424482    def test_compile_ast(self):
     
    443501            co1 = compile(code, '%s1' % fname, 'exec')
    444502            ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
    445             self.assert_(type(ast) == _ast.Module)
     503            self.assertTrue(type(ast) == _ast.Module)
    446504            co2 = compile(ast, '%s3' % fname, 'exec')
    447505            self.assertEqual(co1, co2)
Note: See TracChangeset for help on using the changeset viewer.