-
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull thread-local into _compat module to fix cloudpickling.
Because cloudpickle tries to pickle a function's globals, when it pickled an attrs instance, it would try to pickle the `__repr__` method and its globals, which included a `threading.local`. This broke cloudpickle for all attrs classes unless they explicitly specified `repr=False`. Modules, however, are pickled by reference, not by value, so moving the repr into a different module means we can put `_compat` into the function's globals and not worry about direct references. Includes a test to ensure that attrs and cloudpickle remain compatible. Also adds an explanation of the reason we even *have* that global thread-local variable. It wasn't completely obvious to a reader why the thread-local was needed to track reference cycles in `__repr__` calls, and the test did not previously contain a cycle that touched a non-attrs value. This change adds a comment explaining the need and tests a cycle that contains non-attrs values. Fixes: - #458 - cloudpipe/cloudpickle#320
- Loading branch information
1 parent
9eccd70
commit d7615cc
Showing
6 changed files
with
89 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Attrs classes are now fully compatible with cloudpickle. | ||
`#847 <https://github.com/python-attrs/attrs/issues/847>`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Tests for compatibility against other Python modules. | ||
""" | ||
|
||
import cloudpickle | ||
|
||
from hypothesis import given | ||
|
||
from .strategies import simple_classes | ||
|
||
|
||
class TestCloudpickleCompat(object): | ||
""" | ||
Tests for compatibility with ``cloudpickle``. | ||
""" | ||
|
||
@given(simple_classes()) | ||
def test_repr(self, cls): | ||
""" | ||
attrs instances can be pickled and un-pickled with cloudpickle. | ||
""" | ||
inst = cls() | ||
# Exact values aren't a concern so long as neither direction | ||
# raises an exception. | ||
pkl = cloudpickle.dumps(inst) | ||
cloudpickle.loads(pkl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters