@@ -215,7 +215,18 @@ def ordered_dict_intersection(first_dict, second_dict, compat=equivalent):
215
215
return new_dict
216
216
217
217
218
- class Frozen (Mapping ):
218
+ class SingleSlotPickleMixin (object ):
219
+ """Mixin class to add the ability to pickle objects whose state is defined
220
+ by a single __slots__ attribute. Only necessary under Python 2.
221
+ """
222
+ def __getstate__ (self ):
223
+ return getattr (self , self .__slots__ [0 ])
224
+
225
+ def __setstate__ (self , state ):
226
+ setattr (self , self .__slots__ [0 ], state )
227
+
228
+
229
+ class Frozen (Mapping , SingleSlotPickleMixin ):
219
230
"""Wrapper around an object implementing the mapping interface to make it
220
231
immutable. If you really want to modify the mapping, the mutable version is
221
232
saved under the `mapping` attribute.
@@ -240,16 +251,12 @@ def __contains__(self, key):
240
251
def __repr__ (self ):
241
252
return '%s(%r)' % (type (self ).__name__ , self .mapping )
242
253
243
- if not PY3 :
244
- def __getstate__ (self ):
245
- return self .__dict__
246
-
247
254
248
255
def FrozenOrderedDict (* args , ** kwargs ):
249
256
return Frozen (OrderedDict (* args , ** kwargs ))
250
257
251
258
252
- class SortedKeysDict (MutableMapping ):
259
+ class SortedKeysDict (MutableMapping , SingleSlotPickleMixin ):
253
260
"""An wrapper for dictionary-like objects that always iterates over its
254
261
items in sorted order by key but is otherwise equivalent to the underlying
255
262
mapping.
@@ -283,12 +290,8 @@ def __repr__(self):
283
290
def copy (self ):
284
291
return type (self )(self .mapping .copy ())
285
292
286
- if not PY3 :
287
- def __getstate__ (self ):
288
- return self .__dict__
289
-
290
293
291
- class ChainMap (MutableMapping ):
294
+ class ChainMap (MutableMapping , SingleSlotPickleMixin ):
292
295
"""Partial backport of collections.ChainMap from Python>=3.3
293
296
294
297
Don't return this from any public APIs, since some of the public methods
@@ -319,10 +322,6 @@ def __iter__(self):
319
322
def __len__ (self ):
320
323
raise NotImplementedError
321
324
322
- if not PY3 :
323
- def __getstate__ (self ):
324
- return self .__dict__
325
-
326
325
327
326
class NDArrayMixin (object ):
328
327
"""Mixin class for making wrappers of N-dimensional arrays that conform to
0 commit comments