diff --git a/news/23f96da0-5535-40c4-ad79-3feb7f694ec2.trivial.rst b/news/23f96da0-5535-40c4-ad79-3feb7f694ec2.trivial.rst new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/pip/_internal/commands/debug.py b/src/pip/_internal/commands/debug.py index 7e5271c9886..567ca967e5b 100644 --- a/src/pip/_internal/commands/debug.py +++ b/src/pip/_internal/commands/debug.py @@ -1,4 +1,3 @@ -import importlib.resources import locale import logging import os @@ -17,6 +16,7 @@ from pip._internal.cli.status_codes import SUCCESS from pip._internal.configuration import Configuration from pip._internal.metadata import get_environment +from pip._internal.utils.compat import open_text_resource from pip._internal.utils.logging import indent_log from pip._internal.utils.misc import get_pip_version @@ -35,7 +35,7 @@ def show_sys_implementation() -> None: def create_vendor_txt_map() -> Dict[str, str]: - with importlib.resources.open_text("pip._vendor", "vendor.txt") as f: + with open_text_resource("pip._vendor", "vendor.txt") as f: # Purge non version specifying lines. # Also, remove any space prefix or suffixes (including comments). lines = [ diff --git a/src/pip/_internal/utils/compat.py b/src/pip/_internal/utils/compat.py index 3f4d300cef0..d8b54e4ee51 100644 --- a/src/pip/_internal/utils/compat.py +++ b/src/pip/_internal/utils/compat.py @@ -1,9 +1,11 @@ """Stuff that differs in different Python versions and platform distributions.""" +import importlib.resources import logging import os import sys +from typing import IO __all__ = ["get_path_uid", "stdlib_pkgs", "WINDOWS"] @@ -51,6 +53,20 @@ def get_path_uid(path: str) -> int: return file_uid +# The importlib.resources.open_text function was deprecated in 3.11 with suggested +# replacement we use below. +if sys.version_info < (3, 11): + open_text_resource = importlib.resources.open_text +else: + + def open_text_resource( + package: str, resource: str, encoding: str = "utf-8", errors: str = "strict" + ) -> IO[str]: + return (importlib.resources.files(package) / resource).open( + "r", encoding=encoding, errors=errors + ) + + # packages in the stdlib that may have installation metadata, but should not be # considered 'installed'. this theoretically could be determined based on # dist.location (py27:`sysconfig.get_paths()['stdlib']`,