mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-45020: Identify which frozen modules are actually aliases. (gh-28655)
In the list of generated frozen modules at the top of Tools/scripts/freeze_modules.py, you will find that some of the modules have a different name than the module (or .py file) that is actually frozen. Let's call each case an "alias". Aliases do not come into play until we get to the (generated) list of modules in Python/frozen.c. (The tool for freezing modules, Programs/_freeze_module, is only concerned with the source file, not the module it will be used for.) Knowledge of which frozen modules are aliases (and the identity of the original module) normally isn't important. However, this information is valuable when we go to set __file__ on frozen stdlib modules. This change updates Tools/scripts/freeze_modules.py to map aliases to the original module name (or None if not a stdlib module) in Python/frozen.c. We also add a helper function in Python/import.c to look up a frozen module's alias and add the result of that function to the frozen info returned from find_frozen(). https://bugs.python.org/issue45020
This commit is contained in:
parent
444429142c
commit
08285d563e
10 changed files with 225 additions and 37 deletions
7
Python/clinic/import.c.h
generated
7
Python/clinic/import.c.h
generated
|
@ -178,7 +178,10 @@ PyDoc_STRVAR(_imp_find_frozen__doc__,
|
|||
"The returned info (a 2-tuple):\n"
|
||||
"\n"
|
||||
" * data the raw marshalled bytes\n"
|
||||
" * is_package whether or not it is a package");
|
||||
" * is_package whether or not it is a package\n"
|
||||
" * origname the originally frozen module\'s name, or None if not\n"
|
||||
" a stdlib module (this will usually be the same as\n"
|
||||
" the module\'s current name)");
|
||||
|
||||
#define _IMP_FIND_FROZEN_METHODDEF \
|
||||
{"find_frozen", (PyCFunction)_imp_find_frozen, METH_O, _imp_find_frozen__doc__},
|
||||
|
@ -545,4 +548,4 @@ exit:
|
|||
#ifndef _IMP_EXEC_DYNAMIC_METHODDEF
|
||||
#define _IMP_EXEC_DYNAMIC_METHODDEF
|
||||
#endif /* !defined(_IMP_EXEC_DYNAMIC_METHODDEF) */
|
||||
/*[clinic end generated code: output=a31e1c00653359ff input=a9049054013a1b77]*/
|
||||
/*[clinic end generated code: output=8c8dd08158f9ac7c input=a9049054013a1b77]*/
|
||||
|
|
|
@ -36,6 +36,7 @@
|
|||
and __phello__.spam. Loading any will print some famous words... */
|
||||
|
||||
#include "Python.h"
|
||||
#include "pycore_import.h"
|
||||
|
||||
/* Includes for frozen modules: */
|
||||
#include "frozen_modules/importlib._bootstrap.h"
|
||||
|
@ -102,9 +103,24 @@ static const struct _frozen _PyImport_FrozenModules[] = {
|
|||
{"__phello__.spam", _Py_M____phello___spam,
|
||||
(int)sizeof(_Py_M____phello___spam)},
|
||||
{"__hello_only__", _Py_M__frozen_only, (int)sizeof(_Py_M__frozen_only)},
|
||||
{0, 0, 0} /* sentinel */
|
||||
{0, 0, 0} /* modules sentinel */
|
||||
};
|
||||
|
||||
static const struct _module_alias aliases[] = {
|
||||
{"_frozen_importlib", "importlib._bootstrap"},
|
||||
{"_frozen_importlib_external", "importlib._bootstrap_external"},
|
||||
{"os.path", "posixpath"},
|
||||
{"__hello_alias__", "__hello__"},
|
||||
{"__phello_alias__", "__hello__"},
|
||||
{"__phello_alias__.spam", "__hello__"},
|
||||
{"__phello__.__init__", "<__phello__"},
|
||||
{"__phello__.ham.__init__", "<__phello__.ham"},
|
||||
{"__hello_only__", NULL},
|
||||
{0, 0} /* aliases sentinel */
|
||||
};
|
||||
const struct _module_alias *_PyImport_FrozenAliases = aliases;
|
||||
|
||||
|
||||
/* Embedding apps may change this pointer to point to their favorite
|
||||
collection of frozen modules: */
|
||||
|
||||
|
|
|
@ -1046,6 +1046,29 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
|
|||
}
|
||||
|
||||
|
||||
/* Return true if the name is an alias. In that case, "alias" is set
|
||||
to the original module name. If it is an alias but the original
|
||||
module isn't known then "alias" is set to NULL while true is returned. */
|
||||
static bool
|
||||
resolve_module_alias(const char *name, const struct _module_alias *aliases,
|
||||
const char **alias)
|
||||
{
|
||||
const struct _module_alias *entry;
|
||||
for (entry = aliases; ; entry++) {
|
||||
if (entry->name == NULL) {
|
||||
/* It isn't an alias. */
|
||||
return false;
|
||||
}
|
||||
if (strcmp(name, entry->name) == 0) {
|
||||
if (alias != NULL) {
|
||||
*alias = entry->orig;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Frozen modules */
|
||||
|
||||
static bool
|
||||
|
@ -1161,16 +1184,15 @@ struct frozen_info {
|
|||
const char *data;
|
||||
Py_ssize_t size;
|
||||
bool is_package;
|
||||
bool is_alias;
|
||||
const char *origname;
|
||||
};
|
||||
|
||||
static frozen_status
|
||||
find_frozen(PyObject *nameobj, struct frozen_info *info)
|
||||
{
|
||||
if (info != NULL) {
|
||||
info->nameobj = NULL;
|
||||
info->data = NULL;
|
||||
info->size = 0;
|
||||
info->is_package = false;
|
||||
memset(info, 0, sizeof(*info));
|
||||
}
|
||||
|
||||
if (nameobj == NULL || nameobj == Py_None) {
|
||||
|
@ -1205,6 +1227,9 @@ find_frozen(PyObject *nameobj, struct frozen_info *info)
|
|||
info->data = (const char *)p->code;
|
||||
info->size = p->size < 0 ? -(p->size) : p->size;
|
||||
info->is_package = p->size < 0 ? true : false;
|
||||
info->origname = name;
|
||||
info->is_alias = resolve_module_alias(name, _PyImport_FrozenAliases,
|
||||
&info->origname);
|
||||
}
|
||||
|
||||
if (p->code == NULL) {
|
||||
|
@ -1246,7 +1271,8 @@ int
|
|||
PyImport_ImportFrozenModuleObject(PyObject *name)
|
||||
{
|
||||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
PyObject *co, *m, *d;
|
||||
PyObject *co, *m, *d = NULL;
|
||||
int err;
|
||||
|
||||
struct frozen_info info;
|
||||
frozen_status status = find_frozen(name, &info);
|
||||
|
@ -1267,7 +1293,6 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
|
|||
if (info.is_package) {
|
||||
/* Set __path__ to the empty list */
|
||||
PyObject *l;
|
||||
int err;
|
||||
m = import_add_module(tstate, name);
|
||||
if (m == NULL)
|
||||
goto err_return;
|
||||
|
@ -1288,15 +1313,33 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
|
|||
goto err_return;
|
||||
}
|
||||
m = exec_code_in_module(tstate, name, d, co);
|
||||
Py_DECREF(d);
|
||||
if (m == NULL) {
|
||||
goto err_return;
|
||||
}
|
||||
Py_DECREF(co);
|
||||
Py_DECREF(m);
|
||||
/* Set __origname__ (consumed in FrozenImporter._setup_module()). */
|
||||
PyObject *origname;
|
||||
if (info.origname) {
|
||||
origname = PyUnicode_FromString(info.origname);
|
||||
if (origname == NULL) {
|
||||
goto err_return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
Py_INCREF(Py_None);
|
||||
origname = Py_None;
|
||||
}
|
||||
err = PyDict_SetItemString(d, "__origname__", origname);
|
||||
Py_DECREF(origname);
|
||||
if (err != 0) {
|
||||
goto err_return;
|
||||
}
|
||||
Py_DECREF(d);
|
||||
Py_DECREF(co);
|
||||
return 1;
|
||||
|
||||
err_return:
|
||||
Py_XDECREF(d);
|
||||
Py_DECREF(co);
|
||||
return -1;
|
||||
}
|
||||
|
@ -2014,11 +2057,14 @@ The returned info (a 2-tuple):
|
|||
|
||||
* data the raw marshalled bytes
|
||||
* is_package whether or not it is a package
|
||||
* origname the originally frozen module's name, or None if not
|
||||
a stdlib module (this will usually be the same as
|
||||
the module's current name)
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
_imp_find_frozen_impl(PyObject *module, PyObject *name)
|
||||
/*[clinic end generated code: output=3fd17da90d417e4e input=4e52b3ac95f6d7ab]*/
|
||||
/*[clinic end generated code: output=3fd17da90d417e4e input=6aa7b9078a89280a]*/
|
||||
{
|
||||
struct frozen_info info;
|
||||
frozen_status status = find_frozen(name, &info);
|
||||
|
@ -2032,12 +2078,25 @@ _imp_find_frozen_impl(PyObject *module, PyObject *name)
|
|||
set_frozen_error(status, name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *data = PyBytes_FromStringAndSize(info.data, info.size);
|
||||
if (data == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
PyObject *result = PyTuple_Pack(2, data,
|
||||
info.is_package ? Py_True : Py_False);
|
||||
|
||||
PyObject *origname = NULL;
|
||||
if (info.origname != NULL && info.origname[0] != '\0') {
|
||||
origname = PyUnicode_FromString(info.origname);
|
||||
if (origname == NULL) {
|
||||
Py_DECREF(data);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
PyObject *result = PyTuple_Pack(3, data,
|
||||
info.is_package ? Py_True : Py_False,
|
||||
origname ? origname : Py_None);
|
||||
Py_XDECREF(origname);
|
||||
Py_DECREF(data);
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue