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