Skip to content

Commit

Permalink
Integrate zlib-ng
Browse files Browse the repository at this point in the history
  • Loading branch information
rhpvorderman committed Dec 23, 2023
1 parent 2b4d1f1 commit 0b33d81
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ classifiers = [
requires-python = ">=3.8"
dynamic = ["version"]
dependencies = [
'isal>=1.4.1; platform.machine == "x86_64" or platform.machine == "AMD64" or platform.machine == "aarch64"'
'isal>=1.4.1; platform.machine == "x86_64" or platform.machine == "AMD64" or platform.machine == "aarch64"',
'zlib-ng>=0.4.0; platform.machine == "x86_64" or platform.machine == "AMD64" or platform.machine == "aarch64"'
]

[project.urls]
Expand Down
25 changes: 25 additions & 0 deletions src/xopen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
isal_zlib = None
igzip_threaded = None

try:
from zlib_ng import gzip_ng, gzip_ng_threaded, zlib_ng
except ImportError:
gzip_ng = None
gzip_ng_threaded = None
zlib_ng = None

try:
import zstandard # type: ignore
except ImportError:
Expand Down Expand Up @@ -1062,6 +1069,14 @@ def _open_gz( # noqa: C901
)
except ValueError: # Wrong compression level
pass
if gzip_ng_threaded and threads != 0:
return gzip_ng_threaded.open(
filename,
mode,
zlib_ng.Z_DEFAULT_COMPRESSION if compresslevel is None else compresslevel,
**text_mode_kwargs,
threads=1 if threads is None else threads,
)
if threads != 0:
try:
if "r" in mode:
Expand All @@ -1078,6 +1093,8 @@ def _open_gz( # noqa: C901
if "r" in mode:
if igzip is not None:
return igzip.open(filename, mode, **text_mode_kwargs)
elif gzip_ng is not None:
return gzip_ng.open(filename, mode, **text_mode_kwargs)
return gzip.open(filename, mode, **text_mode_kwargs)

g = _open_reproducible_gzip(
Expand Down Expand Up @@ -1119,6 +1136,14 @@ def _open_reproducible_gzip(filename, mode, compresslevel):
except ValueError:
# Compression level not supported, move to built-in gzip.
pass
elif gzip_ng is not None:
gzip_file = gzip_ng.GzipNGFile(
**kwargs,
compresslevel=zlib_ng.Z_DEFAULT_COMPRESSION
if compresslevel is None
else compresslevel,
)

if gzip_file is None:
gzip_file = gzip.GzipFile(
**kwargs,
Expand Down

0 comments on commit 0b33d81

Please sign in to comment.