diff options
author | Lars Gustäbel <lars@gustaebel.de> | 2009-11-18 20:24:54 (GMT) |
---|---|---|
committer | Lars Gustäbel <lars@gustaebel.de> | 2009-11-18 20:24:54 (GMT) |
commit | 355538e5f5f2e2e71beecf891e450797b8a02586 (patch) | |
tree | 653639dd84d3bc4c861f0e0a6e42973ccded2e88 /Lib/tarfile.py | |
parent | c029942ea0cf2311e6a6af232a21d7d2085edcb8 (diff) | |
download | cpython-355538e5f5f2e2e71beecf891e450797b8a02586.zip cpython-355538e5f5f2e2e71beecf891e450797b8a02586.tar.gz cpython-355538e5f5f2e2e71beecf891e450797b8a02586.tar.bz2 |
Issue #7341: Close the internal file object in the TarFile
constructor in case of an error.
Diffstat (limited to 'Lib/tarfile.py')
-rw-r--r-- | Lib/tarfile.py | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index a87fe22..29e12f0 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1557,27 +1557,33 @@ class TarFile(object): self.inodes = {} # dictionary caching the inodes of # archive members already added - if self.mode == "r": - self.firstmember = None - self.firstmember = self.next() - - if self.mode == "a": - # Move to the end of the archive, - # before the first empty block. - self.firstmember = None - while True: - if self.next() is None: - if self.offset > 0: - self.fileobj.seek(- BLOCKSIZE, 1) - break - - if self.mode in "aw": - self._loaded = True - - if self.pax_headers: - buf = self.tarinfo.create_pax_global_header(self.pax_headers.copy()) - self.fileobj.write(buf) - self.offset += len(buf) + try: + if self.mode == "r": + self.firstmember = None + self.firstmember = self.next() + + if self.mode == "a": + # Move to the end of the archive, + # before the first empty block. + self.firstmember = None + while True: + if self.next() is None: + if self.offset > 0: + self.fileobj.seek(- BLOCKSIZE, 1) + break + + if self.mode in "aw": + self._loaded = True + + if self.pax_headers: + buf = self.tarinfo.create_pax_global_header(self.pax_headers.copy()) + self.fileobj.write(buf) + self.offset += len(buf) + except: + if not self._extfileobj: + self.fileobj.close() + self.closed = True + raise def _getposix(self): return self.format == USTAR_FORMAT |