Skip to content

Commit 9fd4545

Browse files
authored
Fix pickling of ZipStore (#2762)
* Fix pickling of ZipStore * Add changelog entry
1 parent ad99a67 commit 9fd4545

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

changes/2762.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed ZipStore to make sure the correct attributes are saved when instances are pickled.
2+
This fixes a previous bug that prevent using ZipStore with a ProcessPoolExecutor.

src/zarr/storage/_zip.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,14 @@ def _sync_open(self) -> None:
107107
async def _open(self) -> None:
108108
self._sync_open()
109109

110-
def __getstate__(self) -> tuple[Path, ZipStoreAccessModeLiteral, int, bool]:
111-
return self.path, self._zmode, self.compression, self.allowZip64
112-
113-
def __setstate__(self, state: Any) -> None:
114-
self.path, self._zmode, self.compression, self.allowZip64 = state
110+
def __getstate__(self) -> dict[str, Any]:
111+
state = self.__dict__
112+
for attr in ["_zf", "_lock"]:
113+
state.pop(attr, None)
114+
return state
115+
116+
def __setstate__(self, state: dict[str, Any]) -> None:
117+
self.__dict__ = state
115118
self._is_open = False
116119
self._sync_open()
117120

src/zarr/testing/store.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ def test_store_eq(self, store: S, store_kwargs: dict[str, Any]) -> None:
7676
assert store == store2
7777

7878
def test_serializable_store(self, store: S) -> None:
79-
foo = pickle.dumps(store)
80-
assert pickle.loads(foo) == store
79+
new_store: S = pickle.loads(pickle.dumps(store))
80+
assert new_store == store
81+
assert new_store.read_only == store.read_only
8182

8283
def test_store_read_only(self, store: S) -> None:
8384
assert not store.read_only

0 commit comments

Comments
 (0)