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