frozenmain.c 2.92 KB
Newer Older
1 2 3

/* Python interpreter main program for frozen scripts */

Guido van Rossum's avatar
Guido van Rossum committed
4
#include "Python.h"
5
#include "internal/pystate.h"
6
#include <locale.h>
7

8
#ifdef MS_WINDOWS
9 10 11
extern void PyWinFreeze_ExeInit(void);
extern void PyWinFreeze_ExeTerm(void);
extern int PyInitFrozenExtensions(void);
12 13
#endif

14 15 16
/* Main program */

int
17
Py_FrozenMain(int argc, char **argv)
18
{
19 20 21 22 23 24 25
    _PyInitError err = _PyRuntime_Initialize();
    if (_Py_INIT_FAILED(err)) {
        fprintf(stderr, "Fatal Python error: %s\n", err.msg);
        fflush(stderr);
        exit(1);
    }

26
    const char *p;
27
    int i, n, sts = 1;
28 29
    int inspect = 0;
    int unbuffered = 0;
30 31
    char *oldloc = NULL;
    wchar_t **argv_copy = NULL;
32
    /* We need a second copies, as Python might modify the first one. */
33
    wchar_t **argv_copy2 = NULL;
34

35 36 37 38 39 40 41
    if (argc > 0) {
        argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
        argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
        if (!argv_copy || !argv_copy2) {
            fprintf(stderr, "out of memory\n");
            goto error;
        }
42
    }
43 44 45 46 47 48 49 50 51 52 53 54 55 56

    Py_FrozenFlag = 1; /* Suppress errors from getpath.c */

    if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
        inspect = 1;
    if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
        unbuffered = 1;

    if (unbuffered) {
        setbuf(stdin, (char *)NULL);
        setbuf(stdout, (char *)NULL);
        setbuf(stderr, (char *)NULL);
    }

57 58
    oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
    if (!oldloc) {
59
        fprintf(stderr, "out of memory\n");
60
        goto error;
61 62 63 64
    }

    setlocale(LC_ALL, "");
    for (i = 0; i < argc; i++) {
65
        argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
66
        argv_copy2[i] = argv_copy[i];
67
        if (!argv_copy[i]) {
68 69
            fprintf(stderr, "Unable to decode the command line argument #%i\n",
                            i + 1);
70 71
            argc = i;
            goto error;
72 73 74
        }
    }
    setlocale(LC_ALL, oldloc);
75 76
    PyMem_RawFree(oldloc);
    oldloc = NULL;
77

78
#ifdef MS_WINDOWS
79
    PyInitFrozenExtensions();
80
#endif /* MS_WINDOWS */
81 82
    if (argc >= 1)
        Py_SetProgramName(argv_copy[0]);
83
    Py_Initialize();
84
#ifdef MS_WINDOWS
85
    PyWinFreeze_ExeInit();
86
#endif
87

88 89 90
    if (Py_VerboseFlag)
        fprintf(stderr, "Python %s\n%s\n",
            Py_GetVersion(), Py_GetCopyright());
91

92
    PySys_SetArgv(argc, argv_copy);
93

94 95 96 97 98 99 100 101 102
    n = PyImport_ImportFrozenModule("__main__");
    if (n == 0)
        Py_FatalError("__main__ not frozen");
    if (n < 0) {
        PyErr_Print();
        sts = 1;
    }
    else
        sts = 0;
103

104 105
    if (inspect && isatty((int)fileno(stdin)))
        sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
106

107
#ifdef MS_WINDOWS
108
    PyWinFreeze_ExeTerm();
109
#endif
110 111 112
    if (Py_FinalizeEx() < 0) {
        sts = 120;
    }
113 114

error:
115
    PyMem_RawFree(argv_copy);
116 117 118 119 120 121
    if (argv_copy2) {
        for (i = 0; i < argc; i++)
            PyMem_RawFree(argv_copy2[i]);
        PyMem_RawFree(argv_copy2);
    }
    PyMem_RawFree(oldloc);
122
    return sts;
123
}