bpo-45019: Do some cleanup related to frozen modules. (gh-28319)

There are a few things I missed in gh-27980. This is a follow-up that will make subsequent PRs cleaner. It includes fixes to tests and tools that reference the frozen modules.

https://bugs.python.org/issue45019
This commit is contained in:
Eric Snow 2021-09-13 16:18:37 -06:00 committed by GitHub
parent 1fc41ae870
commit a2d8c4b81b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 342 additions and 140 deletions

View file

@ -297,6 +297,24 @@ exit:
return return_value;
}
PyDoc_STRVAR(_imp__frozen_module_names__doc__,
"_frozen_module_names($module, /)\n"
"--\n"
"\n"
"Returns the list of available frozen modules.");
#define _IMP__FROZEN_MODULE_NAMES_METHODDEF \
{"_frozen_module_names", (PyCFunction)_imp__frozen_module_names, METH_NOARGS, _imp__frozen_module_names__doc__},
static PyObject *
_imp__frozen_module_names_impl(PyObject *module);
static PyObject *
_imp__frozen_module_names(PyObject *module, PyObject *Py_UNUSED(ignored))
{
return _imp__frozen_module_names_impl(module);
}
#if defined(HAVE_DYNAMIC_LOADING)
PyDoc_STRVAR(_imp_create_dynamic__doc__,
@ -449,4 +467,4 @@ exit:
#ifndef _IMP_EXEC_DYNAMIC_METHODDEF
#define _IMP_EXEC_DYNAMIC_METHODDEF
#endif /* !defined(_IMP_EXEC_DYNAMIC_METHODDEF) */
/*[clinic end generated code: output=7c31c433af88af6b input=a9049054013a1b77]*/
/*[clinic end generated code: output=0ab3fa7c5808bba4 input=a9049054013a1b77]*/

View file

@ -47,7 +47,7 @@
/* Note that a negative size indicates a package. */
static const struct _frozen _PyImport_FrozenModules[] = {
/* importlib */
/* import system */
{"_frozen_importlib", _Py_M__importlib__bootstrap,
(int)sizeof(_Py_M__importlib__bootstrap)},
{"_frozen_importlib_external", _Py_M__importlib__bootstrap_external,

12
Python/frozen_modules/MANIFEST generated Normal file
View file

@ -0,0 +1,12 @@
# The list of frozen modules with key information.
# Note that the "check_generated_files" CI job will identify
# when source files were changed but regen-frozen wasn't run.
# This file is auto-generated by Tools/scripts/freeze_modules.py.
module ispkg source frozen checksum
-------------------------- ----- ------------------------------- ------------------------------- ------------
_frozen_importlib no <importlib._bootstrap> importlib__bootstrap.h 749d553f858d
_frozen_importlib_external no <importlib._bootstrap_external> importlib__bootstrap_external.h e4539e6347d7
zipimport no <zipimport> zipimport.h 374879e5d43d
__hello__ no Tools/freeze/flag.py hello.h af6fb665713f
__phello__ YES Tools/freeze/flag.py hello.h af6fb665713f
__phello__.spam no Tools/freeze/flag.py hello.h af6fb665713f

View file

@ -0,0 +1,7 @@
This directory contains the generated .h files for all the frozen
modules. Python/frozen.c depends on these files.
Note that, other than the required frozen modules, none of these files
are committed into the repo.
See Tools/scripts/freeze_modules.py for more info.

View file

@ -16,6 +16,7 @@
#include "code.h"
#include "importdl.h"
#include "pydtrace.h"
#include <stdbool.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
@ -1049,6 +1050,32 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
/* Frozen modules */
static PyObject *
list_frozen_module_names(bool force)
{
PyObject *names = PyList_New(0);
if (names == NULL) {
return NULL;
}
for (const struct _frozen *p = PyImport_FrozenModules; ; p++) {
if (p->name == NULL) {
break;
}
PyObject *name = PyUnicode_FromString(p->name);
if (name == NULL) {
Py_DECREF(names);
return NULL;
}
int res = PyList_Append(names, name);
Py_DECREF(name);
if (res != 0) {
Py_DECREF(names);
return NULL;
}
}
return names;
}
static const struct _frozen *
find_frozen(PyObject *name)
{
@ -1954,6 +1981,19 @@ _imp_is_frozen_impl(PyObject *module, PyObject *name)
return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
}
/*[clinic input]
_imp._frozen_module_names
Returns the list of available frozen modules.
[clinic start generated code]*/
static PyObject *
_imp__frozen_module_names_impl(PyObject *module)
/*[clinic end generated code: output=80609ef6256310a8 input=76237fbfa94460d2]*/
{
return list_frozen_module_names(true);
}
/* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */
static int
exec_builtin_or_dynamic(PyObject *mod) {
@ -2114,6 +2154,7 @@ static PyMethodDef imp_methods[] = {
_IMP_INIT_FROZEN_METHODDEF
_IMP_IS_BUILTIN_METHODDEF
_IMP_IS_FROZEN_METHODDEF
_IMP__FROZEN_MODULE_NAMES_METHODDEF
_IMP_CREATE_DYNAMIC_METHODDEF
_IMP_EXEC_DYNAMIC_METHODDEF
_IMP_EXEC_BUILTIN_METHODDEF