blob: 03f8295045cfc65609ea742d5f8dcc3cefa1e44c [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/* Access private pylifecycle helper API to better handle the legacy C locale
19 *
20 * The legacy C locale assumes ASCII as the default text encoding, which
21 * causes problems not only for the CPython runtime, but also other
22 * components like GNU readline.
23 *
24 * Accordingly, when the CLI detects it, it attempts to coerce it to a
25 * more capable UTF-8 based alternative.
26 *
27 * See the documentation of the PYTHONCOERCECLOCALE setting for more details.
28 *
29 */
30extern int _Py_LegacyLocaleDetected(void);
31extern void _Py_CoerceLegacyLocale(void);
32
Guido van Rossum7c141031997-08-15 02:52:08 +000033int
Fredrik Lundhfaa209d62000-07-09 20:35:15 +000034main(int argc, char **argv)
Guido van Rossum4c04be61997-07-19 19:25:33 +000035{
Victor Stinner1a7425f2013-07-07 16:25:15 +020036 wchar_t **argv_copy;
R David Murray296b73c2013-07-10 10:57:39 -040037 /* We need a second copy, as Python might modify the first one. */
Victor Stinner1a7425f2013-07-07 16:25:15 +020038 wchar_t **argv_copy2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 int i, res;
40 char *oldloc;
Victor Stinner1a7425f2013-07-07 16:25:15 +020041
Victor Stinner34be8072016-03-14 12:04:26 +010042 /* Force malloc() allocator to bootstrap Python */
Nick Coghlan6ea41862017-06-11 13:16:15 +100043#ifdef Py_DEBUG
44 (void)_PyMem_SetupAllocators("malloc_debug");
45# else
Victor Stinner34be8072016-03-14 12:04:26 +010046 (void)_PyMem_SetupAllocators("malloc");
Nick Coghlan6ea41862017-06-11 13:16:15 +100047# endif
Victor Stinner34be8072016-03-14 12:04:26 +010048
Victor Stinner1a7425f2013-07-07 16:25:15 +020049 argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
50 argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
51 if (!argv_copy || !argv_copy2) {
52 fprintf(stderr, "out of memory\n");
53 return 1;
54 }
55
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 /* 754 requires that FP exceptions run in "no stop" mode by default,
57 * and until C vendors implement C99's ways to control FP exceptions,
58 * Python requires non-stop mode. Alas, some platforms enable FP
59 * exceptions by default. Here we disable them.
60 */
Tim Peters4643bd92002-12-28 21:56:08 +000061#ifdef __FreeBSD__
Victor Stinner7172f502016-01-20 22:27:34 +010062 fedisableexcept(FE_OVERFLOW);
Tim Peters4643bd92002-12-28 21:56:08 +000063#endif
Victor Stinner1a7425f2013-07-07 16:25:15 +020064
Victor Stinner49fc8ec2013-07-07 23:30:24 +020065 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
Victor Stinnerc588fee2013-07-27 02:39:09 +020066 if (!oldloc) {
67 fprintf(stderr, "out of memory\n");
68 return 1;
69 }
70
Nick Coghlan6ea41862017-06-11 13:16:15 +100071#ifdef __ANDROID__
72 /* Passing "" to setlocale() on Android requests the C locale rather
73 * than checking environment variables, so request C.UTF-8 explicitly
74 */
75 setlocale(LC_ALL, "C.UTF-8");
76#else
77 /* Reconfigure the locale to the default for this process */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 setlocale(LC_ALL, "");
Nick Coghlan6ea41862017-06-11 13:16:15 +100079#endif
80
81 if (_Py_LegacyLocaleDetected()) {
82 _Py_CoerceLegacyLocale();
83 }
84
85 /* Convert from char to wchar_t based on the locale settings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 for (i = 0; i < argc; i++) {
Victor Stinnerf6a271a2014-08-01 12:28:48 +020087 argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
Victor Stinner94ba6912011-12-16 23:48:31 +010088 if (!argv_copy[i]) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +020089 PyMem_RawFree(oldloc);
Victor Stinner94ba6912011-12-16 23:48:31 +010090 fprintf(stderr, "Fatal Python error: "
91 "unable to decode the command line argument #%i\n",
92 i + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 return 1;
Victor Stinner94ba6912011-12-16 23:48:31 +010094 }
Victor Stinner052a04d2010-10-13 23:24:06 +000095 argv_copy2[i] = argv_copy[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 }
Stefan Krah0f6ce8d2012-03-26 15:05:22 +020097 argv_copy2[argc] = argv_copy[argc] = NULL;
98
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 setlocale(LC_ALL, oldloc);
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200100 PyMem_RawFree(oldloc);
Victor Stinner34be8072016-03-14 12:04:26 +0100101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 res = Py_Main(argc, argv_copy);
Victor Stinner34be8072016-03-14 12:04:26 +0100103
104 /* Force again malloc() allocator to release memory blocks allocated
105 before Py_Main() */
Nick Coghlan6ea41862017-06-11 13:16:15 +1000106#ifdef Py_DEBUG
107 (void)_PyMem_SetupAllocators("malloc_debug");
108# else
Victor Stinner34be8072016-03-14 12:04:26 +0100109 (void)_PyMem_SetupAllocators("malloc");
Nick Coghlan6ea41862017-06-11 13:16:15 +1000110# endif
Victor Stinner34be8072016-03-14 12:04:26 +0100111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 for (i = 0; i < argc; i++) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200113 PyMem_RawFree(argv_copy2[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200115 PyMem_RawFree(argv_copy);
116 PyMem_RawFree(argv_copy2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 return res;
Guido van Rossum4c04be61997-07-19 19:25:33 +0000118}
Martin v. Löwis790465f2008-04-05 20:41:37 +0000119#endif