• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/build/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 #include "frozen_modules/importlib._bootstrap.h"
45 #include "frozen_modules/importlib._bootstrap_external.h"
46 #include "frozen_modules/zipimport.h"
47 #include "frozen_modules/abc.h"
48 #include "frozen_modules/codecs.h"
49 #include "frozen_modules/io.h"
50 #include "frozen_modules/_collections_abc.h"
51 #include "frozen_modules/_sitebuiltins.h"
52 #include "frozen_modules/genericpath.h"
53 #include "frozen_modules/ntpath.h"
54 #include "frozen_modules/posixpath.h"
55 #include "frozen_modules/os.h"
56 #include "frozen_modules/site.h"
57 #include "frozen_modules/stat.h"
58 #include "frozen_modules/importlib.util.h"
59 #include "frozen_modules/importlib.machinery.h"
60 #include "frozen_modules/runpy.h"
61 #include "frozen_modules/__hello__.h"
62 #include "frozen_modules/__phello__.h"
63 #include "frozen_modules/__phello__.ham.h"
64 #include "frozen_modules/__phello__.ham.eggs.h"
65 #include "frozen_modules/__phello__.spam.h"
66 #include "frozen_modules/frozen_only.h"
67 /* End includes */
68 
69 static const struct _frozen bootstrap_modules[] = {
70     {"_frozen_importlib", _Py_M__importlib__bootstrap, (int)sizeof(_Py_M__importlib__bootstrap), false},
71     {"_frozen_importlib_external", _Py_M__importlib__bootstrap_external, (int)sizeof(_Py_M__importlib__bootstrap_external), false},
72     {"zipimport", _Py_M__zipimport, (int)sizeof(_Py_M__zipimport), false},
73     {0, 0, 0} /* bootstrap sentinel */
74 };
75 static const struct _frozen stdlib_modules[] = {
76     /* stdlib - startup, without site (python -S) */
77     {"abc", _Py_M__abc, (int)sizeof(_Py_M__abc), false},
78     {"codecs", _Py_M__codecs, (int)sizeof(_Py_M__codecs), false},
79     {"io", _Py_M__io, (int)sizeof(_Py_M__io), false},
80 
81     /* stdlib - startup, with site */
82     {"_collections_abc", _Py_M___collections_abc, (int)sizeof(_Py_M___collections_abc), false},
83     {"_sitebuiltins", _Py_M___sitebuiltins, (int)sizeof(_Py_M___sitebuiltins), false},
84     {"genericpath", _Py_M__genericpath, (int)sizeof(_Py_M__genericpath), false},
85     {"ntpath", _Py_M__ntpath, (int)sizeof(_Py_M__ntpath), false},
86     {"posixpath", _Py_M__posixpath, (int)sizeof(_Py_M__posixpath), false},
87     {"os.path", _Py_M__posixpath, (int)sizeof(_Py_M__posixpath), false},
88     {"os", _Py_M__os, (int)sizeof(_Py_M__os), false},
89     {"site", _Py_M__site, (int)sizeof(_Py_M__site), false},
90     {"stat", _Py_M__stat, (int)sizeof(_Py_M__stat), false},
91 
92     /* runpy - run module with -m */
93     {"importlib.util", _Py_M__importlib_util, (int)sizeof(_Py_M__importlib_util), false},
94     {"importlib.machinery", _Py_M__importlib_machinery, (int)sizeof(_Py_M__importlib_machinery), false},
95     {"runpy", _Py_M__runpy, (int)sizeof(_Py_M__runpy), false},
96     {0, 0, 0} /* stdlib sentinel */
97 };
98 static const struct _frozen test_modules[] = {
99     {"__hello__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false},
100     {"__hello_alias__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false},
101     {"__phello_alias__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), true},
102     {"__phello_alias__.spam", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false},
103     {"__phello__", _Py_M____phello__, (int)sizeof(_Py_M____phello__), true},
104     {"__phello__.__init__", _Py_M____phello__, (int)sizeof(_Py_M____phello__), false},
105     {"__phello__.ham", _Py_M____phello___ham, (int)sizeof(_Py_M____phello___ham), true},
106     {"__phello__.ham.__init__", _Py_M____phello___ham, (int)sizeof(_Py_M____phello___ham), false},
107     {"__phello__.ham.eggs", _Py_M____phello___ham_eggs, (int)sizeof(_Py_M____phello___ham_eggs), false},
108     {"__phello__.spam", _Py_M____phello___spam, (int)sizeof(_Py_M____phello___spam), false},
109     {"__hello_only__", _Py_M__frozen_only, (int)sizeof(_Py_M__frozen_only), false},
110     {0, 0, 0} /* test sentinel */
111 };
112 const struct _frozen *_PyImport_FrozenBootstrap = bootstrap_modules;
113 const struct _frozen *_PyImport_FrozenStdlib = stdlib_modules;
114 const struct _frozen *_PyImport_FrozenTest = test_modules;
115 
116 static const struct _module_alias aliases[] = {
117     {"_frozen_importlib", "importlib._bootstrap"},
118     {"_frozen_importlib_external", "importlib._bootstrap_external"},
119     {"os.path", "posixpath"},
120     {"__hello_alias__", "__hello__"},
121     {"__phello_alias__", "__hello__"},
122     {"__phello_alias__.spam", "__hello__"},
123     {"__phello__.__init__", "<__phello__"},
124     {"__phello__.ham.__init__", "<__phello__.ham"},
125     {"__hello_only__", NULL},
126     {0, 0} /* aliases sentinel */
127 };
128 const struct _module_alias *_PyImport_FrozenAliases = aliases;
129 
130 
131 /* Embedding apps may change this pointer to point to their favorite
132    collection of frozen modules: */
133 
134 const struct _frozen *PyImport_FrozenModules = NULL;
135