mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-33462: Add __reversed__ to dict and dict views (GH-6827)
This commit is contained in:
parent
16c8a53490
commit
6531bf6309
10 changed files with 346 additions and 24 deletions
|
@ -1021,7 +1021,7 @@ class DictTest(unittest.TestCase):
|
|||
it = iter(data)
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
self.assertEqual(sorted(it), sorted(data))
|
||||
self.assertEqual(list(it), list(data))
|
||||
|
||||
it = pickle.loads(d)
|
||||
try:
|
||||
|
@ -1031,7 +1031,7 @@ class DictTest(unittest.TestCase):
|
|||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
del data[drop]
|
||||
self.assertEqual(sorted(it), sorted(data))
|
||||
self.assertEqual(list(it), list(data))
|
||||
|
||||
def test_itemiterator_pickling(self):
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
|
@ -1062,7 +1062,7 @@ class DictTest(unittest.TestCase):
|
|||
it = iter(data.values())
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
self.assertEqual(sorted(list(it)), sorted(list(data.values())))
|
||||
self.assertEqual(list(it), list(data.values()))
|
||||
|
||||
it = pickle.loads(d)
|
||||
drop = next(it)
|
||||
|
@ -1071,6 +1071,62 @@ class DictTest(unittest.TestCase):
|
|||
values = list(it) + [drop]
|
||||
self.assertEqual(sorted(values), sorted(list(data.values())))
|
||||
|
||||
def test_reverseiterator_pickling(self):
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
data = {1:"a", 2:"b", 3:"c"}
|
||||
it = reversed(data)
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
self.assertEqual(list(it), list(reversed(data)))
|
||||
|
||||
it = pickle.loads(d)
|
||||
try:
|
||||
drop = next(it)
|
||||
except StopIteration:
|
||||
continue
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
del data[drop]
|
||||
self.assertEqual(list(it), list(reversed(data)))
|
||||
|
||||
def test_reverseitemiterator_pickling(self):
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
data = {1:"a", 2:"b", 3:"c"}
|
||||
# dictviews aren't picklable, only their iterators
|
||||
itorg = reversed(data.items())
|
||||
d = pickle.dumps(itorg, proto)
|
||||
it = pickle.loads(d)
|
||||
# note that the type of the unpickled iterator
|
||||
# is not necessarily the same as the original. It is
|
||||
# merely an object supporting the iterator protocol, yielding
|
||||
# the same objects as the original one.
|
||||
# self.assertEqual(type(itorg), type(it))
|
||||
self.assertIsInstance(it, collections.abc.Iterator)
|
||||
self.assertEqual(dict(it), data)
|
||||
|
||||
it = pickle.loads(d)
|
||||
drop = next(it)
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
del data[drop[0]]
|
||||
self.assertEqual(dict(it), data)
|
||||
|
||||
def test_reversevaluesiterator_pickling(self):
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL):
|
||||
data = {1:"a", 2:"b", 3:"c"}
|
||||
# data.values() isn't picklable, only its iterator
|
||||
it = reversed(data.values())
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
self.assertEqual(list(it), list(reversed(data.values())))
|
||||
|
||||
it = pickle.loads(d)
|
||||
drop = next(it)
|
||||
d = pickle.dumps(it, proto)
|
||||
it = pickle.loads(d)
|
||||
values = list(it) + [drop]
|
||||
self.assertEqual(sorted(values), sorted(data.values()))
|
||||
|
||||
def test_instance_dict_getattr_str_subclass(self):
|
||||
class Foo:
|
||||
def __init__(self, msg):
|
||||
|
@ -1222,6 +1278,13 @@ class DictTest(unittest.TestCase):
|
|||
|
||||
self.assertRaises(RuntimeError, iter_and_mutate)
|
||||
|
||||
def test_reversed(self):
|
||||
d = {"a": 1, "b": 2, "foo": 0, "c": 3, "d": 4}
|
||||
del d["foo"]
|
||||
r = reversed(d)
|
||||
self.assertEqual(list(r), list('dcba'))
|
||||
self.assertRaises(StopIteration, next, r)
|
||||
|
||||
def test_dict_copy_order(self):
|
||||
# bpo-34320
|
||||
od = collections.OrderedDict([('a', 1), ('b', 2)])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue