Example #1
0
    def test_list_weakref(self):
        # Create one ImmutableList
        self.assertEqual(ImmutableList._get_instance_count(), 0)
        d = ImmutableList([1, 2])
        self.assertEqual(ImmutableList._get_instance_count(), 1)

        # Store in a weakref.WeakValueListionary
        wvd = weakref.WeakValueDictionary()
        wvd[1] = d
        self.assertEqual(ImmutableList._get_instance_count(), 1)
        self.assertEqual(len(wvd), 1)
        self.assertTrue(wvd[1] is d)

        # Release the only reference to the ImmutableList
        d = None
        self.assertEqual(ImmutableList._get_instance_count(), 0)
        # Check that it is no longer in wkd
        self.assertEqual(len(wvd), 0)
        with self.assertRaises(KeyError):
            wvd[1]

        # Create a new ImmutableList, check that it not magically appears in
        # wkd
        d = ImmutableList([1, 2])
        self.assertEqual(ImmutableList._get_instance_count(), 1)
        self.assertEqual(len(wvd), 0)
        with self.assertRaises(KeyError):
            wvd[1]
def immutables_count():
    return (ImmutableDict._get_instance_count() +
            ImmutableList._get_instance_count())