Skip to content

Commit

Permalink
bpo-39717: [tarfile] update nested exception raising (GH-23739)
Browse files Browse the repository at this point in the history
- `from None` if the new exception uses, or doesn't need, the previous one
- `from e` if the previous exception is still relevant
  • Loading branch information
ethanfurman committed Dec 12, 2020
1 parent 4b8cdfc commit b5a6db9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
65 changes: 33 additions & 32 deletions Lib/tarfile.py
Expand Up @@ -200,6 +200,7 @@ def itn(n, digits=8, format=DEFAULT_FORMAT):
# base-256 representation. This allows values up to (256**(digits-1))-1.
# A 0o200 byte indicates a positive number, a 0o377 byte a negative
# number.
original_n = n
n = int(n)
if 0 <= n < 8 ** (digits - 1):
s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
Expand Down Expand Up @@ -363,7 +364,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize):
try:
import zlib
except ImportError:
raise CompressionError("zlib module is not available")
raise CompressionError("zlib module is not available") from None
self.zlib = zlib
self.crc = zlib.crc32(b"")
if mode == "r":
Expand All @@ -376,7 +377,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize):
try:
import bz2
except ImportError:
raise CompressionError("bz2 module is not available")
raise CompressionError("bz2 module is not available") from None
if mode == "r":
self.dbuf = b""
self.cmp = bz2.BZ2Decompressor()
Expand All @@ -388,7 +389,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize):
try:
import lzma
except ImportError:
raise CompressionError("lzma module is not available")
raise CompressionError("lzma module is not available") from None
if mode == "r":
self.dbuf = b""
self.cmp = lzma.LZMADecompressor()
Expand Down Expand Up @@ -541,8 +542,8 @@ def _read(self, size):
break
try:
buf = self.cmp.decompress(buf)
except self.exception:
raise ReadError("invalid compressed data")
except self.exception as e:
raise ReadError("invalid compressed data") from e
t.append(buf)
c += len(buf)
t = b"".join(t)
Expand Down Expand Up @@ -1164,8 +1165,8 @@ def _proc_gnulong(self, tarfile):
# Fetch the next header and process it.
try:
next = self.fromtarfile(tarfile)
except HeaderError:
raise SubsequentHeaderError("missing or bad subsequent header")
except HeaderError as e:
raise SubsequentHeaderError(str(e)) from None

# Patch the TarInfo object from the next header with
# the longname information.
Expand Down Expand Up @@ -1277,8 +1278,8 @@ def _proc_pax(self, tarfile):
# Fetch the next header.
try:
next = self.fromtarfile(tarfile)
except HeaderError:
raise SubsequentHeaderError("missing or bad subsequent header")
except HeaderError as e:
raise SubsequentHeaderError(str(e)) from None

# Process GNU sparse information.
if "GNU.sparse.map" in pax_headers:
Expand Down Expand Up @@ -1533,7 +1534,7 @@ def __init__(self, name=None, mode="r", fileobj=None, format=None,
self.fileobj.seek(self.offset)
break
except HeaderError as e:
raise ReadError(str(e))
raise ReadError(str(e)) from None

if self.mode in ("a", "w", "x"):
self._loaded = True
Expand Down Expand Up @@ -1669,21 +1670,21 @@ def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
try:
from gzip import GzipFile
except ImportError:
raise CompressionError("gzip module is not available")
raise CompressionError("gzip module is not available") from None

try:
fileobj = GzipFile(name, mode + "b", compresslevel, fileobj)
except OSError:
except OSError as e:
if fileobj is not None and mode == 'r':
raise ReadError("not a gzip file")
raise ReadError("not a gzip file") from e
raise

try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except OSError:
except OSError as e:
fileobj.close()
if mode == 'r':
raise ReadError("not a gzip file")
raise ReadError("not a gzip file") from e
raise
except:
fileobj.close()
Expand All @@ -1702,16 +1703,16 @@ def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
try:
from bz2 import BZ2File
except ImportError:
raise CompressionError("bz2 module is not available")
raise CompressionError("bz2 module is not available") from None

fileobj = BZ2File(fileobj or name, mode, compresslevel=compresslevel)

try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (OSError, EOFError):
except (OSError, EOFError) as e:
fileobj.close()
if mode == 'r':
raise ReadError("not a bzip2 file")
raise ReadError("not a bzip2 file") from e
raise
except:
fileobj.close()
Expand All @@ -1730,16 +1731,16 @@ def xzopen(cls, name, mode="r", fileobj=None, preset=None, **kwargs):
try:
from lzma import LZMAFile, LZMAError
except ImportError:
raise CompressionError("lzma module is not available")
raise CompressionError("lzma module is not available") from None

fileobj = LZMAFile(fileobj or name, mode, preset=preset)

try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (LZMAError, EOFError):
except (LZMAError, EOFError) as e:
fileobj.close()
if mode == 'r':
raise ReadError("not an lzma file")
raise ReadError("not an lzma file") from e
raise
except:
fileobj.close()
Expand Down Expand Up @@ -2253,7 +2254,7 @@ def makelink(self, tarinfo, targetpath):
self._extract_member(self._find_link_target(tarinfo),
targetpath)
except KeyError:
raise ExtractError("unable to resolve link inside archive")
raise ExtractError("unable to resolve link inside archive") from None

def chown(self, tarinfo, targetpath, numeric_owner):
"""Set owner of targetpath according to tarinfo. If numeric_owner
Expand Down Expand Up @@ -2281,16 +2282,16 @@ def chown(self, tarinfo, targetpath, numeric_owner):
os.lchown(targetpath, u, g)
else:
os.chown(targetpath, u, g)
except OSError:
raise ExtractError("could not change owner")
except OSError as e:
raise ExtractError("could not change owner") from e

def chmod(self, tarinfo, targetpath):
"""Set file permissions of targetpath according to tarinfo.
"""
try:
os.chmod(targetpath, tarinfo.mode)
except OSError:
raise ExtractError("could not change mode")
except OSError as e:
raise ExtractError("could not change mode") from e

def utime(self, tarinfo, targetpath):
"""Set modification time of targetpath according to tarinfo.
Expand All @@ -2299,8 +2300,8 @@ def utime(self, tarinfo, targetpath):
return
try:
os.utime(targetpath, (tarinfo.mtime, tarinfo.mtime))
except OSError:
raise ExtractError("could not change modification time")
except OSError as e:
raise ExtractError("could not change modification time") from e

#--------------------------------------------------------------------------
def next(self):
Expand Down Expand Up @@ -2336,15 +2337,15 @@ def next(self):
self.offset += BLOCKSIZE
continue
elif self.offset == 0:
raise ReadError(str(e))
raise ReadError(str(e)) from None
except EmptyHeaderError:
if self.offset == 0:
raise ReadError("empty file")
raise ReadError("empty file") from None
except TruncatedHeaderError as e:
if self.offset == 0:
raise ReadError(str(e))
raise ReadError(str(e)) from None
except SubsequentHeaderError as e:
raise ReadError(str(e))
raise ReadError(str(e)) from None
break

if tarinfo is not None:
Expand Down
@@ -0,0 +1 @@
[tarfile] update nested exception raising to use `from None` or `from e`

0 comments on commit b5a6db9

Please sign in to comment.