@@ -608,70 +608,26 @@ def default_lib_path(data_dir: str,
608
608
609
609
610
610
@functools .lru_cache (maxsize = None )
611
- def get_search_dirs (python_executable : Optional [str ]) -> Tuple [ List [str ], List [ str ], List [ str ] ]:
611
+ def get_search_dirs (python_executable : Optional [str ]) -> List [str ]:
612
612
"""Find package directories for given python.
613
613
614
- This runs a subprocess call, which generates a list of the egg directories, and the site
615
- package directories. To avoid repeatedly calling a subprocess (which can be slow!) we
614
+ This runs a subprocess call, which generates a list of the directories in sys.path.
615
+ To avoid repeatedly calling a subprocess (which can be slow!) we
616
616
lru_cache the results.
617
617
"""
618
618
619
619
if python_executable is None :
620
- return [], [], []
620
+ return []
621
621
elif python_executable == sys .executable :
622
622
# Use running Python's package dirs
623
- site_packages , sys_path = pyinfo .getsearchdirs ()
623
+ sys_path = pyinfo .getsearchdirs ()
624
624
else :
625
625
# Use subprocess to get the package directory of given Python
626
626
# executable
627
- site_packages , sys_path = ast .literal_eval (
627
+ sys_path = ast .literal_eval (
628
628
subprocess .check_output ([python_executable , pyinfo .__file__ , 'getsearchdirs' ],
629
629
stderr = subprocess .PIPE ).decode ())
630
- return expand_site_packages (site_packages ) + (sys_path ,)
631
-
632
-
633
- def expand_site_packages (site_packages : List [str ]) -> Tuple [List [str ], List [str ]]:
634
- """Expands .pth imports in site-packages directories"""
635
- egg_dirs : List [str ] = []
636
- for dir in site_packages :
637
- if not os .path .isdir (dir ):
638
- continue
639
- pth_filenames = sorted (name for name in os .listdir (dir ) if name .endswith (".pth" ))
640
- for pth_filename in pth_filenames :
641
- egg_dirs .extend (_parse_pth_file (dir , pth_filename ))
642
-
643
- return egg_dirs , site_packages
644
-
645
-
646
- def _parse_pth_file (dir : str , pth_filename : str ) -> Iterator [str ]:
647
- """
648
- Mimics a subset of .pth import hook from Lib/site.py
649
- See https://github.com/python/cpython/blob/3.5/Lib/site.py#L146-L185
650
- """
651
-
652
- pth_file = os .path .join (dir , pth_filename )
653
- try :
654
- f = open (pth_file , "r" )
655
- except OSError :
656
- return
657
- with f :
658
- for line in f .readlines ():
659
- if line .startswith ("#" ):
660
- # Skip comment lines
661
- continue
662
- if line .startswith (("import " , "import\t " )):
663
- # import statements in .pth files are not supported
664
- continue
665
-
666
- yield _make_abspath (line .rstrip (), dir )
667
-
668
-
669
- def _make_abspath (path : str , root : str ) -> str :
670
- """Take a path and make it absolute relative to root if not already absolute."""
671
- if os .path .isabs (path ):
672
- return os .path .normpath (path )
673
- else :
674
- return os .path .join (root , os .path .normpath (path ))
630
+ return sys_path
675
631
676
632
677
633
def add_py2_mypypath_entries (mypypath : List [str ]) -> List [str ]:
@@ -760,20 +716,20 @@ def compute_search_paths(sources: List[BuildSource],
760
716
if options .python_version [0 ] == 2 :
761
717
mypypath = add_py2_mypypath_entries (mypypath )
762
718
763
- egg_dirs , site_packages , sys_path = get_search_dirs (options .python_executable )
764
- for site_dir in site_packages + sys_path :
765
- assert site_dir not in lib_path
766
- if (site_dir in mypypath or
767
- any (p .startswith (site_dir + os .path .sep ) for p in mypypath ) or
768
- os .path .altsep and any (p .startswith (site_dir + os .path .altsep ) for p in mypypath )):
769
- print ("{} is in the MYPYPATH. Please remove it." .format (site_dir ), file = sys .stderr )
719
+ search_dirs = get_search_dirs (options .python_executable )
720
+ for search_dir in search_dirs :
721
+ assert search_dir not in lib_path
722
+ if (search_dir in mypypath or
723
+ any (p .startswith (search_dir + os .path .sep ) for p in mypypath ) or
724
+ os .path .altsep and any (p .startswith (search_dir + os .path .altsep ) for p in mypypath )):
725
+ print ("{} is in the MYPYPATH. Please remove it." .format (search_dir ), file = sys .stderr )
770
726
print ("See https://mypy.readthedocs.io/en/stable/running_mypy.html"
771
727
"#how-mypy-handles-imports for more info" , file = sys .stderr )
772
728
sys .exit (1 )
773
729
774
730
return SearchPaths (python_path = tuple (reversed (python_path )),
775
731
mypy_path = tuple (mypypath ),
776
- package_path = tuple (egg_dirs + site_packages + sys_path ),
732
+ package_path = tuple (search_dirs ),
777
733
typeshed_path = tuple (lib_path ))
778
734
779
735
0 commit comments