Skip to content

Commit

Permalink
allow for roundtrip of cloudpaths through pickle serialization
Browse files Browse the repository at this point in the history
This avoids an exception thrown because the client is not serialized
into the pickeld object, and thus when __getstate__ is called the second
time, there is no _client field to delete.

Closes #450
  • Loading branch information
kujenga committed Jul 21, 2024
1 parent 08b018b commit 47490cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cloudpathlib/cloudpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ def __getstate__(self) -> Dict[str, Any]:
state = self.__dict__.copy()

# don't pickle client
del state["_client"]
if "_client" in state:
del state["_client"]

return state

Expand Down
14 changes: 14 additions & 0 deletions tests/test_cloudpath_serialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pickle

from cloudpathlib import CloudPath


def test_pickle_roundtrip():
path1 = CloudPath("s3://bucket/key")
pkl1 = pickle.dumps(path1)

path2 = pickle.loads(pkl1)
pkl2 = pickle.dumps(path2)

assert path1 == path2
assert pkl1 == pkl2

0 comments on commit 47490cf

Please sign in to comment.