From ed08fc5dc65eaeb799c7f34aee948c73b31b1896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Fri, 31 Dec 2021 13:12:41 +0100 Subject: [PATCH 1/4] Use ``sysconfig`` to determine ``stdlib`` paths for ``PyPy`` --- astroid/modutils.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index e2f6e886a2..cf4f1479a5 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -46,6 +46,7 @@ import os import platform import sys +import sysconfig import types from distutils.errors import DistutilsPlatformError # pylint: disable=import-error from distutils.sysconfig import get_python_lib # pylint: disable=import-error @@ -101,26 +102,15 @@ pass if platform.python_implementation() == "PyPy": - # The get_python_lib(standard_lib=True) function does not give valid - # result with pypy in a virtualenv. - # In a virtual environment, with CPython implementation the call to this function returns a path toward - # the binary (its libraries) which has been used to create the virtual environment. - # Not with pypy implementation. - # The only way to retrieve such information is to use the sys.base_prefix hint. - # It's worth noticing that under CPython implementation the return values of - # get_python_lib(standard_lib=True) and get_python_lib(santdard_lib=True, prefix=sys.base_prefix) - # are the same. - # In the lines above, we could have replace the call to get_python_lib(standard=True) - # with the one using prefix=sys.base_prefix but we prefer modifying only what deals with pypy. - STD_LIB_DIRS.add(get_python_lib(standard_lib=True, prefix=sys.base_prefix)) - _root = os.path.join(sys.prefix, "lib_pypy") - STD_LIB_DIRS.add(_root) - try: - # real_prefix is defined when running inside virtualenv. - STD_LIB_DIRS.add(os.path.join(sys.base_prefix, "lib_pypy")) - except AttributeError: - pass - del _root + # PyPy stores the stdlib in two places: sys.prefix/lib_pypy and sys.prefix/lib-python/3 + # sysconfig.get_path on PyPy only returns the first so we patch this manually. + # Note that sysconfig.get_path expands: '{installed_base}/lib-{implementation_lower}' + # sys.prefix/lib-pypy resolves into sys.prefix/lib_pypy + STD_LIB_DIRS.add(sysconfig.get_path("stdlib")) + STD_LIB_DIRS.add( + sysconfig.get_path("stdlib", vars={"implementation_lower": "python/3"}) + ) + if os.name == "posix": # Need the real prefix if we're in a virtualenv, otherwise # the usual one will do. From d3b8f83932f2dafcf528ecc0ba697d6b2dd7cf70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 31 Jan 2022 20:50:29 +0100 Subject: [PATCH 2/4] Review --- astroid/modutils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index b20c120c55..678a004343 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -53,6 +53,7 @@ import types from distutils.errors import DistutilsPlatformError # pylint: disable=import-error from distutils.sysconfig import get_python_lib # pylint: disable=import-error +from pathlib import Path from typing import Dict, Set from astroid.interpreter._import import spec, util @@ -106,10 +107,8 @@ if platform.python_implementation() == "PyPy": # PyPy stores the stdlib in two places: sys.prefix/lib_pypy and sys.prefix/lib-python/3 - # sysconfig.get_path on PyPy only returns the first so we patch this manually. - # Note that sysconfig.get_path expands: '{installed_base}/lib-{implementation_lower}' - # sys.prefix/lib-pypy resolves into sys.prefix/lib_pypy - STD_LIB_DIRS.add(sysconfig.get_path("stdlib")) + # sysconfig.get_path on PyPy returns the first, but without an underscore so we patch this manually. + STD_LIB_DIRS.add(Path(sysconfig.get_path("stdlib").parent.joinpath("lib_pypy"))) STD_LIB_DIRS.add( sysconfig.get_path("stdlib", vars={"implementation_lower": "python/3"}) ) From ec3da7907617d3115a1305eb2bf90ec91833d23b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 31 Jan 2022 20:52:24 +0100 Subject: [PATCH 3/4] Fix mistake in Path --- astroid/modutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/modutils.py b/astroid/modutils.py index 678a004343..fc9dd43d5e 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -108,7 +108,7 @@ if platform.python_implementation() == "PyPy": # PyPy stores the stdlib in two places: sys.prefix/lib_pypy and sys.prefix/lib-python/3 # sysconfig.get_path on PyPy returns the first, but without an underscore so we patch this manually. - STD_LIB_DIRS.add(Path(sysconfig.get_path("stdlib").parent.joinpath("lib_pypy"))) + STD_LIB_DIRS.add(str(Path(sysconfig.get_path("stdlib")).parent / "lib_pypy")) STD_LIB_DIRS.add( sysconfig.get_path("stdlib", vars={"implementation_lower": "python/3"}) ) From ebdc33061f1060356984ef59b250556a35ea11d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 31 Jan 2022 21:41:30 +0100 Subject: [PATCH 4/4] Add platstdlib --- astroid/modutils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/astroid/modutils.py b/astroid/modutils.py index fc9dd43d5e..adc49444c5 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -112,6 +112,12 @@ STD_LIB_DIRS.add( sysconfig.get_path("stdlib", vars={"implementation_lower": "python/3"}) ) + # TODO: This is a fix for a workaround in virtualenv. At some point we should revisit + # whether this is still necessary. See https://github.com/PyCQA/astroid/pull/1324. + STD_LIB_DIRS.add(str(Path(sysconfig.get_path("platstdlib")).parent / "lib_pypy")) + STD_LIB_DIRS.add( + sysconfig.get_path("platstdlib", vars={"implementation_lower": "python/3"}) + ) if os.name == "posix": # Need the real prefix if we're in a virtualenv, otherwise