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