Skip to content

Commit 04d420f

Browse files
committed
Move from zstandard to pyzstd
It will be part of the stdlib in 3.14, so lets move to that. https://peps.python.org/pep-0784/ and add some tests
1 parent f397101 commit 04d420f

File tree

4 files changed

+143
-223
lines changed

4 files changed

+143
-223
lines changed

msys2_devtools/db.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
import io
22

3-
import zstandard
3+
from pyzstd import ZstdFile, ZstdError
44
import tarfile
55

66

77
class ExtTarFile(tarfile.TarFile):
88
"""Extends TarFile to support zstandard"""
99

1010
@classmethod
11-
def zstdopen(cls, name, mode="r", fileobj=None, cctx=None, dctx=None, **kwargs): # type: ignore
12-
"""Open zstd compressed tar archive name for reading or writing.
13-
Appending is not allowed.
14-
"""
15-
if mode not in ("r"):
16-
raise ValueError("mode must be 'r'")
11+
def zstdopen(cls, name, mode="r", fileobj=None, **kwargs): # type: ignore
12+
"""Open zstd compressed tar archive"""
1713

14+
if mode not in ("r", "w", "x", "a"):
15+
raise ValueError("mode must be 'r', 'w' or 'x' or 'a'")
16+
17+
zstfileobj = None
1818
try:
19-
zobj = zstandard.open(fileobj or name, mode + "b", cctx=cctx, dctx=dctx)
20-
with zobj:
21-
data = zobj.read()
22-
except (zstandard.ZstdError, EOFError) as e:
19+
zstfileobj = ZstdFile(fileobj or name, mode)
20+
if "r" in mode:
21+
zstfileobj.peek(1) # raises ZstdError if not a zstd file
22+
except ZstdError as e:
23+
if zstfileobj is not None:
24+
zstfileobj.close()
2325
raise tarfile.ReadError("not a zstd file") from e
2426

25-
fileobj = io.BytesIO(data)
26-
t = cls.taropen(name, mode, fileobj, **kwargs)
27+
try:
28+
t = cls.taropen(name, mode, zstfileobj, **kwargs)
29+
except Exception:
30+
zstfileobj.close()
31+
raise
32+
2733
t._extfileobj = False
2834
return t
2935

0 commit comments

Comments
 (0)