Skip to content

Commit 8e9d68f

Browse files
committed
Be robust towards invalid versions like "0.3.2d"
.. in past releases when trying to find the latest release
1 parent f91e82c commit 8e9d68f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

hashin.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pip_api
2121
from packaging.requirements import Requirement
2222
from packaging.specifiers import SpecifierSet
23-
from packaging.version import parse
23+
from packaging.version import parse, InvalidVersion
2424

2525
from urllib.request import urlopen
2626
from urllib.error import HTTPError
@@ -436,7 +436,14 @@ def get_latest_version(data, include_prereleases):
436436
all_versions = []
437437
count_prereleases = 0
438438
for version in data["releases"]:
439-
v = parse(version)
439+
# NOTE: We ignore invalid version strings here so that pre-PEP-440
440+
# versions like "0.3.2d" from that past (say 2009) cannot break
441+
# the present
442+
try:
443+
v = parse(version)
444+
except InvalidVersion:
445+
print(f"Invalid version skipped (PEP 440): {version!r}", file=sys.stderr)
446+
continue
440447
if not v.is_prerelease or include_prereleases:
441448
all_versions.append((v, version))
442449
else:

0 commit comments

Comments
 (0)