bpo-39717: [tarfile] update nested exception raising (GH-23739)

- `from None` if the new exception uses, or doesn't need, the previous one
- `from e` if the previous exception is still relevant
This commit is contained in:
Ethan Furman 2020-12-12 13:26:44 -08:00 committed by GitHub
parent 4b8cdfcb22
commit b5a6db9111
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 32 deletions

View File

@ -200,6 +200,7 @@ def itn(n, digits=8, format=DEFAULT_FORMAT):
# base-256 representation. This allows values up to (256**(digits-1))-1. # base-256 representation. This allows values up to (256**(digits-1))-1.
# A 0o200 byte indicates a positive number, a 0o377 byte a negative # A 0o200 byte indicates a positive number, a 0o377 byte a negative
# number. # number.
original_n = n
n = int(n) n = int(n)
if 0 <= n < 8 ** (digits - 1): if 0 <= n < 8 ** (digits - 1):
s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
@ -363,7 +364,7 @@ class _Stream:
try: try:
import zlib import zlib
except ImportError: except ImportError:
raise CompressionError("zlib module is not available") raise CompressionError("zlib module is not available") from None
self.zlib = zlib self.zlib = zlib
self.crc = zlib.crc32(b"") self.crc = zlib.crc32(b"")
if mode == "r": if mode == "r":
@ -376,7 +377,7 @@ class _Stream:
try: try:
import bz2 import bz2
except ImportError: except ImportError:
raise CompressionError("bz2 module is not available") raise CompressionError("bz2 module is not available") from None
if mode == "r": if mode == "r":
self.dbuf = b"" self.dbuf = b""
self.cmp = bz2.BZ2Decompressor() self.cmp = bz2.BZ2Decompressor()
@ -388,7 +389,7 @@ class _Stream:
try: try:
import lzma import lzma
except ImportError: except ImportError:
raise CompressionError("lzma module is not available") raise CompressionError("lzma module is not available") from None
if mode == "r": if mode == "r":
self.dbuf = b"" self.dbuf = b""
self.cmp = lzma.LZMADecompressor() self.cmp = lzma.LZMADecompressor()
@ -541,8 +542,8 @@ class _Stream:
break break
try: try:
buf = self.cmp.decompress(buf) buf = self.cmp.decompress(buf)
except self.exception: except self.exception as e:
raise ReadError("invalid compressed data") raise ReadError("invalid compressed data") from e
t.append(buf) t.append(buf)
c += len(buf) c += len(buf)
t = b"".join(t) t = b"".join(t)
@ -1164,8 +1165,8 @@ class TarInfo(object):
# Fetch the next header and process it. # Fetch the next header and process it.
try: try:
next = self.fromtarfile(tarfile) next = self.fromtarfile(tarfile)
except HeaderError: except HeaderError as e:
raise SubsequentHeaderError("missing or bad subsequent header") raise SubsequentHeaderError(str(e)) from None
# Patch the TarInfo object from the next header with # Patch the TarInfo object from the next header with
# the longname information. # the longname information.
@ -1277,8 +1278,8 @@ class TarInfo(object):
# Fetch the next header. # Fetch the next header.
try: try:
next = self.fromtarfile(tarfile) next = self.fromtarfile(tarfile)
except HeaderError: except HeaderError as e:
raise SubsequentHeaderError("missing or bad subsequent header") raise SubsequentHeaderError(str(e)) from None
# Process GNU sparse information. # Process GNU sparse information.
if "GNU.sparse.map" in pax_headers: if "GNU.sparse.map" in pax_headers:
@ -1533,7 +1534,7 @@ class TarFile(object):
self.fileobj.seek(self.offset) self.fileobj.seek(self.offset)
break break
except HeaderError as e: except HeaderError as e:
raise ReadError(str(e)) raise ReadError(str(e)) from None
if self.mode in ("a", "w", "x"): if self.mode in ("a", "w", "x"):
self._loaded = True self._loaded = True
@ -1669,21 +1670,21 @@ class TarFile(object):
try: try:
from gzip import GzipFile from gzip import GzipFile
except ImportError: except ImportError:
raise CompressionError("gzip module is not available") raise CompressionError("gzip module is not available") from None
try: try:
fileobj = GzipFile(name, mode + "b", compresslevel, fileobj) fileobj = GzipFile(name, mode + "b", compresslevel, fileobj)
except OSError: except OSError as e:
if fileobj is not None and mode == 'r': if fileobj is not None and mode == 'r':
raise ReadError("not a gzip file") raise ReadError("not a gzip file") from e
raise raise
try: try:
t = cls.taropen(name, mode, fileobj, **kwargs) t = cls.taropen(name, mode, fileobj, **kwargs)
except OSError: except OSError as e:
fileobj.close() fileobj.close()
if mode == 'r': if mode == 'r':
raise ReadError("not a gzip file") raise ReadError("not a gzip file") from e
raise raise
except: except:
fileobj.close() fileobj.close()
@ -1702,16 +1703,16 @@ class TarFile(object):
try: try:
from bz2 import BZ2File from bz2 import BZ2File
except ImportError: 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) fileobj = BZ2File(fileobj or name, mode, compresslevel=compresslevel)
try: try:
t = cls.taropen(name, mode, fileobj, **kwargs) t = cls.taropen(name, mode, fileobj, **kwargs)
except (OSError, EOFError): except (OSError, EOFError) as e:
fileobj.close() fileobj.close()
if mode == 'r': if mode == 'r':
raise ReadError("not a bzip2 file") raise ReadError("not a bzip2 file") from e
raise raise
except: except:
fileobj.close() fileobj.close()
@ -1730,16 +1731,16 @@ class TarFile(object):
try: try:
from lzma import LZMAFile, LZMAError from lzma import LZMAFile, LZMAError
except ImportError: 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) fileobj = LZMAFile(fileobj or name, mode, preset=preset)
try: try:
t = cls.taropen(name, mode, fileobj, **kwargs) t = cls.taropen(name, mode, fileobj, **kwargs)
except (LZMAError, EOFError): except (LZMAError, EOFError) as e:
fileobj.close() fileobj.close()
if mode == 'r': if mode == 'r':
raise ReadError("not an lzma file") raise ReadError("not an lzma file") from e
raise raise
except: except:
fileobj.close() fileobj.close()
@ -2253,7 +2254,7 @@ class TarFile(object):
self._extract_member(self._find_link_target(tarinfo), self._extract_member(self._find_link_target(tarinfo),
targetpath) targetpath)
except KeyError: 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): def chown(self, tarinfo, targetpath, numeric_owner):
"""Set owner of targetpath according to tarinfo. If numeric_owner """Set owner of targetpath according to tarinfo. If numeric_owner
@ -2281,16 +2282,16 @@ class TarFile(object):
os.lchown(targetpath, u, g) os.lchown(targetpath, u, g)
else: else:
os.chown(targetpath, u, g) os.chown(targetpath, u, g)
except OSError: except OSError as e:
raise ExtractError("could not change owner") raise ExtractError("could not change owner") from e
def chmod(self, tarinfo, targetpath): def chmod(self, tarinfo, targetpath):
"""Set file permissions of targetpath according to tarinfo. """Set file permissions of targetpath according to tarinfo.
""" """
try: try:
os.chmod(targetpath, tarinfo.mode) os.chmod(targetpath, tarinfo.mode)
except OSError: except OSError as e:
raise ExtractError("could not change mode") raise ExtractError("could not change mode") from e
def utime(self, tarinfo, targetpath): def utime(self, tarinfo, targetpath):
"""Set modification time of targetpath according to tarinfo. """Set modification time of targetpath according to tarinfo.
@ -2299,8 +2300,8 @@ class TarFile(object):
return return
try: try:
os.utime(targetpath, (tarinfo.mtime, tarinfo.mtime)) os.utime(targetpath, (tarinfo.mtime, tarinfo.mtime))
except OSError: except OSError as e:
raise ExtractError("could not change modification time") raise ExtractError("could not change modification time") from e
#-------------------------------------------------------------------------- #--------------------------------------------------------------------------
def next(self): def next(self):
@ -2336,15 +2337,15 @@ class TarFile(object):
self.offset += BLOCKSIZE self.offset += BLOCKSIZE
continue continue
elif self.offset == 0: elif self.offset == 0:
raise ReadError(str(e)) raise ReadError(str(e)) from None
except EmptyHeaderError: except EmptyHeaderError:
if self.offset == 0: if self.offset == 0:
raise ReadError("empty file") raise ReadError("empty file") from None
except TruncatedHeaderError as e: except TruncatedHeaderError as e:
if self.offset == 0: if self.offset == 0:
raise ReadError(str(e)) raise ReadError(str(e)) from None
except SubsequentHeaderError as e: except SubsequentHeaderError as e:
raise ReadError(str(e)) raise ReadError(str(e)) from None
break break
if tarinfo is not None: if tarinfo is not None:

View File

@ -0,0 +1 @@
[tarfile] update nested exception raising to use `from None` or `from e`