From d08a2c2576457b7d0229a00b4977044a38ad0d68 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 19 Apr 2011 10:05:03 -0700 Subject: [PATCH 1/3] Issue 11875: Keep OrderedDict's __reduce__ from temporarily mutating the object. --- Lib/collections.py | 5 ++--- Misc/NEWS | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/collections.py b/Lib/collections.py index fb9464fa87e..9381f516763 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -101,10 +101,9 @@ class OrderedDict(dict): def __reduce__(self): 'Return state information for pickling' items = [[k, self[k]] for k in self] - tmp = self.__map, self.__root, self.__in_repr - del self.__map, self.__root, self.__in_repr inst_dict = vars(self).copy() - self.__map, self.__root, self.__in_repr = tmp + for k in vars(self.__class__()): + inst_dict.pop(k, None) if inst_dict: return (self.__class__, (items,), inst_dict) return self.__class__, (items,) diff --git a/Misc/NEWS b/Misc/NEWS index c424e1eb94b..7e95d80c13f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -58,6 +58,9 @@ Library - Issue #11467: Fix urlparse behavior when handling urls which contains scheme specific part only digits. Patch by Santoso Wijaya. +- Issue #11875: collections.OrderedDict's __reduce__ was temporarily + mutating the object instead of just working on a copy. + - collections.Counter().copy() now works correctly for subclasses. - Issue #11474: Fix the bug with url2pathname() handling of '/C|/' on Windows. From ab694380466be70294aa8e5b99b10e9f0c48b3bf Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 19 Apr 2011 10:05:53 -0700 Subject: [PATCH 2/3] Hmm, __ne__ was missing --- Lib/collections.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/collections.py b/Lib/collections.py index 9381f516763..5ae590763bd 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -112,6 +112,7 @@ class OrderedDict(dict): keys = MutableMapping.keys values = MutableMapping.values items = MutableMapping.items + __ne__ = MutableMapping.__ne__ __marker = object() From 019a97c77cc750d104adcc53ca4b84e29a069317 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 19 Apr 2011 10:21:27 -0700 Subject: [PATCH 3/3] Issue 11875: Keep OrderedDict's __reduce__ from temporarily mutating the object. --- Lib/collections.py | 5 ++--- Misc/NEWS | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/collections.py b/Lib/collections.py index cde734debd9..a1f789082b5 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -154,10 +154,9 @@ class OrderedDict(dict): def __reduce__(self): 'Return state information for pickling' items = [[k, self[k]] for k in self] - tmp = self.__map, self.__root, self.__hardroot - del self.__map, self.__root, self.__hardroot inst_dict = vars(self).copy() - self.__map, self.__root, self.__hardroot = tmp + for k in vars(self.__class__()): + inst_dict.pop(k, None) if inst_dict: return (self.__class__, (items,), inst_dict) return self.__class__, (items,) diff --git a/Misc/NEWS b/Misc/NEWS index fa8f039b82c..1885fb73cf9 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -62,6 +62,9 @@ Library - Issue #11852: Add missing imports and update tests. +- Issue #11875: collections.OrderedDict's __reduce__ was temporarily + mutating the object instead of just working on a copy. + - Issue #11467: Fix urlparse behavior when handling urls which contains scheme specific part only digits. Patch by Santoso Wijaya.