Skip to content

Commit

Permalink
Use !r formatter for error messages with filenames. (#844)
Browse files Browse the repository at this point in the history
Because I've been tracking an empty filename for some time,
and it took me a while to realise it was just here in my logs invisible.

So adding !r everywhere, so that at least there is a pair of quotes.
  • Loading branch information
Carreau authored Oct 21, 2024
1 parent 28e7da7 commit 6c338a8
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/packaging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,36 +96,36 @@ def parse_wheel_filename(
) -> tuple[NormalizedName, Version, BuildTag, frozenset[Tag]]:
if not filename.endswith(".whl"):
raise InvalidWheelFilename(
f"Invalid wheel filename (extension must be '.whl'): {filename}"
f"Invalid wheel filename (extension must be '.whl'): {filename!r}"
)

filename = filename[:-4]
dashes = filename.count("-")
if dashes not in (4, 5):
raise InvalidWheelFilename(
f"Invalid wheel filename (wrong number of parts): {filename}"
f"Invalid wheel filename (wrong number of parts): {filename!r}"
)

parts = filename.split("-", dashes - 2)
name_part = parts[0]
# See PEP 427 for the rules on escaping the project name.
if "__" in name_part or re.match(r"^[\w\d._]*$", name_part, re.UNICODE) is None:
raise InvalidWheelFilename(f"Invalid project name: {filename}")
raise InvalidWheelFilename(f"Invalid project name: {filename!r}")
name = canonicalize_name(name_part)

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

if dashes == 5:
build_part = parts[2]
build_match = _build_tag_regex.match(build_part)
if build_match is None:
raise InvalidWheelFilename(
f"Invalid build number: {build_part} in '{filename}'"
f"Invalid build number: {build_part} in '{filename!r}'"
)
build = cast(BuildTag, (int(build_match.group(1)), build_match.group(2)))
else:
Expand All @@ -142,22 +142,22 @@ def parse_sdist_filename(filename: str) -> tuple[NormalizedName, Version]:
else:
raise InvalidSdistFilename(
f"Invalid sdist filename (extension must be '.tar.gz' or '.zip'):"
f" {filename}"
f" {filename!r}"
)

# We are requiring a PEP 440 version, which cannot contain dashes,
# so we split on the last dash.
name_part, sep, version_part = file_stem.rpartition("-")
if not sep:
raise InvalidSdistFilename(f"Invalid sdist filename: {filename}")
raise InvalidSdistFilename(f"Invalid sdist filename: {filename!r}")

name = canonicalize_name(name_part)

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

return (name, version)

0 comments on commit 6c338a8

Please sign in to comment.