ยปCore Development>Code coverage>Python/frozenmain.c

Python code coverage for Python/frozenmain.c

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