• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* Python interpreter main program for frozen scripts */
3 
4 #include "Python.h"
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
Py_FrozenMain(int argc,char ** argv)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     const char *p;
25     int i, n, sts = 1;
26     int inspect = 0;
27     int unbuffered = 0;
28     char *oldloc = NULL;
29     wchar_t **argv_copy = NULL;
30     /* We need a second copies, as Python might modify the first one. */
31     wchar_t **argv_copy2 = NULL;
32 
33     if (argc > 0) {
34         argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
35         argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
36         if (!argv_copy || !argv_copy2) {
37             fprintf(stderr, "out of memory\n");
38             goto error;
39         }
40     }
41 
42     PyConfig config;
43     PyConfig_InitPythonConfig(&config);
44     config.pathconfig_warnings = 0;   /* Suppress errors from getpath.c */
45 
46     if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
47         inspect = 1;
48     if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
49         unbuffered = 1;
50 
51     if (unbuffered) {
52         setbuf(stdin, (char *)NULL);
53         setbuf(stdout, (char *)NULL);
54         setbuf(stderr, (char *)NULL);
55     }
56 
57     oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
58     if (!oldloc) {
59         fprintf(stderr, "out of memory\n");
60         goto error;
61     }
62 
63     setlocale(LC_ALL, "");
64     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             fprintf(stderr, "Unable to decode the command line argument #%i\n",
69                             i + 1);
70             argc = i;
71             goto error;
72         }
73     }
74     setlocale(LC_ALL, oldloc);
75     PyMem_RawFree(oldloc);
76     oldloc = NULL;
77 
78 #ifdef MS_WINDOWS
79     PyInitFrozenExtensions();
80 #endif /* MS_WINDOWS */
81     if (argc >= 1)
82         Py_SetProgramName(argv_copy[0]);
83 
84     status = Py_InitializeFromConfig(&config);
85     PyConfig_Clear(&config);
86     if (PyStatus_Exception(status)) {
87         Py_ExitStatusException(status);
88     }
89 
90 #ifdef MS_WINDOWS
91     PyWinFreeze_ExeInit();
92 #endif
93 
94     if (Py_VerboseFlag)
95         fprintf(stderr, "Python %s\n%s\n",
96             Py_GetVersion(), Py_GetCopyright());
97 
98     PySys_SetArgv(argc, argv_copy);
99 
100     n = PyImport_ImportFrozenModule("__main__");
101     if (n == 0)
102         Py_FatalError("the __main__ module is not frozen");
103     if (n < 0) {
104         PyErr_Print();
105         sts = 1;
106     }
107     else
108         sts = 0;
109 
110     if (inspect && isatty((int)fileno(stdin)))
111         sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
112 
113 #ifdef MS_WINDOWS
114     PyWinFreeze_ExeTerm();
115 #endif
116     if (Py_FinalizeEx() < 0) {
117         sts = 120;
118     }
119 
120 error:
121     PyMem_RawFree(argv_copy);
122     if (argv_copy2) {
123         for (i = 0; i < argc; i++)
124             PyMem_RawFree(argv_copy2[i]);
125         PyMem_RawFree(argv_copy2);
126     }
127     PyMem_RawFree(oldloc);
128     return sts;
129 }
130