@@ -143,15 +143,45 @@ def find_module(
143
143
type = ModuleType .C_BUILTIN ,
144
144
)
145
145
146
+ if submodule_path is not None :
147
+ search_paths = list (submodule_path )
148
+ else :
149
+ search_paths = sys .path
150
+
151
+ suffixes = (".py" , ".pyi" , importlib .machinery .BYTECODE_SUFFIXES [0 ])
152
+ for entry in search_paths :
153
+ package_directory = os .path .join (entry , modname )
154
+ for suffix in suffixes :
155
+ package_file_name = "__init__" + suffix
156
+ file_path = os .path .join (package_directory , package_file_name )
157
+ if cached_os_path_isfile (file_path ):
158
+ return ModuleSpec (
159
+ name = modname ,
160
+ location = package_directory ,
161
+ type = ModuleType .PKG_DIRECTORY ,
162
+ )
163
+ for suffix , type_ in ImportlibFinder ._SUFFIXES :
164
+ file_name = modname + suffix
165
+ file_path = os .path .join (entry , file_name )
166
+ if cached_os_path_isfile (file_path ):
167
+ return ModuleSpec (name = modname , location = file_path , type = type_ )
168
+
146
169
# sys.stdlib_module_names was added in Python 3.10
147
170
if PY310_PLUS :
148
- # If the module is a stdlib module, check whether this is a frozen module. Note that
149
- # `find_spec` actually imports the module, so we want to make sure we only run this code
150
- # for stuff that can be expected to be frozen. For now this is only stdlib.
171
+ # If the module name matches a stdlib module name, check whether this is a frozen
172
+ # module. Note that `find_spec` actually imports parent modules, so we want to make
173
+ # sure we only run this code for stuff that can be expected to be frozen. For now
174
+ # this is only stdlib.
151
175
if modname in sys .stdlib_module_names or (
152
176
processed and processed [0 ] in sys .stdlib_module_names
153
177
):
154
- spec = importlib .util .find_spec ("." .join ((* processed , modname )))
178
+ try :
179
+ with warnings .catch_warnings ():
180
+ warnings .filterwarnings ("ignore" , category = Warning )
181
+ spec = importlib .util .find_spec ("." .join ((* processed , modname )))
182
+ except ValueError :
183
+ spec = None
184
+
155
185
if (
156
186
spec
157
187
and spec .loader # type: ignore[comparison-overlap] # noqa: E501
@@ -187,28 +217,6 @@ def find_module(
187
217
except ValueError :
188
218
pass
189
219
190
- if submodule_path is not None :
191
- search_paths = list (submodule_path )
192
- else :
193
- search_paths = sys .path
194
-
195
- suffixes = (".py" , ".pyi" , importlib .machinery .BYTECODE_SUFFIXES [0 ])
196
- for entry in search_paths :
197
- package_directory = os .path .join (entry , modname )
198
- for suffix in suffixes :
199
- package_file_name = "__init__" + suffix
200
- file_path = os .path .join (package_directory , package_file_name )
201
- if cached_os_path_isfile (file_path ):
202
- return ModuleSpec (
203
- name = modname ,
204
- location = package_directory ,
205
- type = ModuleType .PKG_DIRECTORY ,
206
- )
207
- for suffix , type_ in ImportlibFinder ._SUFFIXES :
208
- file_name = modname + suffix
209
- file_path = os .path .join (entry , file_name )
210
- if cached_os_path_isfile (file_path ):
211
- return ModuleSpec (name = modname , location = file_path , type = type_ )
212
220
return None
213
221
214
222
def contribute_to_path (
0 commit comments