diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-04-10 10:24:41 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-04-10 10:24:41 (GMT) |
commit | 7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd (patch) | |
tree | a0777a3e70ae76f294fac756c684ec4e24d5df1d /Lib/gzip.py | |
parent | 842f00e72509db50957ceb00d289b305dbc5a0a5 (diff) | |
download | cpython-7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd.zip cpython-7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd.tar.gz cpython-7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd.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.py | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index 8b12225..7ad00e1 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -500,19 +500,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() |