diff options
-rw-r--r-- | Lib/test/test_weakref.py | 127 |
1 files changed, 62 insertions, 65 deletions
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 551d95c..cccb515 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -97,11 +97,9 @@ class ReferencesTestCase(TestBase): ref1 = weakref.ref(o, self.callback) ref2 = weakref.ref(o, self.callback) del o - self.assertTrue(ref1() is None, - "expected reference to be invalidated") - self.assertTrue(ref2() is None, - "expected reference to be invalidated") - self.assertTrue(self.cbcalled == 2, + self.assertIsNone(ref1(), "expected reference to be invalidated") + self.assertIsNone(ref2(), "expected reference to be invalidated") + self.assertEqual(self.cbcalled, 2, "callback not called the right number of times") def test_multiple_selfref_callbacks(self): @@ -140,10 +138,10 @@ class ReferencesTestCase(TestBase): def check_basic_ref(self, factory): o = factory() ref = weakref.ref(o) - self.assertTrue(ref() is not None, + self.assertIsNotNone(ref(), "weak reference to live object should be live") o2 = ref() - self.assertTrue(o is o2, + self.assertIs(o, o2, "<ref>() should return original object if live") def check_basic_callback(self, factory): @@ -151,9 +149,9 @@ class ReferencesTestCase(TestBase): o = factory() ref = weakref.ref(o, self.callback) del o - self.assertTrue(self.cbcalled == 1, + self.assertEqual(self.cbcalled, 1, "callback did not properly set 'cbcalled'") - self.assertTrue(ref() is None, + self.assertIsNone(ref(), "ref2 should be dead after deleting object reference") def test_ref_reuse(self): @@ -163,19 +161,19 @@ class ReferencesTestCase(TestBase): # between these two; it should make no difference proxy = weakref.proxy(o) ref2 = weakref.ref(o) - self.assertTrue(ref1 is ref2, + self.assertIs(ref1, ref2, "reference object w/out callback should be re-used") o = C() proxy = weakref.proxy(o) ref1 = weakref.ref(o) ref2 = weakref.ref(o) - self.assertTrue(ref1 is ref2, + self.assertIs(ref1, ref2, "reference object w/out callback should be re-used") - self.assertTrue(weakref.getweakrefcount(o) == 2, + self.assertEqual(weakref.getweakrefcount(o), 2, "wrong weak ref count for object") del proxy - self.assertTrue(weakref.getweakrefcount(o) == 1, + self.assertEqual(weakref.getweakrefcount(o), 1, "wrong weak ref count for object after deleting proxy") def test_proxy_reuse(self): @@ -183,7 +181,7 @@ class ReferencesTestCase(TestBase): proxy1 = weakref.proxy(o) ref = weakref.ref(o) proxy2 = weakref.proxy(o) - self.assertTrue(proxy1 is proxy2, + self.assertIs(proxy1, proxy2, "proxy object w/out callback should have been re-used") def test_basic_proxy(self): @@ -263,19 +261,19 @@ class ReferencesTestCase(TestBase): o = Object(1) p1 = makeref(o, None) p2 = makeref(o, None) - self.assertTrue(p1 is p2, "both callbacks were None in the C API") + self.assertIs(p1, p2, "both callbacks were None in the C API") del p1, p2 p1 = makeref(o) p2 = makeref(o, None) - self.assertTrue(p1 is p2, "callbacks were NULL, None in the C API") + self.assertIs(p1, p2, "callbacks were NULL, None in the C API") del p1, p2 p1 = makeref(o) p2 = makeref(o) - self.assertTrue(p1 is p2, "both callbacks were NULL in the C API") + self.assertIs(p1, p2, "both callbacks were NULL in the C API") del p1, p2 p1 = makeref(o, None) p2 = makeref(o) - self.assertTrue(p1 is p2, "callbacks were None, NULL in the C API") + self.assertIs(p1, p2, "callbacks were None, NULL in the C API") def test_callable_proxy(self): o = Callable() @@ -283,13 +281,13 @@ class ReferencesTestCase(TestBase): self.check_proxy(o, ref1) - self.assertTrue(type(ref1) is weakref.CallableProxyType, + self.assertIs(type(ref1), weakref.CallableProxyType, "proxy is not of callable type") ref1('twinkies!') - self.assertTrue(o.bar == 'twinkies!', + self.assertEqual(o.bar, 'twinkies!', "call through proxy not passed through to original") ref1(x='Splat.') - self.assertTrue(o.bar == 'Splat.', + self.assertEqual(o.bar, 'Splat.', "call through proxy not passed through to original") # expect due to too few args @@ -300,24 +298,23 @@ class ReferencesTestCase(TestBase): def check_proxy(self, o, proxy): o.foo = 1 - self.assertTrue(proxy.foo == 1, + self.assertEqual(proxy.foo, 1, "proxy does not reflect attribute addition") o.foo = 2 - self.assertTrue(proxy.foo == 2, + self.assertEqual(proxy.foo, 2, "proxy does not reflect attribute modification") del o.foo - self.assertTrue(not hasattr(proxy, 'foo'), + self.assertFalse(hasattr(proxy, 'foo'), "proxy does not reflect attribute removal") proxy.foo = 1 - self.assertTrue(o.foo == 1, + self.assertEqual(o.foo, 1, "object does not reflect attribute addition via proxy") proxy.foo = 2 - self.assertTrue( - o.foo == 2, + self.assertEqual(o.foo, 2, "object does not reflect attribute modification via proxy") del proxy.foo - self.assertTrue(not hasattr(o, 'foo'), + self.assertFalse(hasattr(o, 'foo'), "object does not reflect attribute removal via proxy") def test_proxy_deletion(self): @@ -341,21 +338,21 @@ class ReferencesTestCase(TestBase): o = C() ref1 = weakref.ref(o) ref2 = weakref.ref(o, self.callback) - self.assertTrue(weakref.getweakrefcount(o) == 2, + self.assertEqual(weakref.getweakrefcount(o), 2, "got wrong number of weak reference objects") proxy1 = weakref.proxy(o) proxy2 = weakref.proxy(o, self.callback) - self.assertTrue(weakref.getweakrefcount(o) == 4, + self.assertEqual(weakref.getweakrefcount(o), 4, "got wrong number of weak reference objects") del ref1, ref2, proxy1, proxy2 - self.assertTrue(weakref.getweakrefcount(o) == 0, + self.assertEqual(weakref.getweakrefcount(o), 0, "weak reference objects not unlinked from" " referent when discarded.") # assumes ints do not support weakrefs - self.assertTrue(weakref.getweakrefcount(1) == 0, + self.assertEqual(weakref.getweakrefcount(1), 0, "got wrong number of weak reference objects for int") def test_getweakrefs(self): @@ -363,22 +360,22 @@ class ReferencesTestCase(TestBase): ref1 = weakref.ref(o, self.callback) ref2 = weakref.ref(o, self.callback) del ref1 - self.assertTrue(weakref.getweakrefs(o) == [ref2], + self.assertEqual(weakref.getweakrefs(o), [ref2], "list of refs does not match") o = C() ref1 = weakref.ref(o, self.callback) ref2 = weakref.ref(o, self.callback) del ref2 - self.assertTrue(weakref.getweakrefs(o) == [ref1], + self.assertEqual(weakref.getweakrefs(o), [ref1], "list of refs does not match") del ref1 - self.assertTrue(weakref.getweakrefs(o) == [], + self.assertEqual(weakref.getweakrefs(o), [], "list of refs not cleared") # assumes ints do not support weakrefs - self.assertTrue(weakref.getweakrefs(1) == [], + self.assertEqual(weakref.getweakrefs(1), [], "list of refs does not match for int") def test_newstyle_number_ops(self): @@ -386,8 +383,8 @@ class ReferencesTestCase(TestBase): pass f = F(2.0) p = weakref.proxy(f) - self.assertTrue(p + 1.0 == 3.0) - self.assertTrue(1.0 + p == 3.0) # this used to SEGV + self.assertEqual(p + 1.0, 3.0) + self.assertEqual(1.0 + p, 3.0) # this used to SEGV def test_callbacks_protected(self): # Callbacks protected from already-set exceptions? @@ -640,7 +637,7 @@ class ReferencesTestCase(TestBase): c.wr = weakref.ref(d, callback) # this won't trigger d.wr = weakref.ref(callback, d.cb) # ditto external_wr = weakref.ref(callback, safe_callback) # but this will - self.assertTrue(external_wr() is callback) + self.assertIs(external_wr(), callback) # The weakrefs attached to c and d should get cleared, so that # C.cb is never called. But external_wr isn't part of the cyclic @@ -843,11 +840,11 @@ class SubclassableWeakrefTestCase(TestBase): return super().__call__() o = Object("foo") mr = MyRef(o, value=24) - self.assertTrue(mr() is o) + self.assertIs(mr(), o) self.assertTrue(mr.called) self.assertEqual(mr.value, 24) del o - self.assertTrue(mr() is None) + self.assertIsNone(mr()) self.assertTrue(mr.called) def test_subclass_refs_dont_replace_standard_refs(self): @@ -856,14 +853,14 @@ class SubclassableWeakrefTestCase(TestBase): o = Object(42) r1 = MyRef(o) r2 = weakref.ref(o) - self.assertTrue(r1 is not r2) + self.assertIsNot(r1, r2) self.assertEqual(weakref.getweakrefs(o), [r2, r1]) self.assertEqual(weakref.getweakrefcount(o), 2) r3 = MyRef(o) self.assertEqual(weakref.getweakrefcount(o), 3) refs = weakref.getweakrefs(o) self.assertEqual(len(refs), 3) - self.assertTrue(r2 is refs[0]) + self.assertIs(r2, refs[0]) self.assertIn(r1, refs[1:]) self.assertIn(r3, refs[1:]) @@ -873,7 +870,7 @@ class SubclassableWeakrefTestCase(TestBase): o = Object(42) r1 = MyRef(o, id) r2 = MyRef(o, str) - self.assertTrue(r1 is not r2) + self.assertIsNot(r1, r2) refs = weakref.getweakrefs(o) self.assertIn(r1, refs) self.assertIn(r2, refs) @@ -1135,7 +1132,7 @@ class MappingTestCase(TestBase): dict, objects = self.make_weak_valued_dict() for o in objects: self.assertEqual(weakref.getweakrefcount(o), 1) - self.assertTrue(o is dict[o.arg], + self.assertIs(o, dict[o.arg], "wrong object returned by weak dict!") items1 = list(dict.items()) items2 = list(dict.copy().items()) @@ -1164,9 +1161,9 @@ class MappingTestCase(TestBase): # dict, objects = self.make_weak_keyed_dict() for o in objects: - self.assertTrue(weakref.getweakrefcount(o) == 1, + self.assertEqual(weakref.getweakrefcount(o), 1, "wrong number of weak references to %r!" % o) - self.assertTrue(o.arg is dict[o], + self.assertIs(o.arg, dict[o], "wrong object returned by weak dict!") items1 = dict.items() items2 = dict.copy().items() @@ -1175,10 +1172,10 @@ class MappingTestCase(TestBase): del items1, items2 self.assertEqual(len(dict), self.COUNT) del objects[0] - self.assertTrue(len(dict) == (self.COUNT - 1), + self.assertEqual(len(dict), (self.COUNT - 1), "deleting object did not cause dictionary update") del objects, o - self.assertTrue(len(dict) == 0, + self.assertEqual(len(dict), 0, "deleting the keys did not clear the dictionary") o = Object(42) dict[o] = "What is the meaning of the universe?" @@ -1388,15 +1385,15 @@ class MappingTestCase(TestBase): k, v = weakdict.popitem() self.assertEqual(len(weakdict), 1) if k is key1: - self.assertTrue(v is value1) + self.assertIs(v, value1) else: - self.assertTrue(v is value2) + self.assertIs(v, value2) k, v = weakdict.popitem() self.assertEqual(len(weakdict), 0) if k is key1: - self.assertTrue(v is value1) + self.assertIs(v, value1) else: - self.assertTrue(v is value2) + self.assertIs(v, value2) def test_weak_valued_dict_popitem(self): self.check_popitem(weakref.WeakValueDictionary, @@ -1407,21 +1404,21 @@ class MappingTestCase(TestBase): C(), "value 1", C(), "value 2") def check_setdefault(self, klass, key, value1, value2): - self.assertTrue(value1 is not value2, + self.assertIsNot(value1, value2, "invalid test" " -- value parameters must be distinct objects") weakdict = klass() o = weakdict.setdefault(key, value1) - self.assertTrue(o is value1) + self.assertIs(o, value1) self.assertIn(key, weakdict) - self.assertTrue(weakdict.get(key) is value1) - self.assertTrue(weakdict[key] is value1) + self.assertIs(weakdict.get(key), value1) + self.assertIs(weakdict[key], value1) o = weakdict.setdefault(key, value2) - self.assertTrue(o is value1) + self.assertIs(o, value1) self.assertIn(key, weakdict) - self.assertTrue(weakdict.get(key) is value1) - self.assertTrue(weakdict[key] is value1) + self.assertIs(weakdict.get(key), value1) + self.assertIs(weakdict[key], value1) def test_weak_valued_dict_setdefault(self): self.check_setdefault(weakref.WeakValueDictionary, @@ -1442,13 +1439,13 @@ class MappingTestCase(TestBase): for k in weakdict.keys(): self.assertIn(k, dict, "mysterious new key appeared in weak dict") v = dict.get(k) - self.assertTrue(v is weakdict[k]) - self.assertTrue(v is weakdict.get(k)) + self.assertIs(v, weakdict[k]) + self.assertIs(v, weakdict.get(k)) for k in dict.keys(): self.assertIn(k, weakdict, "original key disappeared in weak dict") v = dict[k] - self.assertTrue(v is weakdict[k]) - self.assertTrue(v is weakdict.get(k)) + self.assertIs(v, weakdict[k]) + self.assertIs(v, weakdict.get(k)) def test_weak_valued_dict_update(self): self.check_update(weakref.WeakValueDictionary, @@ -1478,7 +1475,7 @@ class MappingTestCase(TestBase): self.assertEqual(len(d), 2) del d['something'] self.assertEqual(len(d), 1) - self.assertTrue(list(d.items()) == [('something else', o2)]) + self.assertEqual(list(d.items()), [('something else', o2)]) def test_weak_keyed_bad_delitem(self): d = weakref.WeakKeyDictionary() |