Skip to content

Commit 47490cf

Browse files
committed
allow for roundtrip of cloudpaths through pickle serialization
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
1 parent 08b018b commit 47490cf

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

cloudpathlib/cloudpath.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ def __getstate__(self) -> Dict[str, Any]:
263263
state = self.__dict__.copy()
264264

265265
# don't pickle client
266-
del state["_client"]
266+
if "_client" in state:
267+
del state["_client"]
267268

268269
return state
269270

tests/test_cloudpath_serialize.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pickle
2+
3+
from cloudpathlib import CloudPath
4+
5+
6+
def test_pickle_roundtrip():
7+
path1 = CloudPath("s3://bucket/key")
8+
pkl1 = pickle.dumps(path1)
9+
10+
path2 = pickle.loads(pkl1)
11+
pkl2 = pickle.dumps(path2)
12+
13+
assert path1 == path2
14+
assert pkl1 == pkl2

0 commit comments

Comments
 (0)