1212import queue
1313import struct
1414import threading
15- from typing import BinaryIO , List , Optional , Tuple
15+ from typing import List , Optional , Tuple
1616
1717from . import igzip , isal_zlib
1818
@@ -56,20 +56,13 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
5656 threads = multiprocessing .cpu_count ()
5757 except : # noqa: E722
5858 threads = 1
59- open_mode = mode .replace ("t" , "b" )
60- if isinstance (filename , (str , bytes )) or hasattr (filename , "__fspath__" ):
61- binary_file = builtins .open (filename , open_mode )
62- elif hasattr (filename , "read" ) or hasattr (filename , "write" ):
63- binary_file = filename
64- else :
65- raise TypeError ("filename must be a str or bytes object, or a file" )
6659 if "r" in mode :
6760 gzip_file = io .BufferedReader (
68- _ThreadedGzipReader (binary_file , block_size = block_size ))
61+ _ThreadedGzipReader (filename , block_size = block_size ))
6962 else :
7063 gzip_file = io .BufferedWriter (
7164 _ThreadedGzipWriter (
72- fp = binary_file ,
65+ filename ,
7366 block_size = block_size ,
7467 level = compresslevel ,
7568 threads = threads
@@ -81,10 +74,20 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
8174 return gzip_file
8275
8376
77+ def open_as_binary_stream (filename , open_mode ):
78+ if isinstance (filename , (str , bytes )) or hasattr (filename , "__fspath__" ):
79+ binary_file = builtins .open (filename , open_mode )
80+ elif hasattr (filename , "read" ) or hasattr (filename , "write" ):
81+ binary_file = filename
82+ else :
83+ raise TypeError ("filename must be a str or bytes object, or a file" )
84+ return binary_file
85+
86+
8487class _ThreadedGzipReader (io .RawIOBase ):
85- def __init__ (self , fp , queue_size = 2 , block_size = 1024 * 1024 ):
86- self .raw = fp
87- self .fileobj = igzip ._IGzipReader (fp , buffersize = 8 * block_size )
88+ def __init__ (self , filename , queue_size = 2 , block_size = 1024 * 1024 ):
89+ self .raw = open_as_binary_stream ( filename , "rb" )
90+ self .fileobj = igzip ._IGzipReader (self . raw , buffersize = 8 * block_size )
8891 self .pos = 0
8992 self .read_file = False
9093 self .queue = queue .Queue (queue_size )
@@ -193,15 +196,14 @@ class _ThreadedGzipWriter(io.RawIOBase):
193196 compressing and output is handled in one thread.
194197 """
195198 def __init__ (self ,
196- fp : BinaryIO ,
199+ filename ,
197200 level : int = isal_zlib .ISAL_DEFAULT_COMPRESSION ,
198201 threads : int = 1 ,
199202 queue_size : int = 1 ,
200203 block_size : int = 1024 * 1024 ,
201204 ):
202205 self .lock = threading .Lock ()
203206 self .exception : Optional [Exception ] = None
204- self .raw = fp
205207 self .level = level
206208 self .previous_block = b""
207209 # Deflating random data results in an output a little larger than the
@@ -236,6 +238,7 @@ def __init__(self,
236238 self .running = False
237239 self ._size = 0
238240 self ._closed = False
241+ self .raw = open_as_binary_stream (filename , "wb" )
239242 self ._write_gzip_header ()
240243 self .start ()
241244
0 commit comments