MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / _find_module

Function _find_module

tools/python-3.11.9-amd64/Lib/modulefinder.py:45–87  ·  view source on GitHub ↗

An importlib reimplementation of imp.find_module (for our purposes).

(name, path=None)

Source from the content-addressed store, hash-verified

43
44
45def _find_module(name, path=None):
46 """An importlib reimplementation of imp.find_module (for our purposes)."""
47
48 # It's necessary to clear the caches for our Finder first, in case any
49 # modules are being added/deleted/modified at runtime. In particular,
50 # test_modulefinder.py changes file tree contents in a cache-breaking way:
51
52 importlib.machinery.PathFinder.invalidate_caches()
53
54 spec = importlib.machinery.PathFinder.find_spec(name, path)
55
56 if spec is None:
57 raise ImportError("No module named {name!r}".format(name=name), name=name)
58
59 # Some special cases:
60
61 if spec.loader is importlib.machinery.BuiltinImporter:
62 return None, None, ("", "", _C_BUILTIN)
63
64 if spec.loader is importlib.machinery.FrozenImporter:
65 return None, None, ("", "", _PY_FROZEN)
66
67 file_path = spec.origin
68
69 if spec.loader.is_package(name):
70 return None, os.path.dirname(file_path), ("", "", _PKG_DIRECTORY)
71
72 if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
73 kind = _PY_SOURCE
74
75 elif isinstance(spec.loader, importlib.machinery.ExtensionFileLoader):
76 kind = _C_EXTENSION
77
78 elif isinstance(spec.loader, importlib.machinery.SourcelessFileLoader):
79 kind = _PY_COMPILED
80
81 else: # Should never happen.
82 return None, None, ("", "", _SEARCH_ERROR)
83
84 file = io.open_code(file_path)
85 suffix = os.path.splitext(file_path)[-1]
86
87 return file, file_path, (suffix, "rb", kind)
88
89
90class Module:

Callers 1

find_moduleMethod · 0.70

Calls 6

open_codeMethod · 0.80
splitextMethod · 0.80
invalidate_cachesMethod · 0.45
find_specMethod · 0.45
formatMethod · 0.45
is_packageMethod · 0.45

Tested by

no test coverage detected