Issue #10233: Close file objects in a timely manner in the tarfile module
and its test suite.
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index e33b982..d49e82f 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1800,20 +1800,18 @@
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