blob: 0f7498d6104fe44b46de56cb9419881da031b059 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Guido van Rossuma075ce11997-12-05 21:56:45 +00004#include "osdefs.h"
Benjamin Peterson42aa93b2017-12-09 10:26:52 -08005#include "internal/import.h"
Benjamin Petersone425bd72017-12-14 23:48:12 -08006#include "internal/pygetopt.h"
Victor Stinnerf7e5b562017-11-15 15:48:08 -08007#include "internal/pystate.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00008
Antoine Pitrou5651eaa2008-09-06 20:46:58 +00009#include <locale.h>
10
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000011#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010012# include <windows.h>
13# ifdef HAVE_IO_H
14# include <io.h>
15# endif
16# ifdef HAVE_FCNTL_H
17# include <fcntl.h>
18# endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000019#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000020
Martin v. Löwis945362c2007-08-30 14:57:25 +000021#ifdef _MSC_VER
Victor Stinner6efcb6d2017-12-18 23:42:55 +010022# include <crtdbg.h>
23#endif
24
25#ifdef __FreeBSD__
26# include <fenv.h>
Martin v. Löwis945362c2007-08-30 14:57:25 +000027#endif
28
Jesus Ceaab70e2a2012-10-05 01:48:08 +020029#if defined(MS_WINDOWS)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010030# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
Guido van Rossuma075ce11997-12-05 21:56:45 +000031#else
Victor Stinner6efcb6d2017-12-18 23:42:55 +010032# define PYTHONHOMEHELP "<prefix>/lib/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000033#endif
34
Guido van Rossuma22865e2000-09-05 04:41:18 +000035#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000036 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
37 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000038
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039#ifdef __cplusplus
40extern "C" {
41#endif
42
Victor Stinner46972b72017-11-24 22:55:40 +010043#define DECODE_LOCALE_ERR(NAME, LEN) \
44 (((LEN) == -2) \
Victor Stinner94540602017-12-16 04:54:22 +010045 ? _Py_INIT_USER_ERR("cannot decode " NAME) \
Victor Stinner46972b72017-11-24 22:55:40 +010046 : _Py_INIT_NO_MEMORY())
47
48
Victor Stinner0327bde2017-11-23 17:03:20 +010049#define SET_DECODE_ERROR(NAME, LEN) \
50 do { \
51 if ((LEN) == (size_t)-2) { \
Victor Stinner94540602017-12-16 04:54:22 +010052 pymain->err = _Py_INIT_USER_ERR("cannot decode " NAME); \
Victor Stinner0327bde2017-11-23 17:03:20 +010053 } \
54 else { \
55 pymain->err = _Py_INIT_NO_MEMORY(); \
56 } \
57 } while (0)
58
Victor Stinnerca719ac2017-12-20 18:00:19 +010059#ifdef MS_WINDOWS
60#define WCSTOK wcstok_s
61#else
62#define WCSTOK wcstok
63#endif
64
Guido van Rossumac56b031996-07-21 02:33:38 +000065/* For Py_GetArgcArgv(); set by main() */
Victor Stinner94540602017-12-16 04:54:22 +010066static wchar_t **orig_argv = NULL;
67static int orig_argc = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +000068
Guido van Rossumbceccf52001-04-10 22:07:43 +000069/* command line options */
Christian Heimesad73a9c2013-08-10 16:36:18 +020070#define BASE_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000071
Guido van Rossumbceccf52001-04-10 22:07:43 +000072#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000073
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080074static const _PyOS_LongOption longoptions[] = {
75 {L"check-hash-based-pycs", 1, 0},
76 {NULL, 0, 0},
77};
78
Guido van Rossum667d7041995-08-04 04:20:48 +000079/* Short usage message (with %s for argv0) */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020080static const char usage_line[] =
Martin v. Löwis790465f2008-04-05 20:41:37 +000081"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000082
83/* Long usage message, split into parts < 512 bytes */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020084static const char usage_1[] = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000085Options and arguments (and corresponding environment variables):\n\
Christian Heimes2ab34442008-09-03 20:31:07 +000086-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
87 and comparing bytes/bytearray with str. (-bb: issue errors)\n\
Xiang Zhang0710d752017-03-11 13:02:52 +080088-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000089-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000090-d : debug output from parser; also PYTHONDEBUG=x\n\
Christian Heimes790c8232008-01-07 21:14:23 +000091-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000093";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020094static const char usage_2[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000095-i : inspect interactively after running script; forces a prompt even\n\
96 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Christian Heimesad73a9c2013-08-10 16:36:18 +020097-I : isolate Python from the user's environment (implies -E and -s)\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000098-m mod : run library module as a script (terminates option list)\n\
Miss Islington (bot)b5655f32018-02-24 19:24:08 -080099-O : remove assert and __debug__-dependent statements; add .opt-1 before\n\
100 .pyc extension; also PYTHONOPTIMIZE=x\n\
101-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
102 .pyc extension\n\
Georg Brandl9d871192010-12-04 10:47:18 +0000103-q : don't print version and copyright messages on interactive startup\n\
Christian Heimes8dc226f2008-05-06 23:45:46 +0000104-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000105-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000106";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200107static const char usage_3[] = "\
Berker Peksag7f580972017-10-13 15:16:31 +0300108-u : force the stdout and stderr streams to be unbuffered;\n\
109 this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
111 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000112-V : print the Python version number and exit (also --version)\n\
INADA Naoki0e175a62016-11-21 20:57:14 +0900113 when given twice, print more information about the build\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenvey0805ca32010-04-07 04:04:10 +0000115 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000116-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000117-X opt : set implementation-specific option\n\
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800118--check-hash-based-pycs always|default|never:\n\
119 control how Python invalidates hash-based .pyc files\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000120";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200121static const char usage_4[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +0000122file : program read from script file\n\
123- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000125Other environment variables:\n\
126PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200127PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000128 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +0000129";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200130static const char usage_5[] =
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200131"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
Victor Stinner9802b392010-08-19 11:36:43 +0000132" The default module search path uses %s.\n"
133"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
134"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
Victor Stinner34be807c2016-03-14 12:04:26 +0100135"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
136static const char usage_6[] =
137"PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
138" to seed the hashes of str, bytes and datetime objects. It can also be\n"
139" set to an integer in the range [0,4294967295] to get hash values with a\n"
140" predictable seed.\n"
141"PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
142" on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000143" hooks.\n"
Stéphane Wirtel7d1017d2017-06-12 13:30:33 +0200144"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000145" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
Victor Stinner5e3806f2017-11-30 11:40:24 +0100146" locale coercion and locale compatibility warnings on stderr.\n"
Steve Dower6332de12018-07-29 12:17:49 +0100147"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
148" debugger. It can be set to the callable of your debugger of choice.\n"
Victor Stinner5e3806f2017-11-30 11:40:24 +0100149"PYTHONDEVMODE: enable the development mode.\n";
Guido van Rossum667d7041995-08-04 04:20:48 +0000150
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800151static void
Victor Stinnera7368ac2017-11-15 18:11:45 -0800152pymain_usage(int error, const wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000153{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800154 FILE *f = error ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 fprintf(f, usage_line, program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800157 if (error)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 fprintf(f, "Try `python -h' for more information.\n");
159 else {
160 fputs(usage_1, f);
161 fputs(usage_2, f);
162 fputs(usage_3, f);
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200163 fprintf(f, usage_4, (wint_t)DELIM);
164 fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100165 fputs(usage_6, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000167}
168
Victor Stinnera7368ac2017-11-15 18:11:45 -0800169
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200170static const char*
Victor Stinner9cfc0022017-12-20 19:36:46 +0100171config_get_env_var(const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -0800172{
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200173 const char *var = Py_GETENV(name);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800174 if (var && var[0] != '\0') {
175 return var;
176 }
177 else {
178 return NULL;
179 }
180}
181
182
Victor Stinnerca719ac2017-12-20 18:00:19 +0100183static int
184config_get_env_var_dup(wchar_t **dest, wchar_t *wname, char *name)
185{
186 if (Py_IgnoreEnvironmentFlag) {
187 *dest = NULL;
188 return 0;
189 }
190
191#ifdef MS_WINDOWS
192 const wchar_t *var = _wgetenv(wname);
193 if (!var || var[0] == '\0') {
194 *dest = NULL;
195 return 0;
196 }
197
198 wchar_t *copy = _PyMem_RawWcsdup(var);
199 if (copy == NULL) {
200 return -1;
201 }
202
203 *dest = copy;
204#else
205 const char *var = getenv(name);
206 if (!var || var[0] == '\0') {
207 *dest = NULL;
208 return 0;
209 }
210
211 size_t len;
212 wchar_t *wvar = Py_DecodeLocale(var, &len);
213 if (!wvar) {
214 if (len == (size_t)-2) {
215 return -2;
216 }
217 else {
218 return -1;
219 }
220 }
221 *dest = wvar;
222#endif
223 return 0;
224}
225
226
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800227static void
Victor Stinner33c377e2017-12-05 15:12:41 +0100228pymain_run_startup(PyCompilerFlags *cf)
Martin v. Löwis6caea372003-11-18 19:46:25 +0000229{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100230 const char *startup = config_get_env_var("PYTHONSTARTUP");
Victor Stinner6bf992a2017-12-06 17:26:10 +0100231 if (startup == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800232 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800234
235 FILE *fp = _Py_fopen(startup, "r");
236 if (fp == NULL) {
237 int save_errno = errno;
238 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
239 errno = save_errno;
240
241 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
242 startup);
243 PyErr_Print();
244 PyErr_Clear();
245 return;
246 }
247
248 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
249 PyErr_Clear();
250 fclose(fp);
Martin v. Löwis6caea372003-11-18 19:46:25 +0000251}
252
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800253static void
254pymain_run_interactive_hook(void)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200255{
256 PyObject *sys, *hook, *result;
257 sys = PyImport_ImportModule("sys");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800258 if (sys == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200259 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800260 }
261
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200262 hook = PyObject_GetAttrString(sys, "__interactivehook__");
263 Py_DECREF(sys);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800264 if (hook == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200265 PyErr_Clear();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800266 return;
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200267 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800268
269 result = _PyObject_CallNoArg(hook);
270 Py_DECREF(hook);
271 if (result == NULL) {
272 goto error;
273 }
274 Py_DECREF(result);
275
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200276 return;
277
278error:
279 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
280 PyErr_Print();
281 PyErr_Clear();
282}
283
Thomas Woutersa9773292006-04-21 09:43:23 +0000284
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800285static int
Victor Stinnerc4bca952017-12-19 23:48:17 +0100286pymain_run_module(const wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 PyObject *module, *runpy, *runmodule, *runargs, *result;
289 runpy = PyImport_ImportModule("runpy");
290 if (runpy == NULL) {
291 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200292 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 return -1;
294 }
295 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
296 if (runmodule == NULL) {
297 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200298 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 Py_DECREF(runpy);
300 return -1;
301 }
302 module = PyUnicode_FromWideChar(modname, wcslen(modname));
303 if (module == NULL) {
304 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200305 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_DECREF(runpy);
307 Py_DECREF(runmodule);
308 return -1;
309 }
310 runargs = Py_BuildValue("(Oi)", module, set_argv0);
311 if (runargs == NULL) {
312 fprintf(stderr,
313 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200314 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 Py_DECREF(runpy);
316 Py_DECREF(runmodule);
317 Py_DECREF(module);
318 return -1;
319 }
320 result = PyObject_Call(runmodule, runargs, NULL);
321 if (result == NULL) {
322 PyErr_Print();
323 }
324 Py_DECREF(runpy);
325 Py_DECREF(runmodule);
326 Py_DECREF(module);
327 Py_DECREF(runargs);
328 if (result == NULL) {
329 return -1;
330 }
331 Py_DECREF(result);
332 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000333}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000334
Nick Coghland2977a32017-03-12 20:38:32 +1000335static PyObject *
Victor Stinnerc4bca952017-12-19 23:48:17 +0100336pymain_get_importer(const wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000337{
Nick Coghland2977a32017-03-12 20:38:32 +1000338 PyObject *sys_path0 = NULL, *importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000339
Nick Coghland2977a32017-03-12 20:38:32 +1000340 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800341 if (sys_path0 == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000342 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800343 }
Victor Stinner4726e402010-10-06 23:24:57 +0000344
Nick Coghland2977a32017-03-12 20:38:32 +1000345 importer = PyImport_GetImporter(sys_path0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800346 if (importer == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000347 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800348 }
Victor Stinner4726e402010-10-06 23:24:57 +0000349
Brett Cannonaa936422012-04-27 15:30:58 -0400350 if (importer == Py_None) {
Nick Coghland2977a32017-03-12 20:38:32 +1000351 Py_DECREF(sys_path0);
Victor Stinner4726e402010-10-06 23:24:57 +0000352 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000353 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800355
Victor Stinner4726e402010-10-06 23:24:57 +0000356 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000357 return sys_path0;
Victor Stinner4726e402010-10-06 23:24:57 +0000358
Nick Coghland2977a32017-03-12 20:38:32 +1000359error:
360 Py_XDECREF(sys_path0);
361 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
362 PyErr_Print();
363 PyErr_Clear();
364 return NULL;
365}
366
367
368static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800369pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
Victor Stinnera62207c2010-08-07 10:57:17 +0000370{
371 PyObject *unicode, *bytes;
372 int ret;
373
374 unicode = PyUnicode_FromWideChar(command, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800375 if (unicode == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000376 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800377 }
378
Victor Stinnera62207c2010-08-07 10:57:17 +0000379 bytes = PyUnicode_AsUTF8String(unicode);
380 Py_DECREF(unicode);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800381 if (bytes == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000382 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800383 }
384
Victor Stinnera62207c2010-08-07 10:57:17 +0000385 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
386 Py_DECREF(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800387 return (ret != 0);
Victor Stinnera62207c2010-08-07 10:57:17 +0000388
389error:
Victor Stinner398356b2010-08-18 22:23:22 +0000390 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000391 PyErr_Print();
392 return 1;
393}
394
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800395
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000396static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800397pymain_run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf)
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000398{
399 PyObject *unicode, *bytes = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200400 const char *filename_str;
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000401 int run;
402
403 /* call pending calls like signal handlers (SIGINT) */
404 if (Py_MakePendingCalls() == -1) {
405 PyErr_Print();
406 return 1;
407 }
408
409 if (filename) {
410 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
411 if (unicode != NULL) {
Victor Stinnere0f32682010-10-17 19:34:51 +0000412 bytes = PyUnicode_EncodeFSDefault(unicode);
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000413 Py_DECREF(unicode);
414 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800415 if (bytes != NULL) {
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000416 filename_str = PyBytes_AsString(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800417 }
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000418 else {
419 PyErr_Clear();
Victor Stinnere0f32682010-10-17 19:34:51 +0000420 filename_str = "<encoding error>";
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000421 }
422 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800423 else {
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000424 filename_str = "<stdin>";
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800425 }
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000426
427 run = PyRun_AnyFileExFlags(fp, filename_str, filename != NULL, p_cf);
428 Py_XDECREF(bytes);
429 return run != 0;
430}
431
Christian Heimes9cd17752007-11-18 19:35:23 +0000432
Guido van Rossum667d7041995-08-04 04:20:48 +0000433/* Main program */
434
Eric Snow6b4be192017-05-22 21:36:03 -0700435typedef struct {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100436 wchar_t **argv;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100437 int nwarnoption; /* Number of -W options */
438 wchar_t **warnoptions; /* -W options */
439 int nenv_warnoption; /* Number of PYTHONWARNINGS options */
440 wchar_t **env_warnoptions; /* PYTHONWARNINGS options */
Eric Snow6b4be192017-05-22 21:36:03 -0700441 int print_help; /* -h, -? options */
442 int print_version; /* -V option */
Victor Stinner6bf992a2017-12-06 17:26:10 +0100443 int bytes_warning; /* Py_BytesWarningFlag, -b */
444 int debug; /* Py_DebugFlag, -b, PYTHONDEBUG */
445 int inspect; /* Py_InspectFlag, -i, PYTHONINSPECT */
446 int interactive; /* Py_InteractiveFlag, -i */
447 int isolated; /* Py_IsolatedFlag, -I */
448 int optimization_level; /* Py_OptimizeFlag, -O, PYTHONOPTIMIZE */
449 int dont_write_bytecode; /* Py_DontWriteBytecodeFlag, -B, PYTHONDONTWRITEBYTECODE */
450 int no_user_site_directory; /* Py_NoUserSiteDirectory, -I, -s, PYTHONNOUSERSITE */
451 int no_site_import; /* Py_NoSiteFlag, -S */
452 int use_unbuffered_io; /* Py_UnbufferedStdioFlag, -u, PYTHONUNBUFFERED */
453 int verbosity; /* Py_VerboseFlag, -v, PYTHONVERBOSE */
454 int quiet_flag; /* Py_QuietFlag, -q */
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800455 const char *check_hash_pycs_mode; /* --check-hash-based-pycs */
Victor Stinner6bf992a2017-12-06 17:26:10 +0100456#ifdef MS_WINDOWS
457 int legacy_windows_fs_encoding; /* Py_LegacyWindowsFSEncodingFlag,
458 PYTHONLEGACYWINDOWSFSENCODING */
459 int legacy_windows_stdio; /* Py_LegacyWindowsStdioFlag,
460 PYTHONLEGACYWINDOWSSTDIO */
461#endif
Eric Snow6b4be192017-05-22 21:36:03 -0700462} _Py_CommandLineDetails;
463
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800464/* Structure used by Py_Main() to pass data to subfunctions */
465typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100466 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800467 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100468 int use_bytes_argv;
469 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100470 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100471
472 /* Exit status or "exit code": result of pymain_main() */
473 int status;
474 /* Error message if a function failed */
475 _PyInitError err;
476
Victor Stinner19760862017-12-20 01:41:59 +0100477 /* non-zero is stdin is a TTY or if -i option is used */
478 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100479 int skip_first_line; /* -x option */
480 wchar_t *filename; /* Trailing arg without -c or -m */
481 wchar_t *command; /* -c argument */
482 wchar_t *module; /* -m argument */
Victor Stinner19760862017-12-20 01:41:59 +0100483
Victor Stinner9cfc0022017-12-20 19:36:46 +0100484 _PyCoreConfig config;
Victor Stinner19760862017-12-20 01:41:59 +0100485
486 PyObject *main_importer_path;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800487} _PyMain;
488
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800489#define _PyMain_INIT \
Victor Stinner9cfc0022017-12-20 19:36:46 +0100490 {.config = _PyCoreConfig_INIT, \
Victor Stinnerd5dda982017-12-13 17:31:16 +0100491 .err = _Py_INIT_OK()}
492/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800493
494
Victor Stinner19760862017-12-20 01:41:59 +0100495/* Non-zero if filename, command (-c) or module (-m) is set
496 on the command line */
497#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100498 (pymain->command != NULL || pymain->filename != NULL \
499 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100500
501
Victor Stinnerca719ac2017-12-20 18:00:19 +0100502static wchar_t*
503pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800504{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100505 wchar_t *str2 = _PyMem_RawWcsdup(str);
506 if (str2 == NULL) {
507 pymain->err = _Py_INIT_NO_MEMORY();
508 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800509 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100510 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800511}
512
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100513
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800514static void
Victor Stinnerca719ac2017-12-20 18:00:19 +0100515clear_wstrlist(int len, wchar_t **list)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800516{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100517 for (int i=0; i < len; i++) {
518 PyMem_RawFree(list[i]);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100519 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100520 PyMem_RawFree(list);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100521}
522
523
524static int
Victor Stinnerca719ac2017-12-20 18:00:19 +0100525pymain_init_cmdline_argv(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100526{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100527 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100528
Victor Stinnerca719ac2017-12-20 18:00:19 +0100529 if (pymain->use_bytes_argv) {
530 /* +1 for a the NULL terminator */
531 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
532 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
533 if (argv == NULL) {
534 pymain->err = _Py_INIT_NO_MEMORY();
535 return -1;
536 }
537
538 for (int i = 0; i < pymain->argc; i++) {
539 size_t len;
540 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
541 if (arg == NULL) {
542 clear_wstrlist(i, argv);
543 pymain->err = DECODE_LOCALE_ERR("command line arguments",
544 (Py_ssize_t)len);
545 return -1;
546 }
547 argv[i] = arg;
548 }
549 argv[pymain->argc] = NULL;
550
551 cmdline->argv = argv;
552 }
553 else {
554 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100555 }
556
Victor Stinnerca719ac2017-12-20 18:00:19 +0100557 wchar_t *program;
558 if (pymain->argc >= 1 && cmdline->argv != NULL) {
559 program = cmdline->argv[0];
560 }
561 else {
562 program = L"";
563 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100564 pymain->config.program = pymain_wstrdup(pymain, program);
565 if (pymain->config.program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100566 return -1;
567 }
568
Victor Stinnerc4bca952017-12-19 23:48:17 +0100569 return 0;
570}
571
572
573static void
Victor Stinnerca719ac2017-12-20 18:00:19 +0100574pymain_clear_cmdline(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100575{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100576 PyMemAllocatorEx old_alloc;
577 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100578
Victor Stinnerca719ac2017-12-20 18:00:19 +0100579 clear_wstrlist(cmdline->nwarnoption, cmdline->warnoptions);
580 cmdline->nwarnoption = 0;
581 cmdline->warnoptions = NULL;
582
583 clear_wstrlist(cmdline->nenv_warnoption, cmdline->env_warnoptions);
584 cmdline->nenv_warnoption = 0;
585 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100586
587 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100588 clear_wstrlist(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100589 }
590 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100591
592 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
593}
594
595
596static void
597pymain_clear_pymain(_PyMain *pymain)
598{
599#define CLEAR(ATTR) \
600 do { \
601 PyMem_RawFree(ATTR); \
602 ATTR = NULL; \
603 } while (0)
604
605 CLEAR(pymain->filename);
606 CLEAR(pymain->command);
607 CLEAR(pymain->module);
608#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100609}
610
Victor Stinnerc4bca952017-12-19 23:48:17 +0100611static void
Victor Stinner9cfc0022017-12-20 19:36:46 +0100612pymain_clear_config(_PyMain *pymain)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100613{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100614 /* Clear core config with the memory allocator
615 used by pymain_read_conf() */
616 PyMemAllocatorEx old_alloc;
617 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
618
Victor Stinner9cfc0022017-12-20 19:36:46 +0100619 _PyCoreConfig_Clear(&pymain->config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100620
621 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
622}
623
624
625static void
626pymain_free_python(_PyMain *pymain)
627{
628 Py_CLEAR(pymain->main_importer_path);
Victor Stinnerd4341102017-11-23 00:12:09 +0100629
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800630#ifdef __INSURE__
631 /* Insure++ is a memory analysis tool that aids in discovering
632 * memory leaks and other memory problems. On Python exit, the
633 * interned string dictionaries are flagged as being in use at exit
634 * (which it is). Under normal circumstances, this is fine because
635 * the memory will be automatically reclaimed by the system. Under
636 * memory debugging, it's a huge source of useless noise, so we
637 * trade off slower shutdown for less distraction in the memory
638 * reports. -baw
639 */
640 _Py_ReleaseInternedUnicodeStrings();
641#endif /* __INSURE__ */
642}
643
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100644
645static void
Victor Stinnerc4bca952017-12-19 23:48:17 +0100646pymain_free_raw(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100647{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100648 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100649
Victor Stinnerc4bca952017-12-19 23:48:17 +0100650 /* Free global variables which cannot be freed in Py_Finalize():
651 configuration options set before Py_Initialize() which should
652 remain valid after Py_Finalize(), since
653 Py_Initialize()-Py_Finalize() can be called multiple times. */
654 _PyPathConfig_Clear(&_Py_path_config);
Victor Stinner94540602017-12-16 04:54:22 +0100655
Victor Stinner31e99082017-12-20 23:41:38 +0100656 pymain_clear_config(pymain);
657
Victor Stinnerc4bca952017-12-19 23:48:17 +0100658 /* Force the allocator used by pymain_read_conf() */
659 PyMemAllocatorEx old_alloc;
660 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100661
Victor Stinnerca719ac2017-12-20 18:00:19 +0100662 pymain_clear_pymain(pymain);
663
664 clear_wstrlist(orig_argc, orig_argv);
665 orig_argc = 0;
666 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100667
Victor Stinnerc4bca952017-12-19 23:48:17 +0100668 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100669}
670
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100671
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800672static void
673pymain_free(_PyMain *pymain)
674{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100675 pymain_free_python(pymain);
676 pymain_free_raw(pymain);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800677}
678
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100679
Eric Snow6b4be192017-05-22 21:36:03 -0700680static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800681pymain_run_main_from_importer(_PyMain *pymain)
Guido van Rossum667d7041995-08-04 04:20:48 +0000682{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800683 /* Assume sys_path0 has already been checked by pymain_get_importer(),
684 * so put it in sys.path[0] and import __main__ */
Victor Stinner11a247d2017-12-13 21:05:57 +0100685 PyObject *sys_path = PySys_GetObject("path");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800686 if (sys_path == NULL) {
687 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
688 goto error;
689 }
690
Victor Stinner11a247d2017-12-13 21:05:57 +0100691 if (PyList_Insert(sys_path, 0, pymain->main_importer_path)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800692 goto error;
693 }
694
Victor Stinner11a247d2017-12-13 21:05:57 +0100695 int sts = pymain_run_module(L"__main__", 0);
696 return (sts != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800697
698error:
699 Py_CLEAR(pymain->main_importer_path);
700 PyErr_Print();
701 return 1;
702}
703
704
Victor Stinner9cfc0022017-12-20 19:36:46 +0100705static _PyInitError
706wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800707{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100708 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800709 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100710 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800711 }
712
Victor Stinnerca719ac2017-12-20 18:00:19 +0100713 size_t size = (*len + 1) * sizeof(list[0]);
714 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
715 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800716 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100717 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800718 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100719 list2[*len] = str2;
720 *list = list2;
721 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100722 return _Py_INIT_OK();
723}
724
725
726static int
727pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
728{
729 _PyInitError err = wstrlist_append(len, list, str);
730 if (_Py_INIT_FAILED(err)) {
731 pymain->err = err;
732 return -1;
733 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800734 return 0;
735}
736
737
738/* Parse the command line arguments
739 Return 0 on success.
740 Return 1 if parsing failed.
741 Set pymain->err and return -1 on other errors. */
742static int
Victor Stinnerca719ac2017-12-20 18:00:19 +0100743pymain_parse_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800744{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100745 _PyCoreConfig *config = &pymain->config;
746
Antoine Pitrou86838b02012-02-21 19:03:47 +0100747 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800748 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800749 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100750 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800751 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800752 if (c == EOF) {
753 break;
754 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 /* -c is the last option; following arguments
758 that look like options are left for the
759 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800760 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
761 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
762 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100763 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800764 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800765 }
Miss Islington (bot)c6de46e2018-05-31 06:43:21 -0700766 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 command[len - 2] = '\n';
768 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100769 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 break;
771 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 if (c == 'm') {
774 /* -m is the last option; following arguments
775 that look like options are left for the
776 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100777 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
778 if (pymain->module == NULL) {
779 return -1;
780 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 break;
782 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800785 case 0:
786 // Handle long option.
787 assert(longindex == 0); // Only one long option now.
788 if (!wcscmp(_PyOS_optarg, L"always")) {
789 cmdline->check_hash_pycs_mode = "always";
790 } else if (!wcscmp(_PyOS_optarg, L"never")) {
791 cmdline->check_hash_pycs_mode = "never";
792 } else if (!wcscmp(_PyOS_optarg, L"default")) {
793 cmdline->check_hash_pycs_mode = "default";
794 } else {
795 fprintf(stderr, "--check-hash-based-pycs must be one of "
796 "'default', 'always', or 'never'\n");
797 return 1;
798 }
799 break;
800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 case 'b':
Eric Snow6b4be192017-05-22 21:36:03 -0700802 cmdline->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 case 'd':
Eric Snow6b4be192017-05-22 21:36:03 -0700806 cmdline->debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 case 'i':
Eric Snow6b4be192017-05-22 21:36:03 -0700810 cmdline->inspect++;
811 cmdline->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000813
Christian Heimesad73a9c2013-08-10 16:36:18 +0200814 case 'I':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100815 config->ignore_environment++;
Eric Snow6b4be192017-05-22 21:36:03 -0700816 cmdline->isolated++;
817 cmdline->no_user_site_directory++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200818 break;
819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 case 'O':
Eric Snow6b4be192017-05-22 21:36:03 -0700823 cmdline->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 case 'B':
Eric Snow6b4be192017-05-22 21:36:03 -0700827 cmdline->dont_write_bytecode++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 case 's':
Eric Snow6b4be192017-05-22 21:36:03 -0700831 cmdline->no_user_site_directory++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 case 'S':
Eric Snow6b4be192017-05-22 21:36:03 -0700835 cmdline->no_site_import++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 case 'E':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100839 config->ignore_environment++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 case 't':
843 /* ignored for backwards compatibility */
844 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 case 'u':
Eric Snow6b4be192017-05-22 21:36:03 -0700847 cmdline->use_unbuffered_io = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 case 'v':
Eric Snow6b4be192017-05-22 21:36:03 -0700851 cmdline->verbosity++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100855 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 case 'h':
859 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -0700860 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -0700864 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100868 if (pymain_wstrlist_append(pymain,
869 &cmdline->nwarnoption,
870 &cmdline->warnoptions,
871 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800872 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800873 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000875
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000876 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100877 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +0100878 &config->nxoption,
879 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +0100880 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800881 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800882 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000883 break;
884
Georg Brandl9d871192010-12-04 10:47:18 +0000885 case 'q':
Eric Snow6b4be192017-05-22 21:36:03 -0700886 cmdline->quiet_flag++;
Georg Brandl9d871192010-12-04 10:47:18 +0000887 break;
888
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100889 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100890 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100891 break;
892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800896 /* unknown argument: parsing failed */
897 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800899 } while (1);
900
Victor Stinnerca719ac2017-12-20 18:00:19 +0100901 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800902 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +0100903 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800904 {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100905 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
906 if (pymain->filename == NULL) {
907 return -1;
908 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000910
Victor Stinnerd5dda982017-12-13 17:31:16 +0100911 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100912 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +0100913
Eric Snow6b4be192017-05-22 21:36:03 -0700914 return 0;
915}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000916
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800917
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800918static int
Victor Stinner9cfc0022017-12-20 19:36:46 +0100919add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +0100920{
921 PyObject *name, *value;
922
923 const wchar_t *name_end = wcschr(s, L'=');
924 if (!name_end) {
925 name = PyUnicode_FromWideChar(s, -1);
926 value = Py_True;
927 Py_INCREF(value);
928 }
929 else {
930 name = PyUnicode_FromWideChar(s, name_end - s);
931 value = PyUnicode_FromWideChar(name_end + 1, -1);
932 }
933 if (name == NULL || value == NULL) {
934 goto error;
935 }
936 if (PyDict_SetItem(opts, name, value) < 0) {
937 goto error;
938 }
939 Py_DECREF(name);
940 Py_DECREF(value);
941 return 0;
942
943error:
944 Py_XDECREF(name);
945 Py_XDECREF(value);
946 return -1;
947}
948
Victor Stinner9cfc0022017-12-20 19:36:46 +0100949
950static PyObject*
951config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800952{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100953 int nxoption = config->nxoption;
954 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +0100955 PyObject *dict = PyDict_New();
956 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100957 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +0100958 }
959
Victor Stinnerca719ac2017-12-20 18:00:19 +0100960 for (int i=0; i < nxoption; i++) {
961 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +0100962 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +0100963 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100964 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800965 }
966 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100967
Victor Stinner9cfc0022017-12-20 19:36:46 +0100968 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -0700969}
970
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800971
Victor Stinner9cfc0022017-12-20 19:36:46 +0100972static _PyInitError
973config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -0700974{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100975 for (int i = 0; i < len; i++) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100976 _PyInitError err = wstrlist_append(&config->nwarnoption,
977 &config->warnoptions,
978 options[i]);
979 if (_Py_INIT_FAILED(err)) {
980 return err;
Eric Snow6b4be192017-05-22 21:36:03 -0700981 }
Eric Snow6b4be192017-05-22 21:36:03 -0700982 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100983 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800984}
Eric Snow6b4be192017-05-22 21:36:03 -0700985
Victor Stinner747f48e2017-12-12 22:59:48 +0100986
Victor Stinner9cfc0022017-12-20 19:36:46 +0100987static _PyInitError
988config_init_warnoptions(_PyCoreConfig *config, _Py_CommandLineDetails *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +0100989{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100990 _PyInitError err;
991
992 assert(config->nwarnoption == 0);
993
Victor Stinner747f48e2017-12-12 22:59:48 +0100994 /* The priority order for warnings configuration is (highest precedence
995 * first):
996 *
997 * - the BytesWarning filter, if needed ('-b', '-bb')
998 * - any '-W' command line options; then
999 * - the 'PYTHONWARNINGS' environment variable; then
1000 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
1001 * - any implicit filters added by _warnings.c/warnings.py
1002 *
1003 * All settings except the last are passed to the warnings module via
1004 * the `sys.warnoptions` list. Since the warnings module works on the basis
1005 * of "the most recently added filter will be checked first", we add
1006 * the lowest precedence entries first so that later entries override them.
1007 */
1008
Victor Stinner9cfc0022017-12-20 19:36:46 +01001009 if (config->dev_mode) {
1010 err = wstrlist_append(&config->nwarnoption,
1011 &config->warnoptions,
1012 L"default");
1013 if (_Py_INIT_FAILED(err)) {
1014 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001015 }
Victor Stinner747f48e2017-12-12 22:59:48 +01001016 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001017
Victor Stinner9cfc0022017-12-20 19:36:46 +01001018 err = config_add_warnings_optlist(config,
1019 cmdline->nenv_warnoption,
1020 cmdline->env_warnoptions);
1021 if (_Py_INIT_FAILED(err)) {
1022 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001023 }
1024
Victor Stinner9cfc0022017-12-20 19:36:46 +01001025 err = config_add_warnings_optlist(config,
1026 cmdline->nwarnoption,
1027 cmdline->warnoptions);
1028 if (_Py_INIT_FAILED(err)) {
1029 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001030 }
1031
1032 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
1033 * don't even try to emit a warning, so we skip setting the filter in that
1034 * case.
1035 */
1036 if (cmdline->bytes_warning) {
1037 wchar_t *filter;
1038 if (cmdline->bytes_warning> 1) {
1039 filter = L"error::BytesWarning";
1040 }
1041 else {
1042 filter = L"default::BytesWarning";
1043 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001044 err = wstrlist_append(&config->nwarnoption,
1045 &config->warnoptions,
1046 filter);
1047 if (_Py_INIT_FAILED(err)) {
1048 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001049 }
1050 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001051 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001052}
1053
1054
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001055/* Get warning options from PYTHONWARNINGS environment variable.
1056 Return 0 on success.
1057 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001058static _PyInitError
1059cmdline_init_env_warnoptions(_Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001060{
1061 if (Py_IgnoreEnvironmentFlag) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001062 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +00001064
Victor Stinnerca719ac2017-12-20 18:00:19 +01001065 wchar_t *env;
1066 int res = config_get_env_var_dup(&env, L"PYTHONWARNINGS", "PYTHONWARNINGS");
1067 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001068 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001069 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001070
Victor Stinnerca719ac2017-12-20 18:00:19 +01001071 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001072 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001073 }
Philip Jenvey0805ca32010-04-07 04:04:10 +00001074
Victor Stinnerca719ac2017-12-20 18:00:19 +01001075
1076 wchar_t *warning, *context = NULL;
1077 for (warning = WCSTOK(env, L",", &context);
1078 warning != NULL;
1079 warning = WCSTOK(NULL, L",", &context))
1080 {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001081 _PyInitError err = wstrlist_append(&cmdline->nenv_warnoption,
1082 &cmdline->env_warnoptions,
1083 warning);
1084 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001085 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001086 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001087 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001089 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001090 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001091}
1092
1093
1094static void
1095pymain_init_stdio(_PyMain *pymain)
1096{
1097 pymain->stdin_is_interactive = (isatty(fileno(stdin))
1098 || Py_InteractiveFlag);
Guido van Rossum775af911997-02-14 19:50:32 +00001099
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +00001100#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +00001101 /* don't translate newlines (\r\n <=> \n) */
1102 _setmode(fileno(stdin), O_BINARY);
1103 _setmode(fileno(stdout), O_BINARY);
1104 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +00001105#endif
Victor Stinner89e34362011-01-07 18:47:22 +00001106
1107 if (Py_UnbufferedStdioFlag) {
Guido van Rossum22ffac11998-03-06 15:30:39 +00001108#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
1110 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
1111 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001112#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 setbuf(stdin, (char *)NULL);
1114 setbuf(stdout, (char *)NULL);
1115 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001116#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 }
1118 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +00001119#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 /* Doesn't have to have line-buffered -- use unbuffered */
1121 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
1122 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001123#else /* !MS_WINDOWS */
1124#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
1126 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001127#endif /* HAVE_SETVBUF */
1128#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 /* Leave stderr alone - it should be unbuffered anyway. */
1130 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001131}
Guido van Rossum667d7041995-08-04 04:20:48 +00001132
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001133
1134/* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
Victor Stinner31a83932017-12-04 13:39:15 +01001135 environment variables on macOS if available. */
1136static _PyInitError
Victor Stinnerca719ac2017-12-20 18:00:19 +01001137config_init_program_name(_PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001138{
Victor Stinner31a83932017-12-04 13:39:15 +01001139 assert(config->program_name == NULL);
1140
1141 /* If Py_SetProgramName() was called, use its value */
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001142 const wchar_t *program_name = _Py_path_config.program_name;
Victor Stinner31a83932017-12-04 13:39:15 +01001143 if (program_name != NULL) {
1144 config->program_name = _PyMem_RawWcsdup(program_name);
1145 if (config->program_name == NULL) {
1146 return _Py_INIT_NO_MEMORY();
1147 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001148 return _Py_INIT_OK();
Victor Stinner31a83932017-12-04 13:39:15 +01001149 }
1150
Just van Rossum2ac79ef2003-03-05 15:46:54 +00001151#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 /* On MacOS X, when the Python interpreter is embedded in an
1153 application bundle, it gets executed by a bootstrapping script
1154 that does os.execve() with an argv[0] that's different from the
1155 actual Python executable. This is needed to keep the Finder happy,
1156 or rather, to work around Apple's overly strict requirements of
1157 the process name. However, we still need a usable sys.executable,
1158 so the actual executable path is passed in an environment variable.
1159 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
1160 script. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001161 const char *p = config_get_env_var("PYTHONEXECUTABLE");
Victor Stinner6bf992a2017-12-06 17:26:10 +01001162 if (p != NULL) {
Victor Stinner31a83932017-12-04 13:39:15 +01001163 size_t len;
1164 wchar_t* program_name = Py_DecodeLocale(p, &len);
1165 if (program_name == NULL) {
1166 return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment "
1167 "variable", (Py_ssize_t)len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 }
Victor Stinner31a83932017-12-04 13:39:15 +01001169 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001170 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001171 }
Vinay Sajip90db6612012-07-17 17:33:46 +01001172#ifdef WITH_NEXT_FRAMEWORK
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001173 else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001174 const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
Vinay Sajip90db6612012-07-17 17:33:46 +01001175 if (pyvenv_launcher && *pyvenv_launcher) {
1176 /* Used by Mac/Tools/pythonw.c to forward
1177 * the argv0 of the stub executable
1178 */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001179 size_t len;
Victor Stinner31a83932017-12-04 13:39:15 +01001180 wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len);
1181 if (program_name == NULL) {
1182 return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment "
1183 "variable", (Py_ssize_t)len);
Vinay Sajip90db6612012-07-17 17:33:46 +01001184 }
Victor Stinner31a83932017-12-04 13:39:15 +01001185 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001186 return _Py_INIT_OK();
Vinay Sajip90db6612012-07-17 17:33:46 +01001187 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001189#endif /* WITH_NEXT_FRAMEWORK */
1190#endif /* __APPLE__ */
Victor Stinneraf5a8952017-12-02 10:11:32 +01001191
Victor Stinnerca719ac2017-12-20 18:00:19 +01001192 /* Use argv[0] by default, if available */
1193 if (config->program != NULL) {
1194 config->program_name = _PyMem_RawWcsdup(config->program);
1195 if (config->program_name == NULL) {
1196 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001197 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001198 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001199 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001200
1201 /* Last fall back: hardcoded string */
1202#ifdef MS_WINDOWS
1203 const wchar_t *default_program_name = L"python";
1204#else
1205 const wchar_t *default_program_name = L"python3";
1206#endif
1207 config->program_name = _PyMem_RawWcsdup(default_program_name);
1208 if (config->program_name == NULL) {
1209 return _Py_INIT_NO_MEMORY();
1210 }
1211 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001212}
1213
1214
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001215static void
1216pymain_header(_PyMain *pymain)
1217{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001218 if (Py_QuietFlag) {
1219 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 }
Guido van Rossum393661d2001-08-31 17:40:15 +00001221
Victor Stinner19760862017-12-20 01:41:59 +01001222 if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001223 return;
1224 }
1225
1226 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
1227 if (!Py_NoSiteFlag) {
1228 fprintf(stderr, "%s\n", COPYRIGHT);
1229 }
1230}
1231
1232
Victor Stinnerc4bca952017-12-19 23:48:17 +01001233static wchar_t**
Victor Stinnerca719ac2017-12-20 18:00:19 +01001234copy_wstrlist(int len, wchar_t **list)
Victor Stinner11a247d2017-12-13 21:05:57 +01001235{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001236 assert((len > 0 && list != NULL) || len == 0);
1237 size_t size = len * sizeof(list[0]);
1238 wchar_t **list_copy = PyMem_RawMalloc(size);
Alexey Izbyshev388bd4b2018-08-25 19:46:58 +03001239 if (list_copy == NULL) {
1240 return NULL;
1241 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001242 for (int i=0; i < len; i++) {
1243 wchar_t* arg = _PyMem_RawWcsdup(list[i]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001244 if (arg == NULL) {
Alexey Izbyshev388bd4b2018-08-25 19:46:58 +03001245 clear_wstrlist(i, list_copy);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001246 return NULL;
1247 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001248 list_copy[i] = arg;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001249 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001250 return list_copy;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001251}
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001252
Victor Stinnerc4bca952017-12-19 23:48:17 +01001253
1254static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001255pymain_init_core_argv(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001256{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001257 /* Copy argv to be able to modify it (to force -c/-m) */
1258 int argc = pymain->argc - _PyOS_optind;
1259 wchar_t **argv;
1260
1261 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +01001262 /* Ensure at least one (empty) argument is seen */
1263 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +01001264 argc = 1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001265 argv = copy_wstrlist(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +01001266 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001267 else {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001268 argv = copy_wstrlist(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001269 }
1270
1271 if (argv == NULL) {
1272 pymain->err = _Py_INIT_NO_MEMORY();
1273 return -1;
1274 }
1275
1276 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001277 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001278 /* Force sys.argv[0] = '-c' */
1279 arg0 = L"-c";
1280 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001281 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001282 /* Force sys.argv[0] = '-m'*/
1283 arg0 = L"-m";
1284 }
1285 if (arg0 != NULL) {
1286 arg0 = _PyMem_RawWcsdup(arg0);
1287 if (arg0 == NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001288 clear_wstrlist(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001289 pymain->err = _Py_INIT_NO_MEMORY();
1290 return -1;
1291 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001292
1293 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001294 PyMem_RawFree(argv[0]);
1295 argv[0] = arg0;
1296 }
1297
Victor Stinner9cfc0022017-12-20 19:36:46 +01001298 pymain->config.argc = argc;
1299 pymain->config.argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001300 return 0;
1301}
1302
1303
Victor Stinner8ded5b82018-01-24 17:03:28 +01001304static PyObject*
1305wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001306{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001307 assert(list != NULL || len < 1);
1308
1309 PyObject *pylist = PyList_New(len);
1310 if (pylist == NULL) {
1311 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001312 }
1313
Victor Stinner8ded5b82018-01-24 17:03:28 +01001314 for (int i = 0; i < len; i++) {
1315 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001316 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001317 Py_DECREF(pylist);
1318 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001319 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001320 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001321 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001322 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001323}
1324
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001325
Victor Stinner11a247d2017-12-13 21:05:57 +01001326static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01001327pymain_compute_path0(_PyMain *pymain, PyObject **path0)
Victor Stinner11a247d2017-12-13 21:05:57 +01001328{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001329 if (pymain->main_importer_path != NULL) {
1330 /* Let pymain_run_main_from_importer() adjust sys.path[0] later */
Victor Stinner19760862017-12-20 01:41:59 +01001331 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001332 return 0;
1333 }
1334
1335 if (Py_IsolatedFlag) {
Victor Stinner19760862017-12-20 01:41:59 +01001336 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001337 return 0;
1338 }
1339
Victor Stinner9cfc0022017-12-20 19:36:46 +01001340 *path0 = _PyPathConfig_ComputeArgv0(pymain->config.argc,
1341 pymain->config.argv);
Victor Stinner19760862017-12-20 01:41:59 +01001342 if (*path0 == NULL) {
1343 pymain->err = _Py_INIT_NO_MEMORY();
1344 return -1;
1345 }
1346 return 0;
1347}
1348
1349
1350static int
1351pymain_update_sys_path(_PyMain *pymain, PyObject *path0)
1352{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001353 /* Prepend argv[0] to sys.path.
1354 If argv[0] is a symlink, use the real path. */
1355 PyObject *sys_path = PySys_GetObject("path");
1356 if (sys_path == NULL) {
1357 pymain->err = _Py_INIT_ERR("can't get sys.path");
Victor Stinnerd5dda982017-12-13 17:31:16 +01001358 return -1;
1359 }
1360
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001361 /* Prepend path0 to sys.path */
1362 if (PyList_Insert(sys_path, 0, path0) < 0) {
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001363 pymain->err = _Py_INIT_ERR("sys.path.insert(0, path0) failed");
1364 return -1;
1365 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001366 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001367}
1368
1369
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001370void
1371_PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config)
1372{
1373#define COPY_FLAG(ATTR, VALUE) \
1374 if (config->ATTR == -1) { \
1375 config->ATTR = VALUE; \
1376 }
1377
1378 COPY_FLAG(ignore_environment, Py_IgnoreEnvironmentFlag);
1379 COPY_FLAG(utf8_mode, Py_UTF8Mode);
1380
1381#undef COPY_FLAG
1382}
1383
1384
Victor Stinner6bf992a2017-12-06 17:26:10 +01001385/* Get Py_xxx global configuration variables */
1386static void
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001387cmdline_get_global_config(_Py_CommandLineDetails *cmdline)
Victor Stinner6bf992a2017-12-06 17:26:10 +01001388{
Victor Stinner91106cd2017-12-13 12:29:09 +01001389 cmdline->bytes_warning = Py_BytesWarningFlag;
1390 cmdline->debug = Py_DebugFlag;
1391 cmdline->inspect = Py_InspectFlag;
1392 cmdline->interactive = Py_InteractiveFlag;
1393 cmdline->isolated = Py_IsolatedFlag;
1394 cmdline->optimization_level = Py_OptimizeFlag;
1395 cmdline->dont_write_bytecode = Py_DontWriteBytecodeFlag;
1396 cmdline->no_user_site_directory = Py_NoUserSiteDirectory;
1397 cmdline->no_site_import = Py_NoSiteFlag;
1398 cmdline->use_unbuffered_io = Py_UnbufferedStdioFlag;
1399 cmdline->verbosity = Py_VerboseFlag;
1400 cmdline->quiet_flag = Py_QuietFlag;
1401#ifdef MS_WINDOWS
1402 cmdline->legacy_windows_fs_encoding = Py_LegacyWindowsFSEncodingFlag;
1403 cmdline->legacy_windows_stdio = Py_LegacyWindowsStdioFlag;
1404#endif
1405 cmdline->check_hash_pycs_mode = _Py_CheckHashBasedPycsMode ;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001406}
Victor Stinner91106cd2017-12-13 12:29:09 +01001407
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001408
1409void
1410_PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config)
1411{
1412 Py_IgnoreEnvironmentFlag = config->ignore_environment;
1413 Py_UTF8Mode = config->utf8_mode;
1414
1415 /* Random or non-zero hash seed */
1416 Py_HashRandomizationFlag = (config->use_hash_seed == 0 ||
1417 config->hash_seed != 0);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001418}
1419
1420
Victor Stinner19760862017-12-20 01:41:59 +01001421/* Set Py_xxx global configuration variables */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001422static void
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001423cmdline_set_global_config(_Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001424{
Victor Stinner91106cd2017-12-13 12:29:09 +01001425 Py_BytesWarningFlag = cmdline->bytes_warning;
1426 Py_DebugFlag = cmdline->debug;
1427 Py_InspectFlag = cmdline->inspect;
1428 Py_InteractiveFlag = cmdline->interactive;
1429 Py_IsolatedFlag = cmdline->isolated;
1430 Py_OptimizeFlag = cmdline->optimization_level;
1431 Py_DontWriteBytecodeFlag = cmdline->dont_write_bytecode;
1432 Py_NoUserSiteDirectory = cmdline->no_user_site_directory;
1433 Py_NoSiteFlag = cmdline->no_site_import;
1434 Py_UnbufferedStdioFlag = cmdline->use_unbuffered_io;
1435 Py_VerboseFlag = cmdline->verbosity;
1436 Py_QuietFlag = cmdline->quiet_flag;
1437 _Py_CheckHashBasedPycsMode = cmdline->check_hash_pycs_mode;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001438#ifdef MS_WINDOWS
Victor Stinner91106cd2017-12-13 12:29:09 +01001439 Py_LegacyWindowsFSEncodingFlag = cmdline->legacy_windows_fs_encoding;
1440 Py_LegacyWindowsStdioFlag = cmdline->legacy_windows_stdio;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001441#endif
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001442}
1443
1444
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001445static void
1446pymain_import_readline(_PyMain *pymain)
1447{
1448 if (Py_IsolatedFlag) {
1449 return;
1450 }
Victor Stinner19760862017-12-20 01:41:59 +01001451 if (!Py_InspectFlag && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001452 return;
1453 }
1454 if (!isatty(fileno(stdin))) {
1455 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001456 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001457
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001458 PyObject *mod = PyImport_ImportModule("readline");
1459 if (mod == NULL) {
1460 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 }
1462 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001463 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001465}
1466
1467
1468static FILE*
1469pymain_open_filename(_PyMain *pymain)
1470{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001471 FILE* fp;
1472
Victor Stinnerca719ac2017-12-20 18:00:19 +01001473 fp = _Py_wfopen(pymain->filename, L"r");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001474 if (fp == NULL) {
1475 char *cfilename_buffer;
1476 const char *cfilename;
1477 int err = errno;
Victor Stinner9dd76202017-12-21 16:20:32 +01001478 cfilename_buffer = _Py_EncodeLocaleRaw(pymain->filename, NULL);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001479 if (cfilename_buffer != NULL)
1480 cfilename = cfilename_buffer;
1481 else
1482 cfilename = "<unprintable file name>";
1483 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinner9cfc0022017-12-20 19:36:46 +01001484 pymain->config.program, cfilename, err, strerror(err));
Victor Stinner9dd76202017-12-21 16:20:32 +01001485 PyMem_RawFree(cfilename_buffer);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001486 pymain->status = 2;
1487 return NULL;
1488 }
1489
Victor Stinnerca719ac2017-12-20 18:00:19 +01001490 if (pymain->skip_first_line) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001491 int ch;
1492 /* Push back first newline so line numbers
1493 remain the same */
1494 while ((ch = getc(fp)) != EOF) {
1495 if (ch == '\n') {
1496 (void)ungetc(ch, fp);
1497 break;
1498 }
1499 }
1500 }
1501
1502 struct _Py_stat_struct sb;
1503 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 &&
1504 S_ISDIR(sb.st_mode)) {
1505 fprintf(stderr,
1506 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinner9cfc0022017-12-20 19:36:46 +01001507 pymain->config.program, pymain->filename);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001508 fclose(fp);
1509 pymain->status = 1;
1510 return NULL;
1511 }
1512
1513 return fp;
1514}
1515
1516
1517static void
Victor Stinner19760862017-12-20 01:41:59 +01001518pymain_run_filename(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001519{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001520 if (pymain->filename == NULL && pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001521 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner19760862017-12-20 01:41:59 +01001522 pymain_run_startup(cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001523 pymain_run_interactive_hook();
1524 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001525
1526 if (pymain->main_importer_path != NULL) {
1527 pymain->status = pymain_run_main_from_importer(pymain);
1528 return;
1529 }
1530
1531 FILE *fp;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001532 if (pymain->filename != NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001533 fp = pymain_open_filename(pymain);
1534 if (fp == NULL) {
1535 return;
1536 }
1537 }
1538 else {
1539 fp = stdin;
1540 }
1541
Victor Stinnerca719ac2017-12-20 18:00:19 +01001542 pymain->status = pymain_run_file(fp, pymain->filename, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001543}
1544
1545
1546static void
Victor Stinner19760862017-12-20 01:41:59 +01001547pymain_repl(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001550 opportunity to set it from Python. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001551 if (!Py_InspectFlag && config_get_env_var("PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 Py_InspectFlag = 1;
1553 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001554
Victor Stinner19760862017-12-20 01:41:59 +01001555 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001556 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001558
1559 Py_InspectFlag = 0;
1560 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001561
Victor Stinner19760862017-12-20 01:41:59 +01001562 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001563 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001564}
1565
1566
1567/* Parse the command line.
1568 Handle --version and --help options directly.
1569
1570 Return 1 if Python must exit.
1571 Return 0 on success.
1572 Set pymain->err and return -1 on failure. */
1573static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001574pymain_parse_cmdline(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001575{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001576 int res = pymain_parse_cmdline_impl(pymain, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001577 if (res < 0) {
1578 return -1;
1579 }
1580 if (res) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001581 pymain_usage(1, pymain->config.program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001582 pymain->status = 2;
1583 return 1;
1584 }
1585
Victor Stinnerca719ac2017-12-20 18:00:19 +01001586 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001587 /* Backup _PyOS_optind */
1588 _PyOS_optind--;
1589 }
1590
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001591 return 0;
1592}
1593
1594
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001595static const wchar_t*
Victor Stinner9cfc0022017-12-20 19:36:46 +01001596config_get_xoption(_PyCoreConfig *config, wchar_t *name)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001597{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001598 int nxoption = config->nxoption;
1599 wchar_t **xoptions = config->xoptions;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001600 for (int i=0; i < nxoption; i++) {
1601 wchar_t *option = xoptions[i];
Victor Stinnera7368ac2017-11-15 18:11:45 -08001602 size_t len;
1603 wchar_t *sep = wcschr(option, L'=');
1604 if (sep != NULL) {
1605 len = (sep - option);
1606 }
1607 else {
1608 len = wcslen(option);
1609 }
1610 if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
1611 return option;
1612 }
1613 }
1614 return NULL;
1615}
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001616
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001617
Victor Stinnera7368ac2017-11-15 18:11:45 -08001618static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001619pymain_str_to_int(const char *str, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001620{
1621 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001622 const char *endptr = str;
1623 long value = strtol(str, (char **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001624 if (*endptr != '\0' || errno == ERANGE) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001625 return -1;
1626 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001627 if (value < INT_MIN || value > INT_MAX) {
1628 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001629 }
1630
Victor Stinnera7368ac2017-11-15 18:11:45 -08001631 *result = (int)value;
1632 return 0;
1633}
1634
1635
1636static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001637pymain_wstr_to_int(const wchar_t *wstr, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001638{
1639 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001640 const wchar_t *endptr = wstr;
1641 long value = wcstol(wstr, (wchar_t **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001642 if (*endptr != '\0' || errno == ERANGE) {
1643 return -1;
1644 }
1645 if (value < INT_MIN || value > INT_MAX) {
1646 return -1;
1647 }
1648
1649 *result = (int)value;
1650 return 0;
1651}
1652
1653
Victor Stinner9cfc0022017-12-20 19:36:46 +01001654static _PyInitError
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001655config_init_tracemalloc(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001656{
1657 int nframe;
1658 int valid;
1659
Victor Stinner9cfc0022017-12-20 19:36:46 +01001660 const char *env = config_get_env_var("PYTHONTRACEMALLOC");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001661 if (env) {
1662 if (!pymain_str_to_int(env, &nframe)) {
1663 valid = (nframe >= 1);
1664 }
1665 else {
1666 valid = 0;
1667 }
1668 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001669 return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number "
1670 "of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001671 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001672 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001673 }
1674
Victor Stinner9cfc0022017-12-20 19:36:46 +01001675 const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001676 if (xoption) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001677 const wchar_t *sep = wcschr(xoption, L'=');
Victor Stinnera7368ac2017-11-15 18:11:45 -08001678 if (sep) {
1679 if (!pymain_wstr_to_int(sep + 1, &nframe)) {
1680 valid = (nframe >= 1);
1681 }
1682 else {
1683 valid = 0;
1684 }
1685 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001686 return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: "
1687 "invalid number of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001688 }
1689 }
1690 else {
1691 /* -X tracemalloc behaves as -X tracemalloc=1 */
1692 nframe = 1;
1693 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001694 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001695 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001696 return _Py_INIT_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001697}
1698
1699
1700static void
Victor Stinner9cfc0022017-12-20 19:36:46 +01001701get_env_flag(int *flag, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001702{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001703 const char *var = config_get_env_var(name);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001704 if (!var) {
1705 return;
1706 }
1707 int value;
1708 if (pymain_str_to_int(var, &value) < 0 || value < 0) {
1709 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
1710 value = 1;
1711 }
1712 if (*flag < value) {
1713 *flag = value;
1714 }
1715}
1716
1717
1718static void
Victor Stinner9cfc0022017-12-20 19:36:46 +01001719cmdline_get_env_flags(_Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001720{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001721 get_env_flag(&cmdline->debug, "PYTHONDEBUG");
1722 get_env_flag(&cmdline->verbosity, "PYTHONVERBOSE");
1723 get_env_flag(&cmdline->optimization_level, "PYTHONOPTIMIZE");
1724 get_env_flag(&cmdline->inspect, "PYTHONINSPECT");
1725 get_env_flag(&cmdline->dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
1726 get_env_flag(&cmdline->no_user_site_directory, "PYTHONNOUSERSITE");
1727 get_env_flag(&cmdline->use_unbuffered_io, "PYTHONUNBUFFERED");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001728#ifdef MS_WINDOWS
Victor Stinner9cfc0022017-12-20 19:36:46 +01001729 get_env_flag(&cmdline->legacy_windows_fs_encoding,
1730 "PYTHONLEGACYWINDOWSFSENCODING");
1731 get_env_flag(&cmdline->legacy_windows_stdio,
1732 "PYTHONLEGACYWINDOWSSTDIO");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001733#endif
1734}
1735
1736
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001737/* Set global variable variables from environment variables */
1738void
1739_Py_Initialize_ReadEnvVarsNoAlloc(void)
1740{
1741 _Py_CommandLineDetails cmdline;
1742 memset(&cmdline, 0, sizeof(cmdline));
1743
1744 cmdline_get_global_config(&cmdline);
1745 if (cmdline.isolated) {
1746 Py_IgnoreEnvironmentFlag = 1;
1747 cmdline.no_user_site_directory = 1;
1748 }
1749 if (!Py_IgnoreEnvironmentFlag) {
1750 cmdline_get_env_flags(&cmdline);
1751 }
1752 cmdline_set_global_config(&cmdline);
1753
1754 /* no need to call pymain_clear_cmdline(), no memory has been allocated */
1755}
1756
1757
Victor Stinner46972b72017-11-24 22:55:40 +01001758static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001759config_init_home(_PyCoreConfig *config)
Victor Stinner1f151112017-11-23 10:43:14 +01001760{
1761 wchar_t *home;
1762
Victor Stinner31a83932017-12-04 13:39:15 +01001763 /* If Py_SetPythonHome() was called, use its value */
1764 home = _Py_path_config.home;
Victor Stinner1f151112017-11-23 10:43:14 +01001765 if (home) {
Victor Stinner46972b72017-11-24 22:55:40 +01001766 config->home = _PyMem_RawWcsdup(home);
1767 if (config->home == NULL) {
1768 return _Py_INIT_NO_MEMORY();
Victor Stinner1f151112017-11-23 10:43:14 +01001769 }
Victor Stinner46972b72017-11-24 22:55:40 +01001770 return _Py_INIT_OK();
Victor Stinner1f151112017-11-23 10:43:14 +01001771 }
1772
Victor Stinner46972b72017-11-24 22:55:40 +01001773 int res = config_get_env_var_dup(&home, L"PYTHONHOME", "PYTHONHOME");
Victor Stinner1f151112017-11-23 10:43:14 +01001774 if (res < 0) {
Victor Stinner46972b72017-11-24 22:55:40 +01001775 return DECODE_LOCALE_ERR("PYTHONHOME", res);
Victor Stinner1f151112017-11-23 10:43:14 +01001776 }
Victor Stinner46972b72017-11-24 22:55:40 +01001777 config->home = home;
1778 return _Py_INIT_OK();
Victor Stinnerd4341102017-11-23 00:12:09 +01001779}
1780
1781
Victor Stinner358e5e12017-12-15 00:51:22 +01001782static _PyInitError
1783config_init_hash_seed(_PyCoreConfig *config)
1784{
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001785 const char *seed_text = config_get_env_var("PYTHONHASHSEED");
1786 int use_hash_seed;
1787 unsigned long hash_seed;
1788 if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
1789 return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
1790 "or an integer in range [0; 4294967295]");
Victor Stinner358e5e12017-12-15 00:51:22 +01001791 }
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001792 config->use_hash_seed = use_hash_seed;
1793 config->hash_seed = hash_seed;
Victor Stinner358e5e12017-12-15 00:51:22 +01001794 return _Py_INIT_OK();
1795}
1796
1797
Victor Stinner9cfc0022017-12-20 19:36:46 +01001798static _PyInitError
1799config_init_utf8_mode(_PyCoreConfig *config)
Victor Stinner91106cd2017-12-13 12:29:09 +01001800{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001801 const wchar_t *xopt = config_get_xoption(config, L"utf8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001802 if (xopt) {
1803 wchar_t *sep = wcschr(xopt, L'=');
1804 if (sep) {
1805 xopt = sep + 1;
1806 if (wcscmp(xopt, L"1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001807 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001808 }
1809 else if (wcscmp(xopt, L"0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001810 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001811 }
1812 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001813 return _Py_INIT_USER_ERR("invalid -X utf8 option value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001814 }
1815 }
1816 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001817 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001818 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001819 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001820 }
1821
Victor Stinner9cfc0022017-12-20 19:36:46 +01001822 const char *opt = config_get_env_var("PYTHONUTF8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001823 if (opt) {
1824 if (strcmp(opt, "1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001825 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001826 }
1827 else if (strcmp(opt, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001828 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001829 }
1830 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001831 return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
1832 "variable value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001833 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001834 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001835 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001836
1837 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001838}
Victor Stinner46972b72017-11-24 22:55:40 +01001839
1840
Victor Stinner9cfc0022017-12-20 19:36:46 +01001841static _PyInitError
1842config_read_env_vars(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001843{
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001844 assert(!config->ignore_environment);
1845
1846 if (config->allocator == NULL) {
1847 config->allocator = config_get_env_var("PYTHONMALLOC");
1848 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001849
Victor Stinner9cfc0022017-12-20 19:36:46 +01001850 if (config_get_env_var("PYTHONDUMPREFS")) {
1851 config->dump_refs = 1;
1852 }
1853 if (config_get_env_var("PYTHONMALLOCSTATS")) {
1854 config->malloc_stats = 1;
Victor Stinner31a83932017-12-04 13:39:15 +01001855 }
1856
Victor Stinner98c49c62018-08-29 01:13:29 +02001857 const char *env = config_get_env_var("PYTHONCOERCECLOCALE");
1858 if (env) {
1859 if (strcmp(env, "0") == 0) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07001860 if (config->coerce_c_locale < 0) {
1861 config->coerce_c_locale = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001862 }
Victor Stinner98c49c62018-08-29 01:13:29 +02001863 }
1864 else if (strcmp(env, "warn") == 0) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07001865 config->coerce_c_locale_warn = 1;
Victor Stinner98c49c62018-08-29 01:13:29 +02001866 }
1867 else {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07001868 if (config->coerce_c_locale < 0) {
1869 config->coerce_c_locale = 1;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001870 }
Victor Stinner94540602017-12-16 04:54:22 +01001871 }
1872 }
1873
Victor Stinner9cfc0022017-12-20 19:36:46 +01001874 wchar_t *path;
1875 int res = config_get_env_var_dup(&path, L"PYTHONPATH", "PYTHONPATH");
1876 if (res < 0) {
Miss Islington (bot)6414da92018-05-19 16:14:42 -07001877 return DECODE_LOCALE_ERR("PYTHONPATH", res);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001878 }
1879 config->module_search_path_env = path;
1880
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001881 if (config->use_hash_seed < 0) {
1882 _PyInitError err = config_init_hash_seed(config);
1883 if (_Py_INIT_FAILED(err)) {
1884 return err;
1885 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001886 }
1887
1888 return _Py_INIT_OK();
1889}
1890
1891
1892static _PyInitError
1893config_read_complex_options(_PyCoreConfig *config)
1894{
1895 /* More complex options configured by env var and -X option */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001896 if (config->faulthandler < 0) {
1897 if (config_get_env_var("PYTHONFAULTHANDLER")
1898 || config_get_xoption(config, L"faulthandler")) {
1899 config->faulthandler = 1;
1900 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001901 }
1902 if (config_get_env_var("PYTHONPROFILEIMPORTTIME")
1903 || config_get_xoption(config, L"importtime")) {
1904 config->import_time = 1;
1905 }
1906 if (config_get_xoption(config, L"dev" ) ||
1907 config_get_env_var("PYTHONDEVMODE"))
1908 {
1909 config->dev_mode = 1;
Victor Stinner9cfc0022017-12-20 19:36:46 +01001910 }
1911
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001912 if (config->tracemalloc < 0) {
1913 _PyInitError err = config_init_tracemalloc(config);
1914 if (_Py_INIT_FAILED(err)) {
1915 return err;
1916 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001917 }
1918 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001919}
1920
1921
Victor Stinnera7368ac2017-11-15 18:11:45 -08001922/* Parse command line options and environment variables.
1923 This code must not use Python runtime apart PyMem_Raw memory allocator.
1924
1925 Return 0 on success.
1926 Return 1 if Python is done and must exit.
1927 Set pymain->err and return -1 on error. */
1928static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001929pymain_read_conf_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001930{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001931 _PyInitError err;
1932
Victor Stinnerca719ac2017-12-20 18:00:19 +01001933 int res = pymain_parse_cmdline(pymain, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01001934 if (res != 0) {
1935 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001936 }
1937
Victor Stinner9cfc0022017-12-20 19:36:46 +01001938 /* Set Py_IgnoreEnvironmentFlag for Py_GETENV() */
1939 _PyCoreConfig *config = &pymain->config;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001940 Py_IgnoreEnvironmentFlag = config->ignore_environment || cmdline->isolated;
Victor Stinner9cfc0022017-12-20 19:36:46 +01001941
1942 /* Get environment variables */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001943 if (!Py_IgnoreEnvironmentFlag) {
1944 cmdline_get_env_flags(cmdline);
1945 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001946
1947 err = cmdline_init_env_warnoptions(cmdline);
1948 if (_Py_INIT_FAILED(err)) {
1949 pymain->err = err;
1950 return -1;
1951 }
1952
1953#ifdef MS_WINDOWS
1954 if (cmdline->legacy_windows_fs_encoding) {
1955 config->utf8_mode = 0;
1956 }
1957#endif
1958
Victor Stinnerca719ac2017-12-20 18:00:19 +01001959 if (pymain_init_core_argv(pymain, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001960 return -1;
1961 }
1962
Victor Stinner2b822a02018-01-25 09:18:36 +01001963 /* On Windows, _PyPathConfig_Init() modifies Py_IsolatedFlag and
1964 Py_NoSiteFlag variables if a "._pth" file is found. */
1965 int init_isolated = Py_IsolatedFlag;
1966 int init_no_site = Py_NoSiteFlag;
1967 Py_IsolatedFlag = cmdline->isolated;
1968 Py_NoSiteFlag = cmdline->no_site_import;
Victor Stinner8ded5b82018-01-24 17:03:28 +01001969
Victor Stinner9cfc0022017-12-20 19:36:46 +01001970 err = _PyCoreConfig_Read(config);
Victor Stinner2b822a02018-01-25 09:18:36 +01001971
1972 cmdline->isolated = Py_IsolatedFlag;
1973 cmdline->no_site_import = Py_NoSiteFlag;
1974 Py_IsolatedFlag = init_isolated;
1975 Py_NoSiteFlag = init_no_site;
1976
Victor Stinner31a83932017-12-04 13:39:15 +01001977 if (_Py_INIT_FAILED(err)) {
1978 pymain->err = err;
1979 return -1;
1980 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001981 return 0;
1982}
1983
1984
Victor Stinner19760862017-12-20 01:41:59 +01001985/* Read the configuration, but initialize also the LC_CTYPE locale:
1986 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
Victor Stinnera7368ac2017-11-15 18:11:45 -08001987static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001988pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001989{
Victor Stinner80a0eba2018-08-23 12:41:35 +02001990 int init_utf8_mode = Py_UTF8Mode;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001991 _PyCoreConfig *config = &pymain->config;
1992 _PyCoreConfig save_config = _PyCoreConfig_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001993 int res = -1;
1994
Victor Stinner94540602017-12-16 04:54:22 +01001995 char *oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
1996 if (oldloc == NULL) {
1997 pymain->err = _Py_INIT_NO_MEMORY();
1998 goto done;
1999 }
2000
2001 /* Reconfigure the locale to the default for this process */
2002 _Py_SetLocaleFromEnv(LC_ALL);
2003
2004 int locale_coerced = 0;
2005 int loops = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002006 int init_ignore_env = config->ignore_environment;
2007
2008 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
2009 pymain->err = _Py_INIT_NO_MEMORY();
2010 goto done;
2011 }
Victor Stinner94540602017-12-16 04:54:22 +01002012
2013 while (1) {
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002014 int init_utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002015 int encoding_changed = 0;
2016
2017 /* Watchdog to prevent an infinite loop */
2018 loops++;
2019 if (loops == 3) {
2020 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
2021 "reading the configuration");
2022 goto done;
2023 }
2024
Victor Stinner80a0eba2018-08-23 12:41:35 +02002025 /* bpo-34207: Py_DecodeLocale(), Py_EncodeLocale() and similar
2026 functions depend on Py_UTF8Mode. */
2027 Py_UTF8Mode = config->utf8_mode;
2028
Victor Stinnerca719ac2017-12-20 18:00:19 +01002029 if (pymain_init_cmdline_argv(pymain, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01002030 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01002031 }
2032
Victor Stinner9cfc0022017-12-20 19:36:46 +01002033 int conf_res = pymain_read_conf_impl(pymain, cmdline);
2034 if (conf_res != 0) {
2035 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01002036 goto done;
2037 }
2038
2039 /* The legacy C locale assumes ASCII as the default text encoding, which
2040 * causes problems not only for the CPython runtime, but also other
2041 * components like GNU readline.
2042 *
2043 * Accordingly, when the CLI detects it, it attempts to coerce it to a
2044 * more capable UTF-8 based alternative.
2045 *
2046 * See the documentation of the PYTHONCOERCECLOCALE setting for more
2047 * details.
2048 */
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002049 if (config->coerce_c_locale && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01002050 locale_coerced = 1;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002051 _Py_CoerceLegacyLocale(config);
Victor Stinner94540602017-12-16 04:54:22 +01002052 encoding_changed = 1;
2053 }
2054
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002055 if (init_utf8_mode == -1) {
2056 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01002057 /* UTF-8 Mode enabled */
2058 encoding_changed = 1;
2059 }
2060 }
2061 else {
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002062 if (config->utf8_mode != init_utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01002063 encoding_changed = 1;
2064 }
2065 }
2066
2067 if (!encoding_changed) {
2068 break;
2069 }
2070
2071 /* Reset the configuration, except UTF-8 Mode. Set Py_UTF8Mode for
2072 Py_DecodeLocale(). Reset Py_IgnoreEnvironmentFlag, modified by
Victor Stinner8ded5b82018-01-24 17:03:28 +01002073 pymain_read_conf_impl(). Reset Py_IsolatedFlag and Py_NoSiteFlag
2074 modified by _PyCoreConfig_Read(). */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002075 int new_utf8_mode = config->utf8_mode;
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002076 int new_coerce_c_locale = config->coerce_c_locale;
Victor Stinner94540602017-12-16 04:54:22 +01002077 Py_IgnoreEnvironmentFlag = init_ignore_env;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002078 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
2079 pymain->err = _Py_INIT_NO_MEMORY();
2080 goto done;
2081 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002082 pymain_clear_cmdline(pymain, cmdline);
Miss Islington (bot)046da162018-06-15 15:26:29 -07002083 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002084
2085 cmdline_get_global_config(cmdline);
2086 _PyCoreConfig_GetGlobalConfig(config);
2087 config->utf8_mode = new_utf8_mode;
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002088 config->coerce_c_locale = new_coerce_c_locale;
Victor Stinner94540602017-12-16 04:54:22 +01002089
2090 /* The encoding changed: read again the configuration
2091 with the new encoding */
2092 }
2093 res = 0;
2094
2095done:
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002096 _PyCoreConfig_Clear(&save_config);
Victor Stinner94540602017-12-16 04:54:22 +01002097 if (oldloc != NULL) {
2098 setlocale(LC_ALL, oldloc);
2099 PyMem_RawFree(oldloc);
2100 }
Victor Stinner80a0eba2018-08-23 12:41:35 +02002101 Py_UTF8Mode = init_utf8_mode ;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002102 return res;
2103}
2104
Victor Stinner91106cd2017-12-13 12:29:09 +01002105
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002106static void
2107config_init_locale(_PyCoreConfig *config)
Victor Stinner9cfc0022017-12-20 19:36:46 +01002108{
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002109 if (config->coerce_c_locale < 0) {
Victor Stinner144f1e22018-09-17 18:01:39 -07002110 /* The C locale enables the C locale coercion (PEP 538) */
2111 if (_Py_LegacyLocaleDetected()) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002112 config->coerce_c_locale = 1;
Victor Stinner144f1e22018-09-17 18:01:39 -07002113 }
2114 }
2115
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002116#ifndef MS_WINDOWS
2117 if (config->utf8_mode < 0) {
2118 /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */
2119 const char *ctype_loc = setlocale(LC_CTYPE, NULL);
2120 if (ctype_loc != NULL
2121 && (strcmp(ctype_loc, "C") == 0
2122 || strcmp(ctype_loc, "POSIX") == 0))
2123 {
2124 config->utf8_mode = 1;
2125 }
2126 }
2127#endif
Victor Stinner9cfc0022017-12-20 19:36:46 +01002128}
2129
2130
Victor Stinner8ded5b82018-01-24 17:03:28 +01002131static _PyInitError
2132config_init_module_search_paths(_PyCoreConfig *config)
2133{
2134 assert(config->module_search_paths == NULL);
2135 assert(config->nmodule_search_path < 0);
2136
2137 config->nmodule_search_path = 0;
2138
2139 const wchar_t *sys_path = Py_GetPath();
2140 const wchar_t delim = DELIM;
2141 const wchar_t *p = sys_path;
2142 while (1) {
2143 p = wcschr(sys_path, delim);
2144 if (p == NULL) {
2145 p = sys_path + wcslen(sys_path); /* End of string */
2146 }
2147
2148 size_t path_len = (p - sys_path);
2149 wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t));
2150 if (path == NULL) {
2151 return _Py_INIT_NO_MEMORY();
2152 }
2153 memcpy(path, sys_path, path_len * sizeof(wchar_t));
2154 path[path_len] = L'\0';
2155
2156 _PyInitError err = wstrlist_append(&config->nmodule_search_path,
2157 &config->module_search_paths,
2158 path);
2159 PyMem_RawFree(path);
2160 if (_Py_INIT_FAILED(err)) {
2161 return err;
2162 }
2163
2164 if (*p == '\0') {
2165 break;
2166 }
2167 sys_path = p + 1;
2168 }
2169 return _Py_INIT_OK();
2170}
2171
2172
2173static _PyInitError
2174config_init_path_config(_PyCoreConfig *config)
2175{
2176 _PyInitError err = _PyPathConfig_Init(config);
2177 if (_Py_INIT_FAILED(err)) {
2178 return err;
2179 }
2180
2181 if (config->nmodule_search_path < 0) {
2182 err = config_init_module_search_paths(config);
2183 if (_Py_INIT_FAILED(err)) {
2184 return err;
2185 }
2186 }
2187
2188 if (config->executable == NULL) {
2189 config->executable = _PyMem_RawWcsdup(Py_GetProgramFullPath());
2190 if (config->executable == NULL) {
2191 return _Py_INIT_NO_MEMORY();
2192 }
2193 }
2194
2195 if (config->prefix == NULL) {
2196 config->prefix = _PyMem_RawWcsdup(Py_GetPrefix());
2197 if (config->prefix == NULL) {
2198 return _Py_INIT_NO_MEMORY();
2199 }
2200 }
2201
2202 if (config->exec_prefix == NULL) {
2203 config->exec_prefix = _PyMem_RawWcsdup(Py_GetExecPrefix());
2204 if (config->exec_prefix == NULL) {
2205 return _Py_INIT_NO_MEMORY();
2206 }
2207 }
2208
2209 if (config->base_prefix == NULL) {
2210 config->base_prefix = _PyMem_RawWcsdup(config->prefix);
2211 if (config->base_prefix == NULL) {
2212 return _Py_INIT_NO_MEMORY();
2213 }
2214 }
2215
2216 if (config->base_exec_prefix == NULL) {
2217 config->base_exec_prefix = _PyMem_RawWcsdup(config->exec_prefix);
2218 if (config->base_exec_prefix == NULL) {
2219 return _Py_INIT_NO_MEMORY();
2220 }
2221 }
2222
2223 return _Py_INIT_OK();
2224}
2225
Victor Stinnerda273412017-12-15 01:46:02 +01002226/* Read configuration settings from standard locations
2227 *
2228 * This function doesn't make any changes to the interpreter state - it
2229 * merely populates any missing configuration settings. This allows an
2230 * embedding application to completely override a config option by
2231 * setting it before calling this function, or else modify the default
2232 * setting before passing the fully populated config to Py_EndInitialization.
2233 *
2234 * More advanced selective initialization tricks are possible by calling
2235 * this function multiple times with various preconfigured settings.
2236 */
2237
2238_PyInitError
2239_PyCoreConfig_Read(_PyCoreConfig *config)
2240{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002241 _PyInitError err;
2242
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002243 _PyCoreConfig_GetGlobalConfig(config);
2244
2245 assert(config->ignore_environment >= 0);
2246 if (!config->ignore_environment) {
2247 err = config_read_env_vars(config);
2248 if (_Py_INIT_FAILED(err)) {
2249 return err;
2250 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002251 }
2252
Victor Stinner9cfc0022017-12-20 19:36:46 +01002253 /* -X options */
2254 if (config_get_xoption(config, L"showrefcount")) {
2255 config->show_ref_count = 1;
2256 }
2257 if (config_get_xoption(config, L"showalloccount")) {
2258 config->show_alloc_count = 1;
2259 }
2260
2261 err = config_read_complex_options(config);
2262 if (_Py_INIT_FAILED(err)) {
2263 return err;
2264 }
2265
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002266 if (config->utf8_mode < 0) {
2267 err = config_init_utf8_mode(config);
2268 if (_Py_INIT_FAILED(err)) {
2269 return err;
2270 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002271 }
2272
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002273 if (config->home == NULL) {
2274 err = config_init_home(config);
2275 if (_Py_INIT_FAILED(err)) {
2276 return err;
2277 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002278 }
2279
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002280 if (config->program_name == NULL) {
2281 err = config_init_program_name(config);
2282 if (_Py_INIT_FAILED(err)) {
2283 return err;
2284 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002285 }
2286
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002287 if (config->utf8_mode < 0 || config->coerce_c_locale < 0) {
2288 config_init_locale(config);
Victor Stinner94540602017-12-16 04:54:22 +01002289 }
2290
Victor Stinner8ded5b82018-01-24 17:03:28 +01002291 if (!config->_disable_importlib) {
2292 err = config_init_path_config(config);
2293 if (_Py_INIT_FAILED(err)) {
2294 return err;
2295 }
2296 }
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002297
2298 /* default values */
2299 if (config->dev_mode) {
2300 if (config->faulthandler < 0) {
2301 config->faulthandler = 1;
2302 }
2303 if (config->allocator == NULL) {
2304 config->allocator = "debug";
2305 }
2306 }
2307 if (config->install_signal_handlers < 0) {
2308 config->install_signal_handlers = 1;
2309 }
2310 if (config->use_hash_seed < 0) {
2311 config->use_hash_seed = 0;
2312 config->hash_seed = 0;
2313 }
2314 if (config->faulthandler < 0) {
2315 config->faulthandler = 0;
2316 }
2317 if (config->tracemalloc < 0) {
2318 config->tracemalloc = 0;
2319 }
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002320 if (config->coerce_c_locale < 0) {
2321 config->coerce_c_locale = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002322 }
2323 if (config->utf8_mode < 0) {
2324 config->utf8_mode = 0;
2325 }
2326 if (config->argc < 0) {
2327 config->argc = 0;
2328 }
2329
Victor Stinnerda273412017-12-15 01:46:02 +01002330 return _Py_INIT_OK();
2331}
2332
2333
2334void
2335_PyCoreConfig_Clear(_PyCoreConfig *config)
2336{
2337#define CLEAR(ATTR) \
2338 do { \
2339 PyMem_RawFree(ATTR); \
2340 ATTR = NULL; \
2341 } while (0)
Victor Stinner8ded5b82018-01-24 17:03:28 +01002342#define CLEAR_WSTRLIST(LEN, LIST) \
2343 do { \
2344 clear_wstrlist(LEN, LIST); \
2345 LEN = 0; \
2346 LIST = NULL; \
2347 } while (0)
Victor Stinnerda273412017-12-15 01:46:02 +01002348
2349 CLEAR(config->module_search_path_env);
2350 CLEAR(config->home);
2351 CLEAR(config->program_name);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002352 CLEAR(config->program);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002353
Victor Stinner8ded5b82018-01-24 17:03:28 +01002354 CLEAR_WSTRLIST(config->argc, config->argv);
2355 config->argc = -1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002356
Victor Stinner8ded5b82018-01-24 17:03:28 +01002357 CLEAR_WSTRLIST(config->nwarnoption, config->warnoptions);
2358 CLEAR_WSTRLIST(config->nxoption, config->xoptions);
2359 CLEAR_WSTRLIST(config->nmodule_search_path, config->module_search_paths);
2360 config->nmodule_search_path = -1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002361
Victor Stinner8ded5b82018-01-24 17:03:28 +01002362 CLEAR(config->executable);
2363 CLEAR(config->prefix);
2364 CLEAR(config->base_prefix);
2365 CLEAR(config->exec_prefix);
2366 CLEAR(config->base_exec_prefix);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002367#undef CLEAR
Victor Stinner8ded5b82018-01-24 17:03:28 +01002368#undef CLEAR_WSTRLIST
Victor Stinnerda273412017-12-15 01:46:02 +01002369}
2370
2371
2372int
2373_PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
2374{
2375 _PyCoreConfig_Clear(config);
2376
2377#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
Victor Stinnerc4bca952017-12-19 23:48:17 +01002378#define COPY_STR_ATTR(ATTR) \
2379 do { \
2380 if (config2->ATTR != NULL) { \
2381 config->ATTR = _PyMem_RawWcsdup(config2->ATTR); \
2382 if (config->ATTR == NULL) { \
2383 return -1; \
2384 } \
2385 } \
2386 } while (0)
Victor Stinner8ded5b82018-01-24 17:03:28 +01002387#define COPY_WSTRLIST(LEN, LIST) \
2388 do { \
2389 if (config2->LIST != NULL) { \
2390 config->LIST = copy_wstrlist(config2->LEN, config2->LIST); \
2391 if (config->LIST == NULL) { \
2392 return -1; \
2393 } \
2394 } \
2395 config->LEN = config2->LEN; \
2396 } while (0)
Victor Stinnerc4bca952017-12-19 23:48:17 +01002397
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002398 COPY_ATTR(install_signal_handlers);
Victor Stinnerda273412017-12-15 01:46:02 +01002399 COPY_ATTR(ignore_environment);
2400 COPY_ATTR(use_hash_seed);
2401 COPY_ATTR(hash_seed);
2402 COPY_ATTR(_disable_importlib);
2403 COPY_ATTR(allocator);
2404 COPY_ATTR(dev_mode);
2405 COPY_ATTR(faulthandler);
2406 COPY_ATTR(tracemalloc);
2407 COPY_ATTR(import_time);
2408 COPY_ATTR(show_ref_count);
2409 COPY_ATTR(show_alloc_count);
2410 COPY_ATTR(dump_refs);
2411 COPY_ATTR(malloc_stats);
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002412
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002413 COPY_ATTR(coerce_c_locale);
2414 COPY_ATTR(coerce_c_locale_warn);
Victor Stinnerda273412017-12-15 01:46:02 +01002415 COPY_ATTR(utf8_mode);
Victor Stinnerda273412017-12-15 01:46:02 +01002416
2417 COPY_STR_ATTR(module_search_path_env);
2418 COPY_STR_ATTR(home);
2419 COPY_STR_ATTR(program_name);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002420 COPY_STR_ATTR(program);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002421
Victor Stinner8ded5b82018-01-24 17:03:28 +01002422 COPY_WSTRLIST(argc, argv);
2423 COPY_WSTRLIST(nwarnoption, warnoptions);
2424 COPY_WSTRLIST(nxoption, xoptions);
2425 COPY_WSTRLIST(nmodule_search_path, module_search_paths);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002426
Victor Stinner8ded5b82018-01-24 17:03:28 +01002427 COPY_STR_ATTR(executable);
2428 COPY_STR_ATTR(prefix);
2429 COPY_STR_ATTR(base_prefix);
2430 COPY_STR_ATTR(exec_prefix);
2431 COPY_STR_ATTR(base_exec_prefix);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002432
Victor Stinnerc4bca952017-12-19 23:48:17 +01002433#undef COPY_ATTR
Victor Stinnerda273412017-12-15 01:46:02 +01002434#undef COPY_STR_ATTR
Victor Stinner8ded5b82018-01-24 17:03:28 +01002435#undef COPY_WSTRLIST
Victor Stinnerda273412017-12-15 01:46:02 +01002436 return 0;
2437}
2438
2439
2440void
2441_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
2442{
2443 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002444 Py_CLEAR(config->executable);
2445 Py_CLEAR(config->prefix);
2446 Py_CLEAR(config->base_prefix);
2447 Py_CLEAR(config->exec_prefix);
2448 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002449 Py_CLEAR(config->warnoptions);
2450 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002451 Py_CLEAR(config->module_search_path);
Victor Stinnerda273412017-12-15 01:46:02 +01002452}
2453
2454
2455static PyObject*
2456config_copy_attr(PyObject *obj)
2457{
2458 if (PyUnicode_Check(obj)) {
2459 Py_INCREF(obj);
2460 return obj;
2461 }
2462 else if (PyList_Check(obj)) {
2463 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
2464 }
2465 else if (PyDict_Check(obj)) {
2466 /* The dict type is used for xoptions. Make the assumption that keys
2467 and values are immutables */
2468 return PyDict_Copy(obj);
2469 }
2470 else {
2471 PyErr_Format(PyExc_TypeError,
2472 "cannot copy config attribute of type %.200s",
2473 Py_TYPE(obj)->tp_name);
2474 return NULL;
2475 }
2476}
2477
2478
2479int
2480_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
2481 const _PyMainInterpreterConfig *config2)
2482{
2483 _PyMainInterpreterConfig_Clear(config);
2484
2485#define COPY_ATTR(ATTR) \
2486 do { \
2487 if (config2->ATTR != NULL) { \
2488 config->ATTR = config_copy_attr(config2->ATTR); \
2489 if (config->ATTR == NULL) { \
2490 return -1; \
2491 } \
2492 } \
2493 } while (0)
2494
2495 COPY_ATTR(argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002496 COPY_ATTR(executable);
2497 COPY_ATTR(prefix);
2498 COPY_ATTR(base_prefix);
2499 COPY_ATTR(exec_prefix);
2500 COPY_ATTR(base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002501 COPY_ATTR(warnoptions);
2502 COPY_ATTR(xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002503 COPY_ATTR(module_search_path);
Victor Stinnerda273412017-12-15 01:46:02 +01002504#undef COPY_ATTR
2505 return 0;
2506}
2507
2508
2509
2510
Victor Stinner41264f12017-12-15 02:05:29 +01002511_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01002512_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
2513 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002514{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002515 if (main_config->install_signal_handlers < 0) {
2516 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002517 }
2518
Victor Stinner9cfc0022017-12-20 19:36:46 +01002519 if (main_config->xoptions == NULL) {
2520 main_config->xoptions = config_create_xoptions_dict(config);
2521 if (main_config->xoptions == NULL) {
2522 return _Py_INIT_NO_MEMORY();
2523 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002524 }
2525
Victor Stinner8ded5b82018-01-24 17:03:28 +01002526#define COPY_WSTR(ATTR) \
2527 do { \
2528 if (main_config->ATTR == NULL) { \
2529 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
2530 if (main_config->ATTR == NULL) { \
2531 return _Py_INIT_NO_MEMORY(); \
2532 } \
2533 } \
2534 } while (0)
2535#define COPY_WSTRLIST(ATTR, LEN, LIST) \
2536 do { \
2537 if (ATTR == NULL) { \
2538 ATTR = wstrlist_as_pylist(LEN, LIST); \
2539 if (ATTR == NULL) { \
2540 return _Py_INIT_NO_MEMORY(); \
2541 } \
2542 } \
2543 } while (0)
2544
2545 COPY_WSTRLIST(main_config->warnoptions,
2546 config->nwarnoption, config->warnoptions);
2547 if (config->argc >= 0) {
2548 COPY_WSTRLIST(main_config->argv,
2549 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002550 }
2551
Victor Stinner8ded5b82018-01-24 17:03:28 +01002552 if (!config->_disable_importlib) {
2553 COPY_WSTR(executable);
2554 COPY_WSTR(prefix);
2555 COPY_WSTR(base_prefix);
2556 COPY_WSTR(exec_prefix);
2557 COPY_WSTR(base_exec_prefix);
2558
2559 COPY_WSTRLIST(main_config->module_search_path,
2560 config->nmodule_search_path, config->module_search_paths);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002561 }
Victor Stinner41264f12017-12-15 02:05:29 +01002562
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002563 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01002564#undef COPY_WSTR
2565#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002566}
2567
2568
2569static int
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002570pymain_init_python_main(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002571{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002572 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002573
Victor Stinner9cfc0022017-12-20 19:36:46 +01002574 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002575 err = _PyMainInterpreterConfig_Read(&main_config, &interp->core_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002576 if (!_Py_INIT_FAILED(err)) {
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002577 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002578 }
2579 _PyMainInterpreterConfig_Clear(&main_config);
2580
2581 if (_Py_INIT_FAILED(err)) {
2582 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002583 return -1;
2584 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002585 return 0;
2586}
Victor Stinnera7368ac2017-11-15 18:11:45 -08002587
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002588
2589static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01002590pymain_init_sys_path(_PyMain *pymain)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002591{
Victor Stinnerca719ac2017-12-20 18:00:19 +01002592 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002593 /* If filename is a package (ex: directory or ZIP file) which contains
2594 __main__.py, main_importer_path is set to filename and will be
2595 prepended to sys.path by pymain_run_main_from_importer(). Otherwise,
2596 main_importer_path is set to NULL. */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002597 pymain->main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01002598 }
2599
Victor Stinner19760862017-12-20 01:41:59 +01002600 PyObject *path0;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002601 if (pymain_compute_path0(pymain, &path0) < 0) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002602 return -1;
2603 }
Victor Stinner19760862017-12-20 01:41:59 +01002604
Victor Stinner9cfc0022017-12-20 19:36:46 +01002605 pymain_clear_config(pymain);
Victor Stinner19760862017-12-20 01:41:59 +01002606
2607 if (path0 != NULL) {
2608 if (pymain_update_sys_path(pymain, path0) < 0) {
2609 Py_DECREF(path0);
2610 return -1;
2611 }
2612 Py_DECREF(path0);
2613 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002614 return 0;
2615}
2616
2617
2618static void
2619pymain_run_python(_PyMain *pymain)
2620{
Victor Stinner19760862017-12-20 01:41:59 +01002621 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08002622
2623 pymain_header(pymain);
2624 pymain_import_readline(pymain);
2625
Victor Stinnerca719ac2017-12-20 18:00:19 +01002626 if (pymain->command) {
2627 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002628 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002629 else if (pymain->module) {
2630 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002631 }
2632 else {
Victor Stinner19760862017-12-20 01:41:59 +01002633 pymain_run_filename(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002634 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002635
Victor Stinner19760862017-12-20 01:41:59 +01002636 pymain_repl(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002637}
2638
2639
Victor Stinnerc4bca952017-12-19 23:48:17 +01002640static void
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002641pymain_init(_PyMain *pymain)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002642{
Victor Stinner94540602017-12-16 04:54:22 +01002643 /* 754 requires that FP exceptions run in "no stop" mode by default,
2644 * and until C vendors implement C99's ways to control FP exceptions,
2645 * Python requires non-stop mode. Alas, some platforms enable FP
2646 * exceptions by default. Here we disable them.
2647 */
2648#ifdef __FreeBSD__
2649 fedisableexcept(FE_OVERFLOW);
2650#endif
2651
Victor Stinner9cfc0022017-12-20 19:36:46 +01002652 pymain->config._disable_importlib = 0;
Victor Stinnere32e79f2017-11-23 01:49:45 +01002653 pymain->config.install_signal_handlers = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002654}
2655
Victor Stinnera7368ac2017-11-15 18:11:45 -08002656
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002657static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01002658pymain_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002659{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002660 pymain->err = _PyRuntime_Initialize();
2661 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002662 return -1;
2663 }
2664
Victor Stinnerca719ac2017-12-20 18:00:19 +01002665 int res = pymain_read_conf(pymain, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002666 if (res < 0) {
2667 return -1;
2668 }
2669 if (res > 0) {
2670 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01002671 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002672 }
2673
Victor Stinner94540602017-12-16 04:54:22 +01002674 if (cmdline->print_help) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002675 pymain_usage(0, pymain->config.program);
Victor Stinner19760862017-12-20 01:41:59 +01002676 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002677 }
2678
2679 if (cmdline->print_version) {
2680 printf("Python %s\n",
2681 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01002682 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002683 }
2684
Victor Stinnerc4bca952017-12-19 23:48:17 +01002685 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002686 orig_argv = copy_wstrlist(pymain->argc, cmdline->argv);
2687 if (orig_argv == NULL) {
2688 pymain->err = _Py_INIT_NO_MEMORY();
2689 return -1;
2690 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01002691 orig_argc = pymain->argc;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002692
Victor Stinner9cfc0022017-12-20 19:36:46 +01002693 _PyInitError err = config_init_warnoptions(&pymain->config, cmdline);
2694 if (_Py_INIT_FAILED(err)) {
2695 pymain->err = err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002696 return -1;
2697 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002698 return 0;
2699}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00002700
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002701
Victor Stinnerca719ac2017-12-20 18:00:19 +01002702/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
2703 LC_CTYPE locale and Py_DecodeLocale().
2704
2705 Configuration:
2706
2707 * Command line arguments
2708 * Environment variables
2709 * Py_xxx global configuration variables
2710
2711 _Py_CommandLineDetails is a temporary structure used to prioritize these
2712 variables. */
2713static int
2714pymain_cmdline(_PyMain *pymain)
2715{
Victor Stinner31e99082017-12-20 23:41:38 +01002716 /* Force default allocator, since pymain_free() and pymain_clear_config()
2717 must use the same allocator than this function. */
2718 PyMemAllocatorEx old_alloc;
2719 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2720#ifdef Py_DEBUG
2721 PyMemAllocatorEx default_alloc;
2722 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
2723#endif
2724
Victor Stinnerca719ac2017-12-20 18:00:19 +01002725 _Py_CommandLineDetails cmdline;
2726 memset(&cmdline, 0, sizeof(cmdline));
2727
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002728 cmdline_get_global_config(&cmdline);
2729 _PyCoreConfig_GetGlobalConfig(&pymain->config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002730
2731 int res = pymain_cmdline_impl(pymain, &cmdline);
2732
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002733 cmdline_set_global_config(&cmdline);
2734 _PyCoreConfig_SetGlobalConfig(&pymain->config);
2735 if (Py_IsolatedFlag) {
2736 Py_IgnoreEnvironmentFlag = 1;
2737 Py_NoUserSiteDirectory = 1;
2738 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002739
2740 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01002741
2742#ifdef Py_DEBUG
2743 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
2744 PyMemAllocatorEx cur_alloc;
2745 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
2746 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
2747#endif
2748 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002749 return res;
2750}
2751
2752
Victor Stinner94540602017-12-16 04:54:22 +01002753static int
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002754pymain_main(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +01002755{
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002756 pymain_init(pymain);
Victor Stinner94540602017-12-16 04:54:22 +01002757
Victor Stinnerca719ac2017-12-20 18:00:19 +01002758 int res = pymain_cmdline(pymain);
Victor Stinner19760862017-12-20 01:41:59 +01002759 if (res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01002760 _Py_FatalInitError(pymain->err);
2761 }
Victor Stinner19760862017-12-20 01:41:59 +01002762 if (res == 1) {
2763 goto done;
2764 }
2765
Victor Stinner9cfc0022017-12-20 19:36:46 +01002766 pymain_init_stdio(pymain);
2767
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002768 PyInterpreterState *interp;
2769 pymain->err = _Py_InitializeCore(&interp, &pymain->config);
2770 if (_Py_INIT_FAILED(pymain->err)) {
2771 _Py_FatalInitError(pymain->err);
Victor Stinner19760862017-12-20 01:41:59 +01002772 }
2773
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002774 if (pymain_init_python_main(pymain, interp) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01002775 _Py_FatalInitError(pymain->err);
2776 }
2777
Victor Stinner9cfc0022017-12-20 19:36:46 +01002778 if (pymain_init_sys_path(pymain) < 0) {
2779 _Py_FatalInitError(pymain->err);
2780 }
2781
Victor Stinner19760862017-12-20 01:41:59 +01002782 pymain_run_python(pymain);
2783
2784 if (Py_FinalizeEx() < 0) {
2785 /* Value unlikely to be confused with a non-error exit status or
2786 other special meaning */
2787 pymain->status = 120;
2788 }
2789
2790done:
Victor Stinner94540602017-12-16 04:54:22 +01002791 pymain_free(pymain);
2792
Victor Stinner94540602017-12-16 04:54:22 +01002793 return pymain->status;
2794}
2795
2796
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002797int
2798Py_Main(int argc, wchar_t **argv)
2799{
2800 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002801 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002802 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002803 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002804
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002805 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00002806}
2807
Victor Stinner94540602017-12-16 04:54:22 +01002808
2809int
2810_Py_UnixMain(int argc, char **argv)
2811{
2812 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01002813 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002814 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01002815 pymain.bytes_argv = argv;
2816
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002817 return pymain_main(&pymain);
Victor Stinner94540602017-12-16 04:54:22 +01002818}
2819
2820
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002821/* this is gonna seem *real weird*, but if you put some other code between
2822 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
2823 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00002824
Guido van Rossum667d7041995-08-04 04:20:48 +00002825/* Make the *original* argc/argv available to other modules.
2826 This is rare, but it is needed by the secureware extension. */
2827
2828void
Martin v. Löwis790465f2008-04-05 20:41:37 +00002829Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00002830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 *argc = orig_argc;
2832 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00002833}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002834
2835#ifdef __cplusplus
2836}
2837#endif