Skip to content

support property pickling in python 3.8 + #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
1.2.3
=====

- Fix a regression in cloudpickle and python3.8 causing an error when trying to
pickle property objects.
([PR #329](https://github.com/cloudpipe/cloudpickle/pull/329)).

- Fix a bug when a thread imports a module while cloudpickle iterates
over the module list
([PR #322](https://github.com/cloudpipe/cloudpickle/pull/322)).
Expand Down
5 changes: 5 additions & 0 deletions cloudpickle/cloudpickle_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ def _root_logger_reduce(obj):
return logging.getLogger, ()


def _property_reduce(obj):
return property, (obj.fget, obj.fset, obj.fdel, obj.__doc__)


def _weakset_reduce(obj):
return weakref.WeakSet, (list(obj),)

Expand Down Expand Up @@ -406,6 +410,7 @@ class CloudPickler(Pickler):
dispatch[logging.Logger] = _logger_reduce
dispatch[logging.RootLogger] = _root_logger_reduce
dispatch[memoryview] = _memoryview_reduce
dispatch[property] = _property_reduce
dispatch[staticmethod] = _classmethod_reduce
dispatch[types.CellType] = _cell_reduce
dispatch[types.CodeType] = _code_reduce
Expand Down
44 changes: 44 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,50 @@ def func(x):
cloned = pickle_depickle(func, protocol=self.protocol)
self.assertEqual(cloned.__qualname__, func.__qualname__)

def test_property(self):
class MyObject:
_read_only_value = 1
_read_write_value = 1

@property
def read_only_value(self):
"A read-only attribute"
return self._read_only_value

@property
def read_write_value(self):
return self._read_write_value

@read_write_value.setter
def read_write_value(self, value):
self._read_write_value = value



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the read-only property, this class should have a read-write property and one should check that the reconstructed instance allows to set the value of the read-write property while it should not be possible for the read-only property of the reconstructed instance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the pickling of the __doc__ info is not tested either.

my_object = MyObject()

assert my_object.read_only_value == 1
assert MyObject.read_only_value.__doc__ == "A read-only attribute"

with pytest.raises(AttributeError):
my_object.read_only_value = 2
my_object.read_write_value = 2

depickled_obj = pickle_depickle(my_object)

assert depickled_obj.read_only_value == 1
assert depickled_obj.read_write_value == 2

# make sure the depickled read_only_value attribute is still read-only
with pytest.raises(AttributeError):
my_object.read_only_value = 2

# make sure the depickled read_write_value attribute is writeable
depickled_obj.read_write_value = 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then check assert depickled_obj.read_write_value == 3 after that.

assert depickled_obj.read_write_value == 3
type(depickled_obj).read_only_value.__doc__ == "A read-only attribute"


def test_namedtuple(self):
MyTuple = collections.namedtuple('MyTuple', ['a', 'b', 'c'])
t1 = MyTuple(1, 2, 3)
Expand Down