summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_dict.py
diff options
context:
space:
mode:
authorRĂ©mi Lapeyre <remi.lapeyre@henki.fr>2018-11-06 00:38:54 (GMT)
committerINADA Naoki <methane@users.noreply.github.com>2018-11-06 00:38:54 (GMT)
commit6531bf6309c8fda1954060a0fb5ea930b1efb656 (patch)
tree64debbc908a8d0dbd043d3114a536a750bb70339 /Lib/test/test_dict.py
parent16c8a53490a22bd4fcde2efaf4694dd06ded882b (diff)
downloadcpython-6531bf6309c8fda1954060a0fb5ea930b1efb656.zip
cpython-6531bf6309c8fda1954060a0fb5ea930b1efb656.tar.gz
cpython-6531bf6309c8fda1954060a0fb5ea930b1efb656.tar.bz2
bpo-33462: Add __reversed__ to dict and dict views (GH-6827)
Diffstat (limited to 'Lib/test/test_dict.py')
-rw-r--r--Lib/test/test_dict.py69
1 files changed, 66 insertions, 3 deletions
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index 90c0a31..71fffe3 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -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)])