blob: 4f6b9198c8504efdc0c854376eda5cb5907773e8 [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
Nick Coghlan6ea41862017-06-11 13:16:15 +100018
Guido van Rossum7c141031997-08-15 02:52:08 +000019int
Fredrik Lundhfaa209d62000-07-09 20:35:15 +000020main(int argc, char **argv)
Guido van Rossum4c04be61997-07-19 19:25:33 +000021{
Victor Stinner1a7425f2013-07-07 16:25:15 +020022 wchar_t **argv_copy;
R David Murray296b73c2013-07-10 10:57:39 -040023 /* We need a second copy, as Python might modify the first one. */
Victor Stinner1a7425f2013-07-07 16:25:15 +020024 wchar_t **argv_copy2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 int i, res;
26 char *oldloc;
Victor Stinner1a7425f2013-07-07 16:25:15 +020027
Victor Stinner34be8072016-03-14 12:04:26 +010028 /* Force malloc() allocator to bootstrap Python */
Nick Coghlan6ea41862017-06-11 13:16:15 +100029#ifdef Py_DEBUG
30 (void)_PyMem_SetupAllocators("malloc_debug");
31# else
Victor Stinner34be8072016-03-14 12:04:26 +010032 (void)_PyMem_SetupAllocators("malloc");
Nick Coghlan6ea41862017-06-11 13:16:15 +100033# endif
Victor Stinner34be8072016-03-14 12:04:26 +010034
Victor Stinner1a7425f2013-07-07 16:25:15 +020035 argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
36 argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
37 if (!argv_copy || !argv_copy2) {
38 fprintf(stderr, "out of memory\n");
39 return 1;
40 }
41
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* 754 requires that FP exceptions run in "no stop" mode by default,
43 * and until C vendors implement C99's ways to control FP exceptions,
44 * Python requires non-stop mode. Alas, some platforms enable FP
45 * exceptions by default. Here we disable them.
46 */
Tim Peters4643bd92002-12-28 21:56:08 +000047#ifdef __FreeBSD__
Victor Stinner7172f502016-01-20 22:27:34 +010048 fedisableexcept(FE_OVERFLOW);
Tim Peters4643bd92002-12-28 21:56:08 +000049#endif
Victor Stinner1a7425f2013-07-07 16:25:15 +020050
Victor Stinner49fc8ec2013-07-07 23:30:24 +020051 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
Victor Stinnerc588fee2013-07-27 02:39:09 +020052 if (!oldloc) {
53 fprintf(stderr, "out of memory\n");
54 return 1;
55 }
56
Nick Coghlan6ea41862017-06-11 13:16:15 +100057#ifdef __ANDROID__
58 /* Passing "" to setlocale() on Android requests the C locale rather
59 * than checking environment variables, so request C.UTF-8 explicitly
60 */
61 setlocale(LC_ALL, "C.UTF-8");
62#else
63 /* Reconfigure the locale to the default for this process */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 setlocale(LC_ALL, "");
Nick Coghlan6ea41862017-06-11 13:16:15 +100065#endif
66
Erik Bray031c4bf2017-10-27 11:46:03 +020067 /* The legacy C locale assumes ASCII as the default text encoding, which
68 * causes problems not only for the CPython runtime, but also other
69 * components like GNU readline.
70 *
71 * Accordingly, when the CLI detects it, it attempts to coerce it to a
72 * more capable UTF-8 based alternative.
73 *
74 * See the documentation of the PYTHONCOERCECLOCALE setting for more
75 * details.
76 */
Nick Coghlan6ea41862017-06-11 13:16:15 +100077 if (_Py_LegacyLocaleDetected()) {
78 _Py_CoerceLegacyLocale();
79 }
80
81 /* Convert from char to wchar_t based on the locale settings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 for (i = 0; i < argc; i++) {
Victor Stinnerf6a271a2014-08-01 12:28:48 +020083 argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
Victor Stinner94ba6912011-12-16 23:48:31 +010084 if (!argv_copy[i]) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +020085 PyMem_RawFree(oldloc);
Victor Stinner94ba6912011-12-16 23:48:31 +010086 fprintf(stderr, "Fatal Python error: "
87 "unable to decode the command line argument #%i\n",
88 i + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 return 1;
Victor Stinner94ba6912011-12-16 23:48:31 +010090 }
Victor Stinner052a04d2010-10-13 23:24:06 +000091 argv_copy2[i] = argv_copy[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 }
Stefan Krah0f6ce8d2012-03-26 15:05:22 +020093 argv_copy2[argc] = argv_copy[argc] = NULL;
94
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 setlocale(LC_ALL, oldloc);
Victor Stinner49fc8ec2013-07-07 23:30:24 +020096 PyMem_RawFree(oldloc);
Victor Stinner34be8072016-03-14 12:04:26 +010097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 res = Py_Main(argc, argv_copy);
Victor Stinner34be8072016-03-14 12:04:26 +010099
100 /* Force again malloc() allocator to release memory blocks allocated
101 before Py_Main() */
Nick Coghlan6ea41862017-06-11 13:16:15 +1000102#ifdef Py_DEBUG
103 (void)_PyMem_SetupAllocators("malloc_debug");
104# else
Victor Stinner34be8072016-03-14 12:04:26 +0100105 (void)_PyMem_SetupAllocators("malloc");
Nick Coghlan6ea41862017-06-11 13:16:15 +1000106# endif
Victor Stinner34be8072016-03-14 12:04:26 +0100107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 for (i = 0; i < argc; i++) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200109 PyMem_RawFree(argv_copy2[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200111 PyMem_RawFree(argv_copy);
112 PyMem_RawFree(argv_copy2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 return res;
Guido van Rossum4c04be61997-07-19 19:25:33 +0000114}
Martin v. Löwis790465f2008-04-05 20:41:37 +0000115#endif