bpo-43445: Add frozen modules to sys.stdlib_module_names (GH-24798)

Add frozen modules to sys.stdlib_module_names. For example, add
"_frozen_importlib" and "_frozen_importlib_external" names.

Add "list_frozen" command to Programs/_testembed.
This commit is contained in:
Victor Stinner 2021-03-10 11:14:07 +01:00 committed by GitHub
parent b4f9089d4a
commit 307745aa42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 4 deletions

View file

@ -11,14 +11,16 @@ SRC_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
STDLIB_PATH = os.path.join(SRC_DIR, 'Lib')
MODULES_SETUP = os.path.join(SRC_DIR, 'Modules', 'Setup')
SETUP_PY = os.path.join(SRC_DIR, 'setup.py')
TEST_EMBED = os.path.join(SRC_DIR, 'Programs', '_testembed')
IGNORE = {
'__init__',
'__pycache__',
'site-packages',
# test modules
'__phello__.foo',
# Test modules and packages
'__hello__',
'__phello__',
'_ctypes_test',
'_testbuffer',
'_testcapi',
@ -103,13 +105,40 @@ def list_modules_setup_extensions(names):
names.add(name)
# List frozen modules of the PyImport_FrozenModules list (Python/frozen.c).
# Use the "./Programs/_testembed list_frozen" command.
def list_frozen(names):
args = [TEST_EMBED, 'list_frozen']
proc = subprocess.run(args, stdout=subprocess.PIPE, text=True)
exitcode = proc.returncode
if exitcode:
cmd = ' '.join(args)
print(f"{cmd} failed with exitcode {exitcode}")
sys.exit(exitcode)
for line in proc.stdout.splitlines():
name = line.strip()
names.add(name)
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)
list_frozen(names)
# Remove ignored packages and modules
for name in list(names):
package_name = name.split('.')[0]
# package_name can be equal to name
if package_name in IGNORE:
names.discard(name)
for name in names:
if "." in name:
raise Exception("sub-modules must not be listed")
return names