Skip to content

Commit b579fa0

Browse files
raise MetadataInconsistent if the name from METADATA doesn't match the install requirement
1 parent 53193cd commit b579fa0

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/pip/_internal/operations/prepare.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
HashMismatch,
2020
HashUnpinned,
2121
InstallationError,
22+
MetadataInconsistent,
2223
NetworkConnectionError,
2324
PreviousBuildDirError,
2425
VcsHashUnsupported,
@@ -365,6 +366,7 @@ def _fetch_metadata_using_link_data_attr(
365366
req: InstallRequirement,
366367
) -> Optional[BaseDistribution]:
367368
"""Fetch metadata from the data-dist-info-metadata attribute, if possible."""
369+
# (1) Get the link to the metadata file, if provided by the backend.
368370
metadata_link = req.link.metadata_link()
369371
if metadata_link is None:
370372
return None
@@ -374,20 +376,31 @@ def _fetch_metadata_using_link_data_attr(
374376
req.req,
375377
metadata_link,
376378
)
377-
# Download the contents of the METADATA file, separate from the dist itself.
379+
# (2) Download the contents of the METADATA file, separate from the dist itself.
378380
metadata_file = get_http_url(
379381
metadata_link,
380382
self._download,
381383
hashes=metadata_link.as_hashes(),
382384
)
383385
with open(metadata_file.path, "rb") as f:
384386
metadata_contents = f.read()
385-
# Generate a dist just from those file contents.
386-
return get_metadata_distribution(
387+
# (3) Generate a dist just from those file contents.
388+
metadata_dist = get_metadata_distribution(
387389
metadata_contents,
388390
req.link.filename,
389391
req.req.name,
390392
)
393+
# (4) Ensure the Name: field from the METADATA file matches the name from the
394+
# install requirement.
395+
#
396+
# NB: raw_name will fall back to the name from the install requirement if
397+
# the Name: field is not present, but it's noted in the raw_name docstring
398+
# that that should NEVER happen anyway.
399+
if metadata_dist.raw_name != req.req.name:
400+
raise MetadataInconsistent(
401+
req, "Name", req.req.name, metadata_dist.raw_name
402+
)
403+
return metadata_dist
391404

392405
def _fetch_metadata_using_lazy_wheel(
393406
self,

0 commit comments

Comments
 (0)