mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
Fix the damage to UserDict and its tests.
Clearly this is not the right way to fix this; UserDict and MixinDict ought to be redesigned with the new dict API in mind. But I'm not claiming to be in charge of library redesign, I only want zero failing tests.
This commit is contained in:
parent
e34cdd1bc1
commit
d81206d152
3 changed files with 17 additions and 18 deletions
|
@ -42,9 +42,6 @@ class UserDict:
|
||||||
return c
|
return c
|
||||||
def keys(self): return self.data.keys()
|
def keys(self): return self.data.keys()
|
||||||
def items(self): return self.data.items()
|
def items(self): return self.data.items()
|
||||||
def iteritems(self): return self.data.items()
|
|
||||||
def iterkeys(self): return self.data.keys()
|
|
||||||
def itervalues(self): return self.data.values()
|
|
||||||
def values(self): return self.data.values()
|
def values(self): return self.data.values()
|
||||||
def update(self, dict=None, **kwargs):
|
def update(self, dict=None, **kwargs):
|
||||||
if dict is None:
|
if dict is None:
|
||||||
|
@ -91,6 +88,8 @@ class DictMixin:
|
||||||
# methods, progressively more efficiency comes with defining
|
# methods, progressively more efficiency comes with defining
|
||||||
# __contains__(), __iter__(), and iteritems().
|
# __contains__(), __iter__(), and iteritems().
|
||||||
|
|
||||||
|
# XXX It would make more sense to expect __iter__ to be primitive.
|
||||||
|
|
||||||
# second level definitions support higher levels
|
# second level definitions support higher levels
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for k in self.keys():
|
for k in self.keys():
|
||||||
|
@ -103,20 +102,20 @@ class DictMixin:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# third level takes advantage of second level definitions
|
# third level takes advantage of second level definitions
|
||||||
|
def iterkeys(self):
|
||||||
|
return self.__iter__()
|
||||||
def iteritems(self):
|
def iteritems(self):
|
||||||
for k in self:
|
for k in self:
|
||||||
yield (k, self[k])
|
yield (k, self[k])
|
||||||
def iterkeys(self):
|
|
||||||
return self.__iter__()
|
|
||||||
|
|
||||||
# fourth level uses definitions from lower levels
|
# fourth level uses definitions from lower levels
|
||||||
def itervalues(self):
|
def itervalues(self):
|
||||||
for _, v in self.items():
|
for _, v in self.iteritems():
|
||||||
yield v
|
yield v
|
||||||
def values(self):
|
def values(self):
|
||||||
return [v for _, v in self.items()]
|
return [v for _, v in self.iteritems()]
|
||||||
def items(self):
|
def items(self):
|
||||||
return list(self.items())
|
return list(self.iteritems())
|
||||||
def clear(self):
|
def clear(self):
|
||||||
for key in self.keys():
|
for key in self.keys():
|
||||||
del self[key]
|
del self[key]
|
||||||
|
@ -140,7 +139,7 @@ class DictMixin:
|
||||||
return value
|
return value
|
||||||
def popitem(self):
|
def popitem(self):
|
||||||
try:
|
try:
|
||||||
k, v = self.items().next()
|
k, v = self.iteritems().next()
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
raise KeyError, 'container is empty'
|
raise KeyError, 'container is empty'
|
||||||
del self[k]
|
del self[k]
|
||||||
|
@ -169,14 +168,14 @@ class DictMixin:
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return default
|
return default
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return repr(dict(self.items()))
|
return repr(dict(self.iteritems()))
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, DictMixin):
|
if isinstance(other, DictMixin):
|
||||||
other = dict(other.items())
|
other = dict(other.iteritems())
|
||||||
return dict(self.items()) == other
|
return dict(self.iteritems()) == other
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
if isinstance(other, DictMixin):
|
if isinstance(other, DictMixin):
|
||||||
other = dict(other.items())
|
other = dict(other.iteritems())
|
||||||
return dict(self.items()) != other
|
return dict(self.iteritems()) != other
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.keys())
|
return len(self.keys())
|
||||||
|
|
|
@ -317,7 +317,7 @@ class TestMappingProtocol(BasicTestMappingProtocol):
|
||||||
def test_keys(self):
|
def test_keys(self):
|
||||||
BasicTestMappingProtocol.test_keys(self)
|
BasicTestMappingProtocol.test_keys(self)
|
||||||
d = self._empty_mapping()
|
d = self._empty_mapping()
|
||||||
self.assertEqual(d.keys(), [])
|
self.assertEqual(list(d.keys()), [])
|
||||||
d = self._full_mapping({'a': 1, 'b': 2})
|
d = self._full_mapping({'a': 1, 'b': 2})
|
||||||
k = d.keys()
|
k = d.keys()
|
||||||
self.assert_('a' in k)
|
self.assert_('a' in k)
|
||||||
|
@ -327,13 +327,13 @@ class TestMappingProtocol(BasicTestMappingProtocol):
|
||||||
def test_values(self):
|
def test_values(self):
|
||||||
BasicTestMappingProtocol.test_values(self)
|
BasicTestMappingProtocol.test_values(self)
|
||||||
d = self._full_mapping({1:2})
|
d = self._full_mapping({1:2})
|
||||||
self.assertEqual(d.values(), [2])
|
self.assertEqual(list(d.values()), [2])
|
||||||
|
|
||||||
def test_items(self):
|
def test_items(self):
|
||||||
BasicTestMappingProtocol.test_items(self)
|
BasicTestMappingProtocol.test_items(self)
|
||||||
|
|
||||||
d = self._full_mapping({1:2})
|
d = self._full_mapping({1:2})
|
||||||
self.assertEqual(d.items(), [(1, 2)])
|
self.assertEqual(list(d.items()), [(1, 2)])
|
||||||
|
|
||||||
def test_contains(self):
|
def test_contains(self):
|
||||||
d = self._empty_mapping()
|
d = self._empty_mapping()
|
||||||
|
|
|
@ -92,7 +92,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
|
||||||
# Test keys, items, values
|
# Test keys, items, values
|
||||||
self.assertEqual(u2.keys(), d2.keys())
|
self.assertEqual(u2.keys(), d2.keys())
|
||||||
self.assertEqual(u2.items(), d2.items())
|
self.assertEqual(u2.items(), d2.items())
|
||||||
self.assertEqual(u2.values(), d2.values())
|
self.assertEqual(list(u2.values()), list(d2.values()))
|
||||||
|
|
||||||
# Test "in".
|
# Test "in".
|
||||||
for i in u2.keys():
|
for i in u2.keys():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue