From 7b465390fa2a23f94d7ce091959885907fe08442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Gust=C3=A4bel?= Date: Wed, 18 Nov 2009 20:29:25 +0000 Subject: [PATCH] Merged revisions 76381 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r76381 | lars.gustaebel | 2009-11-18 21:24:54 +0100 (Wed, 18 Nov 2009) | 3 lines Issue #7341: Close the internal file object in the TarFile constructor in case of an error. ........ --- Lib/tarfile.py | 42 ++++++++++++++++++++++++------------------ Misc/NEWS | 3 +++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 7b7c25e831a..6ce5fb1aadc 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1549,27 +1549,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() + 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(self.fileobj.tell() - BLOCKSIZE) - break + 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(self.fileobj.tell() - BLOCKSIZE) + break - if self.mode in "aw": - self._loaded = True + 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) + 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 #-------------------------------------------------------------------------- # Below are the classmethods which act as alternate constructors to the diff --git a/Misc/NEWS b/Misc/NEWS index 2e3dee3e093..2d47c04cce8 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -137,6 +137,9 @@ C-API Library ------- +- Issue #7341: Close the internal file object in the TarFile constructor in + case of an error. + - Issue #7293: distutils.test_msvc9compiler is fixed to work on any fresh Windows box. Help provided by David Bolen.