ยปCore Development>Code coverage>Programs/python.c

Python code coverage for Programs/python.c

#countcontent
1n/a/* Minimal main program -- everything is loaded from the library */
2n/a
3n/a#include "Python.h"
4n/a#include <locale.h>
5n/a
6n/a#ifdef __FreeBSD__
7n/a#include <fenv.h>
8n/a#endif
9n/a
10n/a#ifdef MS_WINDOWS
11n/aint
12n/awmain(int argc, wchar_t **argv)
13n/a{
14n/a return Py_Main(argc, argv);
15n/a}
16n/a#else
17n/a
18n/aint
19n/amain(int argc, char **argv)
20n/a{
21n/a wchar_t **argv_copy;
22n/a /* We need a second copy, as Python might modify the first one. */
23n/a wchar_t **argv_copy2;
24n/a int i, res;
25n/a char *oldloc;
26n/a
27n/a /* Force malloc() allocator to bootstrap Python */
28n/a (void)_PyMem_SetupAllocators("malloc");
29n/a
30n/a argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
31n/a argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
32n/a if (!argv_copy || !argv_copy2) {
33n/a fprintf(stderr, "out of memory\n");
34n/a return 1;
35n/a }
36n/a
37n/a /* 754 requires that FP exceptions run in "no stop" mode by default,
38n/a * and until C vendors implement C99's ways to control FP exceptions,
39n/a * Python requires non-stop mode. Alas, some platforms enable FP
40n/a * exceptions by default. Here we disable them.
41n/a */
42n/a#ifdef __FreeBSD__
43n/a fedisableexcept(FE_OVERFLOW);
44n/a#endif
45n/a
46n/a oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
47n/a if (!oldloc) {
48n/a fprintf(stderr, "out of memory\n");
49n/a return 1;
50n/a }
51n/a
52n/a setlocale(LC_ALL, "");
53n/a for (i = 0; i < argc; i++) {
54n/a argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
55n/a if (!argv_copy[i]) {
56n/a PyMem_RawFree(oldloc);
57n/a fprintf(stderr, "Fatal Python error: "
58n/a "unable to decode the command line argument #%i\n",
59n/a i + 1);
60n/a return 1;
61n/a }
62n/a argv_copy2[i] = argv_copy[i];
63n/a }
64n/a argv_copy2[argc] = argv_copy[argc] = NULL;
65n/a
66n/a setlocale(LC_ALL, oldloc);
67n/a PyMem_RawFree(oldloc);
68n/a
69n/a res = Py_Main(argc, argv_copy);
70n/a
71n/a /* Force again malloc() allocator to release memory blocks allocated
72n/a before Py_Main() */
73n/a (void)_PyMem_SetupAllocators("malloc");
74n/a
75n/a for (i = 0; i < argc; i++) {
76n/a PyMem_RawFree(argv_copy2[i]);
77n/a }
78n/a PyMem_RawFree(argv_copy);
79n/a PyMem_RawFree(argv_copy2);
80n/a return res;
81n/a}
82n/a#endif