8000 bpo-45020: Identify which frozen modules are actually aliases. by ericsnowcurrently · Pull Request #28655 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-45020: Identify which frozen modules are actually aliases. #28655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Next Next commit
Add _PyImport_FrozenAliases.
  • Loading branch information
ericsnowcurrently committed Oct 5, 2021
commit be15831c6d1c56d98161df1c4f2ae35110a11327
7 changes: 7 additions & 0 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ extern PyStatus _PyImport_ReInitLock(void);
#endif
extern PyObject* _PyImport_BootstrapImp(PyThreadState *tstate);

struct _module_alias {
const char *name; /* ASCII encoded string */
const char *orig; /* ASCII encoded string */
};

extern const struct _module_alias * _PyImport_FrozenAliases;

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions Programs/_freeze_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <Python.h>
#include <marshal.h>
#include <pycore_import.h>

#include <stdio.h>
#include <sys/types.h>
Expand All @@ -24,8 +25,12 @@
static const struct _frozen _PyImport_FrozenModules[] = {
{0, 0, 0} /* sentinel */
};
static const struct _module_alias aliases[] = {
{0, 0} /* sentinel */
};

const struct _frozen *PyImport_FrozenModules;
const struct _module_alias *_PyImport_FrozenAliases;

static const char header[] =
"/* Auto-generated by Programs/_freeze_module.c */";
Expand Down Expand Up @@ -183,6 +188,7 @@ main(int argc, char *argv[])
const char *name, *inpath, *outpath;

PyImport_FrozenModules = _PyImport_FrozenModules;
_PyImport_FrozenAliases = aliases;

if (argc != 4) {
fprintf(stderr, "need to specify the name, input and output paths\n");
Expand Down
18 changes: 17 additions & 1 deletion Python/frozen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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__", ""},
{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: */

Expand Down
25 changes: 24 additions & 1 deletion Tools/scripts/freeze_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ def __getattr__(self, name):
def modname(self):
return self.name

@property
def orig(self):
return self.source.modname

@property
def isalias(self):
orig = self.source.modname
if not orig:
return True
return self.name != orig

def summarize(self):
source = self.source.modname
if source:
Expand Down Expand Up @@ -507,6 +518,7 @@ def regen_frozen(modules):
headerlines.append(f'#include "{header}"')

deflines = []
aliaslines = []
indent = ' '
lastsection = None
for mod in modules:
Expand All @@ -528,6 +540,10 @@ def regen_frozen(modules):
deflines.append(line1)
deflines.append(indent + line2)

if mod.isalias:
entry = '{"%s", "%s"},' % (mod.name, mod.orig or "")
aliaslines.append(indent + entry)

if not deflines[0]:
del deflines[0]
for i, line in enumerate(deflines):
Expand All @@ -549,10 +565,17 @@ def regen_frozen(modules):
lines = replace_block(
lines,
"static const struct _frozen _PyImport_FrozenModules[] =",
"/* sentinel */",
"/* modules sentinel */",
deflines,
FROZEN_FILE,
)
lines = replace_block(
lines,
"const struct _module_alias aliases[] =",
"/* aliases sentinel */",
aliaslines,
FROZEN_FILE,
)
outfile.writelines(lines)


Expand Down
0