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 7 #ifdef HAVE_UNISTD_H 8 # include <unistd.h> // isatty() 9 #endif 10 11 12 #ifdef MS_WINDOWS 13 extern void PyWinFreeze_ExeInit(void); 14 extern void PyWinFreeze_ExeTerm(void); 15 extern int PyInitFrozenExtensions(void); 16 #endif 17 18 /* Main program */ 19 20 int Py_FrozenMain(int argc,char ** argv)21Py_FrozenMain(int argc, char **argv) 22 { 23 PyStatus status = _PyRuntime_Initialize(); 24 if (PyStatus_Exception(status)) { 25 Py_ExitStatusException(status); 26 } 27 28 PyConfig config; 29 PyConfig_InitPythonConfig(&config); 30 // Suppress errors from getpath.c 31 config.pathconfig_warnings = 0; 32 // Don't parse command line options like -E 33 config.parse_argv = 0; 34 35 status = PyConfig_SetBytesArgv(&config, argc, argv); 36 if (PyStatus_Exception(status)) { 37 PyConfig_Clear(&config); 38 Py_ExitStatusException(status); 39 } 40 41 const char *p; 42 int inspect = 0; 43 if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') { 44 inspect = 1; 45 } 46 47 #ifdef MS_WINDOWS 48 PyInitFrozenExtensions(); 49 #endif /* MS_WINDOWS */ 50 51 status = Py_InitializeFromConfig(&config); 52 PyConfig_Clear(&config); 53 if (PyStatus_Exception(status)) { 54 Py_ExitStatusException(status); 55 } 56 57 PyInterpreterState *interp = PyInterpreterState_Get(); 58 if (_PyInterpreterState_SetRunningMain(interp) < 0) { 59 PyErr_Print(); 60 exit(1); 61 } 62 63 #ifdef MS_WINDOWS 64 PyWinFreeze_ExeInit(); 65 #endif 66 67 if (_Py_GetConfig()->verbose) { 68 fprintf(stderr, "Python %s\n%s\n", 69 Py_GetVersion(), Py_GetCopyright()); 70 } 71 72 int sts = 1; 73 int n = PyImport_ImportFrozenModule("__main__"); 74 if (n == 0) { 75 Py_FatalError("the __main__ module is not frozen"); 76 } 77 if (n < 0) { 78 PyErr_Print(); 79 sts = 1; 80 } 81 else { 82 sts = 0; 83 } 84 85 if (inspect && isatty((int)fileno(stdin))) { 86 sts = PyRun_AnyFile(stdin, "<stdin>") != 0; 87 } 88 89 #ifdef MS_WINDOWS 90 PyWinFreeze_ExeTerm(); 91 #endif 92 93 _PyInterpreterState_SetNotRunningMain(interp); 94 95 if (Py_FinalizeEx() < 0) { 96 sts = 120; 97 } 98 return sts; 99 } 100