Skip to content

Commit 5af68d7

Browse files
normanrzdaniel-wer
andauthored
Fix ssl context pickling, again (#1221)
* Add regression test for dataset picklability * try just string? * try copyreg * changelog * fix pickling, again * Fix typing, because 3.9 doesn't support Union | operator --------- Co-authored-by: Daniel Werner <[email protected]>
1 parent 02c02d5 commit 5af68d7

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

webknossos/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ For upgrade instructions, please check the respective _Breaking Changes_ section
1919
### Changed
2020

2121
### Fixed
22+
- Fixed pickling issue that has been introduced in 0.15.9. [#1218](https://github.com/scalableminds/webknossos-libs/pull/1218)
2223

2324

2425
## [0.15.10](https://github.com/scalableminds/webknossos-libs/releases/tag/v0.15.10) - 2024-11-25

webknossos/webknossos/dataset/defaults.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
import copyreg
22
import os
33
import ssl
4+
from typing import Any, Callable, Optional
45

56
import certifi
67

78
from ..geometry import Vec3Int
89
from .data_format import DataFormat
910

1011

12+
def _create_sslcontext() -> ssl.SSLContext:
13+
cafile = certifi.where()
14+
ssl_context = ssl.create_default_context(cafile=cafile)
15+
ssl_context.cafile = cafile # type: ignore
16+
return ssl_context
17+
18+
1119
def _save_sslcontext(
1220
obj: ssl.SSLContext,
13-
) -> tuple[type[ssl.SSLContext], tuple[ssl._SSLMethod]]:
14-
return obj.__class__, (obj.protocol,)
21+
) -> tuple[Callable[[Any, Any], ssl.SSLContext], tuple[ssl._SSLMethod, Optional[str]]]:
22+
cafile = getattr(obj, "cafile")
23+
return _rebuild_sslcontext, (obj.protocol, cafile)
24+
25+
26+
def _rebuild_sslcontext(
27+
protocol: ssl._SSLMethod, cafile: Optional[str]
28+
) -> ssl.SSLContext:
29+
ssl_context = ssl.SSLContext(protocol)
30+
if cafile is not None:
31+
ssl_context.load_verify_locations(cafile=cafile)
32+
return ssl_context
1533

1634

1735
copyreg.pickle(ssl.SSLContext, _save_sslcontext)
@@ -35,4 +53,4 @@ def _save_sslcontext(
3553
ZGROUP_FILE_NAME = ".zgroup"
3654
ZATTRS_FILE_NAME = ".zattrs"
3755
ZARR_JSON_FILE_NAME = "zarr.json"
38-
SSL_CONTEXT = ssl.create_default_context(cafile=certifi.where())
56+
SSL_CONTEXT = _create_sslcontext()

0 commit comments

Comments
 (0)