Skip to content

Commit 03fbcd6

Browse files
committed
Use compression.zstd (PEP-784)
1 parent 7a36a8f commit 03fbcd6

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ azure = ["azure-storage-blob", "azure-common", "azure-core"]
3939
http = ["requests"]
4040
webhdfs = ["requests"]
4141
ssh = ["paramiko"]
42-
zst = ["zstandard"]
42+
zst = ["backports.zstd>=1.0.0;python_version<'3.14'"]
4343
all = ["smart_open[s3,gcs,azure,http,webhdfs,ssh,zst]"]
4444
test = [
4545
"smart_open[all]",

smart_open/compression.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,12 @@ def _handle_gzip(file_obj, mode):
119119

120120

121121
def _handle_zstd(file_obj, mode):
122-
import zstandard
123-
result = zstandard.open(filename=file_obj, mode=mode)
122+
import sys
123+
if sys.version_info >= (3, 14):
124+
from compression import zstd
125+
else:
126+
from backports import zstd
127+
result = zstd.open(file_obj, mode=mode)
124128
return _maybe_wrap_buffered(result, mode)
125129

126130

tests/test_compression.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
import gzip
1010
import io
1111
import lzma
12+
import sys
1213

1314
import pytest
14-
import zstandard as zstd
15+
16+
if sys.version_info >= (3, 14):
17+
from compression import zstd
18+
else:
19+
from backports import zstd
1520

1621
import smart_open.compression
1722

@@ -35,10 +40,10 @@ def label(thing, name):
3540
(io.BytesIO(gzip.compress(plain)), 'infer_from_extension', 'file.GZ'),
3641
(label(io.BytesIO(gzip.compress(plain)), 'file.gz'), 'infer_from_extension', ''),
3742
(io.BytesIO(gzip.compress(plain)), '.gz', 'file.gz'),
38-
(io.BytesIO(zstd.ZstdCompressor().compress(plain)), 'infer_from_extension', 'file.zst'),
39-
(io.BytesIO(zstd.ZstdCompressor().compress(plain)), 'infer_from_extension', 'file.ZST'),
40-
(label(io.BytesIO(zstd.ZstdCompressor().compress(plain)), 'file.zst'), 'infer_from_extension', ''),
41-
(io.BytesIO(zstd.ZstdCompressor().compress(plain)), '.zst', 'file.zst'),
43+
(io.BytesIO(zstd.compress(plain)), 'infer_from_extension', 'file.zst'),
44+
(io.BytesIO(zstd.compress(plain)), 'infer_from_extension', 'file.ZST'),
45+
(label(io.BytesIO(zstd.compress(plain)), 'file.zst'), 'infer_from_extension', ''),
46+
(io.BytesIO(zstd.compress(plain)), '.zst', 'file.zst'),
4247
(io.BytesIO(lzma.compress(plain)), 'infer_from_extension', 'file.xz'),
4348
(io.BytesIO(lzma.compress(plain)), 'infer_from_extension', 'file.XZ'),
4449
(label(io.BytesIO(lzma.compress(plain)), 'file.xz'), 'infer_from_extension', ''),

0 commit comments

Comments
 (0)