Return all importable stdlib modules at runtime.
()
| 2264 | |
| 2265 | |
| 2266 | def get_importable_stdlib_modules() -> set[str]: |
| 2267 | """Return all importable stdlib modules at runtime.""" |
| 2268 | importable_stdlib_modules: set[str] = set() |
| 2269 | for module_name in sys.stdlib_module_names: |
| 2270 | if module_name in ANNOYING_STDLIB_MODULES: |
| 2271 | continue |
| 2272 | |
| 2273 | try: |
| 2274 | runtime = silent_import_module(module_name) |
| 2275 | except ImportError: |
| 2276 | continue |
| 2277 | else: |
| 2278 | importable_stdlib_modules.add(module_name) |
| 2279 | |
| 2280 | try: |
| 2281 | # some stdlib modules (e.g. `nt`) don't have __path__ set... |
| 2282 | runtime_path = runtime.__path__ |
| 2283 | runtime_name = runtime.__name__ |
| 2284 | except AttributeError: |
| 2285 | continue |
| 2286 | |
| 2287 | for submodule in pkgutil.walk_packages(runtime_path, runtime_name + "."): |
| 2288 | submodule_name = submodule.name |
| 2289 | |
| 2290 | # There are many annoying *.__main__ stdlib modules, |
| 2291 | # and including stubs for them isn't really that useful anyway: |
| 2292 | # tkinter.__main__ opens a tkinter windows; unittest.__main__ raises SystemExit; etc. |
| 2293 | # |
| 2294 | # The idlelib.* submodules are similarly annoying in opening random tkinter windows, |
| 2295 | # and we're unlikely to ever add stubs for idlelib in typeshed |
| 2296 | # (see discussion in https://github.com/python/typeshed/pull/9193) |
| 2297 | # |
| 2298 | # test.* modules do weird things like raising exceptions in __del__ methods, |
| 2299 | # leading to unraisable exceptions being logged to the terminal |
| 2300 | # as a warning at the end of the stubtest run |
| 2301 | if submodule_name.endswith(".__main__") or submodule_name.startswith( |
| 2302 | ("idlelib.", "test.") |
| 2303 | ): |
| 2304 | continue |
| 2305 | |
| 2306 | try: |
| 2307 | silent_import_module(submodule_name) |
| 2308 | except KeyboardInterrupt: |
| 2309 | raise |
| 2310 | # importing multiprocessing.popen_forkserver on Windows raises AttributeError... |
| 2311 | # some submodules also appear to raise SystemExit as well on some Python versions |
| 2312 | # (not sure exactly which) |
| 2313 | except BaseException: |
| 2314 | continue |
| 2315 | else: |
| 2316 | importable_stdlib_modules.add(submodule_name) |
| 2317 | |
| 2318 | return importable_stdlib_modules |
| 2319 | |
| 2320 | |
| 2321 | def get_allowlist_entries(allowlist_file: str) -> Iterator[str]: |
no test coverage detected
searching dependent graphs…