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 cell setting completely best effort #334

Merged
merged 4 commits into from
Jan 27, 2018
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 changelog.d/321.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Setting the cell type is now completely best effort.
This fixes ``attrs`` on Jython.

We cannot make any guarantees regarding Jython though, because our test suite cannot run due to dependency incompatabilities.
4 changes: 4 additions & 0 deletions changelog.d/334.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Setting the cell type is now completely best effort.
This fixes ``attrs`` on Jython.

We cannot make any guarantees regarding Jython though, because our test suite cannot run due to dependency incompatabilities.
18 changes: 9 additions & 9 deletions src/attr/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,12 @@ def metadata_proxy(d):
return types.MappingProxyType(dict(d))


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


if not PY2:
Expand Down Expand Up @@ -126,12 +123,15 @@ def make_set_closure_cell():
def set_closure_cell(cell, value):
cell.__setstate__((value,))
else:
ctypes = import_ctypes()
if ctypes is not None:
try:
ctypes = 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
else:
except Exception:
# We try best effort to set the cell, but sometimes it's not
# possible. For example on Jython or on GAE.
set_closure_cell = just_warn
return set_closure_cell

Expand Down