blob: 22d55bbc4ceb0e6aa4824d3cd314ab815256052a [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
Guido van Rossum7c141031997-08-15 02:52:08 +000020int
Fredrik Lundhfaa209d62000-07-09 20:35:15 +000021main(int argc, char **argv)
Guido van Rossum4c04be61997-07-19 19:25:33 +000022{
Victor Stinner1a7425f2013-07-07 16:25:15 +020023 wchar_t **argv_copy;
R David Murray296b73c2013-07-10 10:57:39 -040024 /* We need a second copy, as Python might modify the first one. */
Victor Stinner1a7425f2013-07-07 16:25:15 +020025 wchar_t **argv_copy2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080026 int i, status;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 char *oldloc;
Victor Stinner1a7425f2013-07-07 16:25:15 +020028
Victor Stinnerf7e5b562017-11-15 15:48:08 -080029 _PyInitError err = _PyRuntime_Initialize();
30 if (_Py_INIT_FAILED(err)) {
31 fprintf(stderr, "Fatal Python error: %s\n", err.msg);
32 fflush(stderr);
33 exit(1);
34 }
35
Victor Stinner5d39e042017-11-29 17:20:38 +010036 /* Force default allocator, to be able to release memory above
37 with a known allocator. */
38 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, NULL);
Victor Stinner34be8072016-03-14 12:04:26 +010039
Victor Stinner1a7425f2013-07-07 16:25:15 +020040 argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
41 argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
42 if (!argv_copy || !argv_copy2) {
43 fprintf(stderr, "out of memory\n");
44 return 1;
45 }
46
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 /* 754 requires that FP exceptions run in "no stop" mode by default,
48 * and until C vendors implement C99's ways to control FP exceptions,
49 * Python requires non-stop mode. Alas, some platforms enable FP
50 * exceptions by default. Here we disable them.
51 */
Tim Peters4643bd92002-12-28 21:56:08 +000052#ifdef __FreeBSD__
Victor Stinner7172f502016-01-20 22:27:34 +010053 fedisableexcept(FE_OVERFLOW);
Tim Peters4643bd92002-12-28 21:56:08 +000054#endif
Victor Stinner1a7425f2013-07-07 16:25:15 +020055
Victor Stinner49fc8ec2013-07-07 23:30:24 +020056 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
Victor Stinnerc588fee2013-07-27 02:39:09 +020057 if (!oldloc) {
58 fprintf(stderr, "out of memory\n");
59 return 1;
60 }
61
Nick Coghlan6ea41862017-06-11 13:16:15 +100062 /* Reconfigure the locale to the default for this process */
xdegaye1588be62017-11-12 12:45:59 +010063 _Py_SetLocaleFromEnv(LC_ALL);
Nick Coghlan6ea41862017-06-11 13:16:15 +100064
Erik Bray031c4bf2017-10-27 11:46:03 +020065 /* The legacy C locale assumes ASCII as the default text encoding, which
66 * causes problems not only for the CPython runtime, but also other
67 * components like GNU readline.
68 *
69 * Accordingly, when the CLI detects it, it attempts to coerce it to a
70 * more capable UTF-8 based alternative.
71 *
72 * See the documentation of the PYTHONCOERCECLOCALE setting for more
73 * details.
74 */
Nick Coghlan6ea41862017-06-11 13:16:15 +100075 if (_Py_LegacyLocaleDetected()) {
76 _Py_CoerceLegacyLocale();
77 }
78
79 /* Convert from char to wchar_t based on the locale settings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 for (i = 0; i < argc; i++) {
Victor Stinnerf6a271a2014-08-01 12:28:48 +020081 argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
Victor Stinner94ba6912011-12-16 23:48:31 +010082 if (!argv_copy[i]) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +020083 PyMem_RawFree(oldloc);
Victor Stinner94ba6912011-12-16 23:48:31 +010084 fprintf(stderr, "Fatal Python error: "
85 "unable to decode the command line argument #%i\n",
86 i + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 return 1;
Victor Stinner94ba6912011-12-16 23:48:31 +010088 }
Victor Stinner052a04d2010-10-13 23:24:06 +000089 argv_copy2[i] = argv_copy[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 }
Stefan Krah0f6ce8d2012-03-26 15:05:22 +020091 argv_copy2[argc] = argv_copy[argc] = NULL;
92
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 setlocale(LC_ALL, oldloc);
Victor Stinner49fc8ec2013-07-07 23:30:24 +020094 PyMem_RawFree(oldloc);
Victor Stinner34be8072016-03-14 12:04:26 +010095
Victor Stinnerf7e5b562017-11-15 15:48:08 -080096 status = Py_Main(argc, argv_copy);
Victor Stinner34be8072016-03-14 12:04:26 +010097
Victor Stinner5d39e042017-11-29 17:20:38 +010098 /* Py_Main() can change PyMem_RawMalloc() allocator, so restore the default
99 to release memory blocks allocated before Py_Main() */
100 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, NULL);
Victor Stinner34be8072016-03-14 12:04:26 +0100101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 for (i = 0; i < argc; i++) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200103 PyMem_RawFree(argv_copy2[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200105 PyMem_RawFree(argv_copy);
106 PyMem_RawFree(argv_copy2);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800107 return status;
Guido van Rossum4c04be61997-07-19 19:25:33 +0000108}
Martin v. Löwis790465f2008-04-05 20:41:37 +0000109#endif