Skip to content

Commit 2217713

Browse files
committed
refactor gzip closing
1 parent c9cdbf1 commit 2217713

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

Lib/gzip.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,7 @@ def __init__(self, filename=None, mode=None,
250250
self._write_gzip_header(compresslevel)
251251
except BaseException:
252252
# Avoid a ResourceWarning if the write fails, eg read-only file or KeyboardInterrupt
253-
try:
254-
if self.myfileobj is not None:
255-
self.myfileobj.close()
256-
finally:
257-
self.fileobj = None
253+
self._close()
258254
raise
259255

260256
@property
@@ -396,11 +392,14 @@ def close(self):
396392
elif self.mode == READ:
397393
self._buffer.close()
398394
finally:
399-
self.fileobj = None
400-
myfileobj = self.myfileobj
401-
if myfileobj:
402-
self.myfileobj = None
403-
myfileobj.close()
395+
self._close()
396+
397+
def _close(self):
398+
self.fileobj = None
399+
myfileobj = self.myfileobj
400+
if myfileobj is not None:
401+
self.myfileobj = None
402+
myfileobj.close()
404403

405404
def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH):
406405
self._check_not_closed()

0 commit comments

Comments
 (0)