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