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