1 /* This is built as a stand-alone executable by the Makefile, and helps turn
2    Lib/importlib/_bootstrap.py into a frozen module in Python/importlib.h
3 */
4 
5 #include <Python.h>
6 #include <marshal.h>
7 
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #ifndef MS_WINDOWS
12 #include <unistd.h>
13 #endif
14 
15 /* To avoid a circular dependency on frozen.o, we create our own structure
16    of frozen modules instead, left deliberately blank so as to avoid
17    unintentional import of a stale version of _frozen_importlib. */
18 
19 static const struct _frozen _PyImport_FrozenModules[] = {
20     {0, 0, 0} /* sentinel */
21 };
22 
23 #ifndef MS_WINDOWS
24 /* On Windows, this links with the regular pythonXY.dll, so this variable comes
25    from frozen.obj. In the Makefile, frozen.o is not linked into this executable,
26    so we define the variable here. */
27 const struct _frozen *PyImport_FrozenModules;
28 #endif
29 
30 const char header[] = "/* Auto-generated by Programs/_freeze_importlib.c */";
31 
32 int
main(int argc,char * argv[])33 main(int argc, char *argv[])
34 {
35     char *inpath, *outpath, *code_name;
36     FILE *infile = NULL, *outfile = NULL;
37     struct _Py_stat_struct status;
38     size_t text_size, data_size, n;
39     char *text = NULL;
40     unsigned char *data;
41     PyObject *code = NULL, *marshalled = NULL;
42     int is_bootstrap = 1;
43 
44     PyImport_FrozenModules = _PyImport_FrozenModules;
45 
46     if (argc != 3) {
47         fprintf(stderr, "need to specify input and output paths\n");
48         return 2;
49     }
50     inpath = argv[1];
51     outpath = argv[2];
52     infile = fopen(inpath, "rb");
53     if (infile == NULL) {
54         fprintf(stderr, "cannot open '%s' for reading\n", inpath);
55         goto error;
56     }
57     if (_Py_fstat_noraise(fileno(infile), &status)) {
58         fprintf(stderr, "cannot fstat '%s'\n", inpath);
59         goto error;
60     }
61     text_size = (size_t)status.st_size;
62     text = (char *) malloc(text_size + 1);
63     if (text == NULL) {
64         fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
65         goto error;
66     }
67     n = fread(text, 1, text_size, infile);
68     fclose(infile);
69     infile = NULL;
70     if (n < text_size) {
71         fprintf(stderr, "read too short: got %ld instead of %ld bytes\n",
72                 (long) n, (long) text_size);
73         goto error;
74     }
75     text[text_size] = '\0';
76 
77     _PyCoreConfig config = _PyCoreConfig_INIT;
78     config.program_name = L"./_freeze_importlib";
79     /* Don't install importlib, since it could execute outdated bytecode. */
80     config._disable_importlib = 1;
81 
82     Py_NoUserSiteDirectory++;
83     Py_NoSiteFlag++;
84     Py_IgnoreEnvironmentFlag++;
85     Py_FrozenFlag++;
86 
87 
88     _PyInitError err = _Py_InitializeFromConfig(&config);
89     /* No need to call _PyCoreConfig_Clear() since we didn't allocate any
90        memory: program_name is a constant string. */
91     if (_Py_INIT_FAILED(err)) {
92         _Py_FatalInitError(err);
93     }
94 
95 
96     if (strstr(inpath, "_external") != NULL) {
97         is_bootstrap = 0;
98     }
99 
100     code_name = is_bootstrap ?
101         "<frozen importlib._bootstrap>" :
102         "<frozen importlib._bootstrap_external>";
103     code = Py_CompileStringExFlags(text, code_name, Py_file_input, NULL, 0);
104     if (code == NULL)
105         goto error;
106     free(text);
107     text = NULL;
108 
109     marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
110     Py_CLEAR(code);
111     if (marshalled == NULL)
112         goto error;
113 
114     assert(PyBytes_CheckExact(marshalled));
115     data = (unsigned char *) PyBytes_AS_STRING(marshalled);
116     data_size = PyBytes_GET_SIZE(marshalled);
117 
118     /* Open the file in text mode. The hg checkout should be using the eol extension,
119        which in turn should cause the EOL style match the C library's text mode */
120     outfile = fopen(outpath, "w");
121     if (outfile == NULL) {
122         fprintf(stderr, "cannot open '%s' for writing\n", outpath);
123         goto error;
124     }
125     fprintf(outfile, "%s\n", header);
126     if (is_bootstrap)
127         fprintf(outfile, "const unsigned char _Py_M__importlib[] = {\n");
128     else
129         fprintf(outfile,
130                 "const unsigned char _Py_M__importlib_external[] = {\n");
131     for (n = 0; n < data_size; n += 16) {
132         size_t i, end = Py_MIN(n + 16, data_size);
133         fprintf(outfile, "    ");
134         for (i = n; i < end; i++) {
135             fprintf(outfile, "%d,", (unsigned int) data[i]);
136         }
137         fprintf(outfile, "\n");
138     }
139     fprintf(outfile, "};\n");
140 
141     Py_CLEAR(marshalled);
142 
143     Py_Finalize();
144     if (outfile) {
145         if (ferror(outfile)) {
146             fprintf(stderr, "error when writing to '%s'\n", outpath);
147             goto error;
148         }
149         fclose(outfile);
150     }
151     return 0;
152 
153 error:
154     PyErr_Print();
155     Py_Finalize();
156     if (infile)
157         fclose(infile);
158     if (outfile)
159         fclose(outfile);
160     if (text)
161         free(text);
162     if (marshalled)
163         Py_DECREF(marshalled);
164     return 1;
165 }
166