From 7585870c05894c2de7d36ccadc3b2e55272efb86 Mon Sep 17 00:00:00 2001 From: Sylvain Desodt Date: Sat, 31 Jul 2021 23:01:12 +0200 Subject: [PATCH 1/5] Check support of sysconfig.get_preferred_scheme() in a different way Support for the feature sysconfig.get_preferred_scheme (added on Python 3.10 - see bpo-43312) was checked based on the Python version (see commit ca4aa121a9f7d876b4513cadf8d9e54c8515416a "Use Python 3.10 sysconfig.get_preferred_scheme()"). Unfortunately, this API seems to be introduced with the 3.10.beta Cpython builds and does not seem to be part of the 3.10.alpha versions. Hence, on these versions, pip fails with exception: AttributeError: module 'sysconfig' has no attribute 'get_preferred_scheme' The solution chosen is to check if the function exists in the sysconfig module using introspection instead of relying on Python versions. --- src/pip/_internal/locations/_sysconfig.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/locations/_sysconfig.py b/src/pip/_internal/locations/_sysconfig.py index 0fc67843f7a..c5020af0391 100644 --- a/src/pip/_internal/locations/_sysconfig.py +++ b/src/pip/_internal/locations/_sysconfig.py @@ -24,7 +24,7 @@ _AVAILABLE_SCHEMES = set(sysconfig.get_scheme_names()) -_HAS_PREFERRED_SCHEME_API = sys.version_info >= (3, 10) +_HAS_PREFERRED_SCHEME_API = 'get_preferred_scheme' in dir(sysconfig) def _infer_prefix() -> str: From 966e67c8d4552828d49d7d2afa4680afb70346b6 Mon Sep 17 00:00:00 2001 From: Sylvain Desodt Date: Sun, 1 Aug 2021 08:51:07 +0200 Subject: [PATCH 2/5] Follow-up for previous commit - PR comments --- news/10252.bugfix.rst | 4 ++++ src/pip/_internal/locations/_sysconfig.py | 14 +++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 news/10252.bugfix.rst diff --git a/news/10252.bugfix.rst b/news/10252.bugfix.rst new file mode 100644 index 00000000000..ab8f6df2d62 --- /dev/null +++ b/news/10252.bugfix.rst @@ -0,0 +1,4 @@ +Check if the sysconfig.get_preferred_scheme function +exists in a different way. The original method relied +on Python version but led to AttributeError on Python +3.10.alpha as the function was added in 3.10.beta. diff --git a/src/pip/_internal/locations/_sysconfig.py b/src/pip/_internal/locations/_sysconfig.py index c5020af0391..0ae343225a8 100644 --- a/src/pip/_internal/locations/_sysconfig.py +++ b/src/pip/_internal/locations/_sysconfig.py @@ -24,7 +24,7 @@ _AVAILABLE_SCHEMES = set(sysconfig.get_scheme_names()) -_HAS_PREFERRED_SCHEME_API = 'get_preferred_scheme' in dir(sysconfig) +_PREFERRED_SCHEME_API = getattr(sysconfig, "get_preferred_scheme", None) def _infer_prefix() -> str: @@ -41,8 +41,8 @@ def _infer_prefix() -> str: If none of the above works, fall back to ``posix_prefix``. """ - if _HAS_PREFERRED_SCHEME_API: - return sysconfig.get_preferred_scheme("prefix") # type: ignore + if _PREFERRED_SCHEME_API: + return _PREFERRED_SCHEME_API("prefix") # type: ignore os_framework_global = is_osx_framework() and not running_under_virtualenv() if os_framework_global and "osx_framework_library" in _AVAILABLE_SCHEMES: return "osx_framework_library" @@ -61,8 +61,8 @@ def _infer_prefix() -> str: def _infer_user() -> str: """Try to find a user scheme for the current platform.""" - if _HAS_PREFERRED_SCHEME_API: - return sysconfig.get_preferred_scheme("user") # type: ignore + if _PREFERRED_SCHEME_API: + return _PREFERRED_SCHEME_API("user") # type: ignore if is_osx_framework() and not running_under_virtualenv(): suffixed = "osx_framework_user" else: @@ -76,8 +76,8 @@ def _infer_user() -> str: def _infer_home() -> str: """Try to find a home for the current platform.""" - if _HAS_PREFERRED_SCHEME_API: - return sysconfig.get_preferred_scheme("home") # type: ignore + if _PREFERRED_SCHEME_API: + return _PREFERRED_SCHEME_API("home") # type: ignore suffixed = f"{os.name}_home" if suffixed in _AVAILABLE_SCHEMES: return suffixed From e3484400e2c4d50a26f64a58a82d71cbf8b600d3 Mon Sep 17 00:00:00 2001 From: Sylvain Desodt Date: Sun, 1 Aug 2021 09:22:54 +0200 Subject: [PATCH 3/5] Follow-up for previous commit - Removing type annotation to fix CI --- src/pip/_internal/locations/_sysconfig.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pip/_internal/locations/_sysconfig.py b/src/pip/_internal/locations/_sysconfig.py index 0ae343225a8..86dbee7e954 100644 --- a/src/pip/_internal/locations/_sysconfig.py +++ b/src/pip/_internal/locations/_sysconfig.py @@ -42,7 +42,7 @@ def _infer_prefix() -> str: If none of the above works, fall back to ``posix_prefix``. """ if _PREFERRED_SCHEME_API: - return _PREFERRED_SCHEME_API("prefix") # type: ignore + return _PREFERRED_SCHEME_API("prefix") os_framework_global = is_osx_framework() and not running_under_virtualenv() if os_framework_global and "osx_framework_library" in _AVAILABLE_SCHEMES: return "osx_framework_library" @@ -62,7 +62,7 @@ def _infer_prefix() -> str: def _infer_user() -> str: """Try to find a user scheme for the current platform.""" if _PREFERRED_SCHEME_API: - return _PREFERRED_SCHEME_API("user") # type: ignore + return _PREFERRED_SCHEME_API("user") if is_osx_framework() and not running_under_virtualenv(): suffixed = "osx_framework_user" else: @@ -77,7 +77,7 @@ def _infer_user() -> str: def _infer_home() -> str: """Try to find a home for the current platform.""" if _PREFERRED_SCHEME_API: - return _PREFERRED_SCHEME_API("home") # type: ignore + return _PREFERRED_SCHEME_API("home") suffixed = f"{os.name}_home" if suffixed in _AVAILABLE_SCHEMES: return suffixed From 02d3121e4b6f090612c11703ab570f8709766312 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Sun, 1 Aug 2021 17:11:00 +0800 Subject: [PATCH 4/5] Rewrite news to better fit existing style --- news/10252.bugfix.rst | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/news/10252.bugfix.rst b/news/10252.bugfix.rst index ab8f6df2d62..b2c727236c5 100644 --- a/news/10252.bugfix.rst +++ b/news/10252.bugfix.rst @@ -1,4 +1,2 @@ -Check if the sysconfig.get_preferred_scheme function -exists in a different way. The original method relied -on Python version but led to AttributeError on Python -3.10.alpha as the function was added in 3.10.beta. +Modify the `sysconfig.get_preferred_scheme` function check to be +compatible with CPython 3.10’s alphareleases. From 291eb357431fdec5a2db7bccba76f0a485775f0d Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Sun, 1 Aug 2021 17:14:19 +0800 Subject: [PATCH 5/5] Double ticks --- news/10252.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/10252.bugfix.rst b/news/10252.bugfix.rst index b2c727236c5..ebeeefd25a8 100644 --- a/news/10252.bugfix.rst +++ b/news/10252.bugfix.rst @@ -1,2 +1,2 @@ -Modify the `sysconfig.get_preferred_scheme` function check to be +Modify the ``sysconfig.get_preferred_scheme`` function check to be compatible with CPython 3.10’s alphareleases.