Index: Lib/tarfile.py =================================================================== --- Lib/tarfile.py (revision 76357) +++ Lib/tarfile.py (working copy) @@ -1549,27 +1549,33 @@ 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