2012-06-19 17:29:35 -03:00
|
|
|
/* This is built as a stand-alone executable by the Makefile, and helps turn
|
2021-08-30 20:25:11 -03:00
|
|
|
modules into frozen modules (like Lib/importlib/_bootstrap.py
|
|
|
|
into Python/importlib.h).
|
|
|
|
|
|
|
|
This is used directly by Tools/scripts/freeze_modules.py, and indirectly by "make regen-frozen".
|
|
|
|
|
|
|
|
See Python/frozen.c for more info.
|
2021-12-13 15:48:46 -04:00
|
|
|
|
|
|
|
Keep this file in sync with Programs/_freeze_module.py.
|
2012-06-19 17:29:35 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
#include <marshal.h>
|
2022-01-11 06:56:16 -04:00
|
|
|
#include "pycore_fileutils.h" // _Py_stat_struct
|
2021-10-05 14:26:37 -03:00
|
|
|
#include <pycore_import.h>
|
2012-06-19 17:29:35 -03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2021-10-13 14:25:53 -03:00
|
|
|
#include <stdlib.h> // malloc()
|
2012-06-19 17:29:35 -03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-07-28 15:46:52 -03:00
|
|
|
#ifndef MS_WINDOWS
|
2012-06-19 17:29:35 -03:00
|
|
|
#include <unistd.h>
|
2012-07-28 15:46:52 -03:00
|
|
|
#endif
|
2012-06-19 17:29:35 -03:00
|
|
|
|
2022-02-24 12:54:06 -04:00
|
|
|
/* Empty initializer for deepfrozen modules */
|
2022-02-26 12:35:03 -04:00
|
|
|
int _Py_Deepfreeze_Init(void)
|
2022-02-24 12:54:06 -04:00
|
|
|
{
|
2022-02-26 12:35:03 -04:00
|
|
|
return 0;
|
2022-02-24 12:54:06 -04:00
|
|
|
}
|
2022-01-27 09:03:47 -04:00
|
|
|
/* Empty finalizer for deepfrozen modules */
|
|
|
|
void
|
|
|
|
_Py_Deepfreeze_Fini(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-19 17:29:35 -03: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. */
|
|
|
|
|
2021-10-28 18:04:33 -03:00
|
|
|
static const struct _frozen no_modules[] = {
|
2012-06-19 17:29:35 -03:00
|
|
|
{0, 0, 0} /* sentinel */
|
|
|
|
};
|
2021-10-05 14:26:37 -03:00
|
|
|
static const struct _module_alias aliases[] = {
|
|
|
|
{0, 0} /* sentinel */
|
|
|
|
};
|
2012-06-19 17:29:35 -03:00
|
|
|
|
2021-10-28 18:04:33 -03:00
|
|
|
const struct _frozen *_PyImport_FrozenBootstrap;
|
|
|
|
const struct _frozen *_PyImport_FrozenStdlib;
|
|
|
|
const struct _frozen *_PyImport_FrozenTest;
|
2013-03-13 16:06:39 -03:00
|
|
|
const struct _frozen *PyImport_FrozenModules;
|
2021-10-05 14:26:37 -03:00
|
|
|
const struct _module_alias *_PyImport_FrozenAliases;
|
2012-07-28 16:55:20 -03:00
|
|
|
|
2018-09-18 16:22:29 -03:00
|
|
|
static const char header[] =
|
2021-08-30 20:25:11 -03:00
|
|
|
"/* Auto-generated by Programs/_freeze_module.c */";
|
2012-06-19 17:29:35 -03:00
|
|
|
|
2021-08-30 20:25:11 -03:00
|
|
|
static void
|
|
|
|
runtime_init(void)
|
2012-06-19 17:29:35 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig config;
|
2019-10-01 07:06:16 -03:00
|
|
|
PyConfig_InitIsolatedConfig(&config);
|
2019-05-17 22:21:27 -03:00
|
|
|
|
2018-07-24 21:49:17 -03:00
|
|
|
config.site_import = 0;
|
2019-05-22 18:58:50 -03:00
|
|
|
|
2019-10-01 07:06:16 -03:00
|
|
|
PyStatus status;
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetString(&config, &config.program_name,
|
2021-08-30 20:25:11 -03:00
|
|
|
L"./_freeze_module");
|
2019-05-27 11:39:22 -03:00
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-22 18:58:50 -03:00
|
|
|
}
|
|
|
|
|
2018-07-24 21:49:17 -03:00
|
|
|
/* Don't install importlib, since it could execute outdated bytecode. */
|
|
|
|
config._install_importlib = 0;
|
2019-05-16 12:38:16 -03:00
|
|
|
config._init_main = 0;
|
2012-06-19 17:29:35 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2017-11-15 19:48:08 -04:00
|
|
|
}
|
2021-08-30 20:25:11 -03:00
|
|
|
}
|
2012-06-19 17:29:35 -03:00
|
|
|
|
2021-08-30 20:25:11 -03:00
|
|
|
static const char *
|
|
|
|
read_text(const char *inpath)
|
|
|
|
{
|
|
|
|
FILE *infile = fopen(inpath, "rb");
|
|
|
|
if (infile == NULL) {
|
|
|
|
fprintf(stderr, "cannot open '%s' for reading\n", inpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-07-21 18:05:04 -03:00
|
|
|
|
2021-08-30 20:25:11 -03:00
|
|
|
struct _Py_stat_struct stat;
|
|
|
|
if (_Py_fstat_noraise(fileno(infile), &stat)) {
|
|
|
|
fprintf(stderr, "cannot fstat '%s'\n", inpath);
|
|
|
|
fclose(infile);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
size_t text_size = (size_t)stat.st_size;
|
2012-06-19 17:29:35 -03:00
|
|
|
|
2021-08-30 20:25:11 -03:00
|
|
|
char *text = (char *) malloc(text_size + 1);
|
|
|
|
if (text == NULL) {
|
|
|
|
fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
|
|
|
|
fclose(infile);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
size_t n = fread(text, 1, text_size, infile);
|
|
|
|
fclose(infile);
|
2012-06-19 17:29:35 -03:00
|
|
|
|
2021-08-30 20:25:11 -03:00
|
|
|
if (n < text_size) {
|
|
|
|
fprintf(stderr, "read too short: got %ld instead of %ld bytes\n",
|
|
|
|
(long) n, (long) text_size);
|
|
|
|
free(text);
|
|
|
|
return NULL;
|
2012-06-19 17:29:35 -03:00
|
|
|
}
|
2021-08-30 20:25:11 -03:00
|
|
|
|
|
|
|
text[text_size] = '\0';
|
|
|
|
return (const char *)text;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
compile_and_marshal(const char *name, const char *text)
|
|
|
|
{
|
|
|
|
char *filename = (char *) malloc(strlen(name) + 10);
|
|
|
|
sprintf(filename, "<frozen %s>", name);
|
|
|
|
PyObject *code = Py_CompileStringExFlags(text, filename,
|
|
|
|
Py_file_input, NULL, 0);
|
|
|
|
free(filename);
|
|
|
|
if (code == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
|
|
|
|
Py_CLEAR(code);
|
|
|
|
if (marshalled == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
assert(PyBytes_CheckExact(marshalled));
|
|
|
|
|
|
|
|
return marshalled;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_varname(const char *name, const char *prefix)
|
|
|
|
{
|
|
|
|
size_t n = strlen(prefix);
|
|
|
|
char *varname = (char *) malloc(strlen(name) + n + 1);
|
|
|
|
(void)strcpy(varname, prefix);
|
|
|
|
for (size_t i = 0; name[i] != '\0'; i++) {
|
|
|
|
if (name[i] == '.') {
|
|
|
|
varname[n++] = '_';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
varname[n++] = name[i];
|
2018-09-18 16:22:29 -03:00
|
|
|
}
|
|
|
|
}
|
2021-08-30 20:25:11 -03:00
|
|
|
varname[n] = '\0';
|
|
|
|
return varname;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
write_code(FILE *outfile, PyObject *marshalled, const char *varname)
|
|
|
|
{
|
|
|
|
unsigned char *data = (unsigned char *) PyBytes_AS_STRING(marshalled);
|
|
|
|
size_t data_size = PyBytes_GET_SIZE(marshalled);
|
|
|
|
|
|
|
|
fprintf(outfile, "const unsigned char %s[] = {\n", varname);
|
|
|
|
for (size_t n = 0; n < data_size; n += 16) {
|
2012-06-19 17:29:35 -03:00
|
|
|
size_t i, end = Py_MIN(n + 16, data_size);
|
|
|
|
fprintf(outfile, " ");
|
|
|
|
for (i = n; i < end; i++) {
|
2019-05-06 13:56:51 -03:00
|
|
|
fprintf(outfile, "%u,", (unsigned int) data[i]);
|
2012-06-19 17:29:35 -03:00
|
|
|
}
|
|
|
|
fprintf(outfile, "\n");
|
|
|
|
}
|
|
|
|
fprintf(outfile, "};\n");
|
2021-08-30 20:25:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
write_frozen(const char *outpath, const char *inpath, const char *name,
|
|
|
|
PyObject *marshalled)
|
|
|
|
{
|
|
|
|
/* Open the file in text mode. The hg checkout should be using the eol extension,
|
|
|
|
which in turn should cause the EOL style match the C library's text mode */
|
|
|
|
FILE *outfile = fopen(outpath, "w");
|
|
|
|
if (outfile == NULL) {
|
|
|
|
fprintf(stderr, "cannot open '%s' for writing\n", outpath);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-06-19 17:29:35 -03:00
|
|
|
|
2021-08-30 20:25:11 -03:00
|
|
|
fprintf(outfile, "%s\n", header);
|
|
|
|
char *arrayname = get_varname(name, "_Py_M__");
|
|
|
|
write_code(outfile, marshalled, arrayname);
|
|
|
|
free(arrayname);
|
2012-06-19 17:29:35 -03:00
|
|
|
|
2021-08-30 20:25:11 -03:00
|
|
|
if (ferror(outfile)) {
|
|
|
|
fprintf(stderr, "error when writing to '%s'\n", outpath);
|
|
|
|
return -1;
|
2012-06-19 17:29:35 -03:00
|
|
|
}
|
2021-08-30 20:25:11 -03:00
|
|
|
fclose(outfile);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const char *name, *inpath, *outpath;
|
|
|
|
|
2021-10-28 18:04:33 -03:00
|
|
|
_PyImport_FrozenBootstrap = no_modules;
|
|
|
|
_PyImport_FrozenStdlib = no_modules;
|
|
|
|
_PyImport_FrozenTest = no_modules;
|
|
|
|
PyImport_FrozenModules = NULL;
|
2021-10-05 14:26:37 -03:00
|
|
|
_PyImport_FrozenAliases = aliases;
|
2021-08-30 20:25:11 -03:00
|
|
|
|
|
|
|
if (argc != 4) {
|
|
|
|
fprintf(stderr, "need to specify the name, input and output paths\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
name = argv[1];
|
|
|
|
inpath = argv[2];
|
|
|
|
outpath = argv[3];
|
|
|
|
|
|
|
|
runtime_init();
|
|
|
|
|
|
|
|
const char *text = read_text(inpath);
|
|
|
|
if (text == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *marshalled = compile_and_marshal(name, text);
|
|
|
|
free((char *)text);
|
|
|
|
if (marshalled == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = write_frozen(outpath, inpath, name, marshalled);
|
|
|
|
Py_DECREF(marshalled);
|
|
|
|
if (res != 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_Finalize();
|
2012-06-19 17:29:35 -03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
PyErr_Print();
|
|
|
|
Py_Finalize();
|
|
|
|
return 1;
|
|
|
|
}
|
2022-01-27 09:03:47 -04:00
|
|
|
|