Find which elements of lib_path could contain a particular top-level module. In practice, almost all modules can be routed to the correct entry in lib_path by looking at just the first component of the module name. We take advantage of this by enumerating the contents of al
(self, lib_path: tuple[str, ...], id: str)
| 276 | return dirs |
| 277 | |
| 278 | def get_toplevel_possibilities(self, lib_path: tuple[str, ...], id: str) -> list[str]: |
| 279 | """Find which elements of lib_path could contain a particular top-level module. |
| 280 | |
| 281 | In practice, almost all modules can be routed to the correct entry in |
| 282 | lib_path by looking at just the first component of the module name. |
| 283 | |
| 284 | We take advantage of this by enumerating the contents of all of the |
| 285 | directories on the lib_path and building a map of which entries in |
| 286 | the lib_path could contain each potential top-level module that appears. |
| 287 | """ |
| 288 | |
| 289 | if lib_path in self.initial_components: |
| 290 | return self.initial_components[lib_path].get(id, []) |
| 291 | |
| 292 | # Enumerate all the files in the directories on lib_path and produce the map |
| 293 | components: dict[str, list[str]] = {} |
| 294 | for dir in lib_path: |
| 295 | try: |
| 296 | contents = self.fscache.listdir(dir) |
| 297 | except OSError: |
| 298 | contents = [] |
| 299 | # False positives are fine for correctness here, since we will check |
| 300 | # precisely later, so we only look at the root of every filename without |
| 301 | # any concern for the exact details. |
| 302 | for name in contents: |
| 303 | name = os.path.splitext(name)[0] |
| 304 | components.setdefault(name, []).append(dir) |
| 305 | |
| 306 | self.initial_components[lib_path] = components |
| 307 | return components.get(id, []) |
| 308 | |
| 309 | def find_module(self, id: str, *, fast_path: bool = False) -> ModuleSearchResult: |
| 310 | """Return the path of the module source file or why it wasn't found. |
no test coverage detected