Skip to content

Commit f3a1c5f

Browse files
authored
Switch zstandard library to stdlib compression.zstd or backports.zstd (#198)
1 parent b0cb20a commit f3a1c5f

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

flow/record/base.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,33 @@
2727
from flow.record.exceptions import RecordAdapterNotFound, RecordDescriptorError
2828
from flow.record.utils import get_stdin, get_stdout
2929

30+
# lz4
3031
try:
3132
import lz4.frame as lz4
3233

3334
HAS_LZ4 = True
3435
except ImportError:
3536
HAS_LZ4 = False
37+
38+
# bzip2
3639
try:
3740
import bz2
3841

3942
HAS_BZ2 = True
4043
except ImportError:
4144
HAS_BZ2 = False
42-
try:
43-
import zstandard as zstd
4445

46+
# zstandard
47+
try:
48+
if sys.version_info >= (3, 14):
49+
from compression import zstd # novermin
50+
else:
51+
from backports import zstd
4552
HAS_ZSTD = True
4653
except ImportError:
4754
HAS_ZSTD = False
4855

56+
# fastavro
4957
try:
5058
import fastavro as avro # noqa
5159

@@ -727,8 +735,7 @@ def open_stream(fp: BinaryIO, mode: str) -> BinaryIO:
727735
elif HAS_LZ4 and peek_data[:4] == LZ4_MAGIC:
728736
fp = lz4.open(fp, mode=mode)
729737
elif HAS_ZSTD and peek_data[:4] == ZSTD_MAGIC:
730-
dctx = zstd.ZstdDecompressor()
731-
fp = dctx.stream_reader(fp)
738+
fp = zstd.ZstdFile(fp, mode=mode)
732739

733740
return fp
734741

@@ -804,13 +811,8 @@ def open_path(path: str, mode: str, clobber: bool = True) -> IO:
804811
fp = lz4.open(path, mode)
805812
elif path.endswith((".zstd", ".zst")):
806813
if not HAS_ZSTD:
807-
raise RuntimeError("zstandard python module not available")
808-
if not out:
809-
dctx = zstd.ZstdDecompressor()
810-
fp = dctx.stream_reader(pathobj.open("rb"))
811-
else:
812-
cctx = zstd.ZstdCompressor()
813-
fp = cctx.stream_writer(pathobj.open("wb"))
814+
raise RuntimeError("backports.zstd python module not available")
815+
fp = zstd.ZstdFile(path, mode)
814816

815817
# normal file or stdio for reading or writing
816818
if not fp:

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ repository = "https://github.com/fox-it/flow.record"
3737
# Note: these compression libraries do not work well with pypy
3838
compression = [
3939
"lz4",
40-
"zstandard; platform_python_implementation != 'PyPy'",
40+
"backports.zstd; python_version < '3.14'",
4141
]
4242
elastic = [
4343
"elasticsearch",
@@ -68,7 +68,7 @@ full = [
6868
[dependency-groups]
6969
compression = [
7070
"lz4",
71-
"zstandard; platform_python_implementation != 'PyPy'",
71+
"backports.zstd; python_version < '3.14'",
7272
]
7373
elastic = [
7474
"elasticsearch",

tests/record/test_adapter.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ def test_compressed_writer_reader(tmp_path: Path, compression: str) -> None:
8585
if compression == "lz4" and not HAS_LZ4:
8686
pytest.skip("lz4 module not installed")
8787
if compression == "zstd" and not HAS_ZSTD:
88-
pytest.skip("zstandard module not installed")
88+
pytest.skip("backports.zstd module not installed")
8989

9090
if compression == "lz4" and platform.python_implementation() == "PyPy":
9191
pytest.skip("lz4 module not supported on PyPy")
92-
if compression == "zstd" and platform.python_implementation() == "PyPy":
93-
pytest.skip("zstandard module not supported on PyPy")
9492

9593
p = tmp_path.joinpath(f"{compression}-test")
9694
p.mkdir()

0 commit comments

Comments
 (0)