19
19
HashMismatch ,
20
20
HashUnpinned ,
21
21
InstallationError ,
22
+ MetadataInconsistent ,
22
23
NetworkConnectionError ,
23
24
PreviousBuildDirError ,
24
25
VcsHashUnsupported ,
@@ -365,6 +366,7 @@ def _fetch_metadata_using_link_data_attr(
365
366
req : InstallRequirement ,
366
367
) -> Optional [BaseDistribution ]:
367
368
"""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.
368
370
metadata_link = req .link .metadata_link ()
369
371
if metadata_link is None :
370
372
return None
@@ -374,20 +376,31 @@ def _fetch_metadata_using_link_data_attr(
374
376
req .req ,
375
377
metadata_link ,
376
378
)
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.
378
380
metadata_file = get_http_url (
379
381
metadata_link ,
380
382
self ._download ,
381
383
hashes = metadata_link .as_hashes (),
382
384
)
383
385
with open (metadata_file .path , "rb" ) as f :
384
386
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 (
387
389
metadata_contents ,
388
390
req .link .filename ,
389
391
req .req .name ,
390
392
)
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
391
404
392
405
def _fetch_metadata_using_lazy_wheel (
393
406
self ,
0 commit comments