2323
2424import fnmatch
2525import importlib .machinery
26- import os
2726import sys
2827import warnings
28+ from pathlib import Path
2929
3030from .line import (
3131 current_word ,
@@ -129,33 +129,27 @@ def complete(self, cursor_offset, line):
129129
130130 def find_modules (self , path ):
131131 """Find all modules (and packages) for a given directory."""
132- if not os . path .isdir ( path ):
132+ if not path .is_dir ( ):
133133 # Perhaps a zip file
134134 return
135- basepath = os .path .basename (path )
136- if any (fnmatch .fnmatch (basepath , entry ) for entry in self .skiplist ):
135+ if any (fnmatch .fnmatch (path .name , entry ) for entry in self .skiplist ):
137136 # Path is on skiplist
138137 return
139138
140- try :
141- filenames = os .listdir (path )
142- except OSError :
143- filenames = []
144-
145- finder = importlib .machinery .FileFinder (path )
146-
147- for name in filenames :
148- if any (fnmatch .fnmatch (name , entry ) for entry in self .skiplist ):
139+ finder = importlib .machinery .FileFinder (str (path ))
140+ for p in path .iterdir ():
141+ if any (fnmatch .fnmatch (p .name , entry ) for entry in self .skiplist ):
149142 # Path is on skiplist
150143 continue
151- elif not any (name .endswith (suffix ) for suffix in SUFFIXES ):
144+ elif not any (p . name .endswith (suffix ) for suffix in SUFFIXES ):
152145 # Possibly a package
153- if "." in name :
146+ if "." in p . name :
154147 continue
155- elif os . path . isdir ( os . path . join ( path , name ) ):
148+ elif p . is_dir ( ):
156149 # Unfortunately, CPython just crashes if there is a directory
157150 # which ends with a python extension, so work around.
158151 continue
152+ name = p .name
159153 for suffix in SUFFIXES :
160154 if name .endswith (suffix ):
161155 name = name [: - len (suffix )]
@@ -183,10 +177,10 @@ def find_modules(self, path):
183177 continue
184178 else :
185179 if is_package :
186- path_real = os . path . realpath (pathname )
180+ path_real = Path (pathname ). resolve ( )
187181 if path_real not in self .paths :
188182 self .paths .add (path_real )
189- for subname in self .find_modules (pathname ):
183+ for subname in self .find_modules (path_real ):
190184 if subname != "__init__" :
191185 yield f"{ name } .{ subname } "
192186 yield name
@@ -200,8 +194,7 @@ def find_all_modules(self, path=None):
200194 path = sys .path
201195
202196 for p in path :
203- if not p :
204- p = os .curdir
197+ p = Path (p ).resolve () if p else Path .cwd ()
205198 for module in self .find_modules (p ):
206199 self .modules .add (module )
207200 yield
0 commit comments