Skip to content

Commit

Permalink
FIX pickle getset_descriptor (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
pierreglaser authored and ogrisel committed Jun 17, 2019
1 parent 6ca95a8 commit 2db58f1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- Fix a bug affecting bound classmethod saving on Python 2.
([issue #288](https://github.com/cloudpipe/cloudpickle/issues/288))

- Add support for pickling "getset" descriptors
([issue #290](https://github.com/cloudpipe/cloudpickle/pull/290))

1.2.1
=====

Expand Down
5 changes: 5 additions & 0 deletions cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,11 @@ def save_builtin_function_or_method(self, obj):
method_descriptor = type(str.upper)
dispatch[method_descriptor] = save_builtin_function_or_method

def save_getset_descriptor(self, obj):
return self.save_reduce(getattr, (obj.__objclass__, obj.__name__))

dispatch[types.GetSetDescriptorType] = save_getset_descriptor

def save_global(self, obj, name=None, pack=struct.pack):
"""
Save a "global".
Expand Down
5 changes: 5 additions & 0 deletions cloudpickle/cloudpickle_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ def _file_reduce(obj):
return _file_reconstructor, (retval,)


def _getset_descriptor_reduce(obj):
return getattr, (obj.__objclass__, obj.__name__)


def _mappingproxy_reduce(obj):
return types.MappingProxyType, (dict(obj),)

Expand Down Expand Up @@ -405,6 +409,7 @@ class CloudPickler(Pickler):
dispatch[staticmethod] = _classmethod_reduce
dispatch[types.CellType] = _cell_reduce
dispatch[types.CodeType] = _code_reduce
dispatch[types.GetSetDescriptorType] = _getset_descriptor_reduce
dispatch[types.ModuleType] = _module_reduce
dispatch[types.MethodType] = _method_reduce
dispatch[types.MappingProxyType] = _mappingproxy_reduce
Expand Down
5 changes: 5 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,11 @@ def test_logger(self):
# logging.Logger object
self.check_logger('cloudpickle.dummy_test_logger')

def test_getset_descriptor(self):
assert isinstance(float.real, types.GetSetDescriptorType)
depickled_descriptor = pickle_depickle(float.real)
self.assertIs(depickled_descriptor, float.real)

def test_abc(self):

@abc.abstractmethod
Expand Down

0 comments on commit 2db58f1

Please sign in to comment.