summaryrefslogtreecommitdiffstats
path: root/Lib/gzip.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-04-10 10:29:28 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-04-10 10:29:28 (GMT)
commit2116b12da59f77358cc539b90f58a3cdea43c2fd (patch)
tree693d26652b1dbf8b2ac058c5725f9726c3d14a0b /Lib/gzip.py
parentfcbf8f3e3d46eb023dd9a3954c6f9ed9a533d427 (diff)
parent7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd (diff)
downloadcpython-2116b12da59f77358cc539b90f58a3cdea43c2fd.zip
cpython-2116b12da59f77358cc539b90f58a3cdea43c2fd.tar.gz
cpython-2116b12da59f77358cc539b90f58a3cdea43c2fd.tar.bz2
Issue #23865: close() methods in multiple modules now are idempotent and more
robust at shutdown. If needs to release multiple resources, they are released even if errors are occured.
Diffstat (limited to 'Lib/gzip.py')
-rw-r--r--Lib/gzip.py26
1 files changed, 14 insertions, 12 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py
index 21d83e6..deaf15d 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -503,19 +503,21 @@ class GzipFile(io.BufferedIOBase):
return self.fileobj is None
def close(self):
- if self.fileobj is None:
+ fileobj = self.fileobj
+ if fileobj is None:
return
- if self.mode == WRITE:
- self.fileobj.write(self.compress.flush())
- write32u(self.fileobj, self.crc)
- # self.size may exceed 2GB, or even 4GB
- write32u(self.fileobj, self.size & 0xffffffff)
- self.fileobj = None
- elif self.mode == READ:
- self.fileobj = None
- if self.myfileobj:
- self.myfileobj.close()
- self.myfileobj = None
+ self.fileobj = None
+ try:
+ if self.mode == WRITE:
+ fileobj.write(self.compress.flush())
+ write32u(fileobj, self.crc)
+ # self.size may exceed 2GB, or even 4GB
+ write32u(fileobj, self.size & 0xffffffff)
+ finally:
+ myfileobj = self.myfileobj
+ if myfileobj:
+ self.myfileobj = None
+ myfileobj.close()
def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH):
self._check_closed()