Skip to content

Commit f9084ee

Browse files
committed
Consistently handle gzip compression levels
Fixes #144
1 parent 9c642b1 commit f9084ee

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/xopen/__init__.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,12 @@ def _open_gz(
551551
# Force the same compression level on every tool regardless of
552552
# library defaults
553553
compresslevel = XOPEN_DEFAULT_GZIP_COMPRESSION
554+
if compresslevel not in range(10):
555+
# Level 0-9 are supported regardless of backend support
556+
# (zlib_ng supports -1, pigz supports 11 etc.)
557+
raise ValueError(
558+
f"gzip compresslevel must be in range 0-9, got {compresslevel}."
559+
)
554560

555561
if threads != 0:
556562
# Igzip level 0 does not output uncompressed deflate blocks as zlib does
@@ -563,17 +569,14 @@ def _open_gz(
563569
threads=1,
564570
)
565571
if gzip_ng_threaded and zlib_ng:
566-
try:
567-
return gzip_ng_threaded.open(
568-
filename,
569-
mode,
570-
# zlib-ng level 1 is 50% bigger than zlib level 1. Level
571-
# 2 gives a size close to expectations.
572-
compresslevel=2 if compresslevel == 1 else compresslevel,
573-
threads=threads or max(_available_cpu_count(), 4),
574-
)
575-
except zlib_ng.error: # Bad compression level
576-
pass
572+
return gzip_ng_threaded.open(
573+
filename,
574+
mode,
575+
# zlib-ng level 1 is 50% bigger than zlib level 1. Level
576+
# 2 gives a size close to expectations.
577+
compresslevel=2 if compresslevel == 1 else compresslevel,
578+
threads=threads or max(_available_cpu_count(), 4),
579+
)
577580

578581
for program in ("pigz", "gzip"):
579582
try:
@@ -584,7 +587,8 @@ def _open_gz(
584587
threads,
585588
_PROGRAM_SETTINGS[program],
586589
)
587-
except OSError:
590+
# ValueError when compresslevel is not supported. i.e. gzip and level 0
591+
except (OSError, ValueError):
588592
pass # We try without threads.
589593
return _open_reproducible_gzip(filename, mode=mode, compresslevel=compresslevel)
590594

0 commit comments

Comments
 (0)