Ignore __weakref__ in __setstate__ & __getstate__ (#326)
This commit is contained in:
parent
1f0d851996
commit
8278088a70
|
@ -0,0 +1 @@
|
|||
In slots classes, ``__getstate__`` and ``__setstate__`` now ignore the ``__weakref__`` attribute.
|
|
@ -0,0 +1 @@
|
|||
In slots classes, ``__getstate__`` and ``__setstate__`` now ignore the ``__weakref__`` attribute.
|
|
@ -443,20 +443,26 @@ class _ClassBuilder(object):
|
|||
if qualname is not None:
|
||||
cd["__qualname__"] = qualname
|
||||
|
||||
attr_names = tuple(self._attr_names)
|
||||
# __weakref__ is not writable.
|
||||
state_attr_names = tuple(
|
||||
an for an in self._attr_names if an != "__weakref__"
|
||||
)
|
||||
|
||||
def slots_getstate(self):
|
||||
"""
|
||||
Automatically created by attrs.
|
||||
"""
|
||||
return tuple(getattr(self, name) for name in attr_names)
|
||||
return tuple(
|
||||
getattr(self, name)
|
||||
for name in state_attr_names
|
||||
)
|
||||
|
||||
def slots_setstate(self, state):
|
||||
"""
|
||||
Automatically created by attrs.
|
||||
"""
|
||||
__bound_setattr = _obj_setattr.__get__(self, Attribute)
|
||||
for name, value in zip(attr_names, state):
|
||||
for name, value in zip(state_attr_names, state):
|
||||
__bound_setattr(name, value)
|
||||
|
||||
# slots and frozen require __getstate__/__setstate__ to work
|
||||
|
|
|
@ -4,6 +4,7 @@ Tests for `attr._make`.
|
|||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
import copy
|
||||
import inspect
|
||||
import itertools
|
||||
import sys
|
||||
|
@ -1054,3 +1055,16 @@ class TestClassBuilder(object):
|
|||
|
||||
assert "42" == rv.__module__ == fake_meth.__module__
|
||||
assert "23" == rv.__qualname__ == fake_meth.__qualname__
|
||||
|
||||
def test_weakref_setstate(self):
|
||||
"""
|
||||
__weakref__ is not set on in setstate because it's not writable in
|
||||
slots classes.
|
||||
"""
|
||||
@attr.s(slots=True)
|
||||
class C(object):
|
||||
__weakref__ = attr.ib(
|
||||
init=False, hash=False, repr=False, cmp=False
|
||||
)
|
||||
|
||||
assert C() == copy.deepcopy(C())
|
||||
|
|
Loading…
Reference in New Issue