Line data Source code
1 :
2 : /* Python interpreter main program for frozen scripts */
3 :
4 : #include "Python.h"
5 : #include <locale.h>
6 :
7 : #ifdef MS_WINDOWS
8 : extern void PyWinFreeze_ExeInit(void);
9 : extern void PyWinFreeze_ExeTerm(void);
10 : extern int PyInitFrozenExtensions(void);
11 : #endif
12 :
13 : /* Main program */
14 :
15 : int
16 0 : Py_FrozenMain(int argc, char **argv)
17 : {
18 : char *p;
19 : int i, n, sts;
20 0 : int inspect = 0;
21 0 : int unbuffered = 0;
22 : char *oldloc;
23 0 : wchar_t **argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc);
24 : /* We need a second copies, as Python might modify the first one. */
25 0 : wchar_t **argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc);
26 :
27 0 : Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
28 :
29 0 : if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
30 0 : inspect = 1;
31 0 : if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
32 0 : unbuffered = 1;
33 :
34 0 : if (unbuffered) {
35 0 : setbuf(stdin, (char *)NULL);
36 0 : setbuf(stdout, (char *)NULL);
37 0 : setbuf(stderr, (char *)NULL);
38 : }
39 :
40 0 : if (!argv_copy) {
41 0 : fprintf(stderr, "out of memory\n");
42 0 : return 1;
43 : }
44 :
45 0 : oldloc = setlocale(LC_ALL, NULL);
46 0 : setlocale(LC_ALL, "");
47 0 : for (i = 0; i < argc; i++) {
48 : #ifdef HAVE_BROKEN_MBSTOWCS
49 : size_t argsize = strlen(argv[i]);
50 : #else
51 0 : size_t argsize = mbstowcs(NULL, argv[i], 0);
52 : #endif
53 : size_t count;
54 0 : if (argsize == (size_t)-1) {
55 0 : fprintf(stderr, "Could not convert argument %d to string\n", i);
56 0 : return 1;
57 : }
58 0 : argv_copy[i] = PyMem_Malloc((argsize+1)*sizeof(wchar_t));
59 0 : argv_copy2[i] = argv_copy[i];
60 0 : if (!argv_copy[i]) {
61 0 : fprintf(stderr, "out of memory\n");
62 0 : return 1;
63 : }
64 0 : count = mbstowcs(argv_copy[i], argv[i], argsize+1);
65 0 : if (count == (size_t)-1) {
66 0 : fprintf(stderr, "Could not convert argument %d to string\n", i);
67 0 : return 1;
68 : }
69 : }
70 0 : setlocale(LC_ALL, oldloc);
71 :
72 : #ifdef MS_WINDOWS
73 : PyInitFrozenExtensions();
74 : #endif /* MS_WINDOWS */
75 0 : Py_SetProgramName(argv_copy[0]);
76 0 : Py_Initialize();
77 : #ifdef MS_WINDOWS
78 : PyWinFreeze_ExeInit();
79 : #endif
80 :
81 0 : if (Py_VerboseFlag)
82 0 : fprintf(stderr, "Python %s\n%s\n",
83 : Py_GetVersion(), Py_GetCopyright());
84 :
85 0 : PySys_SetArgv(argc, argv_copy);
86 :
87 0 : n = PyImport_ImportFrozenModule("__main__");
88 0 : if (n == 0)
89 0 : Py_FatalError("__main__ not frozen");
90 0 : if (n < 0) {
91 0 : PyErr_Print();
92 0 : sts = 1;
93 : }
94 : else
95 0 : sts = 0;
96 :
97 0 : if (inspect && isatty((int)fileno(stdin)))
98 0 : sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
99 :
100 : #ifdef MS_WINDOWS
101 : PyWinFreeze_ExeTerm();
102 : #endif
103 0 : Py_Finalize();
104 0 : for (i = 0; i < argc; i++) {
105 0 : PyMem_Free(argv_copy2[i]);
106 : }
107 0 : PyMem_Free(argv_copy);
108 0 : PyMem_Free(argv_copy2);
109 0 : return sts;
110 : }
|