diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-04-06 17:00:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-06 17:00:14 (GMT) |
commit | 884eba3c76916889fd6bff3b37b8552bfb4f9566 (patch) | |
tree | 51fd55d6170cdff327ac11d70f1e5ff1aa7e735a /Lib/collections | |
parent | f82f9ce3239b9a7e6ffa278658dd9858f64a3c14 (diff) | |
download | cpython-884eba3c76916889fd6bff3b37b8552bfb4f9566.zip cpython-884eba3c76916889fd6bff3b37b8552bfb4f9566.tar.gz cpython-884eba3c76916889fd6bff3b37b8552bfb4f9566.tar.bz2 |
bpo-26579: Add object.__getstate__(). (GH-2821)
Copying and pickling instances of subclasses of builtin types
bytearray, set, frozenset, collections.OrderedDict, collections.deque,
weakref.WeakSet, and datetime.tzinfo now copies and pickles instance attributes
implemented as slots.
Diffstat (limited to 'Lib/collections')
-rw-r--r-- | Lib/collections/__init__.py | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 264435c..7af8dcd 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -271,10 +271,22 @@ class OrderedDict(dict): def __reduce__(self): 'Return state information for pickling' - inst_dict = vars(self).copy() - for k in vars(OrderedDict()): - inst_dict.pop(k, None) - return self.__class__, (), inst_dict or None, None, iter(self.items()) + state = self.__getstate__() + if state: + if isinstance(state, tuple): + state, slots = state + else: + slots = {} + state = state.copy() + slots = slots.copy() + for k in vars(OrderedDict()): + state.pop(k, None) + slots.pop(k, None) + if slots: + state = state, slots + else: + state = state or None + return self.__class__, (), state, None, iter(self.items()) def copy(self): 'od.copy() -> a shallow copy of od' |