Skip to content
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
when importing cloudpickle.
([issue #337](https://github.com/cloudpipe/cloudpickle/pull/337))

- Fix a bug affecting subclasses of slotted classes.
([issue #311](https://github.com/cloudpipe/cloudpickle/issues/311))

1.2.2
=====

Expand Down
2 changes: 1 addition & 1 deletion cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def save_dynamic_class(self, obj):
# doc can't participate in a cycle with the original class.
type_kwargs = {'__doc__': clsdict.pop('__doc__', None)}

if hasattr(obj, "__slots__"):
if "__slots__" in clsdict:
type_kwargs['__slots__'] = obj.__slots__
# pickle string length optimization: member descriptors of obj are
# created automatically from obj's __slots__ attribute, no need to
Expand Down
4 changes: 2 additions & 2 deletions cloudpickle/cloudpickle_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def dumps(obj, protocol=None, buffer_callback=None):

def _class_getnewargs(obj):
type_kwargs = {}
if hasattr(obj, "__slots__"):
if "__slots__" in obj.__dict__:
type_kwargs["__slots__"] = obj.__slots__

__dict__ = obj.__dict__.get('__dict__', None)
Expand Down Expand Up @@ -143,7 +143,7 @@ def _class_getstate(obj):
(registry, _, _, _) = abc._get_dump(obj)
clsdict["_abc_impl"] = [subclass_weakref()
for subclass_weakref in registry]
if hasattr(obj, "__slots__"):
if "__slots__" in clsdict:
# pickle string length optimization: member descriptors of obj are
# created automatically from obj's __slots__ attribute, no need to
# save them in obj's state
Expand Down
11 changes: 11 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,17 @@ def __init__(self):
with pytest.raises(AttributeError):
obj.non_registered_attribute = 1

class SubclassWithSlots(ClassWithSlots):
def __init__(self):
self.unregistered_attribute = 1

obj = SubclassWithSlots()
s = cloudpickle.dumps(obj, protocol=self.protocol)
del SubclassWithSlots
depickled_obj = cloudpickle.loads(s)
assert depickled_obj.unregistered_attribute == 1


@unittest.skipIf(not hasattr(types, "MappingProxyType"),
"Old versions of Python do not have this type.")
def test_mappingproxy(self):
Expand Down