From 5b1afe5dc98aea811328ebbb591d8148a863f073 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Fri, 16 Dec 2022 03:12:38 +0800 Subject: [PATCH 1/2] Prevent pkg_resource access on Python 3.12+ Time is up. If a downstream Python distributor hasn't implemented importlib.metadata support by now, they must expect things to break. --- news/11501.process.rst | 6 ++++++ src/pip/_internal/metadata/__init__.py | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 news/11501.process.rst diff --git a/news/11501.process.rst b/news/11501.process.rst new file mode 100644 index 00000000000..77cebe1bab6 --- /dev/null +++ b/news/11501.process.rst @@ -0,0 +1,6 @@ +Emit an error to prevent the ``pkg_resources`` metadata backend from being used +on Python 3.12 or later. This backend exists for backward compatibility, and +should only be used on newer Python versions by Python distributors that cannot +correctly implement ``importlib.metadata`` support in a timely fashion. Time is +up for transition since CPython is now removing mechanisms that ``pkg_resource`` +relies on, and the backend will no longer work on Python 3.12 or later. diff --git a/src/pip/_internal/metadata/__init__.py b/src/pip/_internal/metadata/__init__.py index 9f73ca7105f..20c1b705226 100644 --- a/src/pip/_internal/metadata/__init__.py +++ b/src/pip/_internal/metadata/__init__.py @@ -4,6 +4,7 @@ import sys from typing import TYPE_CHECKING, List, Optional, Type, cast +from pip._internal.exceptions import PipError from pip._internal.utils.misc import strtobool from .base import BaseDistribution, BaseEnvironment, FilesystemWheel, MemoryWheel, Wheel @@ -60,6 +61,11 @@ def select_backend() -> Backend: from . import importlib return cast(Backend, importlib) + + if sys.version_info >= (3, 12): + message = "Cannot use pkg_resources as metadata backend on Python 3.12+" + raise PipError(message) + from . import pkg_resources return cast(Backend, pkg_resources) From 7cfc915aa6aeb6cf802a0a2b4402988635cba9e2 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Tue, 3 Jan 2023 08:03:50 +0800 Subject: [PATCH 2/2] Silently ignore pkg_resources override on 3.12+ This customization is generally provided by the distributor, and the user can do nothing if the environment is configured incorrectly. So instead of breaking pip entirely, we silently ignore the customization and let the distributor correct the situation. --- news/11501.process.rst | 10 +++++----- src/pip/_internal/metadata/__init__.py | 11 ++++------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/news/11501.process.rst b/news/11501.process.rst index 77cebe1bab6..af6dbb47417 100644 --- a/news/11501.process.rst +++ b/news/11501.process.rst @@ -1,6 +1,6 @@ -Emit an error to prevent the ``pkg_resources`` metadata backend from being used -on Python 3.12 or later. This backend exists for backward compatibility, and -should only be used on newer Python versions by Python distributors that cannot -correctly implement ``importlib.metadata`` support in a timely fashion. Time is -up for transition since CPython is now removing mechanisms that ``pkg_resource`` +Prevent the ``pkg_resources`` metadata backend from being used on Python 3.12 +or later. This backend exists for backward compatibility, and should only be +used on newer Python versions by Python distributors that cannot correctly +implement ``importlib.metadata`` support in a timely fashion. Time is up for +transition since CPython is now removing mechanisms that ``pkg_resource`` relies on, and the backend will no longer work on Python 3.12 or later. diff --git a/src/pip/_internal/metadata/__init__.py b/src/pip/_internal/metadata/__init__.py index 20c1b705226..8ab78a3065b 100644 --- a/src/pip/_internal/metadata/__init__.py +++ b/src/pip/_internal/metadata/__init__.py @@ -4,7 +4,6 @@ import sys from typing import TYPE_CHECKING, List, Optional, Type, cast -from pip._internal.exceptions import PipError from pip._internal.utils.misc import strtobool from .base import BaseDistribution, BaseEnvironment, FilesystemWheel, MemoryWheel, Wheel @@ -31,7 +30,8 @@ def _should_use_importlib_metadata() -> bool: """Whether to use the ``importlib.metadata`` or ``pkg_resources`` backend. By default, pip uses ``importlib.metadata`` on Python 3.11+, and - ``pkg_resourcess`` otherwise. This can be overridden by a couple of ways: + ``pkg_resources`` otherwise. On Python versions prior to 3.12, this can be + overridden by a couple of ways for transitional purposes: * If environment variable ``_PIP_USE_IMPORTLIB_METADATA`` is set, it dictates whether ``importlib.metadata`` is used, regardless of Python @@ -41,6 +41,8 @@ def _should_use_importlib_metadata() -> bool: makes pip use ``pkg_resources`` (unless the user set the aforementioned environment variable to *True*). """ + if sys.version_info >= (3, 12): + return True with contextlib.suppress(KeyError, ValueError): return bool(strtobool(os.environ["_PIP_USE_IMPORTLIB_METADATA"])) if sys.version_info < (3, 11): @@ -61,11 +63,6 @@ def select_backend() -> Backend: from . import importlib return cast(Backend, importlib) - - if sys.version_info >= (3, 12): - message = "Cannot use pkg_resources as metadata backend on Python 3.12+" - raise PipError(message) - from . import pkg_resources return cast(Backend, pkg_resources)