xref: /third_party/python/Python/frozen.c (revision 7db96d56)
1
2/* Frozen modules initializer
3 *
4 * Frozen modules are written to header files by Programs/_freeze_module.
5 * These files are typically put in Python/frozen_modules/.  Each holds
6 * an array of bytes named "_Py_M__<module>", which is used below.
7 *
8 * These files must be regenerated any time the corresponding .pyc
9 * file would change (including with changes to the compiler, bytecode
10 * format, marshal format).  This can be done with "make regen-frozen".
11 * That make target just runs Tools/scripts/freeze_modules.py.
12 *
13 * The freeze_modules.py script also determines which modules get
14 * frozen.  Update the list at the top of the script to add, remove,
15 * or modify the target modules.  Then run the script
16 * (or run "make regen-frozen").
17 *
18 * The script does the following:
19 *
20 * 1. run Programs/_freeze_module on the target modules
21 * 2. update the includes and _PyImport_FrozenModules[] in this file
22 * 3. update the FROZEN_FILES variable in Makefile.pre.in
23 * 4. update the per-module targets in Makefile.pre.in
24 * 5. update the lists of modules in PCbuild/_freeze_module.vcxproj and
25 *    PCbuild/_freeze_module.vcxproj.filters
26 *
27 * (Note that most of the data in this file is auto-generated by the script.)
28 *
29 * Those steps can also be done manually, though this is not recommended.
30 * Expect such manual changes to be removed the next time
31 * freeze_modules.py runs.
32 * */
33
34/* In order to test the support for frozen modules, by default we
35   define some simple frozen modules: __hello__, __phello__ (a package),
36   and __phello__.spam.  Loading any will print some famous words... */
37
38#include "Python.h"
39#include "pycore_import.h"
40
41#include <stdbool.h>
42
43/* Includes for frozen modules: */
44/* End includes */
45
46#define GET_CODE(name) _Py_get_##name##_toplevel
47
48/* Start extern declarations */
49extern PyObject *_Py_get_importlib__bootstrap_toplevel(void);
50extern PyObject *_Py_get_importlib__bootstrap_external_toplevel(void);
51extern PyObject *_Py_get_zipimport_toplevel(void);
52extern PyObject *_Py_get_abc_toplevel(void);
53extern PyObject *_Py_get_codecs_toplevel(void);
54extern PyObject *_Py_get_io_toplevel(void);
55extern PyObject *_Py_get__collections_abc_toplevel(void);
56extern PyObject *_Py_get__sitebuiltins_toplevel(void);
57extern PyObject *_Py_get_genericpath_toplevel(void);
58extern PyObject *_Py_get_ntpath_toplevel(void);
59extern PyObject *_Py_get_posixpath_toplevel(void);
60extern PyObject *_Py_get_posixpath_toplevel(void);
61extern PyObject *_Py_get_os_toplevel(void);
62extern PyObject *_Py_get_site_toplevel(void);
63extern PyObject *_Py_get_stat_toplevel(void);
64extern PyObject *_Py_get_importlib_util_toplevel(void);
65extern PyObject *_Py_get_importlib_machinery_toplevel(void);
66extern PyObject *_Py_get_runpy_toplevel(void);
67extern PyObject *_Py_get___hello___toplevel(void);
68extern PyObject *_Py_get___hello___toplevel(void);
69extern PyObject *_Py_get___hello___toplevel(void);
70extern PyObject *_Py_get___hello___toplevel(void);
71extern PyObject *_Py_get___phello___toplevel(void);
72extern PyObject *_Py_get___phello___toplevel(void);
73extern PyObject *_Py_get___phello___ham_toplevel(void);
74extern PyObject *_Py_get___phello___ham_toplevel(void);
75extern PyObject *_Py_get___phello___ham_eggs_toplevel(void);
76extern PyObject *_Py_get___phello___spam_toplevel(void);
77extern PyObject *_Py_get_frozen_only_toplevel(void);
78/* End extern declarations */
79
80static const struct _frozen bootstrap_modules[] = {
81    {"_frozen_importlib", NULL, 0, false, GET_CODE(importlib__bootstrap)},
82    {"_frozen_importlib_external", NULL, 0, false, GET_CODE(importlib__bootstrap_external)},
83    {"zipimport", NULL, 0, false, GET_CODE(zipimport)},
84    {0, 0, 0} /* bootstrap sentinel */
85};
86static const struct _frozen stdlib_modules[] = {
87    /* stdlib - startup, without site (python -S) */
88    {"abc", NULL, 0, false, GET_CODE(abc)},
89    {"codecs", NULL, 0, false, GET_CODE(codecs)},
90    {"io", NULL, 0, false, GET_CODE(io)},
91
92    /* stdlib - startup, with site */
93    {"_collections_abc", NULL, 0, false, GET_CODE(_collections_abc)},
94    {"_sitebuiltins", NULL, 0, false, GET_CODE(_sitebuiltins)},
95    {"genericpath", NULL, 0, false, GET_CODE(genericpath)},
96    {"ntpath", NULL, 0, false, GET_CODE(ntpath)},
97    {"posixpath", NULL, 0, false, GET_CODE(posixpath)},
98    {"os.path", NULL, 0, false, GET_CODE(posixpath)},
99    {"os", NULL, 0, false, GET_CODE(os)},
100    {"site", NULL, 0, false, GET_CODE(site)},
101    {"stat", NULL, 0, false, GET_CODE(stat)},
102
103    /* runpy - run module with -m */
104    {"importlib.util", NULL, 0, false, GET_CODE(importlib_util)},
105    {"importlib.machinery", NULL, 0, false, GET_CODE(importlib_machinery)},
106    {"runpy", NULL, 0, false, GET_CODE(runpy)},
107    {0, 0, 0} /* stdlib sentinel */
108};
109static const struct _frozen test_modules[] = {
110    {"__hello__", NULL, 0, false, GET_CODE(__hello__)},
111    {"__hello_alias__", NULL, 0, false, GET_CODE(__hello__)},
112    {"__phello_alias__", NULL, 0, true, GET_CODE(__hello__)},
113    {"__phello_alias__.spam", NULL, 0, false, GET_CODE(__hello__)},
114    {"__phello__", NULL, 0, true, GET_CODE(__phello__)},
115    {"__phello__.__init__", NULL, 0, false, GET_CODE(__phello__)},
116    {"__phello__.ham", NULL, 0, true, GET_CODE(__phello___ham)},
117    {"__phello__.ham.__init__", NULL, 0, false, GET_CODE(__phello___ham)},
118    {"__phello__.ham.eggs", NULL, 0, false, GET_CODE(__phello___ham_eggs)},
119    {"__phello__.spam", NULL, 0, false, GET_CODE(__phello___spam)},
120    {"__hello_only__", NULL, 0, false, GET_CODE(frozen_only)},
121    {0, 0, 0} /* test sentinel */
122};
123const struct _frozen *_PyImport_FrozenBootstrap = bootstrap_modules;
124const struct _frozen *_PyImport_FrozenStdlib = stdlib_modules;
125const struct _frozen *_PyImport_FrozenTest = test_modules;
126
127static const struct _module_alias aliases[] = {
128    {"_frozen_importlib", "importlib._bootstrap"},
129    {"_frozen_importlib_external", "importlib._bootstrap_external"},
130    {"os.path", "posixpath"},
131    {"__hello_alias__", "__hello__"},
132    {"__phello_alias__", "__hello__"},
133    {"__phello_alias__.spam", "__hello__"},
134    {"__phello__.__init__", "<__phello__"},
135    {"__phello__.ham.__init__", "<__phello__.ham"},
136    {"__hello_only__", NULL},
137    {0, 0} /* aliases sentinel */
138};
139const struct _module_alias *_PyImport_FrozenAliases = aliases;
140
141
142/* Embedding apps may change this pointer to point to their favorite
143   collection of frozen modules: */
144
145const struct _frozen *PyImport_FrozenModules = NULL;
146