Nailed test_weakref.py. Pfew, messy!

This commit is contained in:
Guido van Rossum 2007-02-11 22:59:48 +00:00
parent 9afbb4e364
commit 07f2436fa8
4 changed files with 42 additions and 39 deletions

View File

@ -141,8 +141,7 @@ class PrettyPrinter:
if length: if length:
context[objid] = 1 context[objid] = 1
indent = indent + self._indent_per_level indent = indent + self._indent_per_level
items = object.items() items = sorted(object.items())
items.sort()
key, ent = items[0] key, ent = items[0]
rep = self._repr(key, context, level) rep = self._repr(key, context, level)
write(rep) write(rep)

View File

@ -49,7 +49,7 @@ class BasicTestMappingProtocol(unittest.TestCase):
#Indexing #Indexing
for key, value in self.reference.items(): for key, value in self.reference.items():
self.assertEqual(d[key], value) self.assertEqual(d[key], value)
knownkey = self.other.keys()[0] knownkey = list(self.other.keys())[0]
self.failUnlessRaises(KeyError, lambda:d[knownkey]) self.failUnlessRaises(KeyError, lambda:d[knownkey])
#len #len
self.assertEqual(len(p), 0) self.assertEqual(len(p), 0)
@ -73,13 +73,16 @@ class BasicTestMappingProtocol(unittest.TestCase):
self.assert_(hasattr(iter, '__iter__')) self.assert_(hasattr(iter, '__iter__'))
x = list(iter) x = list(iter)
self.assert_(set(x)==set(lst)==set(ref)) self.assert_(set(x)==set(lst)==set(ref))
check_iterandlist(d.keys(), d.keys(), self.reference.keys()) check_iterandlist(iter(d.keys()), list(d.keys()),
check_iterandlist(iter(d), d.keys(), self.reference.keys()) self.reference.keys())
check_iterandlist(d.values(), d.values(), self.reference.values()) check_iterandlist(iter(d), list(d.keys()), self.reference.keys())
check_iterandlist(d.items(), d.items(), self.reference.items()) check_iterandlist(iter(d.values()), list(d.values()),
self.reference.values())
check_iterandlist(iter(d.items()), list(d.items()),
self.reference.items())
#get #get
key, value = d.items().next() key, value = iter(d.items()).next()
knownkey, knownvalue = self.other.items().next() knownkey, knownvalue = iter(self.other.items()).next()
self.assertEqual(d.get(key, knownvalue), value) self.assertEqual(d.get(key, knownvalue), value)
self.assertEqual(d.get(knownkey, knownvalue), knownvalue) self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
self.failIf(knownkey in d) self.failIf(knownkey in d)
@ -104,8 +107,8 @@ class BasicTestMappingProtocol(unittest.TestCase):
self.assertEqual(dict(p), self.reference) self.assertEqual(dict(p), self.reference)
d = self._full_mapping(self.reference) d = self._full_mapping(self.reference)
#setdefault #setdefault
key, value = d.items().next() key, value = iter(d.items()).next()
knownkey, knownvalue = self.other.items().next() knownkey, knownvalue = iter(self.other.items()).next()
self.assertEqual(d.setdefault(key, knownvalue), value) self.assertEqual(d.setdefault(key, knownvalue), value)
self.assertEqual(d[key], value) self.assertEqual(d[key], value)
self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue) self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue)
@ -137,21 +140,21 @@ class BasicTestMappingProtocol(unittest.TestCase):
def test_keys(self): def test_keys(self):
d = self._empty_mapping() d = self._empty_mapping()
self.assertEqual(d.keys(), []) self.assertEqual(list(d.keys()), [])
d = self.reference d = self.reference
self.assert_(self.inmapping.keys()[0] in d.keys()) self.assert_(list(self.inmapping.keys())[0] in d.keys())
self.assert_(self.other.keys()[0] not in d.keys()) self.assert_(list(self.other.keys())[0] not in d.keys())
self.assertRaises(TypeError, d.keys, None) self.assertRaises(TypeError, d.keys, None)
def test_values(self): def test_values(self):
d = self._empty_mapping() d = self._empty_mapping()
self.assertEqual(d.values(), []) self.assertEqual(list(d.values()), [])
self.assertRaises(TypeError, d.values, None) self.assertRaises(TypeError, d.values, None)
def test_items(self): def test_items(self):
d = self._empty_mapping() d = self._empty_mapping()
self.assertEqual(d.items(), []) self.assertEqual(list(d.items()), [])
self.assertRaises(TypeError, d.items, None) self.assertRaises(TypeError, d.items, None)
@ -161,7 +164,8 @@ class BasicTestMappingProtocol(unittest.TestCase):
def test_getitem(self): def test_getitem(self):
d = self.reference d = self.reference
self.assertEqual(d[self.inmapping.keys()[0]], self.inmapping.values()[0]) self.assertEqual(d[list(self.inmapping.keys())[0]],
list(self.inmapping.values())[0])
self.assertRaises(TypeError, d.__getitem__) self.assertRaises(TypeError, d.__getitem__)
@ -169,7 +173,7 @@ class BasicTestMappingProtocol(unittest.TestCase):
# mapping argument # mapping argument
d = self._empty_mapping() d = self._empty_mapping()
d.update(self.other) d.update(self.other)
self.assertEqual(d.items(), self.other.items()) self.assertEqual(list(d.items()), list(self.other.items()))
# No argument # No argument
d = self._empty_mapping() d = self._empty_mapping()
@ -179,12 +183,12 @@ class BasicTestMappingProtocol(unittest.TestCase):
# item sequence # item sequence
d = self._empty_mapping() d = self._empty_mapping()
d.update(self.other.items()) d.update(self.other.items())
self.assertEqual(d.items(), self.other.items()) self.assertEqual(list(d.items()), list(self.other.items()))
# Iterator # Iterator
d = self._empty_mapping() d = self._empty_mapping()
d.update(self.other.items()) d.update(self.other.items())
self.assertEqual(d.items(), self.other.items()) self.assertEqual(list(d.items()), list(self.other.items()))
# FIXME: Doesn't work with UserDict # FIXME: Doesn't work with UserDict
# self.assertRaises((TypeError, AttributeError), d.update, None) # self.assertRaises((TypeError, AttributeError), d.update, None)
@ -200,10 +204,8 @@ class BasicTestMappingProtocol(unittest.TestCase):
return self.d[i] return self.d[i]
d.clear() d.clear()
d.update(SimpleUserDict()) d.update(SimpleUserDict())
i1 = d.items() i1 = sorted(d.items())
i2 = self.reference.items() i2 = sorted(self.reference.items())
i1.sort()
i2.sort()
self.assertEqual(i1, i2) self.assertEqual(i1, i2)
class Exc(Exception): pass class Exc(Exception): pass
@ -266,13 +268,15 @@ class BasicTestMappingProtocol(unittest.TestCase):
def test_get(self): def test_get(self):
d = self._empty_mapping() d = self._empty_mapping()
self.assert_(d.get(self.other.keys()[0]) is None) self.assert_(d.get(list(self.other.keys())[0]) is None)
self.assertEqual(d.get(self.other.keys()[0], 3), 3) self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
d = self.reference d = self.reference
self.assert_(d.get(self.other.keys()[0]) is None) self.assert_(d.get(list(self.other.keys())[0]) is None)
self.assertEqual(d.get(self.other.keys()[0], 3), 3) self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
self.assertEqual(d.get(self.inmapping.keys()[0]), self.inmapping.values()[0]) self.assertEqual(d.get(list(self.inmapping.keys())[0]),
self.assertEqual(d.get(self.inmapping.keys()[0], 3), self.inmapping.values()[0]) list(self.inmapping.values())[0])
self.assertEqual(d.get(list(self.inmapping.keys())[0], 3),
list(self.inmapping.values())[0])
self.assertRaises(TypeError, d.get) self.assertRaises(TypeError, d.get)
self.assertRaises(TypeError, d.get, None, None, None) self.assertRaises(TypeError, d.get, None, None, None)
@ -287,9 +291,9 @@ class BasicTestMappingProtocol(unittest.TestCase):
def test_pop(self): def test_pop(self):
d = self._empty_mapping() d = self._empty_mapping()
k, v = self.inmapping.items()[0] k, v = list(self.inmapping.items())[0]
d[k] = v d[k] = v
self.assertRaises(KeyError, d.pop, self.other.keys()[0]) self.assertRaises(KeyError, d.pop, list(self.other.keys())[0])
self.assertEqual(d.pop(k), v) self.assertEqual(d.pop(k), v)
self.assertEqual(len(d), 0) self.assertEqual(len(d), 0)

