|
53 | 53 | import types
|
54 | 54 | from distutils.errors import DistutilsPlatformError # pylint: disable=import-error
|
55 | 55 | from distutils.sysconfig import get_python_lib # pylint: disable=import-error
|
| 56 | +from pathlib import Path |
56 | 57 | from typing import Dict, Set
|
57 | 58 |
|
58 | 59 | from astroid.interpreter._import import spec, util
|
|
105 | 106 | pass
|
106 | 107 |
|
107 | 108 | 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 | + |
128 | 122 | if os.name == "posix":
|
129 | 123 | # Need the real prefix if we're in a virtualenv, otherwise
|
130 | 124 | # the usual one will do.
|
|
0 commit comments