Skip to content

Commit

Permalink
Make compression check code more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
rhpvorderman committed Oct 11, 2021
1 parent 5ede0f9 commit 3811b15
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/xopen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import subprocess
import tempfile
import time
import _compression
from abc import ABC, abstractmethod
from subprocess import Popen, PIPE, DEVNULL
from typing import Optional, TextIO, AnyStr, IO, List, Set
Expand Down Expand Up @@ -744,8 +743,10 @@ def xopen(
# The "write" method for GzipFile is very costly. Lots of python calls are
# made. To a lesser extent this is true for LzmaFile and BZ2File. By
# putting a buffer in between, the expensive write method is called much
# less. The effect is very noticable when writing small units such as lines
# or FASTQ records.
if isinstance(opened_file, _compression.BaseStream) and "w" in mode:
# less. The effect is very noticeable when writing small units such as
# lines or FASTQ records.
if (isinstance(opened_file, gzip.GzipFile) or
isinstance(opened_file, bz2.BZ2File) or
isinstance(opened_file, lzma.LZMAFile)) and "w" in mode:
opened_file = io.BufferedWriter(opened_file) # type: ignore
return opened_file

0 comments on commit 3811b15

Please sign in to comment.