blob: a7afbc774b3a55b01bec9ed41eed1a376a2c4232 [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__
Victor Stinner7172f502016-01-20 22:27:34 +01007#include <fenv.h>
Tim Peters4643bd92002-12-28 21:56:08 +00008#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{
Victor Stinner1a7425f2013-07-07 16:25:15 +020021 wchar_t **argv_copy;
R David Murray296b73c2013-07-10 10:57:39 -040022 /* We need a second copy, as Python might modify the first one. */
Victor Stinner1a7425f2013-07-07 16:25:15 +020023 wchar_t **argv_copy2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 int i, res;
25 char *oldloc;
Victor Stinner1a7425f2013-07-07 16:25:15 +020026
Victor Stinner34be8072016-03-14 12:04:26 +010027 /* Force malloc() allocator to bootstrap Python */
28 (void)_PyMem_SetupAllocators("malloc");
29
Victor Stinner1a7425f2013-07-07 16:25:15 +020030 argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
31 argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
32 if (!argv_copy || !argv_copy2) {
33 fprintf(stderr, "out of memory\n");
34 return 1;
35 }
36
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 /* 754 requires that FP exceptions run in "no stop" mode by default,
38 * and until C vendors implement C99's ways to control FP exceptions,
39 * Python requires non-stop mode. Alas, some platforms enable FP
40 * exceptions by default. Here we disable them.
41 */
Tim Peters4643bd92002-12-28 21:56:08 +000042#ifdef __FreeBSD__
Victor Stinner7172f502016-01-20 22:27:34 +010043 fedisableexcept(FE_OVERFLOW);
Tim Peters4643bd92002-12-28 21:56:08 +000044#endif
Victor Stinner1a7425f2013-07-07 16:25:15 +020045
Victor Stinner49fc8ec2013-07-07 23:30:24 +020046 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
Victor Stinnerc588fee2013-07-27 02:39:09 +020047 if (!oldloc) {
48 fprintf(stderr, "out of memory\n");
49 return 1;
50 }
51
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 setlocale(LC_ALL, "");
53 for (i = 0; i < argc; i++) {
Victor Stinnerf6a271a2014-08-01 12:28:48 +020054 argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
Victor Stinner94ba6912011-12-16 23:48:31 +010055 if (!argv_copy[i]) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +020056 PyMem_RawFree(oldloc);
Victor Stinner94ba6912011-12-16 23:48:31 +010057 fprintf(stderr, "Fatal Python error: "
58 "unable to decode the command line argument #%i\n",
59 i + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 return 1;
Victor Stinner94ba6912011-12-16 23:48:31 +010061 }
Victor Stinner052a04d2010-10-13 23:24:06 +000062 argv_copy2[i] = argv_copy[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 }
Stefan Krah0f6ce8d2012-03-26 15:05:22 +020064 argv_copy2[argc] = argv_copy[argc] = NULL;
65
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 setlocale(LC_ALL, oldloc);
Victor Stinner49fc8ec2013-07-07 23:30:24 +020067 PyMem_RawFree(oldloc);
Victor Stinner34be8072016-03-14 12:04:26 +010068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 res = Py_Main(argc, argv_copy);
Victor Stinner34be8072016-03-14 12:04:26 +010070
71 /* Force again malloc() allocator to release memory blocks allocated
72 before Py_Main() */
73 (void)_PyMem_SetupAllocators("malloc");
74
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 for (i = 0; i < argc; i++) {
Victor Stinner1a7425f2013-07-07 16:25:15 +020076 PyMem_RawFree(argv_copy2[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 }
Victor Stinner1a7425f2013-07-07 16:25:15 +020078 PyMem_RawFree(argv_copy);
79 PyMem_RawFree(argv_copy2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 return res;
Guido van Rossum4c04be61997-07-19 19:25:33 +000081}
Martin v. Löwis790465f2008-04-05 20:41:37 +000082#endif