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 static const char header[] =
31     "/* Auto-generated by Programs/_freeze_importlib.c */";
32 
33 int
main(int argc,char * argv[])34 main(int argc, char *argv[])
35 {
36     const char *name, *inpath, *outpath;
37     char buf[100];
38     FILE *infile = NULL, *outfile = NULL;
39     struct _Py_stat_struct stat;
40     size_t text_size, data_size, i, n;
41     char *text = NULL;
42     unsigned char *data;
43     PyObject *code = NULL, *marshalled = NULL;
44 
45     PyImport_FrozenModules = _PyImport_FrozenModules;
46 
47     if (argc != 4) {
48         fprintf(stderr, "need to specify the name, input and output paths\n");
49         return 2;
50     }
51     name = argv[1];
52     inpath = argv[2];
53     outpath = argv[3];
54     infile = fopen(inpath, "rb");
55     if (infile == NULL) {
56         fprintf(stderr, "cannot open '%s' for reading\n", inpath);
57         goto error;
58     }
59     if (_Py_fstat_noraise(fileno(infile), &stat)) {
60         fprintf(stderr, "cannot fstat '%s'\n", inpath);
61         goto error;
62     }
63     text_size = (size_t)stat.st_size;
64     text = (char *) malloc(text_size + 1);
65     if (text == NULL) {
66         fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
67         goto error;
68     }
69     n = fread(text, 1, text_size, infile);
70     fclose(infile);
71     infile = NULL;
72     if (n < text_size) {
73         fprintf(stderr, "read too short: got %ld instead of %ld bytes\n",
74                 (long) n, (long) text_size);
75         goto error;
76     }
77     text[text_size] = '\0';
78 
79     PyConfig config;
80     PyConfig_InitIsolatedConfig(&config);
81 
82     config.site_import = 0;
83 
84     PyStatus status;
85     status = PyConfig_SetString(&config, &config.program_name,
86                                 L"./_freeze_importlib");
87     if (PyStatus_Exception(status)) {
88         PyConfig_Clear(&config);
89         Py_ExitStatusException(status);
90     }
91 
92     /* Don't install importlib, since it could execute outdated bytecode. */
93     config._install_importlib = 0;
94     config._init_main = 0;
95 
96     status = Py_InitializeFromConfig(&config);
97     PyConfig_Clear(&config);
98     if (PyStatus_Exception(status)) {
99         Py_ExitStatusException(status);
100     }
101 
102     sprintf(buf, "<frozen %s>", name);
103     code = Py_CompileStringExFlags(text, buf, 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     for (i = n = 0; name[i] != '\0'; i++) {
127         if (name[i] != '.') {
128             buf[n++] = name[i];
129         }
130     }
131     buf[n] = '\0';
132     fprintf(outfile, "const unsigned char _Py_M__%s[] = {\n", buf);
133     for (n = 0; n < data_size; n += 16) {
134         size_t i, end = Py_MIN(n + 16, data_size);
135         fprintf(outfile, "    ");
136         for (i = n; i < end; i++) {
137             fprintf(outfile, "%u,", (unsigned int) data[i]);
138         }
139         fprintf(outfile, "\n");
140     }
141     fprintf(outfile, "};\n");
142 
143     Py_CLEAR(marshalled);
144 
145     Py_Finalize();
146     if (outfile) {
147         if (ferror(outfile)) {
148             fprintf(stderr, "error when writing to '%s'\n", outpath);
149             goto error;
150         }
151         fclose(outfile);
152     }
153     return 0;
154 
155 error:
156     PyErr_Print();
157     Py_Finalize();
158     if (infile)
159         fclose(infile);
160     if (outfile)
161         fclose(outfile);
162     if (text)
163         free(text);
164     if (marshalled)
165         Py_DECREF(marshalled);
166     return 1;
167 }
168