@@ -88,6 +88,10 @@ def __init__(self, fp, queue_size=4, block_size=8 * 1024 * 1024):
8888 self .running = True
8989 self .worker .start ()
9090
91+ def _check_closed (self , msg = None ):
92+ if self ._closed :
93+ raise ValueError ("I/O operation on closed file" )
94+
9195 def _decompress (self ):
9296 block_size = self .block_size
9397 block_queue = self .queue
@@ -107,8 +111,7 @@ def _decompress(self):
107111 pass
108112
109113 def readinto (self , b ):
110- if self ._closed :
111- raise ValueError ("I/O operation on closed file" )
114+ self ._check_closed ()
112115 result = self .buffer .readinto (b )
113116 if result == 0 :
114117 while True :
@@ -130,11 +133,12 @@ def readable(self) -> bool:
130133 return True
131134
132135 def tell (self ) -> int :
133- if self ._closed :
134- raise ValueError ("I/O operation on closed file" )
136+ self ._check_closed ()
135137 return self .pos
136138
137139 def close (self ) -> None :
140+ if self ._closed :
141+ return
138142 self .running = False
139143 self .worker .join ()
140144 self .fileobj .close ()
@@ -208,6 +212,10 @@ def __init__(self,
208212 self ._write_gzip_header ()
209213 self .start ()
210214
215+ def _check_closed (self , msg = None ):
216+ if self ._closed :
217+ raise ValueError ("I/O operation on closed file" )
218+
211219 def _write_gzip_header (self ):
212220 """Simple gzip header. Only xfl flag is set according to level."""
213221 magic1 = 0x1f
@@ -234,11 +242,10 @@ def stop(self):
234242 self .output_worker .join ()
235243
236244 def write (self , b ) -> int :
245+ self ._check_closed ()
237246 with self .lock :
238247 if self .exception :
239248 raise self .exception
240- if self ._closed :
241- raise IOError ("Can not write closed file" )
242249 index = self .index
243250 data = bytes (b )
244251 zdict = memoryview (self .previous_block )[- DEFLATE_WINDOW_SIZE :]
@@ -249,8 +256,7 @@ def write(self, b) -> int:
249256 return len (data )
250257
251258 def flush (self ):
252- if self ._closed :
253- raise IOError ("Can not write closed file" )
259+ self ._check_closed ()
254260 # Wait for all data to be compressed
255261 for in_q in self .input_queues :
256262 in_q .join ()
0 commit comments