diff options
author | Ram Rachum <ram@rachum.com> | 2020-06-16 13:31:12 +0300 |
---|---|---|
committer | Ram Rachum <ram@rachum.com> | 2020-06-28 22:20:23 +0300 |
commit | a9eb9e73def8ca6c469e59f1b008746e368ad4c1 (patch) | |
tree | 8c31a8e9003fd08f998ecc691d57ab08637926ce | |
parent | 308314268233cc56e7a8c5870fec75b566230411 (diff) | |
download | external_python_setuptools-a9eb9e73def8ca6c469e59f1b008746e368ad4c1.tar.gz external_python_setuptools-a9eb9e73def8ca6c469e59f1b008746e368ad4c1.tar.bz2 external_python_setuptools-a9eb9e73def8ca6c469e59f1b008746e368ad4c1.zip |
Fix exception causes all over the codebase
-rw-r--r-- | changelog.d/2199.misc.rst | 1 | ||||
-rw-r--r-- | pkg_resources/__init__.py | 18 | ||||
-rw-r--r-- | setuptools/archive_util.py | 4 | ||||
-rw-r--r-- | setuptools/command/easy_install.py | 14 | ||||
-rw-r--r-- | setuptools/command/egg_info.py | 4 | ||||
-rw-r--r-- | setuptools/command/rotate.py | 4 | ||||
-rw-r--r-- | setuptools/config.py | 5 | ||||
-rw-r--r-- | setuptools/dist.py | 38 | ||||
-rw-r--r-- | setuptools/installer.py | 2 | ||||
-rw-r--r-- | setuptools/msvc.py | 2 |
10 files changed, 51 insertions, 41 deletions
diff --git a/changelog.d/2199.misc.rst b/changelog.d/2199.misc.rst new file mode 100644 index 00000000..f795256b --- /dev/null +++ b/changelog.d/2199.misc.rst @@ -0,0 +1 @@ +Fix exception causes all over the codebase by using ``raise new_exception from old_exception``
\ No newline at end of file diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 2c589d55..c40f184a 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -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() diff --git a/setuptools/archive_util.py b/setuptools/archive_util.py index 64528ca7..0ce190b8 100644 --- a/setuptools/archive_util.py +++ b/setuptools/archive_util.py @@ -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 diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py index 27b4558b..8890ec88 100644 --- a/setuptools/command/easy_install.py +++ b/setuptools/command/easy_install.py @@ -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'] diff --git a/setuptools/command/egg_info.py b/setuptools/command/egg_info.py index 7fa89541..0855207c 100644 --- a/setuptools/command/egg_info.py +++ b/setuptools/command/egg_info.py @@ -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 diff --git a/setuptools/command/rotate.py b/setuptools/command/rotate.py index b89353f5..e398834f 100644 --- a/setuptools/command/rotate.py +++ b/setuptools/command/rotate.py @@ -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(',') diff --git a/setuptools/config.py b/setuptools/config.py index 45df2e3f..a8f8b6b0 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -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 diff --git a/setuptools/dist.py b/setuptools/dist.py index fe64afa9..e813b11c 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -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): diff --git a/setuptools/installer.py b/setuptools/installer.py index 1f183bd9..e5acec27 100644 --- a/setuptools/installer.py +++ b/setuptools/installer.py @@ -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) diff --git a/setuptools/msvc.py b/setuptools/msvc.py index 213e39c9..72ba0d0c 100644 --- a/setuptools/msvc.py +++ b/setuptools/msvc.py @@ -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 |