summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2014-08-28 00:31:21 (GMT)
committerBenjamin Peterson <benjamin@python.org>2014-08-28 00:31:21 (GMT)
commit7fd59e09d23bd5a27824fdea47ce0b2d0d830bcc (patch)
tree17388c0a5634257a9bc29ec96ff88743e83c039f /Lib
parentce2ec49d92b4c31b6ad358402d8a124e7b03e239 (diff)
downloadcpython-7fd59e09d23bd5a27824fdea47ce0b2d0d830bcc.zip
cpython-7fd59e09d23bd5a27824fdea47ce0b2d0d830bcc.tar.gz
cpython-7fd59e09d23bd5a27824fdea47ce0b2d0d830bcc.tar.bz2
properly handle file closing in error cases (closes #22266)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/tarfile.py77
1 files changed, 48 insertions, 29 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 57ea877..b0d1292 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -417,28 +417,34 @@ class _Stream:
self.pos = 0L
self.closed = False
- if comptype == "gz":
- try:
- import zlib
- except ImportError:
- raise CompressionError("zlib module is not available")
- self.zlib = zlib
- self.crc = zlib.crc32("") & 0xffffffffL
- if mode == "r":
- self._init_read_gz()
- else:
- self._init_write_gz()
+ try:
+ if comptype == "gz":
+ try:
+ import zlib
+ except ImportError:
+ raise CompressionError("zlib module is not available")
+ self.zlib = zlib
+ self.crc = zlib.crc32("") & 0xffffffffL
+ if mode == "r":
+ self._init_read_gz()
+ else:
+ self._init_write_gz()
- if comptype == "bz2":
- try:
- import bz2
- except ImportError:
- raise CompressionError("bz2 module is not available")
- if mode == "r":
- self.dbuf = ""
- self.cmp = bz2.BZ2Decompressor()
- else:
- self.cmp = bz2.BZ2Compressor()
+ elif comptype == "bz2":
+ try:
+ import bz2
+ except ImportError:
+ raise CompressionError("bz2 module is not available")
+ if mode == "r":
+ self.dbuf = ""
+ self.cmp = bz2.BZ2Decompressor()
+ else:
+ self.cmp = bz2.BZ2Compressor()
+ except:
+ if not self._extfileobj:
+ self.fileobj.close()
+ self.closed = True
+ raise
def __del__(self):
if hasattr(self, "closed") and not self.closed:
@@ -1685,9 +1691,12 @@ class TarFile(object):
if filemode not in ("r", "w"):
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
@@ -1718,17 +1727,23 @@ class TarFile(object):
except (ImportError, AttributeError):
raise CompressionError("gzip module is not available")
- if fileobj is None:
- fileobj = bltn_open(name, mode + "b")
+ try:
+ fileobj = gzip.GzipFile(name, mode, compresslevel, fileobj)
+ except OSError:
+ if fileobj is not None and mode == 'r':
+ raise ReadError("not a gzip file")
+ raise
try:
- t = cls.taropen(name, mode,
- gzip.GzipFile(name, mode, compresslevel, fileobj),
- **kwargs)
+ t = cls.taropen(name, mode, fileobj, **kwargs)
except IOError:
+ fileobj.close()
if mode == 'r':
raise ReadError("not a gzip file")
raise
+ except:
+ fileobj.close()
+ raise
t._extfileobj = False
return t
@@ -1753,9 +1768,13 @@ class TarFile(object):
try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (IOError, EOFError):
+ fileobj.close()
if mode == 'r':
raise ReadError("not a bzip2 file")
raise
+ except:
+ fileobj.close()
+ raise
t._extfileobj = False
return t