def test_map_put_discard_right_refcnts(self): a = 4200 s = PyObjectMap() old_ref_cnt = sys.getrefcount(a) s[a] = a self.assertEqual(sys.getrefcount(a), old_ref_cnt + 2, msg="first add") s[a] = a # shouldn't do anything self.assertEqual(sys.getrefcount(a), old_ref_cnt + 2, msg="second add") s.discard(a) self.assertEqual(sys.getrefcount(a), old_ref_cnt, msg="discard")
def test_discard_with_equivalent_object(self): a, b = float("nan"), float("nan") self.assertTrue(a is not b) #prerequisite old_ref_cnt_a = sys.getrefcount(a) old_ref_cnt_b = sys.getrefcount(b) s = PyObjectMap() s[a] = None s[b] = None self.assertEqual(sys.getrefcount(a), old_ref_cnt_a + 1) self.assertEqual(sys.getrefcount(b), old_ref_cnt_b) self.assertTrue(b in s) self.assertTrue(a in s) self.assertEqual(sys.getrefcount(a), old_ref_cnt_a + 1) self.assertEqual(sys.getrefcount(b), old_ref_cnt_b) s.discard(b) self.assertEqual(sys.getrefcount(a), old_ref_cnt_a) self.assertEqual(sys.getrefcount(b), old_ref_cnt_b) s.discard(b) self.assertEqual(sys.getrefcount(a), old_ref_cnt_a) self.assertEqual(sys.getrefcount(b), old_ref_cnt_b) s.discard(a) self.assertEqual(sys.getrefcount(a), old_ref_cnt_a) self.assertEqual(sys.getrefcount(b), old_ref_cnt_b) s[b] = None self.assertEqual(sys.getrefcount(a), old_ref_cnt_a) self.assertEqual(sys.getrefcount(b), old_ref_cnt_b + 1)