40
40
BUFFER_SIZE = max (io .DEFAULT_BUFFER_SIZE , 128 * 1024 )
41
41
42
42
XOPEN_DEFAULT_GZIP_COMPRESSION = 1
43
+ XOPEN_DEFAULT_BZ2_COMPRESSION = 9
44
+ XOPEN_DEFAULT_XZ_COMPRESSION = 6
45
+ XOPEN_DEFAULT_ZST_COMPRESSION = 3
43
46
44
47
igzip : Optional [ModuleType ]
45
48
isal_zlib : Optional [ModuleType ]
@@ -434,21 +437,29 @@ def _open_stdin_or_out(mode: str) -> BinaryIO:
434
437
return open (std .fileno (), mode = mode , closefd = False ) # type: ignore
435
438
436
439
437
- def _open_bz2 (filename : FileOrPath , mode : str , threads : Optional [int ]):
440
+ def _open_bz2 (
441
+ filename : FileOrPath ,
442
+ mode : str ,
443
+ compresslevel : Optional [int ],
444
+ threads : Optional [int ],
445
+ ):
438
446
assert "b" in mode
447
+ if compresslevel is None :
448
+ compresslevel = XOPEN_DEFAULT_BZ2_COMPRESSION
439
449
if threads != 0 :
440
450
try :
441
451
# pbzip2 can compress using multiple cores.
442
452
return _PipedCompressionProgram (
443
453
filename ,
444
454
mode ,
455
+ compresslevel ,
445
456
threads = threads ,
446
457
program_settings = _PROGRAM_SETTINGS ["pbzip2" ],
447
458
)
448
459
except OSError :
449
460
pass # We try without threads.
450
461
451
- return bz2 .open (filename , mode )
462
+ return bz2 .open (filename , mode , compresslevel )
452
463
453
464
454
465
def _open_xz (
@@ -459,7 +470,7 @@ def _open_xz(
459
470
):
460
471
assert "b" in mode
461
472
if compresslevel is None :
462
- compresslevel = 6
473
+ compresslevel = XOPEN_DEFAULT_XZ_COMPRESSION
463
474
464
475
if threads != 0 :
465
476
try :
@@ -481,7 +492,7 @@ def _open_xz(
481
492
)
482
493
483
494
484
- def _open_zst ( # noqa: C901
495
+ def _open_zst (
485
496
filename : FileOrPath ,
486
497
mode : str ,
487
498
compresslevel : Optional [int ],
@@ -490,7 +501,7 @@ def _open_zst( # noqa: C901
490
501
assert "b" in mode
491
502
assert compresslevel != 0
492
503
if compresslevel is None :
493
- compresslevel = 3
504
+ compresslevel = XOPEN_DEFAULT_ZST_COMPRESSION
494
505
if threads != 0 :
495
506
try :
496
507
# zstd can compress using multiple cores
@@ -520,7 +531,12 @@ def _open_zst( # noqa: C901
520
531
return f
521
532
522
533
523
- def _open_gz (filename : FileOrPath , mode : str , compresslevel , threads ):
534
+ def _open_gz (
535
+ filename : FileOrPath ,
536
+ mode : str ,
537
+ compresslevel : Optional [int ],
538
+ threads : Optional [int ],
539
+ ):
524
540
"""
525
541
Open a gzip file. The ISA-L library is preferred when applicable because
526
542
it is the fastest. Then zlib-ng which is not as fast, but supports all
@@ -797,7 +813,7 @@ def xopen( # noqa: C901 # The function is complex, but readable.
797
813
elif detected_format == "xz" :
798
814
opened_file = _open_xz (filename , binary_mode , compresslevel , threads )
799
815
elif detected_format == "bz2" :
800
- opened_file = _open_bz2 (filename , binary_mode , threads )
816
+ opened_file = _open_bz2 (filename , binary_mode , compresslevel , threads )
801
817
elif detected_format == "zst" :
802
818
opened_file = _open_zst (filename , binary_mode , compresslevel , threads )
803
819
else :
0 commit comments