Skip to content

Commit d038b9f

Browse files
authored
Use sysconfig to determine stdlib and platstdlib paths for PyPy (#1324)
1 parent f86e53e commit d038b9f

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

astroid/modutils.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import types
5454
from distutils.errors import DistutilsPlatformError # pylint: disable=import-error
5555
from distutils.sysconfig import get_python_lib # pylint: disable=import-error
56+
from pathlib import Path
5657
from typing import Dict, Set
5758

5859
from astroid.interpreter._import import spec, util
@@ -105,26 +106,19 @@
105106
pass
106107

107108
if platform.python_implementation() == "PyPy":
108-
# The get_python_lib(standard_lib=True) function does not give valid
109-
# result with pypy in a virtualenv.
110-
# In a virtual environment, with CPython implementation the call to this function returns a path toward
111-
# the binary (its libraries) which has been used to create the virtual environment.
112-
# Not with pypy implementation.
113-
# The only way to retrieve such information is to use the sys.base_prefix hint.
114-
# It's worth noticing that under CPython implementation the return values of
115-
# get_python_lib(standard_lib=True) and get_python_lib(santdard_lib=True, prefix=sys.base_prefix)
116-
# are the same.
117-
# In the lines above, we could have replace the call to get_python_lib(standard=True)
118-
# with the one using prefix=sys.base_prefix but we prefer modifying only what deals with pypy.
119-
STD_LIB_DIRS.add(get_python_lib(standard_lib=True, prefix=sys.base_prefix))
120-
_root = os.path.join(sys.prefix, "lib_pypy")
121-
STD_LIB_DIRS.add(_root)
122-
try:
123-
# real_prefix is defined when running inside virtualenv.
124-
STD_LIB_DIRS.add(os.path.join(sys.base_prefix, "lib_pypy"))
125-
except AttributeError:
126-
pass
127-
del _root
109+
# PyPy stores the stdlib in two places: sys.prefix/lib_pypy and sys.prefix/lib-python/3
110+
# sysconfig.get_path on PyPy returns the first, but without an underscore so we patch this manually.
111+
STD_LIB_DIRS.add(str(Path(sysconfig.get_path("stdlib")).parent / "lib_pypy"))
112+
STD_LIB_DIRS.add(
113+
sysconfig.get_path("stdlib", vars={"implementation_lower": "python/3"})
114+
)
115+
# TODO: This is a fix for a workaround in virtualenv. At some point we should revisit
116+
# whether this is still necessary. See https://github.com/PyCQA/astroid/pull/1324.
117+
STD_LIB_DIRS.add(str(Path(sysconfig.get_path("platstdlib")).parent / "lib_pypy"))
118+
STD_LIB_DIRS.add(
119+
sysconfig.get_path("platstdlib", vars={"implementation_lower": "python/3"})
120+
)
121+
128122
if os.name == "posix":
129123
# Need the real prefix if we're in a virtualenv, otherwise
130124
# the usual one will do.

0 commit comments

Comments
 (0)