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

Make ctypes optional #286

Merged
merged 5 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions changelog.d/284.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``ctypes`` is optional now however if it's missing, a bare ``super()`` will not work in slots classes.
This should only happen in special environments like Google App Engine.
2 changes: 2 additions & 0 deletions changelog.d/286.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``ctypes`` is optional now however if it's missing, a bare ``super()`` will not work in slots classes.
This should only happen in special environments like Google App Engine.
43 changes: 35 additions & 8 deletions src/attr/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,38 @@ def iteritems(d):
def metadata_proxy(d):
return types.MappingProxyType(dict(d))

if PYPY: # pragma: no cover
def set_closure_cell(cell, value):
cell.__setstate__((value,))
else:
import ctypes
set_closure_cell = ctypes.pythonapi.PyCell_Set
set_closure_cell.argtypes = (ctypes.py_object, ctypes.py_object)
set_closure_cell.restype = ctypes.c_int

def import_ctypes(): # pragma: nocover
"""
Moved into a function for testability.
"""
try:
import ctypes
return ctypes
except ImportError:
return None


def nop(*args, **kw): # pragma: nocover

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

pass


def make_set_closure_cell():
"""
Moved into a function for testability.
"""
if PYPY: # pragma: no cover
def set_closure_cell(cell, value):
cell.__setstate__((value,))
else:
ctypes = import_ctypes()
if ctypes is not None:
set_closure_cell = ctypes.pythonapi.PyCell_Set
set_closure_cell.argtypes = (ctypes.py_object, ctypes.py_object)
set_closure_cell.restype = ctypes.c_int
else:
set_closure_cell = nop
return set_closure_cell


set_closure_cell = make_set_closure_cell()
158 changes: 84 additions & 74 deletions tests/test_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import attr

from attr._compat import PY2
from attr._compat import PY2, PYPY, nop, make_set_closure_cell


@attr.s
Expand Down Expand Up @@ -325,76 +325,86 @@ class C2(C1Bare):


@pytest.mark.skipif(PY2, reason="closure cell rewriting is PY3-only.")
def test_closure_cell_rewriting():
"""
Slot classes support proper closure cell rewriting.

This affects features like `__class__` and the no-arg super().
"""
non_slot_instance = C1(x=1, y="test")
slot_instance = C1Slots(x=1, y="test")

assert non_slot_instance.my_class() is C1
assert slot_instance.my_class() is C1Slots

# Just assert they return something, and not an exception.
assert non_slot_instance.my_super()
assert slot_instance.my_super()


@pytest.mark.skipif(PY2, reason="closure cell rewriting is PY3-only.")
def test_closure_cell_rewriting_inheritance():
"""
Slot classes support proper closure cell rewriting when inheriting.

This affects features like `__class__` and the no-arg super().
"""
@attr.s
class C2(C1):
def my_subclass(self):
return __class__ # NOQA: F821

@attr.s
class C2Slots(C1Slots):
def my_subclass(self):
return __class__ # NOQA: F821

non_slot_instance = C2(x=1, y="test")
slot_instance = C2Slots(x=1, y="test")

assert non_slot_instance.my_class() is C1
assert slot_instance.my_class() is C1Slots

# Just assert they return something, and not an exception.
assert non_slot_instance.my_super()
assert slot_instance.my_super()

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
class TestClosureCellRewriting(object):
def test_closure_cell_rewriting(self):
"""
Slot classes support proper closure cell rewriting.

This affects features like `__class__` and the no-arg super().
"""
non_slot_instance = C1(x=1, y="test")
slot_instance = C1Slots(x=1, y="test")

assert non_slot_instance.my_class() is C1
assert slot_instance.my_class() is C1Slots

# Just assert they return something, and not an exception.
assert non_slot_instance.my_super()
assert slot_instance.my_super()

def test_inheritance(self):
"""
Slot classes support proper closure cell rewriting when inheriting.

This affects features like `__class__` and the no-arg super().
"""
@attr.s
class C2(C1):
def my_subclass(self):
return __class__ # NOQA: F821

@attr.s
class C2Slots(C1Slots):
def my_subclass(self):
return __class__ # NOQA: F821

non_slot_instance = C2(x=1, y="test")
slot_instance = C2Slots(x=1, y="test")

assert non_slot_instance.my_class() is C1
assert slot_instance.my_class() is C1Slots

# Just assert they return something, and not an exception.
assert non_slot_instance.my_super()
assert slot_instance.my_super()

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

@pytest.mark.parametrize("slots", [True, False])
def test_cls_static(self, 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

@pytest.mark.skipif(
PYPY,
reason="ctypes are used only on CPython 3"

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

)
def test_missing_ctypes(self, monkeypatch):
"""
Keeps working if ctypes is missing.
"""
monkeypatch.setattr(attr._compat, "import_ctypes", lambda: None)
rv = make_set_closure_cell()

assert nop is rv