Debian Codegraph @

Tools/build/generate_stdlib_module_names.py

raw
1 # This script lists the names of standard library modules
2 # to update Python/stdlib_module_names.h
3 from __future__ import annotations
4
5 import _imp
6 import os.path
7 import sys
8 import sysconfig
9 from typing import TextIO
10
11 from check_extension_modules import ModuleChecker
12
13
14 SCRIPT_NAME = 'Tools/build/generate_stdlib_module_names.py'
15
16 SRC_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
17 STDLIB_PATH = os.path.join(SRC_DIR, 'Lib')
18
19 IGNORE = {
20 '__init__',
21 '__pycache__',
22 'site-packages',
23
24 # Test modules and packages
25 '__hello__',
26 '__phello__',
27 '__hello_alias__',
28 '__phello_alias__',
29 '__hello_only__',
30 '_ctypes_test',
31 '_testbuffer',
32 '_testcapi',
33 '_testclinic',
34 '_testclinic_limited',
35 '_testconsole',
36 '_testimportmultiple',
37 '_testinternalcapi',
38 '_testlimitedcapi',
39 '_testmultiphase',
40 '_testsinglephase',
41 '_testexternalinspection',
42 '_xxtestfuzz',
43 'idlelib.idle_test',
44 'test',
45 'xxlimited',
46 'xxlimited_35',
47 'xxsubtype',
48 }
49
50 ALLOW_TEST_MODULES = {
51 'doctest',
52 'unittest',
53 }
54
55 # Built-in modules
56 def list_builtin_modules(names: set[str]) -> None:
57 names |= set(sys.builtin_module_names)
58
59
60 # Pure Python modules (Lib/*.py)
61 def list_python_modules(names: set[str]) -> None:
62 for filename in os.listdir(STDLIB_PATH):
63 if not filename.endswith(".py"):
64 continue
65 name = filename.removesuffix(".py")
66 names.add(name)
67
68
69 # Packages in Lib/
70 def list_packages(names: set[str]) -> None:
71 for name in os.listdir(STDLIB_PATH):
72 if name in IGNORE:
73 continue
74 package_path = os.path.join(STDLIB_PATH, name)
75 if not os.path.isdir(package_path):
76 continue
77 if any(package_file.endswith(".py")
78 for package_file in os.listdir(package_path)):
79 names.add(name)
80
81
82 # Built-in and extension modules built by Modules/Setup*
83 # includes Windows and macOS extensions.
84 def list_modules_setup_extensions(names: set[str]) -> None:
85 checker = ModuleChecker()
86 names.update(checker.list_module_names(all=True))
87
88
89 # List frozen modules of the PyImport_FrozenModules list (Python/frozen.c).
90 # Use the "./Programs/_testembed list_frozen" command.
91 def list_frozen(names: set[str]) -> None:
92 submodules = set()
93 for name in _imp._frozen_module_names(): # type: ignore[attr-defined]
94 # To skip __hello__, __hello_alias__ and etc.
95 if name.startswith('__'):
96 continue
97 if '.' in name:
98 submodules.add(name)
99 else:
100 names.add(name)
101 # Make sure all frozen submodules have a known parent.
102 for name in list(submodules):
103 if name.partition('.')[0] in names:
104 submodules.remove(name)
105 if submodules:
106 raise Exception(f'unexpected frozen submodules: {sorted(submodules)}')
107
108
109 def list_modules() -> set[str]:
110 names: set[str] = set()
111
112 list_builtin_modules(names)
113 list_modules_setup_extensions(names)
114 list_packages(names)
115 list_python_modules(names)
116 list_frozen(names)
117
118 # Remove ignored packages and modules
119 for name in list(names):
120 package_name = name.split('.')[0]
121 # package_name can be equal to name
122 if package_name in IGNORE:
123 names.discard(name)
124
125 # Sanity checks
126 for name in names:
127 if "." in name:
128 raise Exception(f"sub-modules must not be listed: {name}")
129 if ("test" in name or "xx" in name) and name not in ALLOW_TEST_MODULES:
130 raise Exception(f"test modules must not be listed: {name}")
131
132 return names
133
134
135 def write_modules(fp: TextIO, names: set[str]) -> None:
136 print(f"// Auto-generated by {SCRIPT_NAME}.",
137 file=fp)
138 print("// List used to create sys.stdlib_module_names.", file=fp)
139 print(file=fp)
140 print("static const char* _Py_stdlib_module_names[] = {", file=fp)
141 for name in sorted(names):
142 print(f'"{name}",', file=fp)
143 print("};", file=fp)
144
145
146 def main() -> None:
147 if not sysconfig.is_python_build():
148 print(f"ERROR: {sys.executable} is not a Python build",
149 file=sys.stderr)
150 sys.exit(1)
151
152 fp = sys.stdout
153 names = list_modules()
154 write_modules(fp, names)
155
156
157 if __name__ == "__main__":
158 main()