MCPcopy Index your code
hub / github.com/RustPython/RustPython / find_spec

Method find_spec

Lib/importlib/_bootstrap_external.py:1357–1406  ·  view source on GitHub ↗

Try to find a spec for the specified module. Returns the matching spec, or None if not found.

(self, fullname, target=None)

Source from the content-addressed store, hash-verified

1355 submodule_search_locations=smsl)
1356
1357 def find_spec(self, fullname, target=None):
1358 """Try to find a spec for the specified module.
1359
1360 Returns the matching spec, or None if not found.
1361 """
1362 is_namespace = False
1363 tail_module = fullname.rpartition('.')[2]
1364 try:
1365 mtime = _path_stat(self.path or _os.getcwd()).st_mtime
1366 except OSError:
1367 mtime = -1
1368 if mtime != self._path_mtime:
1369 self._fill_cache()
1370 self._path_mtime = mtime
1371 # tail_module keeps the original casing, for __file__ and friends
1372 if _relax_case():
1373 cache = self._relaxed_path_cache
1374 cache_module = tail_module.lower()
1375 else:
1376 cache = self._path_cache
1377 cache_module = tail_module
1378 # Check if the module is the name of a directory (and thus a package).
1379 if cache_module in cache:
1380 base_path = _path_join(self.path, tail_module)
1381 for suffix, loader_class in self._loaders:
1382 init_filename = '__init__' + suffix
1383 full_path = _path_join(base_path, init_filename)
1384 if _path_isfile(full_path):
1385 return self._get_spec(loader_class, fullname, full_path, [base_path], target)
1386 else:
1387 # If a namespace package, return the path if we don't
1388 # find a module in the next section.
1389 is_namespace = _path_isdir(base_path)
1390 # Check for a file w/ a proper suffix exists.
1391 for suffix, loader_class in self._loaders:
1392 try:
1393 full_path = _path_join(self.path, tail_module + suffix)
1394 except ValueError:
1395 return None
1396 _bootstrap._verbose_message('trying {}', full_path, verbosity=2)
1397 if cache_module + suffix in cache:
1398 if _path_isfile(full_path):
1399 return self._get_spec(loader_class, fullname, full_path,
1400 None, target)
1401 if is_namespace:
1402 _bootstrap._verbose_message('possible namespace for {}', base_path)
1403 spec = _bootstrap.ModuleSpec(fullname, None)
1404 spec.submodule_search_locations = [base_path]
1405 return spec
1406 return None
1407
1408 def _fill_cache(self):
1409 """Fill the cache of potential modules and packages for this directory."""

Callers 2

find_specMethod · 0.95
find_specMethod · 0.95

Calls 9

_fill_cacheMethod · 0.95
_get_specMethod · 0.95
_path_statFunction · 0.70
_relax_caseFunction · 0.70
_path_joinFunction · 0.70
_path_isfileFunction · 0.70
_path_isdirFunction · 0.70
rpartitionMethod · 0.45
lowerMethod · 0.45

Tested by 2

find_specMethod · 0.76
find_specMethod · 0.76