Index: Lib/tarfile.py =================================================================== --- Lib/tarfile.py (revision 84663) +++ Lib/tarfile.py (working copy) @@ -1729,9 +1729,12 @@ if filemode not in "rw": raise ValueError("mode must be 'r' or 'w'") - t = cls(name, filemode, - _Stream(name, filemode, comptype, fileobj, bufsize), - **kwargs) + stream = _Stream(name, filemode, comptype, fileobj, bufsize) + try: + t = cls(name, filemode, stream, **kwargs) + except: + stream.close() + raise t._extfileobj = False return t