diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2021-11-15 18:59:26 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-15 18:59:26 -0500 |
commit | 7ed625e0e2311f530768b7d82a61c82d29a86210 (patch) | |
tree | afd04a08aea727eb0ab8cbc6f201cb4c526386cd | |
parent | c5a4c42d38cd0ba5ecd34977e211ae4b6865afec (diff) | |
parent | 6a784770c4087604510092b48403df3a44634858 (diff) | |
download | python-setuptools-git-7ed625e0e2311f530768b7d82a61c82d29a86210.tar.gz |
Merge pull request #2889 from pypa/bugfix/2885-legacy-version
Correct behavior under LegacyVersion when deprecation warnings are enabled.
-rw-r--r-- | changelog.d/2885.misc.rst | 1 | ||||
-rw-r--r-- | pkg_resources/__init__.py | 13 | ||||
-rw-r--r-- | pkg_resources/tests/test_resources.py | 2 | ||||
-rw-r--r-- | pytest.ini | 1 | ||||
-rw-r--r-- | setuptools/config.py | 6 | ||||
-rw-r--r-- | setuptools/package_index.py | 10 |
6 files changed, 26 insertions, 7 deletions
diff --git a/changelog.d/2885.misc.rst b/changelog.d/2885.misc.rst new file mode 100644 index 00000000..1e121061 --- /dev/null +++ b/changelog.d/2885.misc.rst @@ -0,0 +1 @@ +Fixed errors when encountering LegacyVersions. diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 4aece3c4..c615bc09 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -2015,7 +2015,7 @@ def _by_version_descending(names): >>> names = 'bar', 'foo', 'Python-2.7.10.egg', 'Python-2.7.2.egg' >>> _by_version_descending(names) - ['Python-2.7.10.egg', 'Python-2.7.2.egg', 'foo', 'bar'] + ['Python-2.7.10.egg', 'Python-2.7.2.egg', 'bar', 'foo'] >>> names = 'Setuptools-1.2.3b1.egg', 'Setuptools-1.2.3.egg' >>> _by_version_descending(names) ['Setuptools-1.2.3.egg', 'Setuptools-1.2.3b1.egg'] @@ -2023,13 +2023,22 @@ def _by_version_descending(names): >>> _by_version_descending(names) ['Setuptools-1.2.3.post1.egg', 'Setuptools-1.2.3b1.egg'] """ + def try_parse(name): + """ + Attempt to parse as a version or return a null version. + """ + try: + return packaging.version.Version(name) + except Exception: + return packaging.version.Version('0') + def _by_version(name): """ Parse each component of the filename """ name, ext = os.path.splitext(name) parts = itertools.chain(name.split('-'), [ext]) - return [packaging.version.parse(part) for part in parts] + return [try_parse(part) for part in parts] return sorted(names, key=_by_version, reverse=True) diff --git a/pkg_resources/tests/test_resources.py b/pkg_resources/tests/test_resources.py index 965a7c00..107dda7b 100644 --- a/pkg_resources/tests/test_resources.py +++ b/pkg_resources/tests/test_resources.py @@ -703,7 +703,7 @@ class TestParsing: ) def test_local_version(self): - req, = parse_requirements('foo==1.0.org1') + req, = parse_requirements('foo==1.0+org1') def test_spaces_between_multiple_versions(self): req, = parse_requirements('foo>=1.0, <3') @@ -34,7 +34,6 @@ filterwarnings= # https://github.com/pypa/setuptools/issues/2497 ignore:.* is an invalid version and will not be supported::pkg_resources - ignore:Creating a LegacyVersion has been deprecated # https://github.com/pypa/setuptools/pull/2865#issuecomment-965700112 # ideally would apply to Python 3.10+ when diff --git a/setuptools/config.py b/setuptools/config.py index e3e44c25..b4e968e5 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -13,7 +13,7 @@ from glob import iglob import contextlib from distutils.errors import DistutilsOptionError, DistutilsFileError -from setuptools.extern.packaging.version import LegacyVersion, parse +from setuptools.extern.packaging.version import Version, InvalidVersion from setuptools.extern.packaging.specifiers import SpecifierSet @@ -585,7 +585,9 @@ class ConfigMetadataHandler(ConfigHandler): version = version.strip() # Be strict about versions loaded from file because it's easy to # accidentally include newlines and other unintended content - if isinstance(parse(version), LegacyVersion): + try: + Version(version) + except InvalidVersion: tmpl = ( 'Version loaded from {value} does not ' 'comply with PEP 440: {version}' diff --git a/setuptools/package_index.py b/setuptools/package_index.py index d818f44a..270e7f3c 100644 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -21,7 +21,7 @@ import setuptools from pkg_resources import ( CHECKOUT_DIST, Distribution, BINARY_DIST, normalize_path, SOURCE_DIST, Environment, find_distributions, safe_name, safe_version, - to_filename, Requirement, DEVELOP_DIST, EGG_DIST, + to_filename, Requirement, DEVELOP_DIST, EGG_DIST, parse_version, ) from distutils import log from distutils.errors import DistutilsError @@ -294,6 +294,14 @@ class PackageIndex(Environment): self.to_scan = [] self.opener = urllib.request.urlopen + def add(self, dist): + # ignore invalid versions + try: + parse_version(dist.version) + except Exception: + return + return super().add(dist) + # FIXME: 'PackageIndex.process_url' is too complex (14) def process_url(self, url, retrieve=False): # noqa: C901 """Evaluate a URL as a possible download, and maybe retrieve it""" |