Skip to content

Commit bddd908

Browse files
committed
Consistent signatures and compresslevel handling for _open functions
1 parent 62353e7 commit bddd908

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/xopen/__init__.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
BUFFER_SIZE = max(io.DEFAULT_BUFFER_SIZE, 128 * 1024)
4141

4242
XOPEN_DEFAULT_GZIP_COMPRESSION = 1
43+
XOPEN_DEFAULT_BZ2_COMPRESSION = 9
44+
XOPEN_DEFAULT_XZ_COMPRESSION = 6
45+
XOPEN_DEFAULT_ZST_COMPRESSION = 3
4346

4447
igzip: Optional[ModuleType]
4548
isal_zlib: Optional[ModuleType]
@@ -434,21 +437,29 @@ def _open_stdin_or_out(mode: str) -> BinaryIO:
434437
return open(std.fileno(), mode=mode, closefd=False) # type: ignore
435438

436439

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+
):
438446
assert "b" in mode
447+
if compresslevel is None:
448+
compresslevel = XOPEN_DEFAULT_BZ2_COMPRESSION
439449
if threads != 0:
440450
try:
441451
# pbzip2 can compress using multiple cores.
442452
return _PipedCompressionProgram(
443453
filename,
444454
mode,
455+
compresslevel,
445456
threads=threads,
446457
program_settings=_PROGRAM_SETTINGS["pbzip2"],
447458
)
448459
except OSError:
449460
pass # We try without threads.
450461

451-
return bz2.open(filename, mode)
462+
return bz2.open(filename, mode, compresslevel)
452463

453464

454465
def _open_xz(
@@ -459,7 +470,7 @@ def _open_xz(
459470
):
460471
assert "b" in mode
461472
if compresslevel is None:
462-
compresslevel = 6
473+
compresslevel = XOPEN_DEFAULT_XZ_COMPRESSION
463474

464475
if threads != 0:
465476
try:
@@ -481,7 +492,7 @@ def _open_xz(
481492
)
482493

483494

484-
def _open_zst( # noqa: C901
495+
def _open_zst(
485496
filename: FileOrPath,
486497
mode: str,
487498
compresslevel: Optional[int],
@@ -490,7 +501,7 @@ def _open_zst( # noqa: C901
490501
assert "b" in mode
491502
assert compresslevel != 0
492503
if compresslevel is None:
493-
compresslevel = 3
504+
compresslevel = XOPEN_DEFAULT_ZST_COMPRESSION
494505
if threads != 0:
495506
try:
496507
# zstd can compress using multiple cores
@@ -520,7 +531,12 @@ def _open_zst( # noqa: C901
520531
return f
521532

522533

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+
):
524540
"""
525541
Open a gzip file. The ISA-L library is preferred when applicable because
526542
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.
797813
elif detected_format == "xz":
798814
opened_file = _open_xz(filename, binary_mode, compresslevel, threads)
799815
elif detected_format == "bz2":
800-
opened_file = _open_bz2(filename, binary_mode, threads)
816+
opened_file = _open_bz2(filename, binary_mode, compresslevel, threads)
801817
elif detected_format == "zst":
802818
opened_file = _open_zst(filename, binary_mode, compresslevel, threads)
803819
else:

0 commit comments

Comments
 (0)