Commit a9eb9e73 authored by Ram Rachum's avatar Ram Rachum

Fix exception causes all over the codebase

parent 30831426
Fix exception causes all over the codebase by using ``raise new_exception from old_exception``
\ No newline at end of file
......@@ -1378,7 +1378,7 @@ def evaluate_marker(text, extra=None):
marker = packaging.markers.Marker(text)
return marker.evaluate()
except packaging.markers.InvalidMarker as e:
raise SyntaxError(e)
raise SyntaxError(e) from e
class NullProvider:
......@@ -2287,8 +2287,8 @@ def declare_namespace(packageName):
__import__(parent)
try:
path = sys.modules[parent].__path__
except AttributeError:
raise TypeError("Not a package:", parent)
except AttributeError as e:
raise TypeError("Not a package:", parent) from e
# Track what packages are namespaces, so when new path items are added,
# they can be updated
......@@ -2468,7 +2468,7 @@ class EntryPoint:
try:
return functools.reduce(getattr, self.attrs, module)
except AttributeError as exc:
raise ImportError(str(exc))
raise ImportError(str(exc)) from exc
def require(self, env=None, installer=None):
if self.extras and not self.dist:
......@@ -2688,14 +2688,14 @@ class Distribution:
def version(self):
try:
return self._version
except AttributeError:
except AttributeError as e:
version = self._get_version()
if version is None:
path = self._get_metadata_path_for_display(self.PKG_INFO)
msg = (
"Missing 'Version:' header and/or {} file at path: {}"
).format(self.PKG_INFO, path)
raise ValueError(msg, self)
raise ValueError(msg, self) from e
return version
......@@ -2748,10 +2748,10 @@ class Distribution:
for ext in extras:
try:
deps.extend(dm[safe_extra(ext)])
except KeyError:
except KeyError as e:
raise UnknownExtra(
"%s has no such extra feature %r" % (self, ext)
)
) from e
return deps
def _get_metadata_path_for_display(self, name):
......@@ -3109,7 +3109,7 @@ class Requirement(packaging.requirements.Requirement):
try:
super(Requirement, self).__init__(requirement_string)
except packaging.requirements.InvalidRequirement as e:
raise RequirementParseError(str(e))
raise RequirementParseError(str(e)) from e
self.unsafe_name = self.name
project_name = safe_name(self.name)
self.project_name, self.key = project_name, project_name.lower()
......
......@@ -134,10 +134,10 @@ def unpack_tarfile(filename, extract_dir, progress_filter=default_filter):
"""
try:
tarobj = tarfile.open(filename)
except tarfile.TarError:
except tarfile.TarError as e:
raise UnrecognizedFormat(
"%s is not a compressed or uncompressed tar file" % (filename,)
)
) from e
with contextlib.closing(tarobj):
# don't do any chowning!
tarobj.chown = lambda *args: None
......
......@@ -356,8 +356,10 @@ class easy_install(Command):
self.optimize = int(self.optimize)
if not (0 <= self.optimize <= 2):
raise ValueError
except ValueError:
raise DistutilsOptionError("--optimize must be 0, 1, or 2")
except ValueError as e:
raise DistutilsOptionError(
"--optimize must be 0, 1, or 2"
) from e
if self.editable and not self.build_directory:
raise DistutilsArgError(
......@@ -765,9 +767,9 @@ class easy_install(Command):
[requirement], self.local_index, self.easy_install
)
except DistributionNotFound as e:
raise DistutilsError(str(e))
raise DistutilsError(str(e)) from e
except VersionConflict as e:
raise DistutilsError(e.report())
raise DistutilsError(e.report()) from e
if self.always_copy or self.always_copy_from:
# Force all the relevant distros to be copied or activated
for dist in distros:
......@@ -1156,7 +1158,9 @@ class easy_install(Command):
try:
run_setup(setup_script, args)
except SystemExit as v:
raise DistutilsError("Setup script exited with %s" % (v.args[0],))
raise DistutilsError(
"Setup script exited with %s" % (v.args[0],)
) from v
def build_and_install(self, setup_script, setup_base):
args = ['bdist_egg', '--dist-dir']
......
......@@ -208,11 +208,11 @@ class egg_info(InfoCommon, Command):
list(
parse_requirements(spec % (self.egg_name, self.egg_version))
)
except ValueError:
except ValueError as e:
raise distutils.errors.DistutilsOptionError(
"Invalid distribution name or version syntax: %s-%s" %
(self.egg_name, self.egg_version)
)
) from e
if self.egg_base is None:
dirs = self.distribution.package_dir
......
......@@ -36,8 +36,8 @@ class rotate(Command):
raise DistutilsOptionError("Must specify number of files to keep")
try:
self.keep = int(self.keep)
except ValueError:
raise DistutilsOptionError("--keep must be an integer")
except ValueError as e:
raise DistutilsOptionError("--keep must be an integer") from e
if isinstance(self.match, six.string_types):
self.match = [
convert_path(p.strip()) for p in self.match.split(',')
......
......@@ -42,9 +42,10 @@ class StaticModule:
for target in statement.targets
if isinstance(target, ast.Name) and target.id == attr
)
except Exception:
except Exception as e:
raise AttributeError(
"{self.name} has no attribute {attr}".format(**locals()))
"{self.name} has no attribute {attr}".format(**locals())
) from e
@contextlib.contextmanager
......
......@@ -204,11 +204,11 @@ def check_importable(dist, attr, value):
try:
ep = pkg_resources.EntryPoint.parse('x=' + value)
assert not ep.extras
except (TypeError, ValueError, AttributeError, AssertionError):
except (TypeError, ValueError, AttributeError, AssertionError) as e:
raise DistutilsSetupError(
"%r must be importable 'module:attrs' string (got %r)"
% (attr, value)
)
) from e
def assert_string_list(dist, attr, value):
......@@ -219,10 +219,10 @@ def assert_string_list(dist, attr, value):
assert isinstance(value, (list, tuple))
# verify that elements of value are strings
assert ''.join(value) != value
except (TypeError, ValueError, AttributeError, AssertionError):
except (TypeError, ValueError, AttributeError, AssertionError) as e:
raise DistutilsSetupError(
"%r must be a list of strings (got %r)" % (attr, value)
)
) from e
def check_nsp(dist, attr, value):
......@@ -247,12 +247,12 @@ def check_extras(dist, attr, value):
"""Verify that extras_require mapping is valid"""
try:
list(itertools.starmap(_check_extra, value.items()))
except (TypeError, ValueError, AttributeError):
except (TypeError, ValueError, AttributeError) as e:
raise DistutilsSetupError(
"'extras_require' must be a dictionary whose values are "
"strings or lists of strings containing valid project/version "
"requirement specifiers."
)
) from e
def _check_extra(extra, reqs):
......@@ -280,7 +280,9 @@ def check_requirements(dist, attr, value):
"{attr!r} must be a string or list of strings "
"containing valid project/version requirement specifiers; {error}"
)
raise DistutilsSetupError(tmpl.format(attr=attr, error=error))
raise DistutilsSetupError(
tmpl.format(attr=attr, error=error)
) from error
def check_specifier(dist, attr, value):
......@@ -292,7 +294,9 @@ def check_specifier(dist, attr, value):
"{attr!r} must be a string "
"containing valid version specifiers; {error}"
)
raise DistutilsSetupError(tmpl.format(attr=attr, error=error))
raise DistutilsSetupError(
tmpl.format(attr=attr, error=error)
) from error
def check_entry_points(dist, attr, value):
......@@ -300,7 +304,7 @@ def check_entry_points(dist, attr, value):
try:
pkg_resources.EntryPoint.parse_map(value)
except ValueError as e:
raise DistutilsSetupError(e)
raise DistutilsSetupError(e) from e
def check_test_suite(dist, attr, value):
......@@ -609,8 +613,8 @@ class Distribution(_Distribution):
setattr(self, opt, strtobool(val))
else:
setattr(self, opt, val)
except ValueError as msg:
raise DistutilsOptionError(msg)
except ValueError as e:
raise DistutilsOptionError(e) from e
@staticmethod
def _try_str(val):
......@@ -676,8 +680,8 @@ class Distribution(_Distribution):
raise DistutilsOptionError(
"error in %s: command '%s' has no such option '%s'"
% (source, command_name, option))
except ValueError as msg:
raise DistutilsOptionError(msg)
except ValueError as e:
raise DistutilsOptionError(e) from e
def parse_config_files(self, filenames=None, ignore_option_errors=False):
"""Parses configuration files from various levels
......@@ -843,10 +847,10 @@ class Distribution(_Distribution):
)
try:
old = getattr(self, name)
except AttributeError:
except AttributeError as e:
raise DistutilsSetupError(
"%s: No such distribution setting" % name
)
) from e
if old is not None and not isinstance(old, sequence):
raise DistutilsSetupError(
name + ": this setting cannot be changed via include/exclude"
......@@ -863,10 +867,10 @@ class Distribution(_Distribution):
)
try:
old = getattr(self, name)
except AttributeError:
except AttributeError as e:
raise DistutilsSetupError(
"%s: No such distribution setting" % name
)
) from e
if old is None:
setattr(self, name, value)
elif not isinstance(old, sequence):
......
......@@ -127,7 +127,7 @@ def fetch_build_egg(dist, req):
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError as e:
raise DistutilsError(str(e))
raise DistutilsError(str(e)) from e
wheel = Wheel(glob.glob(os.path.join(tmpdir, '*.whl'))[0])
dist_location = os.path.join(eggs_dir, wheel.egg_name())
wheel.install_as_egg(dist_location)
......
......@@ -277,7 +277,7 @@ def _msvc14_get_vc_env(plat_spec):
except subprocess.CalledProcessError as exc:
raise distutils.errors.DistutilsPlatformError(
"Error executing {}".format(exc.cmd)
)
) from exc
env = {
key.lower(): value
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment