@@ -88,6 +88,10 @@ def __init__(self, fp, queue_size=4, block_size=8 * 1024 * 1024):
88
88
self .running = True
89
89
self .worker .start ()
90
90
91
+ def _check_closed (self , msg = None ):
92
+ if self ._closed :
93
+ raise ValueError ("I/O operation on closed file" )
94
+
91
95
def _decompress (self ):
92
96
block_size = self .block_size
93
97
block_queue = self .queue
@@ -107,8 +111,7 @@ def _decompress(self):
107
111
pass
108
112
109
113
def readinto (self , b ):
110
- if self ._closed :
111
- raise ValueError ("I/O operation on closed file" )
114
+ self ._check_closed ()
112
115
result = self .buffer .readinto (b )
113
116
if result == 0 :
114
117
while True :
@@ -130,11 +133,12 @@ def readable(self) -> bool:
130
133
return True
131
134
132
135
def tell (self ) -> int :
133
- if self ._closed :
134
- raise ValueError ("I/O operation on closed file" )
136
+ self ._check_closed ()
135
137
return self .pos
136
138
137
139
def close (self ) -> None :
140
+ if self ._closed :
141
+ return
138
142
self .running = False
139
143
self .worker .join ()
140
144
self .fileobj .close ()
@@ -208,6 +212,10 @@ def __init__(self,
208
212
self ._write_gzip_header ()
209
213
self .start ()
210
214
215
+ def _check_closed (self , msg = None ):
216
+ if self ._closed :
217
+ raise ValueError ("I/O operation on closed file" )
218
+
211
219
def _write_gzip_header (self ):
212
220
"""Simple gzip header. Only xfl flag is set according to level."""
213
221
magic1 = 0x1f
@@ -234,11 +242,10 @@ def stop(self):
234
242
self .output_worker .join ()
235
243
236
244
def write (self , b ) -> int :
245
+ self ._check_closed ()
237
246
with self .lock :
238
247
if self .exception :
239
248
raise self .exception
240
- if self ._closed :
241
- raise IOError ("Can not write closed file" )
242
249
index = self .index
243
250
data = bytes (b )
244
251
zdict = memoryview (self .previous_block )[- DEFLATE_WINDOW_SIZE :]
@@ -249,8 +256,7 @@ def write(self, b) -> int:
249
256
return len (data )
250
257
251
258
def flush (self ):
252
- if self ._closed :
253
- raise IOError ("Can not write closed file" )
259
+ self ._check_closed ()
254
260
# Wait for all data to be compressed
255
261
for in_q in self .input_queues :
256
262
in_q .join ()
0 commit comments