@@ -551,6 +551,12 @@ def _open_gz(
551
551
# Force the same compression level on every tool regardless of
552
552
# library defaults
553
553
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
+ )
554
560
555
561
if threads != 0 :
556
562
# Igzip level 0 does not output uncompressed deflate blocks as zlib does
@@ -563,17 +569,14 @@ def _open_gz(
563
569
threads = 1 ,
564
570
)
565
571
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
+ )
577
580
578
581
for program in ("pigz" , "gzip" ):
579
582
try :
@@ -584,7 +587,8 @@ def _open_gz(
584
587
threads ,
585
588
_PROGRAM_SETTINGS [program ],
586
589
)
587
- except OSError :
590
+ # ValueError when compresslevel is not supported. i.e. gzip and level 0
591
+ except (OSError , ValueError ):
588
592
pass # We try without threads.
589
593
return _open_reproducible_gzip (filename , mode = mode , compresslevel = compresslevel )
590
594
0 commit comments