2012-06-19 20:29:35 +00:00
|
|
|
/* This is built as a stand-alone executable by the Makefile, and helps turn
|
|
|
|
Lib/importlib/_bootstrap.py into a frozen module in Python/importlib.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
#include <marshal.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-07-28 18:46:52 +00:00
|
|
|
#ifndef MS_WINDOWS
|
2012-06-19 20:29:35 +00:00
|
|
|
#include <unistd.h>
|
2012-07-28 18:46:52 +00:00
|
|
|
#endif
|
2012-06-19 20:29:35 +00:00
|
|
|
|
|
|
|
/* To avoid a circular dependency on frozen.o, we create our own structure
|
|
|
|
of frozen modules instead, left deliberately blank so as to avoid
|
|
|
|
unintentional import of a stale version of _frozen_importlib. */
|
|
|
|
|
2015-02-26 13:27:57 +00:00
|
|
|
static const struct _frozen _PyImport_FrozenModules[] = {
|
2012-06-19 20:29:35 +00:00
|
|
|
{0, 0, 0} /* sentinel */
|
|
|
|
};
|
|
|
|
|
2012-07-28 19:55:20 +00:00
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
/* On Windows, this links with the regular pythonXY.dll, so this variable comes
|
|
|
|
from frozen.obj. In the Makefile, frozen.o is not linked into this executable,
|
|
|
|
so we define the variable here. */
|
2013-03-13 19:06:39 +00:00
|
|
|
const struct _frozen *PyImport_FrozenModules;
|
2012-07-28 19:55:20 +00:00
|
|
|
#endif
|
|
|
|
|
2018-09-18 19:22:29 +00:00
|
|
|
static const char header[] =
|
|
|
|
"/* Auto-generated by Programs/_freeze_importlib.c */";
|
2012-06-19 20:29:35 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2018-09-18 19:22:29 +00:00
|
|
|
const char *name, *inpath, *outpath;
|
|
|
|
char buf[100];
|
2013-07-21 21:05:04 +00:00
|
|
|
FILE *infile = NULL, *outfile = NULL;
|
2015-03-30 08:09:31 +00:00
|
|
|
struct _Py_stat_struct status;
|
2018-09-18 19:22:29 +00:00
|
|
|
size_t text_size, data_size, i, n;
|
2013-07-21 21:05:04 +00:00
|
|
|
char *text = NULL;
|
2012-06-25 15:32:43 +00:00
|
|
|
unsigned char *data;
|
2013-07-21 21:05:04 +00:00
|
|
|
PyObject *code = NULL, *marshalled = NULL;
|
2012-06-19 20:29:35 +00:00
|
|
|
|
2012-07-28 18:46:52 +00:00
|
|
|
PyImport_FrozenModules = _PyImport_FrozenModules;
|
|
|
|
|
2018-09-18 19:22:29 +00:00
|
|
|
if (argc != 4) {
|
|
|
|
fprintf(stderr, "need to specify the name, input and output paths\n");
|
2012-06-19 20:29:35 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-09-18 19:22:29 +00:00
|
|
|
name = argv[1];
|
|
|
|
inpath = argv[2];
|
|
|
|
outpath = argv[3];
|
2012-06-19 20:29:35 +00:00
|
|
|
infile = fopen(inpath, "rb");
|
|
|
|
if (infile == NULL) {
|
|
|
|
fprintf(stderr, "cannot open '%s' for reading\n", inpath);
|
2013-07-21 21:05:04 +00:00
|
|
|
goto error;
|
2012-06-19 20:29:35 +00:00
|
|
|
}
|
2015-03-30 08:09:31 +00:00
|
|
|
if (_Py_fstat_noraise(fileno(infile), &status)) {
|
2012-06-19 20:29:35 +00:00
|
|
|
fprintf(stderr, "cannot fstat '%s'\n", inpath);
|
2013-07-21 21:05:04 +00:00
|
|
|
goto error;
|
2012-06-19 20:29:35 +00:00
|
|
|
}
|
2016-09-07 02:09:15 +00:00
|
|
|
text_size = (size_t)status.st_size;
|
2012-06-19 20:29:35 +00:00
|
|
|
text = (char *) malloc(text_size + 1);
|
|
|
|
if (text == NULL) {
|
|
|
|
fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
|
2013-07-21 21:05:04 +00:00
|
|
|
goto error;
|
2012-06-19 20:29:35 +00:00
|
|
|
}
|
|
|
|
n = fread(text, 1, text_size, infile);
|
|
|
|
fclose(infile);
|
|
|
|
infile = NULL;
|
|
|
|
if (n < text_size) {
|
|
|
|
fprintf(stderr, "read too short: got %ld instead of %ld bytes\n",
|
|
|
|
(long) n, (long) text_size);
|
2013-07-21 21:05:04 +00:00
|
|
|
goto error;
|
2012-06-19 20:29:35 +00:00
|
|
|
}
|
|
|
|
text[text_size] = '\0';
|
|
|
|
|
2019-05-17 17:01:14 +00:00
|
|
|
_PyCoreConfig config;
|
|
|
|
_PyCoreConfig_Init(&config);
|
2019-03-26 01:31:11 +00:00
|
|
|
config.use_environment = 0;
|
2018-07-25 00:49:17 +00:00
|
|
|
config.user_site_directory = 0;
|
|
|
|
config.site_import = 0;
|
|
|
|
config.program_name = L"./_freeze_importlib";
|
|
|
|
/* Don't install importlib, since it could execute outdated bytecode. */
|
|
|
|
config._install_importlib = 0;
|
2019-05-16 15:38:16 +00:00
|
|
|
config.pathconfig_warnings = 0;
|
|
|
|
config._init_main = 0;
|
2012-06-19 20:29:35 +00:00
|
|
|
|
2019-03-27 12:40:14 +00:00
|
|
|
_PyInitError err = _Py_InitializeFromConfig(&config);
|
2018-07-25 00:49:17 +00:00
|
|
|
/* No need to call _PyCoreConfig_Clear() since we didn't allocate any
|
|
|
|
memory: program_name is a constant string. */
|
2019-05-17 21:54:00 +00:00
|
|
|
if (_PyInitError_Failed(err)) {
|
2019-03-01 11:14:41 +00:00
|
|
|
_Py_ExitInitError(err);
|
2017-11-15 23:48:08 +00:00
|
|
|
}
|
2012-06-19 20:29:35 +00:00
|
|
|
|
2018-09-18 19:22:29 +00:00
|
|
|
sprintf(buf, "<frozen %s>", name);
|
|
|
|
code = Py_CompileStringExFlags(text, buf, Py_file_input, NULL, 0);
|
2012-06-19 20:29:35 +00:00
|
|
|
if (code == NULL)
|
|
|
|
goto error;
|
2013-07-21 21:05:04 +00:00
|
|
|
free(text);
|
|
|
|
text = NULL;
|
|
|
|
|
2012-06-19 20:29:35 +00:00
|
|
|
marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
|
2013-07-21 21:05:04 +00:00
|
|
|
Py_CLEAR(code);
|
2012-06-19 20:29:35 +00:00
|
|
|
if (marshalled == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
assert(PyBytes_CheckExact(marshalled));
|
2012-06-25 15:32:43 +00:00
|
|
|
data = (unsigned char *) PyBytes_AS_STRING(marshalled);
|
2012-06-19 20:29:35 +00:00
|
|
|
data_size = PyBytes_GET_SIZE(marshalled);
|
|
|
|
|
2012-07-28 18:46:52 +00:00
|
|
|
/* Open the file in text mode. The hg checkout should be using the eol extension,
|
2012-07-28 19:55:20 +00:00
|
|
|
which in turn should cause the EOL style match the C library's text mode */
|
|
|
|
outfile = fopen(outpath, "w");
|
2012-06-19 20:29:35 +00:00
|
|
|
if (outfile == NULL) {
|
|
|
|
fprintf(stderr, "cannot open '%s' for writing\n", outpath);
|
2013-07-21 21:05:04 +00:00
|
|
|
goto error;
|
2012-06-19 20:29:35 +00:00
|
|
|
}
|
|
|
|
fprintf(outfile, "%s\n", header);
|
2018-09-18 19:22:29 +00:00
|
|
|
for (i = n = 0; name[i] != '\0'; i++) {
|
|
|
|
if (name[i] != '.') {
|
|
|
|
buf[n++] = name[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf[n] = '\0';
|
|
|
|
fprintf(outfile, "const unsigned char _Py_M__%s[] = {\n", buf);
|
2012-06-19 20:29:35 +00:00
|
|
|
for (n = 0; n < data_size; n += 16) {
|
|
|
|
size_t i, end = Py_MIN(n + 16, data_size);
|
|
|
|
fprintf(outfile, " ");
|
|
|
|
for (i = n; i < end; i++) {
|
2019-05-06 16:56:51 +00:00
|
|
|
fprintf(outfile, "%u,", (unsigned int) data[i]);
|
2012-06-19 20:29:35 +00:00
|
|
|
}
|
|
|
|
fprintf(outfile, "\n");
|
|
|
|
}
|
|
|
|
fprintf(outfile, "};\n");
|
|
|
|
|
2013-07-21 21:05:04 +00:00
|
|
|
Py_CLEAR(marshalled);
|
2012-06-19 20:29:35 +00:00
|
|
|
|
|
|
|
Py_Finalize();
|
|
|
|
if (outfile) {
|
|
|
|
if (ferror(outfile)) {
|
|
|
|
fprintf(stderr, "error when writing to '%s'\n", outpath);
|
2013-07-21 21:05:04 +00:00
|
|
|
goto error;
|
2012-06-19 20:29:35 +00:00
|
|
|
}
|
|
|
|
fclose(outfile);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
PyErr_Print();
|
|
|
|
Py_Finalize();
|
|
|
|
if (infile)
|
|
|
|
fclose(infile);
|
|
|
|
if (outfile)
|
|
|
|
fclose(outfile);
|
2013-07-21 21:05:04 +00:00
|
|
|
if (text)
|
|
|
|
free(text);
|
|
|
|
if (marshalled)
|
|
|
|
Py_DECREF(marshalled);
|
2012-06-19 20:29:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|