Skip to content

Use zstandard implementation from stdlib (PEP-784) #2034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ dependencies = [
"userpath~=1.7",
"uv>=0.5.23",
"virtualenv>=20.26.6",
"zstandard<1",
"backports.zstd>=0.4.0 ; python_version<'3.14'",
]
dynamic = ["version"]

Expand Down
43 changes: 18 additions & 25 deletions src/hatch/python/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
from abc import ABC, abstractmethod
from functools import cached_property
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Literal

from hatch.config.constants import PythonEnvVars
from hatch.errors import PythonDistributionResolutionError, PythonDistributionUnknownError
Expand Down Expand Up @@ -55,43 +55,36 @@ def archive_name(self) -> str:
return self.source.rsplit('/', 1)[-1]

def unpack(self, archive: Path, directory: Path) -> None:
# zip
if self.source.endswith('.zip'):
import zipfile

with zipfile.ZipFile(archive, 'r') as zf:
zf.extractall(directory)
elif self.source.endswith(('.tar.gz', '.tgz')):
import tarfile
return

with tarfile.open(archive, 'r:gz') as tf:
if sys.version_info[:2] >= (3, 12):
tf.extractall(directory, filter='data')
else:
tf.extractall(directory) # noqa: S202
elif self.source.endswith(('.tar.bz2', '.bz2')):
# tar
if sys.version_info >= (3, 14):
import tarfile
else:
# for zstd support (introduced in Python 3.14)
# and filter kwarg (introduced in Python 3.12)
from backports.zstd import tarfile

with tarfile.open(archive, 'r:bz2') as tf:
if sys.version_info[:2] >= (3, 12):
tf.extractall(directory, filter='data')
else:
tf.extractall(directory) # noqa: S202
mode: Literal['r:gz', 'r:bz2', 'r:zst']
if self.source.endswith(('.tar.gz', '.tgz')):
mode = 'r:gz'
elif self.source.endswith(('.tar.bz2', '.bz2')):
mode = 'r:bz2'
elif self.source.endswith(('.tar.zst', '.tar.zstd')):
import tarfile

import zstandard

with open(archive, 'rb') as ifh:
dctx = zstandard.ZstdDecompressor()
with dctx.stream_reader(ifh) as reader, tarfile.open(mode='r|', fileobj=reader) as tf:
if sys.version_info[:2] >= (3, 12):
tf.extractall(directory, filter='data')
else:
tf.extractall(directory) # noqa: S202
mode = 'r:zst'
else:
message = f'Unknown archive type: {archive}'
raise ValueError(message)

with tarfile.open(archive, mode) as tf:
tf.extractall(directory, filter='data')

@property
@abstractmethod
def version(self) -> Version:
Expand Down