Kaydet (Commit) 3ee6dabf authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #17900: Allowed pickling of recursive OrderedDicts. Decreased pickled

size and pickling time.
üst b10c71da
...@@ -199,13 +199,10 @@ class OrderedDict(dict): ...@@ -199,13 +199,10 @@ class OrderedDict(dict):
def __reduce__(self): def __reduce__(self):
'Return state information for pickling' 'Return state information for pickling'
items = [[k, self[k]] for k in self]
inst_dict = vars(self).copy() inst_dict = vars(self).copy()
for k in vars(OrderedDict()): for k in vars(OrderedDict()):
inst_dict.pop(k, None) inst_dict.pop(k, None)
if inst_dict: return self.__class__, (), inst_dict or None, None, iter(self.items())
return (self.__class__, (items,), inst_dict)
return self.__class__, (items,)
def copy(self): def copy(self):
'od.copy() -> a shallow copy of od' 'od.copy() -> a shallow copy of od'
......
...@@ -1245,9 +1245,18 @@ class TestOrderedDict(unittest.TestCase): ...@@ -1245,9 +1245,18 @@ class TestOrderedDict(unittest.TestCase):
# do not save instance dictionary if not needed # do not save instance dictionary if not needed
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
od = OrderedDict(pairs) od = OrderedDict(pairs)
self.assertEqual(len(od.__reduce__()), 2) self.assertIsNone(od.__reduce__()[2])
od.x = 10 od.x = 10
self.assertEqual(len(od.__reduce__()), 3) self.assertIsNotNone(od.__reduce__()[2])
def test_pickle_recursive(self):
od = OrderedDict()
od[1] = od
for proto in range(-1, pickle.HIGHEST_PROTOCOL + 1):
dup = pickle.loads(pickle.dumps(od, proto))
self.assertIsNot(dup, od)
self.assertEqual(list(dup.keys()), [1])
self.assertIs(dup[1], dup)
def test_repr(self): def test_repr(self):
od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]) od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])
......
...@@ -99,6 +99,9 @@ Core and Builtins ...@@ -99,6 +99,9 @@ Core and Builtins
Library Library
------- -------
- Issue #17900: Allowed pickling of recursive OrderedDicts. Decreased pickled
size and pickling time.
- Issue #17914: Add os.cpu_count(). Patch by Yogesh Chaudhari, based on an - Issue #17914: Add os.cpu_count(). Patch by Yogesh Chaudhari, based on an
initial patch by Trent Nelson. initial patch by Trent Nelson.
......
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