blob: 2c08b9685687e0fe119cffe517fdee80d05adf6d [file] [log] [blame]
Guido van Rossum4c04be61997-07-19 19:25:33 +00001/* Minimal main program -- everything is loaded from the library */
2
Guido van Rossumbe10c201998-08-08 20:01:22 +00003#include "Python.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +00004#include <locale.h>
Guido van Rossumbe10c201998-08-08 20:01:22 +00005
Tim Peters4643bd92002-12-28 21:56:08 +00006#ifdef __FreeBSD__
7#include <floatingpoint.h>
8#endif
9
Martin v. Löwis790465f2008-04-05 20:41:37 +000010#ifdef MS_WINDOWS
11int
12wmain(int argc, wchar_t **argv)
13{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014 return Py_Main(argc, argv);
Martin v. Löwis790465f2008-04-05 20:41:37 +000015}
16#else
Martin v. Löwis011e8422009-05-05 04:43:17 +000017
Guido van Rossum7c141031997-08-15 02:52:08 +000018int
Fredrik Lundhfaa209d62000-07-09 20:35:15 +000019main(int argc, char **argv)
Guido van Rossum4c04be61997-07-19 19:25:33 +000020{
Stefan Krah0f6ce8d2012-03-26 15:05:22 +020021 wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 /* We need a second copies, as Python might modify the first one. */
Stefan Krah0f6ce8d2012-03-26 15:05:22 +020023 wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 int i, res;
25 char *oldloc;
26 /* 754 requires that FP exceptions run in "no stop" mode by default,
27 * and until C vendors implement C99's ways to control FP exceptions,
28 * Python requires non-stop mode. Alas, some platforms enable FP
29 * exceptions by default. Here we disable them.
30 */
Tim Peters4643bd92002-12-28 21:56:08 +000031#ifdef __FreeBSD__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 fp_except_t m;
Tim Peters4643bd92002-12-28 21:56:08 +000033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 m = fpgetmask();
35 fpsetmask(m & ~FP_X_OFL);
Tim Peters4643bd92002-12-28 21:56:08 +000036#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 if (!argv_copy || !argv_copy2) {
38 fprintf(stderr, "out of memory\n");
39 return 1;
40 }
41 oldloc = strdup(setlocale(LC_ALL, NULL));
42 setlocale(LC_ALL, "");
43 for (i = 0; i < argc; i++) {
Victor Stinner168e1172010-10-16 23:16:16 +000044 argv_copy[i] = _Py_char2wchar(argv[i], NULL);
Victor Stinner94ba6912011-12-16 23:48:31 +010045 if (!argv_copy[i]) {
Brett Cannon0b1b9ce2012-06-25 16:25:28 -040046 free(oldloc);
Victor Stinner94ba6912011-12-16 23:48:31 +010047 fprintf(stderr, "Fatal Python error: "
48 "unable to decode the command line argument #%i\n",
49 i + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 return 1;
Victor Stinner94ba6912011-12-16 23:48:31 +010051 }
Victor Stinner052a04d2010-10-13 23:24:06 +000052 argv_copy2[i] = argv_copy[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 }
Stefan Krah0f6ce8d2012-03-26 15:05:22 +020054 argv_copy2[argc] = argv_copy[argc] = NULL;
55
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 setlocale(LC_ALL, oldloc);
57 free(oldloc);
58 res = Py_Main(argc, argv_copy);
59 for (i = 0; i < argc; i++) {
60 PyMem_Free(argv_copy2[i]);
61 }
62 PyMem_Free(argv_copy);
63 PyMem_Free(argv_copy2);
64 return res;
Guido van Rossum4c04be61997-07-19 19:25:33 +000065}
Martin v. Löwis790465f2008-04-05 20:41:37 +000066#endif