xref: /third_party/python/Python/frozenmain.c (revision 7db96d56)
1/* Python interpreter main program for frozen scripts */
2
3#include "Python.h"
4#include "pycore_runtime.h"  // _PyRuntime_Initialize()
5#include <locale.h>
6
7#ifdef MS_WINDOWS
8extern void PyWinFreeze_ExeInit(void);
9extern void PyWinFreeze_ExeTerm(void);
10extern int PyInitFrozenExtensions(void);
11#endif
12
13/* Main program */
14
15int
16Py_FrozenMain(int argc, char **argv)
17{
18    PyStatus status = _PyRuntime_Initialize();
19    if (PyStatus_Exception(status)) {
20        Py_ExitStatusException(status);
21    }
22
23    PyConfig config;
24    PyConfig_InitPythonConfig(&config);
25    // Suppress errors from getpath.c
26    config.pathconfig_warnings = 0;
27    // Don't parse command line options like -E
28    config.parse_argv = 0;
29
30    status = PyConfig_SetBytesArgv(&config, argc, argv);
31    if (PyStatus_Exception(status)) {
32        PyConfig_Clear(&config);
33        Py_ExitStatusException(status);
34    }
35
36    const char *p;
37    int inspect = 0;
38    if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') {
39        inspect = 1;
40    }
41
42#ifdef MS_WINDOWS
43    PyInitFrozenExtensions();
44#endif /* MS_WINDOWS */
45
46    status = Py_InitializeFromConfig(&config);
47    PyConfig_Clear(&config);
48    if (PyStatus_Exception(status)) {
49        Py_ExitStatusException(status);
50    }
51
52#ifdef MS_WINDOWS
53    PyWinFreeze_ExeInit();
54#endif
55
56    if (Py_VerboseFlag) {
57        fprintf(stderr, "Python %s\n%s\n",
58                Py_GetVersion(), Py_GetCopyright());
59    }
60
61    int sts = 1;
62    int n = PyImport_ImportFrozenModule("__main__");
63    if (n == 0) {
64        Py_FatalError("the __main__ module is not frozen");
65    }
66    if (n < 0) {
67        PyErr_Print();
68        sts = 1;
69    }
70    else {
71        sts = 0;
72    }
73
74    if (inspect && isatty((int)fileno(stdin))) {
75        sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
76    }
77
78#ifdef MS_WINDOWS
79    PyWinFreeze_ExeTerm();
80#endif
81    if (Py_FinalizeEx() < 0) {
82        sts = 120;
83    }
84    return sts;
85}
86