Skip to content
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

Fix cell rewriting some more. #270

Merged
merged 3 commits into from
Oct 19, 2017
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
8 changes: 7 additions & 1 deletion src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,13 @@ def slots_setstate(self, state):
# as `method.__closure__`. Since we replace the class with a
# clone, we rewrite these references so it keeps working.
for item in cls.__dict__.values():
closure_cells = getattr(item, "__closure__", None)
if isinstance(item, (classmethod, staticmethod)):
# Class- and staticmethods hide their functions inside.
# These might need to be rewritten as well.
closure_cells = getattr(item.__func__, "__closure__", None)
else:
closure_cells = getattr(item, "__closure__", None)

if not closure_cells: # Catch None or the empty list.
continue
for cell in closure_cells:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,30 @@ def my_subclass(self):

assert non_slot_instance.my_subclass() is C2
assert slot_instance.my_subclass() is C2Slots


@pytest.mark.skipif(PY2, reason="closure cell rewriting is PY3-only.")
@pytest.mark.parametrize("slots", [True, False])
def test_closure_cell_rewriting_cls_static(slots):
"""
Slot classes support proper closure cell rewriting for class- and static
methods.
"""
# Python can reuse closure cells, so we create new classes just for
# this test.

@attr.s(slots=slots)
class C:
@classmethod
def clsmethod(cls):
return __class__ # noqa: F821

assert C.clsmethod() is C

@attr.s(slots=slots)
class D:
@staticmethod
def statmethod():
return __class__ # noqa: F821

assert D.statmethod() is D