2012-06-20 04:29:35 +08:00
|
|
|
/* This is built as a stand-alone executable by the Makefile, and helps turn
|
2023-04-13 02:27:14 +08:00
|
|
|
modules into frozen modules.
|
2021-08-31 07:25:11 +08:00
|
|
|
|
2022-10-17 18:01:00 +08:00
|
|
|
This is used directly by Tools/build/freeze_modules.py, and indirectly by "make regen-frozen".
|
2021-08-31 07:25:11 +08:00
|
|
|
|
|
|
|
See Python/frozen.c for more info.
|
2021-12-14 03:48:46 +08:00
|
|
|
|
|
|
|
Keep this file in sync with Programs/_freeze_module.py.
|
2012-06-20 04:29:35 +08:00
|
|
|
*/
|
|
|
|
|
2022-12-09 20:18:45 +08:00
|
|
|
|
2012-06-20 04:29:35 +08:00
|
|
|
#include <Python.h>
|
|
|
|
#include <marshal.h>
|
2022-01-11 18:56:16 +08:00
|
|
|
#include "pycore_fileutils.h" // _Py_stat_struct
|
2021-10-06 01:26:37 +08:00
|
|
|
#include <pycore_import.h>
|
2012-06-20 04:29:35 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2021-10-14 01:25:53 +08:00
|
|
|
#include <stdlib.h> // malloc()
|
2012-06-20 04:29:35 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-07-29 02:46:52 +08:00
|
|
|
#ifndef MS_WINDOWS
|
2023-09-02 22:50:18 +08:00
|
|
|
# include <unistd.h>
|
2012-07-29 02:46:52 +08:00
|
|
|
#endif
|
2012-06-20 04:29:35 +08: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-29 05:04:33 +08:00
|
|
|
static const struct _frozen no_modules[] = {
|
2012-06-20 04:29:35 +08:00
|
|
|
{0, 0, 0} /* sentinel */
|
|
|
|
};
|
2021-10-06 01:26:37 +08:00
|
|
|
static const struct _module_alias aliases[] = {
|
|
|
|
{0, 0} /* sentinel */
|
|
|
|
};
|
2012-06-20 04:29:35 +08:00
|
|
|
|
2021-10-29 05:04:33 +08:00
|
|
|
const struct _frozen *_PyImport_FrozenBootstrap;
|
|
|
|
const struct _frozen *_PyImport_FrozenStdlib;
|
|
|
|
const struct _frozen *_PyImport_FrozenTest;
|
2013-03-14 03:06:39 +08:00
|
|
|
const struct _frozen *PyImport_FrozenModules;
|
2021-10-06 01:26:37 +08:00
|
|
|
const struct _module_alias *_PyImport_FrozenAliases;
|
2012-07-29 03:55:20 +08:00
|
|
|
|
2018-09-19 03:22:29 +08:00
|
|
|
static const char header[] =
|
2021-08-31 07:25:11 +08:00
|
|
|
"/* Auto-generated by Programs/_freeze_module.c */";
|
2012-06-20 04:29:35 +08:00
|
|
|
|
2021-08-31 07:25:11 +08:00
|
|
|
static void
|
|
|
|
runtime_init(void)
|
2012-06-20 04:29:35 +08:00
|
|
|
{
|
2019-05-27 22:39:22 +08:00
|
|
|
PyConfig config;
|
2019-10-01 18:06:16 +08:00
|
|
|
PyConfig_InitIsolatedConfig(&config);
|
2019-05-18 09:21:27 +08:00
|
|
|
|
2018-07-25 08:49:17 +08:00
|
|
|
config.site_import = 0;
|
2019-05-23 05:58:50 +08:00
|
|
|
|
2019-10-01 18:06:16 +08:00
|
|
|
PyStatus status;
|
2019-05-27 22:39:22 +08:00
|
|
|
status = PyConfig_SetString(&config, &config.program_name,
|
2021-08-31 07:25:11 +08:00
|
|
|
L"./_freeze_module");
|
2019-05-27 22:39:22 +08:00
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
Py_ExitStatusException(status);
|
2019-05-23 05:58:50 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 08:49:17 +08:00
|
|
|
/* Don't install importlib, since it could execute outdated bytecode. */
|
|
|
|
config._install_importlib = 0;
|
2019-05-16 23:38:16 +08:00
|
|
|
config._init_main = 0;
|
2012-06-20 04:29:35 +08:00
|
|
|
|
2019-05-27 22:39:22 +08:00
|
|
|
status = Py_InitializeFromConfig(&config);
|
|
|
|
PyConfig_Clear(&config);
|
|
|
|
if (PyStatus_Exception(status)) {
|
|
|
|
Py_ExitStatusException(status);
|
2017-11-16 07:48:08 +08:00
|
|
|
}
|
2021-08-31 07:25:11 +08:00
|
|
|
}
|
2012-06-20 04:29:35 +08:00
|
|
|
|
2021-08-31 07:25:11 +08: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-22 05:05:04 +08:00
|
|
|
|
2021-08-31 07:25:11 +08: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-20 04:29:35 +08:00
|
|
|
|
2021-08-31 07:25:11 +08: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-20 04:29:35 +08:00
|
|
|
|
2021-08-31 07:25:11 +08: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-20 04:29:35 +08:00
|
|
|
}
|
2021-08-31 07:25:11 +08: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-19 03:22:29 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-31 07:25:11 +08: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-20 04:29:35 +08:00
|
|
|
size_t i, end = Py_MIN(n + 16, data_size);
|
|
|
|
fprintf(outfile, " ");
|
|
|
|
for (i = n; i < end; i++) {
|
2019-05-07 00:56:51 +08:00
|
|
|
fprintf(outfile, "%u,", (unsigned int) data[i]);
|
2012-06-20 04:29:35 +08:00
|
|
|
}
|
|
|
|
fprintf(outfile, "\n");
|
|
|
|
}
|
|
|
|
fprintf(outfile, "};\n");
|
2021-08-31 07:25:11 +08: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-20 04:29:35 +08:00
|
|
|
|
2021-08-31 07:25:11 +08:00
|
|
|
fprintf(outfile, "%s\n", header);
|
|
|
|
char *arrayname = get_varname(name, "_Py_M__");
|
|
|
|
write_code(outfile, marshalled, arrayname);
|
|
|
|
free(arrayname);
|
2012-06-20 04:29:35 +08:00
|
|
|
|
2021-08-31 07:25:11 +08:00
|
|
|
if (ferror(outfile)) {
|
|
|
|
fprintf(stderr, "error when writing to '%s'\n", outpath);
|
2022-11-24 17:04:19 +08:00
|
|
|
fclose(outfile);
|
2021-08-31 07:25:11 +08:00
|
|
|
return -1;
|
2012-06-20 04:29:35 +08:00
|
|
|
}
|
2021-08-31 07:25:11 +08:00
|
|
|
fclose(outfile);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const char *name, *inpath, *outpath;
|
|
|
|
|
2021-10-29 05:04:33 +08:00
|
|
|
_PyImport_FrozenBootstrap = no_modules;
|
|
|
|
_PyImport_FrozenStdlib = no_modules;
|
|
|
|
_PyImport_FrozenTest = no_modules;
|
|
|
|
PyImport_FrozenModules = NULL;
|
2021-10-06 01:26:37 +08:00
|
|
|
_PyImport_FrozenAliases = aliases;
|
2021-08-31 07:25:11 +08: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-20 04:29:35 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
PyErr_Print();
|
|
|
|
Py_Finalize();
|
|
|
|
return 1;
|
|
|
|
}
|
2022-01-27 21:03:47 +08:00
|
|
|
|