Path: blob/main/Tools/build/generate_stdlib_module_names.py
12 views
# This script lists the names of standard library modules1# to update Python/stdlib_module_names.h2import _imp3import os.path4import sys5import sysconfig67from check_extension_modules import ModuleChecker8910SCRIPT_NAME = 'Tools/build/generate_stdlib_module_names.py'1112SRC_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))13STDLIB_PATH = os.path.join(SRC_DIR, 'Lib')1415IGNORE = {16'__init__',17'__pycache__',18'site-packages',1920# Test modules and packages21'__hello__',22'__phello__',23'__hello_alias__',24'__phello_alias__',25'__hello_only__',26'_ctypes_test',27'_testbuffer',28'_testcapi',29'_testclinic',30'_testconsole',31'_testimportmultiple',32'_testinternalcapi',33'_testmultiphase',34'_testsinglephase',35'_xxsubinterpreters',36'_xxinterpchannels',37'_xxtestfuzz',38'idlelib.idle_test',39'test',40'xxlimited',41'xxlimited_35',42'xxsubtype',43}4445ALLOW_TEST_MODULES = {46'doctest',47'unittest',48}4950# Built-in modules51def list_builtin_modules(names):52names |= set(sys.builtin_module_names)535455# Pure Python modules (Lib/*.py)56def list_python_modules(names):57for filename in os.listdir(STDLIB_PATH):58if not filename.endswith(".py"):59continue60name = filename.removesuffix(".py")61names.add(name)626364# Packages in Lib/65def list_packages(names):66for name in os.listdir(STDLIB_PATH):67if name in IGNORE:68continue69package_path = os.path.join(STDLIB_PATH, name)70if not os.path.isdir(package_path):71continue72if any(package_file.endswith(".py")73for package_file in os.listdir(package_path)):74names.add(name)757677# Built-in and extension modules built by Modules/Setup*78# includes Windows and macOS extensions.79def list_modules_setup_extensions(names):80checker = ModuleChecker()81names.update(checker.list_module_names(all=True))828384# List frozen modules of the PyImport_FrozenModules list (Python/frozen.c).85# Use the "./Programs/_testembed list_frozen" command.86def list_frozen(names):87submodules = set()88for name in _imp._frozen_module_names():89# To skip __hello__, __hello_alias__ and etc.90if name.startswith('__'):91continue92if '.' in name:93submodules.add(name)94else:95names.add(name)96# Make sure all frozen submodules have a known parent.97for name in list(submodules):98if name.partition('.')[0] in names:99submodules.remove(name)100if submodules:101raise Exception(f'unexpected frozen submodules: {sorted(submodules)}')102103104def list_modules():105names = set()106107list_builtin_modules(names)108list_modules_setup_extensions(names)109list_packages(names)110list_python_modules(names)111list_frozen(names)112113# Remove ignored packages and modules114for name in list(names):115package_name = name.split('.')[0]116# package_name can be equal to name117if package_name in IGNORE:118names.discard(name)119120# Sanity checks121for name in names:122if "." in name:123raise Exception(f"sub-modules must not be listed: {name}")124if ("test" in name or "xx" in name) and name not in ALLOW_TEST_MODULES:125raise Exception(f"test modules must not be listed: {name}")126127return names128129130def write_modules(fp, names):131print(f"// Auto-generated by {SCRIPT_NAME}.",132file=fp)133print("// List used to create sys.stdlib_module_names.", file=fp)134print(file=fp)135print("static const char* _Py_stdlib_module_names[] = {", file=fp)136for name in sorted(names):137print(f'"{name}",', file=fp)138print("};", file=fp)139140141def main():142if not sysconfig.is_python_build():143print(f"ERROR: {sys.executable} is not a Python build",144file=sys.stderr)145sys.exit(1)146147fp = sys.stdout148names = list_modules()149write_modules(fp, names)150151152if __name__ == "__main__":153main()154155156