diff options
author | Victor Stinner <vstinner@python.org> | 2021-01-25 13:24:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-25 13:24:42 +0100 |
commit | db584bdad32d81e42b71871077a8008036f5c048 (patch) | |
tree | ef018339067b08456b55a083547f9427b817f1b3 /Tools | |
parent | bpo-43013: Fix old tkinter module names in idlelib (GH-24326) (diff) | |
download | cpython-db584bdad32d81e42b71871077a8008036f5c048.tar.gz cpython-db584bdad32d81e42b71871077a8008036f5c048.tar.bz2 cpython-db584bdad32d81e42b71871077a8008036f5c048.zip |
bpo-42955: Add sys.modules_names (GH-24238)
Add sys.module_names, containing the list of the standard library
module names.
Diffstat (limited to 'Tools')
-rw-r--r-- | Tools/scripts/generate_module_names.py | 102 |
1 files changed, 26 insertions, 76 deletions
diff --git a/Tools/scripts/generate_module_names.py b/Tools/scripts/generate_module_names.py index 985a1a5e5a2..9d363aa04b3 100644 --- a/Tools/scripts/generate_module_names.py +++ b/Tools/scripts/generate_module_names.py @@ -17,27 +17,6 @@ IGNORE = { '__pycache__', 'site-packages', - # Helper modules of public modules. - # For example, sysconfig uses _osx_support. - '_aix_support', - '_collections_abc', - '_compat_pickle', - '_compression', - '_markupbase', - '_osx_support', - '_sitebuiltins', - '_strptime', - '_threading_local', - '_weakrefset', - - # Used to bootstrap setup.py - '_bootsubprocess', - - # pure Python implementation - '_py_abc', - '_pydecimal', - '_pyio', - # test modules '__phello__.foo', '_ctypes_test', @@ -69,40 +48,20 @@ WINDOWS_MODULES = ( ) -def write_comment(fp, comment): - print(f"// {comment}", file=fp) - - -def write_modules(fp, names): - for name in sorted(names): - if name in IGNORE: - continue - print(f'"{name}",', file=fp) - print(file=fp) - - -def list_builtin_modules(fp): - write_comment(fp, "Built-in modules") - write_modules(fp, sys.builtin_module_names) - - # Pure Python modules (Lib/*.py) -def list_python_modules(fp): - write_comment(fp, "Pure Python modules (Lib/*.py)") - names = [] +def list_python_modules(names): for filename in os.listdir(STDLIB_PATH): if not filename.endswith(".py"): continue name = filename.removesuffix(".py") - names.append(name) - write_modules(fp, names) + names.add(name) def _list_sub_packages(path, names, parent=None): for name in os.listdir(path): - package_path = os.path.join(path, name) if name in IGNORE: continue + package_path = os.path.join(path, name) if not os.path.isdir(package_path): continue if not any(package_file.endswith(".py") @@ -114,40 +73,28 @@ def _list_sub_packages(path, names, parent=None): qualname = name if qualname in IGNORE: continue - names.append(qualname) + names.add(qualname) _list_sub_packages(package_path, names, qualname) # Packages and sub-packages -def list_packages(fp): - write_comment(fp, "Packages and sub-packages") - names = [] +def list_packages(names): _list_sub_packages(STDLIB_PATH, names) - write_modules(fp, names) - - -# Windows extensions -def list_windows_extensions(fp): - write_comment(fp, "Windows extension modules") - write_modules(fp, WINDOWS_MODULES) # Extension modules built by setup.py -def list_setup(fp): +def list_setup_extensions(names): cmd = [sys.executable, SETUP_PY, "-q", "build", "--list-module-names"] output = subprocess.check_output(cmd) output = output.decode("utf8") - names = output.splitlines() - - write_comment(fp, "Extension modules built by setup.py") - write_modules(fp, names) + extensions = output.splitlines() + names |= set(extensions) # Built-in and extension modules built by Modules/Setup -def list_modules_setup(fp): +def list_modules_setup_extensions(names): assign_var = re.compile("^[A-Z]+=") - names = [] with open(MODULES_SETUP, encoding="utf-8") as modules_fp: for line in modules_fp: # Strip comment @@ -165,25 +112,26 @@ def list_modules_setup(fp): continue # "errno errnomodule.c" => write "errno" name = parts[0] - names.append(name) + names.add(name) - write_comment(fp, "Built-in and extension modules built by Modules/Setup") - write_modules(fp, names) + +def list_modules(): + names = set(sys.builtin_module_names) | set(WINDOWS_MODULES) + list_modules_setup_extensions(names) + list_setup_extensions(names) + list_packages(names) + list_python_modules(names) + names -= set(IGNORE) + return names -def list_modules(fp): +def write_modules(fp, names): print("// Auto-generated by Tools/scripts/generate_module_names.py.", file=fp) + print("// List used to create sys.module_names.", file=fp) print(file=fp) print("static const char* _Py_module_names[] = {", file=fp) - print(file=fp) - - list_builtin_modules(fp) - list_python_modules(fp) - list_packages(fp) - list_setup(fp) - list_modules_setup(fp) - list_windows_extensions(fp) - + for name in sorted(names): + print(f'"{name}",', file=fp) print("};", file=fp) @@ -193,7 +141,9 @@ def main(): file=sys.stderr) sys.exit(1) - list_modules(sys.stdout) + fp = sys.stdout + names = list_modules() + write_modules(fp, names) if __name__ == "__main__": |