_freeze_importlib.c 4.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* 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>
11
#ifndef MS_WINDOWS
12
#include <unistd.h>
13
#endif
14 15 16 17 18

/* 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. */

19
static const struct _frozen _PyImport_FrozenModules[] = {
20 21 22
    {0, 0, 0} /* sentinel */
};

23 24 25 26
#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. */
27
const struct _frozen *PyImport_FrozenModules;
28 29
#endif

30
const char header[] = "/* Auto-generated by Programs/_freeze_importlib.c */";
31 32 33 34

int
main(int argc, char *argv[])
{
35
    char *inpath, *outpath, *code_name;
36
    FILE *infile = NULL, *outfile = NULL;
37
    struct _Py_stat_struct status;
38
    size_t text_size, data_size, n;
39
    char *text = NULL;
40
    unsigned char *data;
41
    PyObject *code = NULL, *marshalled = NULL;
42
    int is_bootstrap = 1;
43

44 45
    PyImport_FrozenModules = _PyImport_FrozenModules;

46 47 48 49 50 51 52 53 54
    if (argc != 3) {
        fprintf(stderr, "need to specify input and output paths\n");
        return 2;
    }
    inpath = argv[1];
    outpath = argv[2];
    infile = fopen(inpath, "rb");
    if (infile == NULL) {
        fprintf(stderr, "cannot open '%s' for reading\n", inpath);
55
        goto error;
56
    }
57
    if (_Py_fstat_noraise(fileno(infile), &status)) {
58
        fprintf(stderr, "cannot fstat '%s'\n", inpath);
59
        goto error;
60
    }
61
    text_size = status.st_size;
62 63 64
    text = (char *) malloc(text_size + 1);
    if (text == NULL) {
        fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
65
        goto error;
66 67 68 69 70 71 72
    }
    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);
73
        goto error;
74 75 76 77 78 79 80 81 82 83 84
    }
    text[text_size] = '\0';

    Py_NoUserSiteDirectory++;
    Py_NoSiteFlag++;
    Py_IgnoreEnvironmentFlag++;

    Py_SetProgramName(L"./_freeze_importlib");
    /* Don't install importlib, since it could execute outdated bytecode. */
    _Py_InitializeEx_Private(1, 0);

85 86 87 88 89 90 91 92
    if (strstr(inpath, "_external") != NULL) {
        is_bootstrap = 0;
    }

    code_name = is_bootstrap ?
        "<frozen importlib._bootstrap>" :
        "<frozen importlib._bootstrap_external>";
    code = Py_CompileStringExFlags(text, code_name, Py_file_input, NULL, 0);
93 94
    if (code == NULL)
        goto error;
95 96 97
    free(text);
    text = NULL;

98
    marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
99
    Py_CLEAR(code);
100 101 102 103
    if (marshalled == NULL)
        goto error;

    assert(PyBytes_CheckExact(marshalled));
104
    data = (unsigned char *) PyBytes_AS_STRING(marshalled);
105 106
    data_size = PyBytes_GET_SIZE(marshalled);

107
    /* Open the file in text mode. The hg checkout should be using the eol extension,
108 109
       which in turn should cause the EOL style match the C library's text mode */
    outfile = fopen(outpath, "w");
110 111
    if (outfile == NULL) {
        fprintf(stderr, "cannot open '%s' for writing\n", outpath);
112
        goto error;
113 114
    }
    fprintf(outfile, "%s\n", header);
115 116 117 118 119
    if (is_bootstrap)
        fprintf(outfile, "const unsigned char _Py_M__importlib[] = {\n");
    else
        fprintf(outfile,
                "const unsigned char _Py_M__importlib_external[] = {\n");
120 121 122 123
    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++) {
124
            fprintf(outfile, "%d,", (unsigned int) data[i]);
125 126 127 128 129
        }
        fprintf(outfile, "\n");
    }
    fprintf(outfile, "};\n");

130
    Py_CLEAR(marshalled);
131 132 133 134 135

    Py_Finalize();
    if (outfile) {
        if (ferror(outfile)) {
            fprintf(stderr, "error when writing to '%s'\n", outpath);
136
            goto error;
137 138 139 140 141 142 143 144 145 146 147 148
        }
        fclose(outfile);
    }
    return 0;

error:
    PyErr_Print();
    Py_Finalize();
    if (infile)
        fclose(infile);
    if (outfile)
        fclose(outfile);
149 150 151 152
    if (text)
        free(text);
    if (marshalled)
        Py_DECREF(marshalled);
153 154
    return 1;
}