Skip to content

Commit

Permalink
parse_{sdist,wheel}_filename: don't raise InvalidVersion
Browse files Browse the repository at this point in the history
Fixes #660.
  • Loading branch information
SpecLad committed Sep 7, 2023
1 parent 7e68d82 commit fcbf85c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Changelog
* Introduce ``metadata.Metadata`` (along with ``metadata.ExceptionGroup`` and ``metadata.InvalidMetadata``; :issue:`570`)
* Introduce the ``validate`` keyword parameter to ``utils.validate_name()`` (:issue:`570`)
* Introduce ``utils.is_normalized_name()`` (:issue:`570`)
* Make ``utils.parse_sdist_filename()` and ``utils.parse_wheel_filename()`
raise ``InvalidSdistFilename`` and ``InvalidWheelFilename``, respectively,
when the version component of the name is invalid

23.1 - 2023-04-12
~~~~~~~~~~~~~~~~~
Expand Down
18 changes: 16 additions & 2 deletions src/packaging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ def parse_wheel_filename(
if "__" in name_part or re.match(r"^[\w\d._]*$", name_part, re.UNICODE) is None:
raise InvalidWheelFilename(f"Invalid project name: {filename}")
name = canonicalize_name(name_part)
version = Version(parts[1])

try:
version = Version(parts[1])
except InvalidVersion as e:
raise InvalidWheelFilename(
f"Invalid wheel filename (invalid version): {filename}"
) from e

if dashes == 5:
build_part = parts[2]
build_match = _build_tag_regex.match(build_part)
Expand Down Expand Up @@ -154,5 +161,12 @@ def parse_sdist_filename(filename: str) -> Tuple[NormalizedName, Version]:
raise InvalidSdistFilename(f"Invalid sdist filename: {filename}")

name = canonicalize_name(name_part)
version = Version(version_part)

try:
version = Version(version_part)
except InvalidVersion as e:
raise InvalidSdistFilename(
f"Invalid sdist filename (invalid version): {filename}"
) from e

return (name, version)
10 changes: 9 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def test_parse_wheel_filename(filename, name, version, build, tags):
("foo-1.0-py3-none-any.wheel"), # Incorrect file extension (`.wheel`)
("foo__bar-1.0-py3-none-any.whl"), # Invalid name (`__`)
("foo#bar-1.0-py3-none-any.whl"), # Invalid name (`#`)
("foobar-1.x-py3-none-any.whl"), # Invalid version (`1.x`)
# Build number doesn't start with a digit (`abc`)
("foo-1.0-abc-py3-none-any.whl"),
("foo-1.0-200-py3-none-any-junk.whl"), # Too many dashes (`-junk`)
Expand All @@ -154,7 +155,14 @@ def test_parse_sdist_filename(filename, name, version):
assert parse_sdist_filename(filename) == (name, version)


@pytest.mark.parametrize(("filename"), [("foo-1.0.xz"), ("foo1.0.tar.gz")])
@pytest.mark.parametrize(
("filename"),
[
("foo-1.0.xz"), # Incorrect extension
("foo1.0.tar.gz"), # Missing separator
("foo-1.x.tar.gz"), # Invalid version
],
)
def test_parse_sdist_invalid_filename(filename):
with pytest.raises(InvalidSdistFilename):
parse_sdist_filename(filename)

0 comments on commit fcbf85c

Please sign in to comment.