Issue #10233: Close file objects in a timely manner in the tarfile module

and its test suite.
This commit is contained in:
Antoine Pitrou 2010-10-29 23:49:49 +00:00
parent 749afa95ce
commit e1eca4e3f5
3 changed files with 58 additions and 36 deletions

View file

@ -1800,20 +1800,18 @@ class TarFile(object):
except (ImportError, AttributeError):
raise CompressionError("gzip module is not available")
if fileobj is None:
fileobj = bltn_open(name, mode + "b")
extfileobj = False
else:
extfileobj = True
extfileobj = fileobj is not None
try:
t = cls.taropen(name, mode,
gzip.GzipFile(name, mode, compresslevel, fileobj),
**kwargs)
fileobj = gzip.GzipFile(name, mode + "b", compresslevel, fileobj)
t = cls.taropen(name, mode, fileobj, **kwargs)
except IOError:
if not extfileobj:
fileobj.close()
raise ReadError("not a gzip file")
except:
if not extfileobj:
fileobj.close()
raise
t._extfileobj = extfileobj
return t