View File

@ -844,19 +844,19 @@ class MappingTestCase(TestBase):
self.assert_(len(items) == 0, "iteritems() did not touch all items") self.assert_(len(items) == 0, "iteritems() did not touch all items")
# key iterator, via __iter__(): # key iterator, via __iter__():
keys = dict.keys() keys = list(dict.keys())
for k in dict: for k in dict:
keys.remove(k) keys.remove(k)
self.assert_(len(keys) == 0, "__iter__() did not touch all keys") self.assert_(len(keys) == 0, "__iter__() did not touch all keys")
# key iterator, via iterkeys(): # key iterator, via iterkeys():
keys = dict.keys() keys = list(dict.keys())
for k in dict.keys(): for k in dict.keys():
keys.remove(k) keys.remove(k)
self.assert_(len(keys) == 0, "iterkeys() did not touch all keys") self.assert_(len(keys) == 0, "iterkeys() did not touch all keys")
# value iterator: # value iterator:
values = dict.values() values = list(dict.values())
for v in dict.values(): for v in dict.values():
values.remove(v) values.remove(v)
self.assert_(len(values) == 0, self.assert_(len(values) == 0,

View File

@ -106,10 +106,10 @@ class WeakValueDictionary(UserDict.UserDict):
yield wr.key, value yield wr.key, value
def iterkeys(self): def iterkeys(self):
return self.data.keys() return iter(self.data.keys())
def __iter__(self): def __iter__(self):
return self.data.keys() return iter(self.data.keys())
def itervaluerefs(self): def itervaluerefs(self):
"""Return an iterator that yields the weak references to the values. """Return an iterator that yields the weak references to the values.
@ -292,10 +292,10 @@ class WeakKeyDictionary(UserDict.UserDict):
yield obj yield obj
def __iter__(self): def __iter__(self):
return self.keys() return iter(self.keys())
def itervalues(self): def itervalues(self):
return self.data.values() return iter(self.data.values())
def keyrefs(self): def keyrefs(self):
"""Return a list of weak references to the keys. """Return a list of weak references to the keys.