Fix #11513: wrong exception handling for the case that GzipFile itself raises an IOError.
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 63c7554..3d52a81 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1804,11 +1804,13 @@
fileobj = gzip.GzipFile(name, mode + "b", compresslevel, fileobj)
t = cls.taropen(name, mode, fileobj, **kwargs)
except IOError:
- if not extfileobj:
+ if not extfileobj and fileobj is not None:
fileobj.close()
+ if fileobj is None:
+ raise
raise ReadError("not a gzip file")
except:
- if not extfileobj:
+ if not extfileobj and fileobj is not None:
fileobj.close()
raise
t._extfileobj = extfileobj