Voice et bot modif

This commit is contained in:
pi 2026-06-16 17:09:34 +00:00
parent 189d56026b
commit 7333a22bcd
10774 changed files with 634644 additions and 933308 deletions

View file

@ -1,5 +1,3 @@
from __future__ import annotations
import functools
import itertools
import logging
@ -7,12 +5,17 @@ import os
import posixpath
import re
import urllib.parse
from collections.abc import Mapping
from dataclasses import dataclass
from typing import (
TYPE_CHECKING,
Any,
Dict,
List,
Mapping,
NamedTuple,
Optional,
Tuple,
Union,
)
from pip._internal.utils.deprecation import deprecated
@ -24,6 +27,7 @@ from pip._internal.utils.misc import (
split_auth_from_netloc,
splitext,
)
from pip._internal.utils.models import KeyBasedCompareMixin
from pip._internal.utils.urls import path_to_url, url_to_path
if TYPE_CHECKING:
@ -66,8 +70,8 @@ class LinkHash:
assert self.name in _SUPPORTED_HASHES
@classmethod
@functools.cache
def find_hash_url_fragment(cls, url: str) -> LinkHash | None:
@functools.lru_cache(maxsize=None)
def find_hash_url_fragment(cls, url: str) -> Optional["LinkHash"]:
"""Search a string for a checksum algorithm name and encoded output value."""
match = cls._hash_url_fragment_re.search(url)
if match is None:
@ -75,14 +79,14 @@ class LinkHash:
name, value = match.groups()
return cls(name=name, value=value)
def as_dict(self) -> dict[str, str]:
def as_dict(self) -> Dict[str, str]:
return {self.name: self.value}
def as_hashes(self) -> Hashes:
"""Return a Hashes instance which checks only for the current hash."""
return Hashes({self.name: [self.value]})
def is_hash_allowed(self, hashes: Hashes | None) -> bool:
def is_hash_allowed(self, hashes: Optional[Hashes]) -> bool:
"""
Return True if the current hash is allowed by `hashes`.
"""
@ -95,14 +99,14 @@ class LinkHash:
class MetadataFile:
"""Information about a core metadata file associated with a distribution."""
hashes: dict[str, str] | None
hashes: Optional[Dict[str, str]]
def __post_init__(self) -> None:
if self.hashes is not None:
assert all(name in _SUPPORTED_HASHES for name in self.hashes)
def supported_hashes(hashes: dict[str, str] | None) -> dict[str, str] | None:
def supported_hashes(hashes: Optional[Dict[str, str]]) -> Optional[Dict[str, str]]:
# Remove any unsupported hash types from the mapping. If this leaves no
# supported hashes, return None
if hashes is None:
@ -131,11 +135,7 @@ def _clean_file_url_path(part: str) -> str:
# should not be quoted. On Linux where drive letters do not
# exist, the colon should be quoted. We rely on urllib.request
# to do the right thing here.
ret = urllib.request.pathname2url(urllib.request.url2pathname(part))
if ret.startswith("///"):
# Remove any URL authority section, leaving only the URL path.
ret = ret.removeprefix("//")
return ret
return urllib.request.pathname2url(urllib.request.url2pathname(part))
# percent-encoded: /
@ -171,37 +171,20 @@ def _ensure_quoted_url(url: str) -> str:
and without double-quoting other characters.
"""
# Split the URL into parts according to the general structure
# `scheme://netloc/path?query#fragment`.
result = urllib.parse.urlsplit(url)
# `scheme://netloc/path;parameters?query#fragment`.
result = urllib.parse.urlparse(url)
# If the netloc is empty, then the URL refers to a local filesystem path.
is_local_path = not result.netloc
path = _clean_url_path(result.path, is_local_path=is_local_path)
# Temporarily replace scheme with file to ensure the URL generated by
# urlunsplit() contains an empty netloc (file://) as per RFC 1738.
ret = urllib.parse.urlunsplit(result._replace(scheme="file", path=path))
ret = result.scheme + ret[4:] # Restore original scheme.
return ret
return urllib.parse.urlunparse(result._replace(path=path))
def _absolute_link_url(base_url: str, url: str) -> str:
"""
A faster implementation of urllib.parse.urljoin with a shortcut
for absolute http/https URLs.
"""
if url.startswith(("https://", "http://")):
return url
else:
return urllib.parse.urljoin(base_url, url)
@functools.total_ordering
class Link:
class Link(KeyBasedCompareMixin):
"""Represents a parsed link from a Package Index's simple URL"""
__slots__ = [
"_parsed_url",
"_url",
"_path",
"_hashes",
"comes_from",
"requires_python",
@ -214,12 +197,12 @@ class Link:
def __init__(
self,
url: str,
comes_from: str | IndexContent | None = None,
requires_python: str | None = None,
yanked_reason: str | None = None,
metadata_file_data: MetadataFile | None = None,
comes_from: Optional[Union[str, "IndexContent"]] = None,
requires_python: Optional[str] = None,
yanked_reason: Optional[str] = None,
metadata_file_data: Optional[MetadataFile] = None,
cache_link_parsing: bool = True,
hashes: Mapping[str, str] | None = None,
hashes: Optional[Mapping[str, str]] = None,
) -> None:
"""
:param url: url of the resource pointed to (href of the link)
@ -258,8 +241,6 @@ class Link:
# Store the url as a private attribute to prevent accidentally
# trying to set a new value.
self._url = url
# The .path property is hot, so calculate its value ahead of time.
self._path = urllib.parse.unquote(self._parsed_url.path)
link_hash = LinkHash.find_hash_url_fragment(url)
hashes_from_link = {} if link_hash is None else link_hash.as_dict()
@ -273,15 +254,17 @@ class Link:
self.yanked_reason = yanked_reason
self.metadata_file_data = metadata_file_data
super().__init__(key=url, defining_class=Link)
self.cache_link_parsing = cache_link_parsing
self.egg_fragment = self._egg_fragment()
@classmethod
def from_json(
cls,
file_data: dict[str, Any],
file_data: Dict[str, Any],
page_url: str,
) -> Link | None:
) -> Optional["Link"]:
"""
Convert an pypi json document from a simple repository page into a Link.
"""
@ -289,7 +272,7 @@ class Link:
if file_url is None:
return None
url = _ensure_quoted_url(_absolute_link_url(page_url, file_url))
url = _ensure_quoted_url(urllib.parse.urljoin(page_url, file_url))
pyrequire = file_data.get("requires-python")
yanked_reason = file_data.get("yanked")
hashes = file_data.get("hashes", {})
@ -330,10 +313,10 @@ class Link:
@classmethod
def from_element(
cls,
anchor_attribs: dict[str, str | None],
anchor_attribs: Dict[str, Optional[str]],
page_url: str,
base_url: str,
) -> Link | None:
) -> Optional["Link"]:
"""
Convert an anchor element's attributes in a simple repository page to a Link.
"""
@ -341,7 +324,7 @@ class Link:
if not href:
return None
url = _ensure_quoted_url(_absolute_link_url(base_url, href))
url = _ensure_quoted_url(urllib.parse.urljoin(base_url, href))
pyrequire = anchor_attribs.get("data-requires-python")
yanked_reason = anchor_attribs.get("data-yanked")
@ -385,34 +368,17 @@ class Link:
else:
rp = ""
if self.comes_from:
return f"{self.redacted_url} (from {self.comes_from}){rp}"
return f"{redact_auth_from_url(self._url)} (from {self.comes_from}){rp}"
else:
return self.redacted_url
return redact_auth_from_url(str(self._url))
def __repr__(self) -> str:
return f"<Link {self}>"
def __hash__(self) -> int:
return hash(self.url)
def __eq__(self, other: Any) -> bool:
if not isinstance(other, Link):
return NotImplemented
return self.url == other.url
def __lt__(self, other: Any) -> bool:
if not isinstance(other, Link):
return NotImplemented
return self.url < other.url
@property
def url(self) -> str:
return self._url
@property
def redacted_url(self) -> str:
return redact_auth_from_url(self.url)
@property
def filename(self) -> str:
path = self.path.rstrip("/")
@ -444,9 +410,9 @@ class Link:
@property
def path(self) -> str:
return self._path
return urllib.parse.unquote(self._parsed_url.path)
def splitext(self) -> tuple[str, str]:
def splitext(self) -> Tuple[str, str]:
return splitext(posixpath.basename(self.path.rstrip("/")))
@property
@ -465,7 +431,7 @@ class Link:
r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", re.IGNORECASE
)
def _egg_fragment(self) -> str | None:
def _egg_fragment(self) -> Optional[str]:
match = self._egg_fragment_re.search(self._url)
if not match:
return None
@ -475,10 +441,10 @@ class Link:
project_name = match.group(1)
if not self._project_name_re.match(project_name):
deprecated(
reason=f"{self} contains an egg fragment with a non-PEP 508 name.",
reason=f"{self} contains an egg fragment with a non-PEP 508 name",
replacement="to use the req @ url syntax, and remove the egg fragment",
gone_in="26.0",
issue=13157,
gone_in="25.0",
issue=11617,
)
return project_name
@ -486,13 +452,13 @@ class Link:
_subdirectory_fragment_re = re.compile(r"[#&]subdirectory=([^&]*)")
@property
def subdirectory_fragment(self) -> str | None:
def subdirectory_fragment(self) -> Optional[str]:
match = self._subdirectory_fragment_re.search(self._url)
if not match:
return None
return match.group(1)
def metadata_link(self) -> Link | None:
def metadata_link(self) -> Optional["Link"]:
"""Return a link to the associated core metadata file (if any)."""
if self.metadata_file_data is None:
return None
@ -505,11 +471,11 @@ class Link:
return Hashes({k: [v] for k, v in self._hashes.items()})
@property
def hash(self) -> str | None:
def hash(self) -> Optional[str]:
return next(iter(self._hashes.values()), None)
@property
def hash_name(self) -> str | None:
def hash_name(self) -> Optional[str]:
return next(iter(self._hashes), None)
@property
@ -541,7 +507,7 @@ class Link:
def has_hash(self) -> bool:
return bool(self._hashes)
def is_hash_allowed(self, hashes: Hashes | None) -> bool:
def is_hash_allowed(self, hashes: Optional[Hashes]) -> bool:
"""
Return True if the link has a hash and it is allowed by `hashes`.
"""
@ -577,9 +543,9 @@ class _CleanResult(NamedTuple):
"""
parsed: urllib.parse.SplitResult
query: dict[str, list[str]]
query: Dict[str, List[str]]
subdirectory: str
hashes: dict[str, str]
hashes: Dict[str, str]
def _clean_link(link: Link) -> _CleanResult:
@ -608,6 +574,6 @@ def _clean_link(link: Link) -> _CleanResult:
)
@functools.cache
@functools.lru_cache(maxsize=None)
def links_equivalent(link1: Link, link2: Link) -> bool:
return _clean_link(link1) == _clean_link(link2)