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

    r2 r391  
    11from test import test_support, seq_tests
     2
     3import gc
    24
    35class TupleTest(seq_tests.CommonTest):
     
    57
    68    def test_constructors(self):
    7         super(TupleTest, self).test_len()
     9        super(TupleTest, self).test_constructors()
    810        # calling built-in types without argument must return empty
    911        self.assertEqual(tuple(), ())
    1012        t0_3 = (0, 1, 2, 3)
    1113        t0_3_bis = tuple(t0_3)
    12         self.assert_(t0_3 is t0_3_bis)
     14        self.assertTrue(t0_3 is t0_3_bis)
    1315        self.assertEqual(tuple([]), ())
    1416        self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
     
    1820    def test_truth(self):
    1921        super(TupleTest, self).test_truth()
    20         self.assert_(not ())
    21         self.assert_((42, ))
     22        self.assertTrue(not ())
     23        self.assertTrue((42, ))
    2224
    2325    def test_len(self):
     
    3234        u2 = u
    3335        u += (2, 3)
    34         self.assert_(u is not u2)
     36        self.assertTrue(u is not u2)
    3537
    3638    def test_imul(self):
     
    3941        u2 = u
    4042        u *= 3
    41         self.assert_(u is not u2)
     43        self.assertTrue(u is not u2)
    4244
    4345    def test_tupleresizebug(self):
     
    7072                     [(i, j) for i in xp for j in base] + xp + zip(base)
    7173        collisions = len(inps) - len(set(map(hash, inps)))
    72         self.assert_(collisions <= 15)
     74        self.assertTrue(collisions <= 15)
    7375
    7476    def test_repr(self):
     
    8385        self.assertEqual(repr(a2), "(0, 1, 2)")
    8486
     87    def _not_tracked(self, t):
     88        # Nested tuples can take several collections to untrack
     89        gc.collect()
     90        gc.collect()
     91        self.assertFalse(gc.is_tracked(t), t)
     92
     93    def _tracked(self, t):
     94        self.assertTrue(gc.is_tracked(t), t)
     95        gc.collect()
     96        gc.collect()
     97        self.assertTrue(gc.is_tracked(t), t)
     98
     99    @test_support.cpython_only
     100    def test_track_literals(self):
     101        # Test GC-optimization of tuple literals
     102        x, y, z = 1.5, "a", []
     103
     104        self._not_tracked(())
     105        self._not_tracked((1,))
     106        self._not_tracked((1, 2))
     107        self._not_tracked((1, 2, "a"))
     108        self._not_tracked((1, 2, (None, True, False, ()), int))
     109        self._not_tracked((object(),))
     110        self._not_tracked(((1, x), y, (2, 3)))
     111
     112        # Tuples with mutable elements are always tracked, even if those
     113        # elements are not tracked right now.
     114        self._tracked(([],))
     115        self._tracked(([1],))
     116        self._tracked(({},))
     117        self._tracked((set(),))
     118        self._tracked((x, y, z))
     119
     120    def check_track_dynamic(self, tp, always_track):
     121        x, y, z = 1.5, "a", []
     122
     123        check = self._tracked if always_track else self._not_tracked
     124        check(tp())
     125        check(tp([]))
     126        check(tp(set()))
     127        check(tp([1, x, y]))
     128        check(tp(obj for obj in [1, x, y]))
     129        check(tp(set([1, x, y])))
     130        check(tp(tuple([obj]) for obj in [1, x, y]))
     131        check(tuple(tp([obj]) for obj in [1, x, y]))
     132
     133        self._tracked(tp([z]))
     134        self._tracked(tp([[x, y]]))
     135        self._tracked(tp([{x: y}]))
     136        self._tracked(tp(obj for obj in [x, y, z]))
     137        self._tracked(tp(tuple([obj]) for obj in [x, y, z]))
     138        self._tracked(tuple(tp([obj]) for obj in [x, y, z]))
     139
     140    @test_support.cpython_only
     141    def test_track_dynamic(self):
     142        # Test GC-optimization of dynamically constructed tuples.
     143        self.check_track_dynamic(tuple, False)
     144
     145    @test_support.cpython_only
     146    def test_track_subtypes(self):
     147        # Tuple subtypes must always be tracked
     148        class MyTuple(tuple):
     149            pass
     150        self.check_track_dynamic(MyTuple, True)
     151
     152    @test_support.cpython_only
     153    def test_bug7466(self):
     154        # Trying to untrack an unfinished tuple could crash Python
     155        self._not_tracked(tuple(gc.collect() for i in range(101)))
     156
    85157def test_main():
    86158    test_support.run_unittest(TupleTest)
Note: See TracChangeset for help on using the changeset viewer.