• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     Py_NoUserSiteDirectory++;
78     Py_NoSiteFlag++;
79     Py_IgnoreEnvironmentFlag++;
80     Py_FrozenFlag++;
81 
82     Py_SetProgramName(L"./_freeze_importlib");
83     /* Don't install importlib, since it could execute outdated bytecode. */
84     _Py_InitializeEx_Private(1, 0);
85 
86     if (strstr(inpath, "_external") != NULL) {
87         is_bootstrap = 0;
88     }
89 
90     code_name = is_bootstrap ?
91         "<frozen importlib._bootstrap>" :
92         "<frozen importlib._bootstrap_external>";
93     code = Py_CompileStringExFlags(text, code_name, Py_file_input, NULL, 0);
94     if (code == NULL)
95         goto error;
96     free(text);
97     text = NULL;
98 
99     marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
100     Py_CLEAR(code);
101     if (marshalled == NULL)
102         goto error;
103 
104     assert(PyBytes_CheckExact(marshalled));
105     data = (unsigned char *) PyBytes_AS_STRING(marshalled);
106     data_size = PyBytes_GET_SIZE(marshalled);
107 
108     /* Open the file in text mode. The hg checkout should be using the eol extension,
109        which in turn should cause the EOL style match the C library's text mode */
110     outfile = fopen(outpath, "w");
111     if (outfile == NULL) {
112         fprintf(stderr, "cannot open '%s' for writing\n", outpath);
113         goto error;
114     }
115     fprintf(outfile, "%s\n", header);
116     if (is_bootstrap)
117         fprintf(outfile, "const unsigned char _Py_M__importlib[] = {\n");
118     else
119         fprintf(outfile,
120                 "const unsigned char _Py_M__importlib_external[] = {\n");
121     for (n = 0; n < data_size; n += 16) {
122         size_t i, end = Py_MIN(n + 16, data_size);
123         fprintf(outfile, "    ");
124         for (i = n; i < end; i++) {
125             fprintf(outfile, "%d,", (unsigned int) data[i]);
126         }
127         fprintf(outfile, "\n");
128     }
129     fprintf(outfile, "};\n");
130 
131     Py_CLEAR(marshalled);
132 
133     Py_Finalize();
134     if (outfile) {
135         if (ferror(outfile)) {
136             fprintf(stderr, "error when writing to '%s'\n", outpath);
137             goto error;
138         }
139         fclose(outfile);
140     }
141     return 0;
142 
143 error:
144     PyErr_Print();
145     Py_Finalize();
146     if (infile)
147         fclose(infile);
148     if (outfile)
149         fclose(outfile);
150     if (text)
151         free(text);
152     if (marshalled)
153         Py_DECREF(marshalled);
154     return 1;
155 }
156