From c6bba8e6d3d9874191dfea7e9e440bc8183bb7b4 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Wed, 17 Jan 2024 09:31:47 +0100 Subject: [PATCH] Only use Python-isal for levels 1 and 2 --- src/xopen/__init__.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/xopen/__init__.py b/src/xopen/__init__.py index ff7b2f2..3c6e883 100644 --- a/src/xopen/__init__.py +++ b/src/xopen/__init__.py @@ -1021,17 +1021,17 @@ def _open_gz( # noqa: C901 compresslevel = XOPEN_DEFAULT_GZIP_COMPRESSION if threads != 0: - if igzip_threaded: - try: - return igzip_threaded.open( # type: ignore - filename, - mode, - compresslevel, - **text_mode_kwargs, - threads=1, - ) - except ValueError: # Wrong compression level - pass + # igzip level 0 compresses, while zlib outputs data in uncompressed + # deflate format. igzip level 3 is similar in size to 1 and 2 and slower. + # Other libraries do a better job at these levels. + if igzip_threaded and (compresslevel in (1, 2) or "r" in mode): + return igzip_threaded.open( # type: ignore + filename, + mode, + compresslevel, + **text_mode_kwargs, + threads=1, + ) if gzip_ng_threaded and zlib_ng: try: return gzip_ng_threaded.open( @@ -1099,12 +1099,8 @@ def _open_reproducible_gzip(filename, mode: str, compresslevel: int): mtime=0, ) gzip_file = None - if igzip is not None: - try: - gzip_file = igzip.IGzipFile(**kwargs, compresslevel=compresslevel) - except ValueError: - # Compression level not supported, move to built-in gzip. - pass + if igzip is not None and (compresslevel in (1, 2) or "r" in mode): + gzip_file = igzip.IGzipFile(**kwargs, compresslevel=compresslevel) elif gzip_ng is not None: # Compression level should be at least 2 for zlib-ng to prevent very big files. gzip_file = gzip_ng.GzipNGFile(**kwargs, compresslevel=max(compresslevel, 2))