Skip to content

Commit 20df2cf

Browse files
committed
RF: Try to import zstd from compression.zstd, backports.zstd, or pyzstd in
that order. Use module name "zstd" instead of "pyzstd"
1 parent e9188e8 commit 20df2cf

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

nibabel/_compression.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@
2020
import io
2121

2222
import indexed_gzip # type: ignore[import]
23-
import pyzstd
23+
24+
# >= py314
25+
try:
26+
from compression import zstd # type: ignore[import]
27+
# < py314
28+
except ImportError:
29+
from backports import zstd # type: ignore[import]
2430

2531
HAVE_INDEXED_GZIP = True
2632
HAVE_ZSTD = True
2733
else:
2834
indexed_gzip, HAVE_INDEXED_GZIP, _ = optional_package('indexed_gzip')
29-
pyzstd, HAVE_ZSTD, _ = optional_package('pyzstd')
30-
35+
zstd, HAVE_ZSTD, _ = optional_package(('compression.zstd',
36+
'backports.zstd', 'pyzstd'))
3137

3238
# Collections of types for isinstance or exception matching
3339
COMPRESSED_FILE_LIKES: tuple[type[io.IOBase], ...] = (
@@ -47,5 +53,5 @@
4753
IndexedGzipFile = gzip.GzipFile
4854

4955
if HAVE_ZSTD:
50-
COMPRESSED_FILE_LIKES += (pyzstd.ZstdFile,)
51-
COMPRESSION_ERRORS += (pyzstd.ZstdError,)
56+
COMPRESSED_FILE_LIKES += (zstd.ZstdFile,)
57+
COMPRESSION_ERRORS += (zstd.ZstdError,)

nibabel/openers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from bz2 import BZ2File
1717
from os.path import splitext
1818

19-
from ._compression import HAVE_INDEXED_GZIP, IndexedGzipFile, pyzstd
19+
from ._compression import HAVE_INDEXED_GZIP, IndexedGzipFile, zstd
2020

2121
if ty.TYPE_CHECKING:
2222
from types import TracebackType
@@ -103,9 +103,9 @@ def _zstd_open(
103103
mode: Mode = 'r',
104104
*,
105105
level_or_option: int | dict | None = None,
106-
zstd_dict: pyzstd.ZstdDict | None = None,
107-
) -> pyzstd.ZstdFile:
108-
return pyzstd.ZstdFile(filename, mode, level_or_option=level_or_option, zstd_dict=zstd_dict)
106+
zstd_dict: zstd.ZstdDict | None = None,
107+
) -> zstd.ZstdFile:
108+
return zstd.ZstdFile(filename, mode, level_or_option=level_or_option, zstd_dict=zstd_dict)
109109

110110

111111
class Opener:

0 commit comments

Comments
 (0)