Skip to content

Commit 96ca336

Browse files
committed
painfo: Use sys.path instead of the site.getsitepackages procedure.
Fixes python#5701. * mypy/pyinfo.py (getsitepackages): Use sys.path to discover packages. * mypy/modulefinder.py (get_site_packages_dirs): Adjust accordingly. (expand_site_packages, _parse_pth_file, _make_abspath): Remove procedures. (compute_search_paths): Adjust accordingly. * mypy/main.py (process_options): Likewise.
1 parent 4ff8d04 commit 96ca336

File tree

3 files changed

+10
-59
lines changed

3 files changed

+10
-59
lines changed

mypy/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,10 +1033,10 @@ def set_strict_flags() -> None:
10331033
# Set target.
10341034
if special_opts.modules + special_opts.packages:
10351035
options.build_type = BuildType.MODULE
1036-
egg_dirs, site_packages = get_site_packages_dirs(options.python_executable)
1036+
site_packages = get_site_packages_dirs(options.python_executable)
10371037
search_paths = SearchPaths((os.getcwd(),),
10381038
tuple(mypy_path() + options.mypy_path),
1039-
tuple(egg_dirs + site_packages),
1039+
tuple(site_packages),
10401040
())
10411041
targets = []
10421042
# TODO: use the same cache that the BuildManager will

mypy/modulefinder.py

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ def get_prefixes(python_executable: Optional[str]) -> Tuple[str, str]:
629629

630630

631631
@functools.lru_cache(maxsize=None)
632-
def get_site_packages_dirs(python_executable: Optional[str]) -> Tuple[List[str], List[str]]:
632+
def get_site_packages_dirs(python_executable: Optional[str]) -> List[str]:
633633
"""Find package directories for given python.
634634
635635
This runs a subprocess call, which generates a list of the egg directories, and the site
@@ -648,51 +648,7 @@ def get_site_packages_dirs(python_executable: Optional[str]) -> Tuple[List[str],
648648
site_packages = ast.literal_eval(
649649
subprocess.check_output([python_executable, pyinfo.__file__, 'getsitepackages'],
650650
stderr=subprocess.PIPE).decode())
651-
return expand_site_packages(site_packages)
652-
653-
654-
def expand_site_packages(site_packages: List[str]) -> Tuple[List[str], List[str]]:
655-
"""Expands .pth imports in site-packages directories"""
656-
egg_dirs: List[str] = []
657-
for dir in site_packages:
658-
if not os.path.isdir(dir):
659-
continue
660-
pth_filenames = sorted(name for name in os.listdir(dir) if name.endswith(".pth"))
661-
for pth_filename in pth_filenames:
662-
egg_dirs.extend(_parse_pth_file(dir, pth_filename))
663-
664-
return egg_dirs, site_packages
665-
666-
667-
def _parse_pth_file(dir: str, pth_filename: str) -> Iterator[str]:
668-
"""
669-
Mimics a subset of .pth import hook from Lib/site.py
670-
See https://github.com/python/cpython/blob/3.5/Lib/site.py#L146-L185
671-
"""
672-
673-
pth_file = os.path.join(dir, pth_filename)
674-
try:
675-
f = open(pth_file, "r")
676-
except OSError:
677-
return
678-
with f:
679-
for line in f.readlines():
680-
if line.startswith("#"):
681-
# Skip comment lines
682-
continue
683-
if line.startswith(("import ", "import\t")):
684-
# import statements in .pth files are not supported
685-
continue
686-
687-
yield _make_abspath(line.rstrip(), dir)
688-
689-
690-
def _make_abspath(path: str, root: str) -> str:
691-
"""Take a path and make it absolute relative to root if not already absolute."""
692-
if os.path.isabs(path):
693-
return os.path.normpath(path)
694-
else:
695-
return os.path.join(root, os.path.normpath(path))
651+
return site_packages
696652

697653

698654
def add_py2_mypypath_entries(mypypath: List[str]) -> List[str]:
@@ -781,7 +737,7 @@ def compute_search_paths(sources: List[BuildSource],
781737
if options.python_version[0] == 2:
782738
mypypath = add_py2_mypypath_entries(mypypath)
783739

784-
egg_dirs, site_packages = get_site_packages_dirs(options.python_executable)
740+
site_packages = get_site_packages_dirs(options.python_executable)
785741
base_prefix, prefix = get_prefixes(options.python_executable)
786742
is_venv = base_prefix != prefix
787743
for site_dir in site_packages:
@@ -801,7 +757,7 @@ def compute_search_paths(sources: List[BuildSource],
801757

802758
return SearchPaths(python_path=tuple(reversed(python_path)),
803759
mypy_path=tuple(mypypath),
804-
package_path=tuple(egg_dirs + site_packages),
760+
package_path=tuple(site_packages),
805761
typeshed_path=tuple(lib_path))
806762

807763

mypy/pyinfo.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,11 @@ def getprefixes():
2424

2525
def getsitepackages():
2626
# type: () -> List[str]
27-
res = []
28-
if hasattr(site, 'getsitepackages'):
29-
res.extend(site.getsitepackages())
3027

31-
if hasattr(site, 'getusersitepackages') and site.ENABLE_USER_SITE:
32-
res.insert(0, site.getusersitepackages())
33-
else:
34-
from distutils.sysconfig import get_python_lib
35-
res = [get_python_lib()]
36-
return res
28+
# Simply return sys.path, which has already been expanded
29+
# correctly via Python's site.py module, which takes care of .pth,
30+
# sitecustomize.py files, etc.
31+
return sys.path
3732

3833

3934
if __name__ == '__main__':

0 commit comments

Comments
 (0)