Debian Codegraph @

Programs/_freeze_module.c

raw
1 /* This is built as a stand-alone executable by the Makefile, and helps turn
2 modules into frozen modules.
3
4 This is used directly by Tools/build/freeze_modules.py, and indirectly by "make regen-frozen".
5
6 See Python/frozen.c for more info.
7
8 Keep this file in sync with Programs/_freeze_module.py.
9 */
10
11
12 #include <Python.h>
13 #include <marshal.h>
14 #include "pycore_fileutils.h" // _Py_stat_struct
15 #include <pycore_import.h>
16
17 #include <stdio.h>
18 #include <stdlib.h> // malloc()
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #ifndef MS_WINDOWS
22 # include <unistd.h>
23 #endif
24
25 /* To avoid a circular dependency on frozen.o, we create our own structure
26 of frozen modules instead, left deliberately blank so as to avoid
27 unintentional import of a stale version of _frozen_importlib. */
28
29 static const struct _frozen no_modules[] = {
30 {0, 0, 0} /* sentinel */
31 };
32 static const struct _module_alias aliases[] = {
33 {0, 0} /* sentinel */
34 };
35
36 const struct _frozen *_PyImport_FrozenBootstrap;
37 const struct _frozen *_PyImport_FrozenStdlib;
38 const struct _frozen *_PyImport_FrozenTest;
39 const struct _frozen *PyImport_FrozenModules;
40 const struct _module_alias *_PyImport_FrozenAliases;
41
42 static const char header[] =
43 "/* Auto-generated by Programs/_freeze_module.c */";
44
45 static void
46 runtime_init(void)
47 {
48 PyConfig config;
49 PyConfig_InitIsolatedConfig(&config);
50
51 config.site_import = 0;
52
53 PyStatus status;
54 status = PyConfig_SetString(&config, &config.program_name,
55 L"./_freeze_module");
56 if (PyStatus_Exception(status)) {
57 PyConfig_Clear(&config);
58 Py_ExitStatusException(status);
59 }
60
61 /* Don't install importlib, since it could execute outdated bytecode. */
62 config._install_importlib = 0;
63 config._init_main = 0;
64
65 status = Py_InitializeFromConfig(&config);
66 PyConfig_Clear(&config);
67 if (PyStatus_Exception(status)) {
68 Py_ExitStatusException(status);
69 }
70 }
71
72 static const char *
73 read_text(const char *inpath)
74 {
75 FILE *infile = fopen(inpath, "rb");
76 if (infile == NULL) {
77 fprintf(stderr, "cannot open '%s' for reading\n", inpath);
78 return NULL;
79 }
80
81 struct _Py_stat_struct stat;
82 if (_Py_fstat_noraise(fileno(infile), &stat)) {
83 fprintf(stderr, "cannot fstat '%s'\n", inpath);
84 fclose(infile);
85 return NULL;
86 }
87 size_t text_size = (size_t)stat.st_size;
88
89 char *text = (char *) malloc(text_size + 1);
90 if (text == NULL) {
91 fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
92 fclose(infile);
93 return NULL;
94 }
95 size_t n = fread(text, 1, text_size, infile);
96 fclose(infile);
97
98 if (n < text_size) {
99 fprintf(stderr, "read too short: got %ld instead of %ld bytes\n",
100 (long) n, (long) text_size);
101 free(text);
102 return NULL;
103 }
104
105 text[text_size] = '\0';
106 return (const char *)text;
107 }
108
109 static PyObject *
110 compile_and_marshal(const char *name, const char *text)
111 {
112 char *filename = (char *) malloc(strlen(name) + 10);
113 if (filename == NULL) {
114 return PyErr_NoMemory();
115 }
116 sprintf(filename, "<frozen %s>", name);
117 PyObject *code = Py_CompileStringExFlags(text, filename,
118 Py_file_input, NULL, 0);
119 free(filename);
120 if (code == NULL) {
121 return NULL;
122 }
123
124 PyObject *marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
125 Py_CLEAR(code);
126 if (marshalled == NULL) {
127 return NULL;
128 }
129 assert(PyBytes_CheckExact(marshalled));
130
131 return marshalled;
132 }
133
134 static char *
135 get_varname(const char *name, const char *prefix)
136 {
137 size_t n = strlen(prefix);
138 char *varname = (char *) malloc(strlen(name) + n + 1);
139 if (varname == NULL) {
140 return NULL;
141 }
142 (void)strcpy(varname, prefix);
143 for (size_t i = 0; name[i] != '\0'; i++) {
144 if (name[i] == '.') {
145 varname[n++] = '_';
146 }
147 else {
148 varname[n++] = name[i];
149 }
150 }
151 varname[n] = '\0';
152 return varname;
153 }
154
155 static void
156 write_code(FILE *outfile, PyObject *marshalled, const char *varname)
157 {
158 unsigned char *data = (unsigned char *) PyBytes_AS_STRING(marshalled);
159 size_t data_size = PyBytes_GET_SIZE(marshalled);
160
161 fprintf(outfile, "const unsigned char %s[] = {\n", varname);
162 for (size_t n = 0; n < data_size; n += 16) {
163 size_t i, end = Py_MIN(n + 16, data_size);
164 fprintf(outfile, " ");
165 for (i = n; i < end; i++) {
166 fprintf(outfile, "%u,", (unsigned int) data[i]);
167 }
168 fprintf(outfile, "\n");
169 }
170 fprintf(outfile, "};\n");
171 }
172
173 static int
174 write_frozen(const char *outpath, const char *inpath, const char *name,
175 PyObject *marshalled)
176 {
177 /* Open the file in text mode. The hg checkout should be using the eol extension,
178 which in turn should cause the EOL style match the C library's text mode */
179 FILE *outfile = fopen(outpath, "w");
180 if (outfile == NULL) {
181 fprintf(stderr, "cannot open '%s' for writing\n", outpath);
182 return -1;
183 }
184
185 fprintf(outfile, "%s\n", header);
186 char *arrayname = get_varname(name, "_Py_M__");
187 if (arrayname == NULL) {
188 fprintf(stderr, "memory error: could not allocate varname\n");
189 fclose(outfile);
190 return -1;
191 }
192 write_code(outfile, marshalled, arrayname);
193 free(arrayname);
194
195 if (ferror(outfile)) {
196 fprintf(stderr, "error when writing to '%s'\n", outpath);
197 fclose(outfile);
198 return -1;
199 }
200 fclose(outfile);
201 return 0;
202 }
203
204 int
205 main(int argc, char *argv[])
206 {
207 const char *name, *inpath, *outpath;
208
209 _PyImport_FrozenBootstrap = no_modules;
210 _PyImport_FrozenStdlib = no_modules;
211 _PyImport_FrozenTest = no_modules;
212 PyImport_FrozenModules = NULL;
213 _PyImport_FrozenAliases = aliases;
214
215 if (argc != 4) {
216 fprintf(stderr, "need to specify the name, input and output paths\n");
217 return 2;
218 }
219 name = argv[1];
220 inpath = argv[2];
221 outpath = argv[3];
222
223 runtime_init();
224
225 const char *text = read_text(inpath);
226 if (text == NULL) {
227 goto error;
228 }
229
230 PyObject *marshalled = compile_and_marshal(name, text);
231 free((char *)text);
232 if (marshalled == NULL) {
233 goto error;
234 }
235
236 int res = write_frozen(outpath, inpath, name, marshalled);
237 Py_DECREF(marshalled);
238 if (res != 0) {
239 goto error;
240 }
241
242 Py_Finalize();
243 return 0;
244
245 error:
246 PyErr_Print();
247 Py_Finalize();
248 return 1;
249 }
250