blob: aef7122517a0665f2ff6570ce409abc8192cd614 [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"
Victor Stinnerf7e5b562017-11-15 15:48:08 -08004#include "internal/pystate.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +00005#include <locale.h>
Guido van Rossumbe10c201998-08-08 20:01:22 +00006
Tim Peters4643bd92002-12-28 21:56:08 +00007#ifdef __FreeBSD__
Victor Stinner7172f502016-01-20 22:27:34 +01008#include <fenv.h>
Tim Peters4643bd92002-12-28 21:56:08 +00009#endif
10
Martin v. Löwis790465f2008-04-05 20:41:37 +000011#ifdef MS_WINDOWS
12int
13wmain(int argc, wchar_t **argv)
14{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000015 return Py_Main(argc, argv);
Martin v. Löwis790465f2008-04-05 20:41:37 +000016}
17#else
Martin v. Löwis011e8422009-05-05 04:43:17 +000018
Nick Coghlan6ea41862017-06-11 13:16:15 +100019
Victor Stinner91106cd2017-12-13 12:29:09 +010020static void _Py_NO_RETURN
21fatal_error(const char *msg)
22{
23 fprintf(stderr, "Fatal Python error: %s\n", msg);
24 fflush(stderr);
25 exit(1);
26}
27
28
Guido van Rossum7c141031997-08-15 02:52:08 +000029int
Fredrik Lundhfaa209d62000-07-09 20:35:15 +000030main(int argc, char **argv)
Guido van Rossum4c04be61997-07-19 19:25:33 +000031{
Victor Stinner1a7425f2013-07-07 16:25:15 +020032 wchar_t **argv_copy;
R David Murray296b73c2013-07-10 10:57:39 -040033 /* We need a second copy, as Python might modify the first one. */
Victor Stinner1a7425f2013-07-07 16:25:15 +020034 wchar_t **argv_copy2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080035 int i, status;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 char *oldloc;
Victor Stinner1a7425f2013-07-07 16:25:15 +020037
Victor Stinnerf7e5b562017-11-15 15:48:08 -080038 _PyInitError err = _PyRuntime_Initialize();
39 if (_Py_INIT_FAILED(err)) {
Victor Stinner91106cd2017-12-13 12:29:09 +010040 fatal_error(err.msg);
Victor Stinnerf7e5b562017-11-15 15:48:08 -080041 }
42
Victor Stinner5d39e042017-11-29 17:20:38 +010043 /* Force default allocator, to be able to release memory above
44 with a known allocator. */
45 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, NULL);
Victor Stinner34be8072016-03-14 12:04:26 +010046
Victor Stinner1a7425f2013-07-07 16:25:15 +020047 argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
48 argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
49 if (!argv_copy || !argv_copy2) {
Victor Stinner91106cd2017-12-13 12:29:09 +010050 fatal_error("out of memory");
Victor Stinner1a7425f2013-07-07 16:25:15 +020051 return 1;
52 }
53
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 /* 754 requires that FP exceptions run in "no stop" mode by default,
55 * and until C vendors implement C99's ways to control FP exceptions,
56 * Python requires non-stop mode. Alas, some platforms enable FP
57 * exceptions by default. Here we disable them.
58 */
Tim Peters4643bd92002-12-28 21:56:08 +000059#ifdef __FreeBSD__
Victor Stinner7172f502016-01-20 22:27:34 +010060 fedisableexcept(FE_OVERFLOW);
Tim Peters4643bd92002-12-28 21:56:08 +000061#endif
Victor Stinner1a7425f2013-07-07 16:25:15 +020062
Victor Stinner49fc8ec2013-07-07 23:30:24 +020063 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
Victor Stinnerc588fee2013-07-27 02:39:09 +020064 if (!oldloc) {
Victor Stinner91106cd2017-12-13 12:29:09 +010065 fatal_error("out of memory");
Victor Stinnerc588fee2013-07-27 02:39:09 +020066 return 1;
67 }
68
Nick Coghlan6ea41862017-06-11 13:16:15 +100069 /* Reconfigure the locale to the default for this process */
xdegaye1588be62017-11-12 12:45:59 +010070 _Py_SetLocaleFromEnv(LC_ALL);
Nick Coghlan6ea41862017-06-11 13:16:15 +100071
Erik Bray031c4bf2017-10-27 11:46:03 +020072 /* The legacy C locale assumes ASCII as the default text encoding, which
73 * causes problems not only for the CPython runtime, but also other
74 * components like GNU readline.
75 *
76 * Accordingly, when the CLI detects it, it attempts to coerce it to a
77 * more capable UTF-8 based alternative.
78 *
79 * See the documentation of the PYTHONCOERCECLOCALE setting for more
80 * details.
81 */
Nick Coghlan6ea41862017-06-11 13:16:15 +100082 if (_Py_LegacyLocaleDetected()) {
Victor Stinner91106cd2017-12-13 12:29:09 +010083 Py_UTF8Mode = 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +100084 _Py_CoerceLegacyLocale();
85 }
86
87 /* Convert from char to wchar_t based on the locale settings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 for (i = 0; i < argc; i++) {
Victor Stinnerf6a271a2014-08-01 12:28:48 +020089 argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
Victor Stinner94ba6912011-12-16 23:48:31 +010090 if (!argv_copy[i]) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +020091 PyMem_RawFree(oldloc);
Victor Stinner91106cd2017-12-13 12:29:09 +010092 fatal_error("unable to decode the command line arguments");
Victor Stinner94ba6912011-12-16 23:48:31 +010093 }
Victor Stinner052a04d2010-10-13 23:24:06 +000094 argv_copy2[i] = argv_copy[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 }
Stefan Krah0f6ce8d2012-03-26 15:05:22 +020096 argv_copy2[argc] = argv_copy[argc] = NULL;
97
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 setlocale(LC_ALL, oldloc);
Victor Stinner49fc8ec2013-07-07 23:30:24 +020099 PyMem_RawFree(oldloc);
Victor Stinner34be8072016-03-14 12:04:26 +0100100
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800101 status = Py_Main(argc, argv_copy);
Victor Stinner34be8072016-03-14 12:04:26 +0100102
Victor Stinner5d39e042017-11-29 17:20:38 +0100103 /* Py_Main() can change PyMem_RawMalloc() allocator, so restore the default
104 to release memory blocks allocated before Py_Main() */
105 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, NULL);
Victor Stinner34be8072016-03-14 12:04:26 +0100106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 for (i = 0; i < argc; i++) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200108 PyMem_RawFree(argv_copy2[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200110 PyMem_RawFree(argv_copy);
111 PyMem_RawFree(argv_copy2);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800112 return status;
Guido van Rossum4c04be61997-07-19 19:25:33 +0000113}
Martin v. Löwis790465f2008-04-05 20:41:37 +0000114#endif