Commit ca626b12 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #19607: Use specific asserts in weakref tests.

parent 1438b987
......@@ -91,11 +91,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):
......@@ -129,15 +127,15 @@ class ReferencesTestCase(TestBase):
self.assertRaises(weakref.ReferenceError, check, ref1)
self.assertRaises(weakref.ReferenceError, check, ref2)
self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
self.assertTrue(self.cbcalled == 2)
self.assertEqual(self.cbcalled, 2)
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):
......@@ -145,9 +143,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):
......@@ -157,19 +155,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):
......@@ -177,7 +175,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):
......@@ -259,19 +257,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()
......@@ -279,13 +277,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
......@@ -296,24 +294,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):
......@@ -337,21 +334,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):
......@@ -359,22 +356,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):
......@@ -382,8 +379,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?
......@@ -636,7 +633,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
......@@ -808,11 +805,11 @@ class SubclassableWeakrefTestCase(TestBase):
return super(MyRef, self).__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):
......@@ -821,14 +818,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:])
......@@ -838,7 +835,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)
......@@ -965,23 +962,23 @@ class MappingTestCase(TestBase):
#
dict, objects = self.make_weak_valued_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 is dict[o.arg],
self.assertIs(o, dict[o.arg],
"wrong object returned by weak dict!")
items1 = dict.items()
items2 = dict.copy().items()
items1.sort()
items2.sort()
self.assertTrue(items1 == items2,
self.assertEqual(items1, items2,
"cloning of weak-valued dictionary did not work!")
del items1, items2
self.assertTrue(len(dict) == self.COUNT)
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 values did not clear the dictionary")
# regression on SF bug #447152:
dict = weakref.WeakValueDictionary()
......@@ -996,21 +993,21 @@ 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()
self.assertTrue(set(items1) == set(items2),
self.assertEqual(set(items1), set(items2),
"cloning of weak-keyed dictionary did not work!")
del items1, items2
self.assertTrue(len(dict) == self.COUNT)
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?"
......@@ -1072,37 +1069,37 @@ class MappingTestCase(TestBase):
items = dict.items()
for item in dict.iteritems():
items.remove(item)
self.assertTrue(len(items) == 0, "iteritems() did not touch all items")
self.assertEqual(len(items), 0, "iteritems() did not touch all items")
# key iterator, via __iter__():
keys = dict.keys()
for k in dict:
keys.remove(k)
self.assertTrue(len(keys) == 0, "__iter__() did not touch all keys")
self.assertEqual(len(keys), 0, "__iter__() did not touch all keys")
# key iterator, via iterkeys():
keys = dict.keys()
for k in dict.iterkeys():
keys.remove(k)
self.assertTrue(len(keys) == 0, "iterkeys() did not touch all keys")
self.assertEqual(len(keys), 0, "iterkeys() did not touch all keys")
# value iterator:
values = dict.values()
for v in dict.itervalues():
values.remove(v)
self.assertTrue(len(values) == 0,
self.assertEqual(len(values), 0,
"itervalues() did not touch all values")
def test_make_weak_keyed_dict_from_dict(self):
o = Object(3)
dict = weakref.WeakKeyDictionary({o:364})
self.assertTrue(dict[o] == 364)
self.assertEqual(dict[o], 364)
def test_make_weak_keyed_dict_from_weak_keyed_dict(self):
o = Object(3)
dict = weakref.WeakKeyDictionary({o:364})
dict2 = weakref.WeakKeyDictionary(dict)
self.assertTrue(dict[o] == 364)
self.assertEqual(dict[o], 364)
def make_weak_keyed_dict(self):
dict = weakref.WeakKeyDictionary()
......@@ -1122,19 +1119,19 @@ class MappingTestCase(TestBase):
weakdict = klass()
weakdict[key1] = value1
weakdict[key2] = value2
self.assertTrue(len(weakdict) == 2)
self.assertEqual(len(weakdict), 2)
k, v = weakdict.popitem()
self.assertTrue(len(weakdict) == 1)
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.assertTrue(len(weakdict) == 0)
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,
......@@ -1145,7 +1142,7 @@ 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()
......@@ -1204,10 +1201,10 @@ class MappingTestCase(TestBase):
o2 = Object('2')
d[o1] = 'something'
d[o2] = 'something'
self.assertTrue(len(d) == 2)
self.assertEqual(len(d), 2)
del d[o1]
self.assertTrue(len(d) == 1)
self.assertTrue(d.keys() == [o2])
self.assertEqual(len(d), 1)
self.assertEqual(d.keys(), [o2])
def test_weak_valued_delitem(self):
d = weakref.WeakValueDictionary()
......@@ -1215,10 +1212,10 @@ class MappingTestCase(TestBase):
o2 = Object('2')
d['something'] = o1
d['something else'] = o2
self.assertTrue(len(d) == 2)
self.assertEqual(len(d), 2)
del d['something']
self.assertTrue(len(d) == 1)
self.assertTrue(d.items() == [('something else', o2)])
self.assertEqual(len(d), 1)
self.assertEqual(d.items(), [('something else', o2)])
def test_weak_keyed_bad_delitem(self):
d = weakref.WeakKeyDictionary()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment