blob: f94689496a38fca00c7f69566ba16f1eb90a4b3b [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
Victor Stinnerddc163d2018-09-24 05:03:01 -0700462} _PyCmdline;
Eric Snow6b4be192017-05-22 21:36:03 -0700463
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 Stinner19760862017-12-20 01:41:59 +0100484 PyObject *main_importer_path;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800485} _PyMain;
486
Victor Stinnerddc163d2018-09-24 05:03:01 -0700487#define _PyMain_INIT {.err = _Py_INIT_OK()}
Victor Stinnerd5dda982017-12-13 17:31:16 +0100488/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800489
490
Victor Stinner19760862017-12-20 01:41:59 +0100491/* Non-zero if filename, command (-c) or module (-m) is set
492 on the command line */
493#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100494 (pymain->command != NULL || pymain->filename != NULL \
495 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100496
497
Victor Stinnerca719ac2017-12-20 18:00:19 +0100498static wchar_t*
499pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800500{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100501 wchar_t *str2 = _PyMem_RawWcsdup(str);
502 if (str2 == NULL) {
503 pymain->err = _Py_INIT_NO_MEMORY();
504 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800505 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100506 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800507}
508
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100509
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800510static void
Victor Stinnerca719ac2017-12-20 18:00:19 +0100511clear_wstrlist(int len, wchar_t **list)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800512{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100513 for (int i=0; i < len; i++) {
514 PyMem_RawFree(list[i]);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100515 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100516 PyMem_RawFree(list);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100517}
518
519
520static int
Victor Stinnerddc163d2018-09-24 05:03:01 -0700521pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,
522 _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100523{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100524 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100525
Victor Stinnerca719ac2017-12-20 18:00:19 +0100526 if (pymain->use_bytes_argv) {
527 /* +1 for a the NULL terminator */
528 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
529 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
530 if (argv == NULL) {
531 pymain->err = _Py_INIT_NO_MEMORY();
532 return -1;
533 }
534
535 for (int i = 0; i < pymain->argc; i++) {
536 size_t len;
537 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
538 if (arg == NULL) {
539 clear_wstrlist(i, argv);
540 pymain->err = DECODE_LOCALE_ERR("command line arguments",
541 (Py_ssize_t)len);
542 return -1;
543 }
544 argv[i] = arg;
545 }
546 argv[pymain->argc] = NULL;
547
548 cmdline->argv = argv;
549 }
550 else {
551 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100552 }
553
Victor Stinnerca719ac2017-12-20 18:00:19 +0100554 wchar_t *program;
555 if (pymain->argc >= 1 && cmdline->argv != NULL) {
556 program = cmdline->argv[0];
557 }
558 else {
559 program = L"";
560 }
Victor Stinnerddc163d2018-09-24 05:03:01 -0700561 config->program = pymain_wstrdup(pymain, program);
562 if (config->program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100563 return -1;
564 }
565
Victor Stinnerc4bca952017-12-19 23:48:17 +0100566 return 0;
567}
568
569
570static void
Victor Stinnerddc163d2018-09-24 05:03:01 -0700571pymain_clear_cmdline(_PyMain *pymain, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100572{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100573 PyMemAllocatorEx old_alloc;
574 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100575
Victor Stinnerca719ac2017-12-20 18:00:19 +0100576 clear_wstrlist(cmdline->nwarnoption, cmdline->warnoptions);
577 cmdline->nwarnoption = 0;
578 cmdline->warnoptions = NULL;
579
580 clear_wstrlist(cmdline->nenv_warnoption, cmdline->env_warnoptions);
581 cmdline->nenv_warnoption = 0;
582 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100583
584 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100585 clear_wstrlist(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100586 }
587 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100588
589 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
590}
591
592
593static void
594pymain_clear_pymain(_PyMain *pymain)
595{
596#define CLEAR(ATTR) \
597 do { \
598 PyMem_RawFree(ATTR); \
599 ATTR = NULL; \
600 } while (0)
601
602 CLEAR(pymain->filename);
603 CLEAR(pymain->command);
604 CLEAR(pymain->module);
605#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100606}
607
Victor Stinnerc4bca952017-12-19 23:48:17 +0100608static void
Victor Stinnerddc163d2018-09-24 05:03:01 -0700609pymain_clear_config(_PyCoreConfig *config)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100610{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100611 /* Clear core config with the memory allocator
612 used by pymain_read_conf() */
613 PyMemAllocatorEx old_alloc;
614 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
615
Victor Stinnerddc163d2018-09-24 05:03:01 -0700616 _PyCoreConfig_Clear(config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100617
618 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
619}
620
621
622static void
623pymain_free_python(_PyMain *pymain)
624{
625 Py_CLEAR(pymain->main_importer_path);
Victor Stinnerd4341102017-11-23 00:12:09 +0100626
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800627#ifdef __INSURE__
628 /* Insure++ is a memory analysis tool that aids in discovering
629 * memory leaks and other memory problems. On Python exit, the
630 * interned string dictionaries are flagged as being in use at exit
631 * (which it is). Under normal circumstances, this is fine because
632 * the memory will be automatically reclaimed by the system. Under
633 * memory debugging, it's a huge source of useless noise, so we
634 * trade off slower shutdown for less distraction in the memory
635 * reports. -baw
636 */
637 _Py_ReleaseInternedUnicodeStrings();
638#endif /* __INSURE__ */
639}
640
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100641
642static void
Victor Stinnerc4bca952017-12-19 23:48:17 +0100643pymain_free_raw(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100644{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100645 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100646
Victor Stinnerc4bca952017-12-19 23:48:17 +0100647 /* Free global variables which cannot be freed in Py_Finalize():
648 configuration options set before Py_Initialize() which should
649 remain valid after Py_Finalize(), since
650 Py_Initialize()-Py_Finalize() can be called multiple times. */
651 _PyPathConfig_Clear(&_Py_path_config);
Victor Stinner94540602017-12-16 04:54:22 +0100652
Victor Stinnerc4bca952017-12-19 23:48:17 +0100653 /* Force the allocator used by pymain_read_conf() */
654 PyMemAllocatorEx old_alloc;
655 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100656
Victor Stinnerca719ac2017-12-20 18:00:19 +0100657 pymain_clear_pymain(pymain);
658
659 clear_wstrlist(orig_argc, orig_argv);
660 orig_argc = 0;
661 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100662
Victor Stinner935250d2019-03-19 15:08:17 +0100663 _PyRuntime_Finalize();
664
Victor Stinnerc4bca952017-12-19 23:48:17 +0100665 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100666}
667
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100668
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800669static void
670pymain_free(_PyMain *pymain)
671{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100672 pymain_free_python(pymain);
673 pymain_free_raw(pymain);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800674}
675
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100676
Eric Snow6b4be192017-05-22 21:36:03 -0700677static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800678pymain_run_main_from_importer(_PyMain *pymain)
Guido van Rossum667d7041995-08-04 04:20:48 +0000679{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800680 /* Assume sys_path0 has already been checked by pymain_get_importer(),
681 * so put it in sys.path[0] and import __main__ */
Victor Stinner11a247d2017-12-13 21:05:57 +0100682 PyObject *sys_path = PySys_GetObject("path");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800683 if (sys_path == NULL) {
684 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
685 goto error;
686 }
687
Victor Stinner11a247d2017-12-13 21:05:57 +0100688 if (PyList_Insert(sys_path, 0, pymain->main_importer_path)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800689 goto error;
690 }
691
Victor Stinner11a247d2017-12-13 21:05:57 +0100692 int sts = pymain_run_module(L"__main__", 0);
693 return (sts != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800694
695error:
696 Py_CLEAR(pymain->main_importer_path);
697 PyErr_Print();
698 return 1;
699}
700
701
Victor Stinner9cfc0022017-12-20 19:36:46 +0100702static _PyInitError
703wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800704{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100705 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800706 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100707 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800708 }
709
Victor Stinnerca719ac2017-12-20 18:00:19 +0100710 size_t size = (*len + 1) * sizeof(list[0]);
711 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
712 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800713 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100714 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800715 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100716 list2[*len] = str2;
717 *list = list2;
718 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100719 return _Py_INIT_OK();
720}
721
722
723static int
724pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
725{
726 _PyInitError err = wstrlist_append(len, list, str);
727 if (_Py_INIT_FAILED(err)) {
728 pymain->err = err;
729 return -1;
730 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800731 return 0;
732}
733
734
735/* Parse the command line arguments
736 Return 0 on success.
737 Return 1 if parsing failed.
738 Set pymain->err and return -1 on other errors. */
739static int
Victor Stinnerddc163d2018-09-24 05:03:01 -0700740pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
741 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800742{
Antoine Pitrou86838b02012-02-21 19:03:47 +0100743 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800744 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800745 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100746 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800747 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800748 if (c == EOF) {
749 break;
750 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 /* -c is the last option; following arguments
754 that look like options are left for the
755 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800756 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
757 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
758 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100759 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800760 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800761 }
Miss Islington (bot)c6de46e2018-05-31 06:43:21 -0700762 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 command[len - 2] = '\n';
764 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100765 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 break;
767 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (c == 'm') {
770 /* -m is the last option; following arguments
771 that look like options are left for the
772 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100773 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
774 if (pymain->module == NULL) {
775 return -1;
776 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 break;
778 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800781 case 0:
782 // Handle long option.
783 assert(longindex == 0); // Only one long option now.
784 if (!wcscmp(_PyOS_optarg, L"always")) {
785 cmdline->check_hash_pycs_mode = "always";
786 } else if (!wcscmp(_PyOS_optarg, L"never")) {
787 cmdline->check_hash_pycs_mode = "never";
788 } else if (!wcscmp(_PyOS_optarg, L"default")) {
789 cmdline->check_hash_pycs_mode = "default";
790 } else {
791 fprintf(stderr, "--check-hash-based-pycs must be one of "
792 "'default', 'always', or 'never'\n");
793 return 1;
794 }
795 break;
796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 case 'b':
Eric Snow6b4be192017-05-22 21:36:03 -0700798 cmdline->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 case 'd':
Eric Snow6b4be192017-05-22 21:36:03 -0700802 cmdline->debug++;
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 'i':
Eric Snow6b4be192017-05-22 21:36:03 -0700806 cmdline->inspect++;
807 cmdline->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000809
Christian Heimesad73a9c2013-08-10 16:36:18 +0200810 case 'I':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100811 config->ignore_environment++;
Eric Snow6b4be192017-05-22 21:36:03 -0700812 cmdline->isolated++;
813 cmdline->no_user_site_directory++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200814 break;
815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 case 'O':
Eric Snow6b4be192017-05-22 21:36:03 -0700819 cmdline->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 case 'B':
Eric Snow6b4be192017-05-22 21:36:03 -0700823 cmdline->dont_write_bytecode++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 case 's':
Eric Snow6b4be192017-05-22 21:36:03 -0700827 cmdline->no_user_site_directory++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 case 'S':
Eric Snow6b4be192017-05-22 21:36:03 -0700831 cmdline->no_site_import++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 case 'E':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100835 config->ignore_environment++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 case 't':
839 /* ignored for backwards compatibility */
840 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 case 'u':
Eric Snow6b4be192017-05-22 21:36:03 -0700843 cmdline->use_unbuffered_io = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 case 'v':
Eric Snow6b4be192017-05-22 21:36:03 -0700847 cmdline->verbosity++;
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 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100851 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 case 'h':
855 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -0700856 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -0700860 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100864 if (pymain_wstrlist_append(pymain,
865 &cmdline->nwarnoption,
866 &cmdline->warnoptions,
867 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800868 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800869 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000871
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000872 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100873 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +0100874 &config->nxoption,
875 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +0100876 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800877 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800878 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000879 break;
880
Georg Brandl9d871192010-12-04 10:47:18 +0000881 case 'q':
Eric Snow6b4be192017-05-22 21:36:03 -0700882 cmdline->quiet_flag++;
Georg Brandl9d871192010-12-04 10:47:18 +0000883 break;
884
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100885 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100886 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100887 break;
888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800892 /* unknown argument: parsing failed */
893 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800895 } while (1);
896
Victor Stinnerca719ac2017-12-20 18:00:19 +0100897 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800898 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +0100899 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800900 {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100901 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
902 if (pymain->filename == NULL) {
903 return -1;
904 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000906
Victor Stinnerd5dda982017-12-13 17:31:16 +0100907 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100908 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +0100909
Eric Snow6b4be192017-05-22 21:36:03 -0700910 return 0;
911}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000912
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800913
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800914static int
Victor Stinner9cfc0022017-12-20 19:36:46 +0100915add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +0100916{
917 PyObject *name, *value;
918
919 const wchar_t *name_end = wcschr(s, L'=');
920 if (!name_end) {
921 name = PyUnicode_FromWideChar(s, -1);
922 value = Py_True;
923 Py_INCREF(value);
924 }
925 else {
926 name = PyUnicode_FromWideChar(s, name_end - s);
927 value = PyUnicode_FromWideChar(name_end + 1, -1);
928 }
929 if (name == NULL || value == NULL) {
930 goto error;
931 }
932 if (PyDict_SetItem(opts, name, value) < 0) {
933 goto error;
934 }
935 Py_DECREF(name);
936 Py_DECREF(value);
937 return 0;
938
939error:
940 Py_XDECREF(name);
941 Py_XDECREF(value);
942 return -1;
943}
944
Victor Stinner9cfc0022017-12-20 19:36:46 +0100945
946static PyObject*
947config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800948{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100949 int nxoption = config->nxoption;
950 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +0100951 PyObject *dict = PyDict_New();
952 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100953 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +0100954 }
955
Victor Stinnerca719ac2017-12-20 18:00:19 +0100956 for (int i=0; i < nxoption; i++) {
957 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +0100958 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +0100959 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100960 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800961 }
962 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100963
Victor Stinner9cfc0022017-12-20 19:36:46 +0100964 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -0700965}
966
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800967
Victor Stinner9cfc0022017-12-20 19:36:46 +0100968static _PyInitError
969config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -0700970{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100971 for (int i = 0; i < len; i++) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100972 _PyInitError err = wstrlist_append(&config->nwarnoption,
973 &config->warnoptions,
974 options[i]);
975 if (_Py_INIT_FAILED(err)) {
976 return err;
Eric Snow6b4be192017-05-22 21:36:03 -0700977 }
Eric Snow6b4be192017-05-22 21:36:03 -0700978 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100979 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800980}
Eric Snow6b4be192017-05-22 21:36:03 -0700981
Victor Stinner747f48e2017-12-12 22:59:48 +0100982
Victor Stinner9cfc0022017-12-20 19:36:46 +0100983static _PyInitError
Victor Stinnerddc163d2018-09-24 05:03:01 -0700984config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +0100985{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100986 _PyInitError err;
987
988 assert(config->nwarnoption == 0);
989
Victor Stinner747f48e2017-12-12 22:59:48 +0100990 /* The priority order for warnings configuration is (highest precedence
991 * first):
992 *
993 * - the BytesWarning filter, if needed ('-b', '-bb')
994 * - any '-W' command line options; then
995 * - the 'PYTHONWARNINGS' environment variable; then
996 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
997 * - any implicit filters added by _warnings.c/warnings.py
998 *
999 * All settings except the last are passed to the warnings module via
1000 * the `sys.warnoptions` list. Since the warnings module works on the basis
1001 * of "the most recently added filter will be checked first", we add
1002 * the lowest precedence entries first so that later entries override them.
1003 */
1004
Victor Stinner9cfc0022017-12-20 19:36:46 +01001005 if (config->dev_mode) {
1006 err = wstrlist_append(&config->nwarnoption,
1007 &config->warnoptions,
1008 L"default");
1009 if (_Py_INIT_FAILED(err)) {
1010 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001011 }
Victor Stinner747f48e2017-12-12 22:59:48 +01001012 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001013
Victor Stinner9cfc0022017-12-20 19:36:46 +01001014 err = config_add_warnings_optlist(config,
1015 cmdline->nenv_warnoption,
1016 cmdline->env_warnoptions);
1017 if (_Py_INIT_FAILED(err)) {
1018 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001019 }
1020
Victor Stinner9cfc0022017-12-20 19:36:46 +01001021 err = config_add_warnings_optlist(config,
1022 cmdline->nwarnoption,
1023 cmdline->warnoptions);
1024 if (_Py_INIT_FAILED(err)) {
1025 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001026 }
1027
1028 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
1029 * don't even try to emit a warning, so we skip setting the filter in that
1030 * case.
1031 */
1032 if (cmdline->bytes_warning) {
1033 wchar_t *filter;
1034 if (cmdline->bytes_warning> 1) {
1035 filter = L"error::BytesWarning";
1036 }
1037 else {
1038 filter = L"default::BytesWarning";
1039 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001040 err = wstrlist_append(&config->nwarnoption,
1041 &config->warnoptions,
1042 filter);
1043 if (_Py_INIT_FAILED(err)) {
1044 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001045 }
1046 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001047 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001048}
1049
1050
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001051/* Get warning options from PYTHONWARNINGS environment variable.
1052 Return 0 on success.
1053 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001054static _PyInitError
Victor Stinnerddc163d2018-09-24 05:03:01 -07001055cmdline_init_env_warnoptions(_PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001056{
1057 if (Py_IgnoreEnvironmentFlag) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001058 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +00001060
Victor Stinnerca719ac2017-12-20 18:00:19 +01001061 wchar_t *env;
1062 int res = config_get_env_var_dup(&env, L"PYTHONWARNINGS", "PYTHONWARNINGS");
1063 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001064 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001065 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001066
Victor Stinnerca719ac2017-12-20 18:00:19 +01001067 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001068 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001069 }
Philip Jenvey0805ca32010-04-07 04:04:10 +00001070
Victor Stinnerca719ac2017-12-20 18:00:19 +01001071
1072 wchar_t *warning, *context = NULL;
1073 for (warning = WCSTOK(env, L",", &context);
1074 warning != NULL;
1075 warning = WCSTOK(NULL, L",", &context))
1076 {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001077 _PyInitError err = wstrlist_append(&cmdline->nenv_warnoption,
1078 &cmdline->env_warnoptions,
1079 warning);
1080 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001081 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001082 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001083 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001085 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001086 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001087}
1088
1089
1090static void
1091pymain_init_stdio(_PyMain *pymain)
1092{
1093 pymain->stdin_is_interactive = (isatty(fileno(stdin))
1094 || Py_InteractiveFlag);
Guido van Rossum775af911997-02-14 19:50:32 +00001095
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +00001096#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +00001097 /* don't translate newlines (\r\n <=> \n) */
1098 _setmode(fileno(stdin), O_BINARY);
1099 _setmode(fileno(stdout), O_BINARY);
1100 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +00001101#endif
Victor Stinner89e34362011-01-07 18:47:22 +00001102
1103 if (Py_UnbufferedStdioFlag) {
Guido van Rossum22ffac11998-03-06 15:30:39 +00001104#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
1106 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
1107 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001108#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 setbuf(stdin, (char *)NULL);
1110 setbuf(stdout, (char *)NULL);
1111 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001112#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 }
1114 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +00001115#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* Doesn't have to have line-buffered -- use unbuffered */
1117 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
1118 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001119#else /* !MS_WINDOWS */
1120#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
1122 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001123#endif /* HAVE_SETVBUF */
1124#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* Leave stderr alone - it should be unbuffered anyway. */
1126 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001127}
Guido van Rossum667d7041995-08-04 04:20:48 +00001128
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001129
1130/* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
Victor Stinner31a83932017-12-04 13:39:15 +01001131 environment variables on macOS if available. */
1132static _PyInitError
Victor Stinnerca719ac2017-12-20 18:00:19 +01001133config_init_program_name(_PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001134{
Victor Stinner31a83932017-12-04 13:39:15 +01001135 assert(config->program_name == NULL);
1136
1137 /* If Py_SetProgramName() was called, use its value */
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001138 const wchar_t *program_name = _Py_path_config.program_name;
Victor Stinner31a83932017-12-04 13:39:15 +01001139 if (program_name != NULL) {
1140 config->program_name = _PyMem_RawWcsdup(program_name);
1141 if (config->program_name == NULL) {
1142 return _Py_INIT_NO_MEMORY();
1143 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001144 return _Py_INIT_OK();
Victor Stinner31a83932017-12-04 13:39:15 +01001145 }
1146
Just van Rossum2ac79ef2003-03-05 15:46:54 +00001147#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 /* On MacOS X, when the Python interpreter is embedded in an
1149 application bundle, it gets executed by a bootstrapping script
1150 that does os.execve() with an argv[0] that's different from the
1151 actual Python executable. This is needed to keep the Finder happy,
1152 or rather, to work around Apple's overly strict requirements of
1153 the process name. However, we still need a usable sys.executable,
1154 so the actual executable path is passed in an environment variable.
1155 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
1156 script. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001157 const char *p = config_get_env_var("PYTHONEXECUTABLE");
Victor Stinner6bf992a2017-12-06 17:26:10 +01001158 if (p != NULL) {
Victor Stinner31a83932017-12-04 13:39:15 +01001159 size_t len;
1160 wchar_t* program_name = Py_DecodeLocale(p, &len);
1161 if (program_name == NULL) {
1162 return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment "
1163 "variable", (Py_ssize_t)len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 }
Victor Stinner31a83932017-12-04 13:39:15 +01001165 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001166 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001167 }
Vinay Sajip90db6612012-07-17 17:33:46 +01001168#ifdef WITH_NEXT_FRAMEWORK
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001169 else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001170 const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
Vinay Sajip90db6612012-07-17 17:33:46 +01001171 if (pyvenv_launcher && *pyvenv_launcher) {
1172 /* Used by Mac/Tools/pythonw.c to forward
1173 * the argv0 of the stub executable
1174 */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001175 size_t len;
Victor Stinner31a83932017-12-04 13:39:15 +01001176 wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len);
1177 if (program_name == NULL) {
1178 return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment "
1179 "variable", (Py_ssize_t)len);
Vinay Sajip90db6612012-07-17 17:33:46 +01001180 }
Victor Stinner31a83932017-12-04 13:39:15 +01001181 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001182 return _Py_INIT_OK();
Vinay Sajip90db6612012-07-17 17:33:46 +01001183 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001185#endif /* WITH_NEXT_FRAMEWORK */
1186#endif /* __APPLE__ */
Victor Stinneraf5a8952017-12-02 10:11:32 +01001187
Victor Stinnerca719ac2017-12-20 18:00:19 +01001188 /* Use argv[0] by default, if available */
1189 if (config->program != NULL) {
1190 config->program_name = _PyMem_RawWcsdup(config->program);
1191 if (config->program_name == NULL) {
1192 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001193 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001194 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001195 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001196
1197 /* Last fall back: hardcoded string */
1198#ifdef MS_WINDOWS
1199 const wchar_t *default_program_name = L"python";
1200#else
1201 const wchar_t *default_program_name = L"python3";
1202#endif
1203 config->program_name = _PyMem_RawWcsdup(default_program_name);
1204 if (config->program_name == NULL) {
1205 return _Py_INIT_NO_MEMORY();
1206 }
1207 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001208}
1209
1210
Steve Dowere8510492018-11-17 20:42:08 -08001211static _PyInitError
1212config_init_executable(_PyCoreConfig *config)
1213{
1214 assert(config->executable == NULL);
1215
1216 /* If Py_SetProgramFullPath() was called, use its value */
1217 const wchar_t *program_full_path = _Py_path_config.program_full_path;
1218 if (program_full_path != NULL) {
1219 config->executable = _PyMem_RawWcsdup(program_full_path);
1220 if (config->executable == NULL) {
1221 return _Py_INIT_NO_MEMORY();
1222 }
1223 return _Py_INIT_OK();
1224 }
1225
1226 return _Py_INIT_OK();
1227}
1228
1229
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001230static void
1231pymain_header(_PyMain *pymain)
1232{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001233 if (Py_QuietFlag) {
1234 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
Guido van Rossum393661d2001-08-31 17:40:15 +00001236
Victor Stinner19760862017-12-20 01:41:59 +01001237 if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001238 return;
1239 }
1240
1241 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
1242 if (!Py_NoSiteFlag) {
1243 fprintf(stderr, "%s\n", COPYRIGHT);
1244 }
1245}
1246
1247
Victor Stinnerc4bca952017-12-19 23:48:17 +01001248static wchar_t**
Victor Stinnerca719ac2017-12-20 18:00:19 +01001249copy_wstrlist(int len, wchar_t **list)
Victor Stinner11a247d2017-12-13 21:05:57 +01001250{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001251 assert((len > 0 && list != NULL) || len == 0);
1252 size_t size = len * sizeof(list[0]);
1253 wchar_t **list_copy = PyMem_RawMalloc(size);
Alexey Izbyshev388bd4b2018-08-25 19:46:58 +03001254 if (list_copy == NULL) {
1255 return NULL;
1256 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001257 for (int i=0; i < len; i++) {
1258 wchar_t* arg = _PyMem_RawWcsdup(list[i]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001259 if (arg == NULL) {
Alexey Izbyshev388bd4b2018-08-25 19:46:58 +03001260 clear_wstrlist(i, list_copy);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001261 return NULL;
1262 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001263 list_copy[i] = arg;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001264 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001265 return list_copy;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001266}
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001267
Victor Stinnerc4bca952017-12-19 23:48:17 +01001268
1269static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07001270pymain_init_core_argv(_PyMain *pymain, _PyCoreConfig *config,
1271 _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001272{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001273 /* Copy argv to be able to modify it (to force -c/-m) */
1274 int argc = pymain->argc - _PyOS_optind;
1275 wchar_t **argv;
1276
1277 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +01001278 /* Ensure at least one (empty) argument is seen */
1279 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +01001280 argc = 1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001281 argv = copy_wstrlist(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +01001282 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001283 else {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001284 argv = copy_wstrlist(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001285 }
1286
1287 if (argv == NULL) {
1288 pymain->err = _Py_INIT_NO_MEMORY();
1289 return -1;
1290 }
1291
1292 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001293 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001294 /* Force sys.argv[0] = '-c' */
1295 arg0 = L"-c";
1296 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001297 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001298 /* Force sys.argv[0] = '-m'*/
1299 arg0 = L"-m";
1300 }
1301 if (arg0 != NULL) {
1302 arg0 = _PyMem_RawWcsdup(arg0);
1303 if (arg0 == NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001304 clear_wstrlist(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001305 pymain->err = _Py_INIT_NO_MEMORY();
1306 return -1;
1307 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001308
1309 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001310 PyMem_RawFree(argv[0]);
1311 argv[0] = arg0;
1312 }
1313
Victor Stinnerddc163d2018-09-24 05:03:01 -07001314 config->argc = argc;
1315 config->argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001316 return 0;
1317}
1318
1319
Victor Stinner8ded5b82018-01-24 17:03:28 +01001320static PyObject*
Victor Stinner35c28d52018-11-14 02:01:52 +01001321_Py_wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001322{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001323 assert(list != NULL || len < 1);
1324
1325 PyObject *pylist = PyList_New(len);
1326 if (pylist == NULL) {
1327 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001328 }
1329
Victor Stinner8ded5b82018-01-24 17:03:28 +01001330 for (int i = 0; i < len; i++) {
1331 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001332 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001333 Py_DECREF(pylist);
1334 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001335 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001336 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001337 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001338 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001339}
1340
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001341
Victor Stinner11a247d2017-12-13 21:05:57 +01001342static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07001343pymain_compute_path0(_PyMain *pymain, _PyCoreConfig *config, PyObject **path0)
Victor Stinner11a247d2017-12-13 21:05:57 +01001344{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001345 if (pymain->main_importer_path != NULL) {
1346 /* Let pymain_run_main_from_importer() adjust sys.path[0] later */
Victor Stinner19760862017-12-20 01:41:59 +01001347 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001348 return 0;
1349 }
1350
1351 if (Py_IsolatedFlag) {
Victor Stinner19760862017-12-20 01:41:59 +01001352 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001353 return 0;
1354 }
1355
Victor Stinnerddc163d2018-09-24 05:03:01 -07001356 *path0 = _PyPathConfig_ComputeArgv0(config->argc, config->argv);
Victor Stinner19760862017-12-20 01:41:59 +01001357 if (*path0 == NULL) {
1358 pymain->err = _Py_INIT_NO_MEMORY();
1359 return -1;
1360 }
1361 return 0;
1362}
1363
1364
1365static int
1366pymain_update_sys_path(_PyMain *pymain, PyObject *path0)
1367{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001368 /* Prepend argv[0] to sys.path.
1369 If argv[0] is a symlink, use the real path. */
1370 PyObject *sys_path = PySys_GetObject("path");
1371 if (sys_path == NULL) {
1372 pymain->err = _Py_INIT_ERR("can't get sys.path");
Victor Stinnerd5dda982017-12-13 17:31:16 +01001373 return -1;
1374 }
1375
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001376 /* Prepend path0 to sys.path */
1377 if (PyList_Insert(sys_path, 0, path0) < 0) {
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001378 pymain->err = _Py_INIT_ERR("sys.path.insert(0, path0) failed");
1379 return -1;
1380 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001381 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001382}
1383
1384
Victor Stinner35c28d52018-11-14 02:01:52 +01001385PyObject *
1386_Py_GetGlobalVariablesAsDict(void)
1387{
1388 PyObject *dict, *obj;
1389
1390 dict = PyDict_New();
1391 if (dict == NULL) {
1392 return NULL;
1393 }
1394
1395#define SET_ITEM(KEY, EXPR) \
1396 do { \
1397 obj = (EXPR); \
1398 if (obj == NULL) { \
1399 return NULL; \
1400 } \
1401 int res = PyDict_SetItemString(dict, (KEY), obj); \
1402 Py_DECREF(obj); \
1403 if (res < 0) { \
1404 goto fail; \
1405 } \
1406 } while (0)
1407#define SET_ITEM_INT(VAR) \
1408 SET_ITEM(#VAR, PyLong_FromLong(VAR))
1409#define FROM_STRING(STR) \
1410 ((STR != NULL) ? \
1411 PyUnicode_FromString(STR) \
1412 : (Py_INCREF(Py_None), Py_None))
1413#define SET_ITEM_STR(VAR) \
1414 SET_ITEM(#VAR, FROM_STRING(VAR))
1415
1416 SET_ITEM_STR(Py_FileSystemDefaultEncoding);
1417 SET_ITEM_INT(Py_HasFileSystemDefaultEncoding);
1418 SET_ITEM_STR(Py_FileSystemDefaultEncodeErrors);
1419
1420 SET_ITEM_INT(Py_UTF8Mode);
1421 SET_ITEM_INT(Py_DebugFlag);
1422 SET_ITEM_INT(Py_VerboseFlag);
1423 SET_ITEM_INT(Py_QuietFlag);
1424 SET_ITEM_INT(Py_InteractiveFlag);
1425 SET_ITEM_INT(Py_InspectFlag);
1426
1427 SET_ITEM_INT(Py_OptimizeFlag);
1428 SET_ITEM_INT(Py_NoSiteFlag);
1429 SET_ITEM_INT(Py_BytesWarningFlag);
1430 SET_ITEM_INT(Py_FrozenFlag);
1431 SET_ITEM_INT(Py_IgnoreEnvironmentFlag);
1432 SET_ITEM_INT(Py_DontWriteBytecodeFlag);
1433 SET_ITEM_INT(Py_NoUserSiteDirectory);
1434 SET_ITEM_INT(Py_UnbufferedStdioFlag);
1435 SET_ITEM_INT(Py_HashRandomizationFlag);
1436 SET_ITEM_INT(Py_IsolatedFlag);
1437
1438#ifdef MS_WINDOWS
1439 SET_ITEM_INT(Py_LegacyWindowsFSEncodingFlag);
1440 SET_ITEM_INT(Py_LegacyWindowsStdioFlag);
1441#endif
1442
1443 return dict;
1444
1445fail:
1446 Py_DECREF(dict);
1447 return NULL;
1448
1449#undef FROM_STRING
1450#undef SET_ITEM
1451#undef SET_ITEM_INT
1452#undef SET_ITEM_STR
1453}
1454
1455
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001456void
1457_PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config)
1458{
1459#define COPY_FLAG(ATTR, VALUE) \
1460 if (config->ATTR == -1) { \
1461 config->ATTR = VALUE; \
1462 }
1463
1464 COPY_FLAG(ignore_environment, Py_IgnoreEnvironmentFlag);
1465 COPY_FLAG(utf8_mode, Py_UTF8Mode);
1466
1467#undef COPY_FLAG
1468}
1469
1470
Victor Stinner6bf992a2017-12-06 17:26:10 +01001471/* Get Py_xxx global configuration variables */
1472static void
Victor Stinnerddc163d2018-09-24 05:03:01 -07001473cmdline_get_global_config(_PyCmdline *cmdline)
Victor Stinner6bf992a2017-12-06 17:26:10 +01001474{
Victor Stinner91106cd2017-12-13 12:29:09 +01001475 cmdline->bytes_warning = Py_BytesWarningFlag;
1476 cmdline->debug = Py_DebugFlag;
1477 cmdline->inspect = Py_InspectFlag;
1478 cmdline->interactive = Py_InteractiveFlag;
1479 cmdline->isolated = Py_IsolatedFlag;
1480 cmdline->optimization_level = Py_OptimizeFlag;
1481 cmdline->dont_write_bytecode = Py_DontWriteBytecodeFlag;
1482 cmdline->no_user_site_directory = Py_NoUserSiteDirectory;
1483 cmdline->no_site_import = Py_NoSiteFlag;
1484 cmdline->use_unbuffered_io = Py_UnbufferedStdioFlag;
1485 cmdline->verbosity = Py_VerboseFlag;
1486 cmdline->quiet_flag = Py_QuietFlag;
1487#ifdef MS_WINDOWS
1488 cmdline->legacy_windows_fs_encoding = Py_LegacyWindowsFSEncodingFlag;
1489 cmdline->legacy_windows_stdio = Py_LegacyWindowsStdioFlag;
1490#endif
1491 cmdline->check_hash_pycs_mode = _Py_CheckHashBasedPycsMode ;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001492}
Victor Stinner91106cd2017-12-13 12:29:09 +01001493
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001494
1495void
1496_PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config)
1497{
1498 Py_IgnoreEnvironmentFlag = config->ignore_environment;
1499 Py_UTF8Mode = config->utf8_mode;
1500
1501 /* Random or non-zero hash seed */
1502 Py_HashRandomizationFlag = (config->use_hash_seed == 0 ||
1503 config->hash_seed != 0);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001504}
1505
1506
Victor Stinner19760862017-12-20 01:41:59 +01001507/* Set Py_xxx global configuration variables */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001508static void
Victor Stinnerddc163d2018-09-24 05:03:01 -07001509cmdline_set_global_config(_PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001510{
Victor Stinner91106cd2017-12-13 12:29:09 +01001511 Py_BytesWarningFlag = cmdline->bytes_warning;
1512 Py_DebugFlag = cmdline->debug;
1513 Py_InspectFlag = cmdline->inspect;
1514 Py_InteractiveFlag = cmdline->interactive;
1515 Py_IsolatedFlag = cmdline->isolated;
1516 Py_OptimizeFlag = cmdline->optimization_level;
1517 Py_DontWriteBytecodeFlag = cmdline->dont_write_bytecode;
1518 Py_NoUserSiteDirectory = cmdline->no_user_site_directory;
1519 Py_NoSiteFlag = cmdline->no_site_import;
1520 Py_UnbufferedStdioFlag = cmdline->use_unbuffered_io;
1521 Py_VerboseFlag = cmdline->verbosity;
1522 Py_QuietFlag = cmdline->quiet_flag;
1523 _Py_CheckHashBasedPycsMode = cmdline->check_hash_pycs_mode;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001524#ifdef MS_WINDOWS
Victor Stinner91106cd2017-12-13 12:29:09 +01001525 Py_LegacyWindowsFSEncodingFlag = cmdline->legacy_windows_fs_encoding;
1526 Py_LegacyWindowsStdioFlag = cmdline->legacy_windows_stdio;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001527#endif
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001528}
1529
1530
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001531static void
1532pymain_import_readline(_PyMain *pymain)
1533{
1534 if (Py_IsolatedFlag) {
1535 return;
1536 }
Victor Stinner19760862017-12-20 01:41:59 +01001537 if (!Py_InspectFlag && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001538 return;
1539 }
1540 if (!isatty(fileno(stdin))) {
1541 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001542 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001543
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001544 PyObject *mod = PyImport_ImportModule("readline");
1545 if (mod == NULL) {
1546 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 }
1548 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001549 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001551}
1552
1553
1554static FILE*
1555pymain_open_filename(_PyMain *pymain)
1556{
Victor Stinnerddc163d2018-09-24 05:03:01 -07001557 const _PyCoreConfig *config = &_PyGILState_GetInterpreterStateUnsafe()->core_config;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001558 FILE* fp;
1559
Victor Stinnerca719ac2017-12-20 18:00:19 +01001560 fp = _Py_wfopen(pymain->filename, L"r");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001561 if (fp == NULL) {
1562 char *cfilename_buffer;
1563 const char *cfilename;
1564 int err = errno;
Victor Stinner9dd76202017-12-21 16:20:32 +01001565 cfilename_buffer = _Py_EncodeLocaleRaw(pymain->filename, NULL);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001566 if (cfilename_buffer != NULL)
1567 cfilename = cfilename_buffer;
1568 else
1569 cfilename = "<unprintable file name>";
1570 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinnerddc163d2018-09-24 05:03:01 -07001571 config->program, cfilename, err, strerror(err));
Victor Stinner9dd76202017-12-21 16:20:32 +01001572 PyMem_RawFree(cfilename_buffer);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001573 pymain->status = 2;
1574 return NULL;
1575 }
1576
Victor Stinnerca719ac2017-12-20 18:00:19 +01001577 if (pymain->skip_first_line) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001578 int ch;
1579 /* Push back first newline so line numbers
1580 remain the same */
1581 while ((ch = getc(fp)) != EOF) {
1582 if (ch == '\n') {
1583 (void)ungetc(ch, fp);
1584 break;
1585 }
1586 }
1587 }
1588
1589 struct _Py_stat_struct sb;
1590 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 &&
1591 S_ISDIR(sb.st_mode)) {
1592 fprintf(stderr,
1593 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinnerddc163d2018-09-24 05:03:01 -07001594 config->program, pymain->filename);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001595 fclose(fp);
1596 pymain->status = 1;
1597 return NULL;
1598 }
1599
1600 return fp;
1601}
1602
1603
1604static void
Victor Stinner19760862017-12-20 01:41:59 +01001605pymain_run_filename(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001606{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001607 if (pymain->filename == NULL && pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001608 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner19760862017-12-20 01:41:59 +01001609 pymain_run_startup(cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001610 pymain_run_interactive_hook();
1611 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001612
1613 if (pymain->main_importer_path != NULL) {
1614 pymain->status = pymain_run_main_from_importer(pymain);
1615 return;
1616 }
1617
1618 FILE *fp;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001619 if (pymain->filename != NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001620 fp = pymain_open_filename(pymain);
1621 if (fp == NULL) {
1622 return;
1623 }
1624 }
1625 else {
1626 fp = stdin;
1627 }
1628
Victor Stinnerca719ac2017-12-20 18:00:19 +01001629 pymain->status = pymain_run_file(fp, pymain->filename, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001630}
1631
1632
1633static void
Victor Stinner19760862017-12-20 01:41:59 +01001634pymain_repl(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001637 opportunity to set it from Python. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001638 if (!Py_InspectFlag && config_get_env_var("PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 Py_InspectFlag = 1;
1640 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001641
Victor Stinner19760862017-12-20 01:41:59 +01001642 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001643 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001645
1646 Py_InspectFlag = 0;
1647 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001648
Victor Stinner19760862017-12-20 01:41:59 +01001649 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001650 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001651}
1652
1653
1654/* Parse the command line.
1655 Handle --version and --help options directly.
1656
1657 Return 1 if Python must exit.
1658 Return 0 on success.
1659 Set pymain->err and return -1 on failure. */
1660static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07001661pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config,
1662 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001663{
Victor Stinnerddc163d2018-09-24 05:03:01 -07001664 int res = pymain_parse_cmdline_impl(pymain, config, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001665 if (res < 0) {
1666 return -1;
1667 }
1668 if (res) {
Victor Stinnerddc163d2018-09-24 05:03:01 -07001669 pymain_usage(1, config->program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001670 pymain->status = 2;
1671 return 1;
1672 }
1673
Victor Stinnerca719ac2017-12-20 18:00:19 +01001674 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001675 /* Backup _PyOS_optind */
1676 _PyOS_optind--;
1677 }
1678
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001679 return 0;
1680}
1681
1682
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001683static const wchar_t*
Victor Stinner9cfc0022017-12-20 19:36:46 +01001684config_get_xoption(_PyCoreConfig *config, wchar_t *name)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001685{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001686 int nxoption = config->nxoption;
1687 wchar_t **xoptions = config->xoptions;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001688 for (int i=0; i < nxoption; i++) {
1689 wchar_t *option = xoptions[i];
Victor Stinnera7368ac2017-11-15 18:11:45 -08001690 size_t len;
1691 wchar_t *sep = wcschr(option, L'=');
1692 if (sep != NULL) {
1693 len = (sep - option);
1694 }
1695 else {
1696 len = wcslen(option);
1697 }
1698 if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
1699 return option;
1700 }
1701 }
1702 return NULL;
1703}
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001704
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001705
Victor Stinnera7368ac2017-11-15 18:11:45 -08001706static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001707pymain_str_to_int(const char *str, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001708{
1709 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001710 const char *endptr = str;
1711 long value = strtol(str, (char **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001712 if (*endptr != '\0' || errno == ERANGE) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001713 return -1;
1714 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001715 if (value < INT_MIN || value > INT_MAX) {
1716 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001717 }
1718
Victor Stinnera7368ac2017-11-15 18:11:45 -08001719 *result = (int)value;
1720 return 0;
1721}
1722
1723
1724static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001725pymain_wstr_to_int(const wchar_t *wstr, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001726{
1727 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001728 const wchar_t *endptr = wstr;
1729 long value = wcstol(wstr, (wchar_t **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001730 if (*endptr != '\0' || errno == ERANGE) {
1731 return -1;
1732 }
1733 if (value < INT_MIN || value > INT_MAX) {
1734 return -1;
1735 }
1736
1737 *result = (int)value;
1738 return 0;
1739}
1740
1741
Victor Stinner9cfc0022017-12-20 19:36:46 +01001742static _PyInitError
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001743config_init_tracemalloc(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001744{
1745 int nframe;
1746 int valid;
1747
Victor Stinner9cfc0022017-12-20 19:36:46 +01001748 const char *env = config_get_env_var("PYTHONTRACEMALLOC");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001749 if (env) {
1750 if (!pymain_str_to_int(env, &nframe)) {
1751 valid = (nframe >= 1);
1752 }
1753 else {
1754 valid = 0;
1755 }
1756 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001757 return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number "
1758 "of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001759 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001760 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001761 }
1762
Victor Stinner9cfc0022017-12-20 19:36:46 +01001763 const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001764 if (xoption) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001765 const wchar_t *sep = wcschr(xoption, L'=');
Victor Stinnera7368ac2017-11-15 18:11:45 -08001766 if (sep) {
1767 if (!pymain_wstr_to_int(sep + 1, &nframe)) {
1768 valid = (nframe >= 1);
1769 }
1770 else {
1771 valid = 0;
1772 }
1773 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001774 return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: "
1775 "invalid number of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001776 }
1777 }
1778 else {
1779 /* -X tracemalloc behaves as -X tracemalloc=1 */
1780 nframe = 1;
1781 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001782 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001783 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001784 return _Py_INIT_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001785}
1786
1787
1788static void
Victor Stinner9cfc0022017-12-20 19:36:46 +01001789get_env_flag(int *flag, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001790{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001791 const char *var = config_get_env_var(name);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001792 if (!var) {
1793 return;
1794 }
1795 int value;
1796 if (pymain_str_to_int(var, &value) < 0 || value < 0) {
1797 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
1798 value = 1;
1799 }
1800 if (*flag < value) {
1801 *flag = value;
1802 }
1803}
1804
1805
1806static void
Victor Stinnerddc163d2018-09-24 05:03:01 -07001807cmdline_get_env_flags(_PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001808{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001809 get_env_flag(&cmdline->debug, "PYTHONDEBUG");
1810 get_env_flag(&cmdline->verbosity, "PYTHONVERBOSE");
1811 get_env_flag(&cmdline->optimization_level, "PYTHONOPTIMIZE");
1812 get_env_flag(&cmdline->inspect, "PYTHONINSPECT");
1813 get_env_flag(&cmdline->dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
1814 get_env_flag(&cmdline->no_user_site_directory, "PYTHONNOUSERSITE");
1815 get_env_flag(&cmdline->use_unbuffered_io, "PYTHONUNBUFFERED");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001816#ifdef MS_WINDOWS
Victor Stinner9cfc0022017-12-20 19:36:46 +01001817 get_env_flag(&cmdline->legacy_windows_fs_encoding,
1818 "PYTHONLEGACYWINDOWSFSENCODING");
1819 get_env_flag(&cmdline->legacy_windows_stdio,
1820 "PYTHONLEGACYWINDOWSSTDIO");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001821#endif
1822}
1823
1824
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001825/* Set global variable variables from environment variables */
1826void
1827_Py_Initialize_ReadEnvVarsNoAlloc(void)
1828{
Victor Stinnerddc163d2018-09-24 05:03:01 -07001829 _PyCmdline cmdline;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001830 memset(&cmdline, 0, sizeof(cmdline));
1831
1832 cmdline_get_global_config(&cmdline);
1833 if (cmdline.isolated) {
1834 Py_IgnoreEnvironmentFlag = 1;
1835 cmdline.no_user_site_directory = 1;
1836 }
1837 if (!Py_IgnoreEnvironmentFlag) {
1838 cmdline_get_env_flags(&cmdline);
1839 }
1840 cmdline_set_global_config(&cmdline);
1841
1842 /* no need to call pymain_clear_cmdline(), no memory has been allocated */
1843}
1844
1845
Victor Stinner46972b72017-11-24 22:55:40 +01001846static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001847config_init_home(_PyCoreConfig *config)
Victor Stinner1f151112017-11-23 10:43:14 +01001848{
1849 wchar_t *home;
1850
Victor Stinner31a83932017-12-04 13:39:15 +01001851 /* If Py_SetPythonHome() was called, use its value */
1852 home = _Py_path_config.home;
Victor Stinner1f151112017-11-23 10:43:14 +01001853 if (home) {
Victor Stinner46972b72017-11-24 22:55:40 +01001854 config->home = _PyMem_RawWcsdup(home);
1855 if (config->home == NULL) {
1856 return _Py_INIT_NO_MEMORY();
Victor Stinner1f151112017-11-23 10:43:14 +01001857 }
Victor Stinner46972b72017-11-24 22:55:40 +01001858 return _Py_INIT_OK();
Victor Stinner1f151112017-11-23 10:43:14 +01001859 }
1860
Victor Stinner46972b72017-11-24 22:55:40 +01001861 int res = config_get_env_var_dup(&home, L"PYTHONHOME", "PYTHONHOME");
Victor Stinner1f151112017-11-23 10:43:14 +01001862 if (res < 0) {
Victor Stinner46972b72017-11-24 22:55:40 +01001863 return DECODE_LOCALE_ERR("PYTHONHOME", res);
Victor Stinner1f151112017-11-23 10:43:14 +01001864 }
Victor Stinner46972b72017-11-24 22:55:40 +01001865 config->home = home;
1866 return _Py_INIT_OK();
Victor Stinnerd4341102017-11-23 00:12:09 +01001867}
1868
1869
Victor Stinner358e5e12017-12-15 00:51:22 +01001870static _PyInitError
1871config_init_hash_seed(_PyCoreConfig *config)
1872{
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001873 const char *seed_text = config_get_env_var("PYTHONHASHSEED");
1874 int use_hash_seed;
1875 unsigned long hash_seed;
1876 if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
1877 return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
1878 "or an integer in range [0; 4294967295]");
Victor Stinner358e5e12017-12-15 00:51:22 +01001879 }
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001880 config->use_hash_seed = use_hash_seed;
1881 config->hash_seed = hash_seed;
Victor Stinner358e5e12017-12-15 00:51:22 +01001882 return _Py_INIT_OK();
1883}
1884
1885
Victor Stinner9cfc0022017-12-20 19:36:46 +01001886static _PyInitError
1887config_init_utf8_mode(_PyCoreConfig *config)
Victor Stinner91106cd2017-12-13 12:29:09 +01001888{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001889 const wchar_t *xopt = config_get_xoption(config, L"utf8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001890 if (xopt) {
1891 wchar_t *sep = wcschr(xopt, L'=');
1892 if (sep) {
1893 xopt = sep + 1;
1894 if (wcscmp(xopt, L"1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001895 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001896 }
1897 else if (wcscmp(xopt, L"0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001898 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001899 }
1900 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001901 return _Py_INIT_USER_ERR("invalid -X utf8 option value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001902 }
1903 }
1904 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001905 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001906 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001907 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001908 }
1909
Victor Stinner9cfc0022017-12-20 19:36:46 +01001910 const char *opt = config_get_env_var("PYTHONUTF8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001911 if (opt) {
1912 if (strcmp(opt, "1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001913 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001914 }
1915 else if (strcmp(opt, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001916 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001917 }
1918 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001919 return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
1920 "variable value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001921 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001922 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001923 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001924
1925 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001926}
Victor Stinner46972b72017-11-24 22:55:40 +01001927
1928
Victor Stinner9cfc0022017-12-20 19:36:46 +01001929static _PyInitError
1930config_read_env_vars(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001931{
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001932 assert(!config->ignore_environment);
1933
1934 if (config->allocator == NULL) {
1935 config->allocator = config_get_env_var("PYTHONMALLOC");
1936 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001937
Victor Stinner9cfc0022017-12-20 19:36:46 +01001938 if (config_get_env_var("PYTHONDUMPREFS")) {
1939 config->dump_refs = 1;
1940 }
1941 if (config_get_env_var("PYTHONMALLOCSTATS")) {
1942 config->malloc_stats = 1;
Victor Stinner31a83932017-12-04 13:39:15 +01001943 }
1944
Victor Stinner98c49c62018-08-29 01:13:29 +02001945 const char *env = config_get_env_var("PYTHONCOERCECLOCALE");
1946 if (env) {
1947 if (strcmp(env, "0") == 0) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07001948 if (config->coerce_c_locale < 0) {
1949 config->coerce_c_locale = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001950 }
Victor Stinner98c49c62018-08-29 01:13:29 +02001951 }
1952 else if (strcmp(env, "warn") == 0) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07001953 config->coerce_c_locale_warn = 1;
Victor Stinner98c49c62018-08-29 01:13:29 +02001954 }
1955 else {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07001956 if (config->coerce_c_locale < 0) {
1957 config->coerce_c_locale = 1;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001958 }
Victor Stinner94540602017-12-16 04:54:22 +01001959 }
1960 }
1961
Victor Stinner9cfc0022017-12-20 19:36:46 +01001962 wchar_t *path;
1963 int res = config_get_env_var_dup(&path, L"PYTHONPATH", "PYTHONPATH");
1964 if (res < 0) {
Miss Islington (bot)6414da92018-05-19 16:14:42 -07001965 return DECODE_LOCALE_ERR("PYTHONPATH", res);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001966 }
1967 config->module_search_path_env = path;
1968
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001969 if (config->use_hash_seed < 0) {
1970 _PyInitError err = config_init_hash_seed(config);
1971 if (_Py_INIT_FAILED(err)) {
1972 return err;
1973 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001974 }
1975
1976 return _Py_INIT_OK();
1977}
1978
1979
1980static _PyInitError
1981config_read_complex_options(_PyCoreConfig *config)
1982{
1983 /* More complex options configured by env var and -X option */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001984 if (config->faulthandler < 0) {
1985 if (config_get_env_var("PYTHONFAULTHANDLER")
1986 || config_get_xoption(config, L"faulthandler")) {
1987 config->faulthandler = 1;
1988 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001989 }
1990 if (config_get_env_var("PYTHONPROFILEIMPORTTIME")
1991 || config_get_xoption(config, L"importtime")) {
1992 config->import_time = 1;
1993 }
1994 if (config_get_xoption(config, L"dev" ) ||
1995 config_get_env_var("PYTHONDEVMODE"))
1996 {
1997 config->dev_mode = 1;
Victor Stinner9cfc0022017-12-20 19:36:46 +01001998 }
1999
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002000 if (config->tracemalloc < 0) {
2001 _PyInitError err = config_init_tracemalloc(config);
2002 if (_Py_INIT_FAILED(err)) {
2003 return err;
2004 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002005 }
2006 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002007}
2008
2009
Victor Stinnera7368ac2017-11-15 18:11:45 -08002010/* Parse command line options and environment variables.
2011 This code must not use Python runtime apart PyMem_Raw memory allocator.
2012
2013 Return 0 on success.
2014 Return 1 if Python is done and must exit.
2015 Set pymain->err and return -1 on error. */
2016static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002017pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
2018 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002019{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002020 _PyInitError err;
2021
Victor Stinnerddc163d2018-09-24 05:03:01 -07002022 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01002023 if (res != 0) {
2024 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002025 }
2026
Victor Stinner9cfc0022017-12-20 19:36:46 +01002027 /* Set Py_IgnoreEnvironmentFlag for Py_GETENV() */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002028 Py_IgnoreEnvironmentFlag = config->ignore_environment || cmdline->isolated;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002029
2030 /* Get environment variables */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002031 if (!Py_IgnoreEnvironmentFlag) {
2032 cmdline_get_env_flags(cmdline);
2033 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002034
2035 err = cmdline_init_env_warnoptions(cmdline);
2036 if (_Py_INIT_FAILED(err)) {
2037 pymain->err = err;
2038 return -1;
2039 }
2040
2041#ifdef MS_WINDOWS
2042 if (cmdline->legacy_windows_fs_encoding) {
2043 config->utf8_mode = 0;
2044 }
2045#endif
2046
Victor Stinnerddc163d2018-09-24 05:03:01 -07002047 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01002048 return -1;
2049 }
2050
Victor Stinner2b822a02018-01-25 09:18:36 +01002051 /* On Windows, _PyPathConfig_Init() modifies Py_IsolatedFlag and
2052 Py_NoSiteFlag variables if a "._pth" file is found. */
2053 int init_isolated = Py_IsolatedFlag;
2054 int init_no_site = Py_NoSiteFlag;
2055 Py_IsolatedFlag = cmdline->isolated;
2056 Py_NoSiteFlag = cmdline->no_site_import;
Victor Stinner8ded5b82018-01-24 17:03:28 +01002057
Victor Stinner9cfc0022017-12-20 19:36:46 +01002058 err = _PyCoreConfig_Read(config);
Victor Stinner2b822a02018-01-25 09:18:36 +01002059
2060 cmdline->isolated = Py_IsolatedFlag;
2061 cmdline->no_site_import = Py_NoSiteFlag;
2062 Py_IsolatedFlag = init_isolated;
2063 Py_NoSiteFlag = init_no_site;
2064
Victor Stinner31a83932017-12-04 13:39:15 +01002065 if (_Py_INIT_FAILED(err)) {
2066 pymain->err = err;
2067 return -1;
2068 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002069 return 0;
2070}
2071
2072
Victor Stinner19760862017-12-20 01:41:59 +01002073/* Read the configuration, but initialize also the LC_CTYPE locale:
2074 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
Victor Stinnera7368ac2017-11-15 18:11:45 -08002075static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002076pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002077{
Victor Stinner80a0eba2018-08-23 12:41:35 +02002078 int init_utf8_mode = Py_UTF8Mode;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002079 _PyCoreConfig save_config = _PyCoreConfig_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01002080 int res = -1;
2081
Victor Stinner94540602017-12-16 04:54:22 +01002082 char *oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
2083 if (oldloc == NULL) {
2084 pymain->err = _Py_INIT_NO_MEMORY();
2085 goto done;
2086 }
2087
2088 /* Reconfigure the locale to the default for this process */
2089 _Py_SetLocaleFromEnv(LC_ALL);
2090
2091 int locale_coerced = 0;
2092 int loops = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002093 int init_ignore_env = config->ignore_environment;
2094
2095 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
2096 pymain->err = _Py_INIT_NO_MEMORY();
2097 goto done;
2098 }
Victor Stinner94540602017-12-16 04:54:22 +01002099
2100 while (1) {
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002101 int init_utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002102 int encoding_changed = 0;
2103
2104 /* Watchdog to prevent an infinite loop */
2105 loops++;
2106 if (loops == 3) {
2107 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
2108 "reading the configuration");
2109 goto done;
2110 }
2111
Victor Stinner80a0eba2018-08-23 12:41:35 +02002112 /* bpo-34207: Py_DecodeLocale(), Py_EncodeLocale() and similar
2113 functions depend on Py_UTF8Mode. */
2114 Py_UTF8Mode = config->utf8_mode;
2115
Victor Stinnerddc163d2018-09-24 05:03:01 -07002116 if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01002117 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01002118 }
2119
Victor Stinnerddc163d2018-09-24 05:03:01 -07002120 int conf_res = pymain_read_conf_impl(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002121 if (conf_res != 0) {
2122 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01002123 goto done;
2124 }
2125
2126 /* The legacy C locale assumes ASCII as the default text encoding, which
2127 * causes problems not only for the CPython runtime, but also other
2128 * components like GNU readline.
2129 *
2130 * Accordingly, when the CLI detects it, it attempts to coerce it to a
2131 * more capable UTF-8 based alternative.
2132 *
2133 * See the documentation of the PYTHONCOERCECLOCALE setting for more
2134 * details.
2135 */
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002136 if (config->coerce_c_locale && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01002137 locale_coerced = 1;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002138 _Py_CoerceLegacyLocale(config);
Victor Stinner94540602017-12-16 04:54:22 +01002139 encoding_changed = 1;
2140 }
2141
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002142 if (init_utf8_mode == -1) {
2143 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01002144 /* UTF-8 Mode enabled */
2145 encoding_changed = 1;
2146 }
2147 }
2148 else {
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002149 if (config->utf8_mode != init_utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01002150 encoding_changed = 1;
2151 }
2152 }
2153
2154 if (!encoding_changed) {
2155 break;
2156 }
2157
2158 /* Reset the configuration, except UTF-8 Mode. Set Py_UTF8Mode for
2159 Py_DecodeLocale(). Reset Py_IgnoreEnvironmentFlag, modified by
Victor Stinner8ded5b82018-01-24 17:03:28 +01002160 pymain_read_conf_impl(). Reset Py_IsolatedFlag and Py_NoSiteFlag
2161 modified by _PyCoreConfig_Read(). */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002162 int new_utf8_mode = config->utf8_mode;
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002163 int new_coerce_c_locale = config->coerce_c_locale;
Victor Stinner94540602017-12-16 04:54:22 +01002164 Py_IgnoreEnvironmentFlag = init_ignore_env;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002165 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
2166 pymain->err = _Py_INIT_NO_MEMORY();
2167 goto done;
2168 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002169 pymain_clear_cmdline(pymain, cmdline);
Miss Islington (bot)f71e7432019-01-22 08:42:13 -08002170 pymain_clear_pymain(pymain);
Miss Islington (bot)046da162018-06-15 15:26:29 -07002171 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002172
2173 cmdline_get_global_config(cmdline);
2174 _PyCoreConfig_GetGlobalConfig(config);
2175 config->utf8_mode = new_utf8_mode;
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002176 config->coerce_c_locale = new_coerce_c_locale;
Victor Stinner94540602017-12-16 04:54:22 +01002177
2178 /* The encoding changed: read again the configuration
2179 with the new encoding */
2180 }
2181 res = 0;
2182
2183done:
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002184 _PyCoreConfig_Clear(&save_config);
Victor Stinner94540602017-12-16 04:54:22 +01002185 if (oldloc != NULL) {
2186 setlocale(LC_ALL, oldloc);
2187 PyMem_RawFree(oldloc);
2188 }
Victor Stinner80a0eba2018-08-23 12:41:35 +02002189 Py_UTF8Mode = init_utf8_mode ;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002190 return res;
2191}
2192
Victor Stinner91106cd2017-12-13 12:29:09 +01002193
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002194static void
2195config_init_locale(_PyCoreConfig *config)
Victor Stinner9cfc0022017-12-20 19:36:46 +01002196{
Victor Stinnerdf738d52018-11-30 12:19:48 +01002197 /* Test also if coerce_c_locale equals 1: PYTHONCOERCECLOCALE=1 doesn't
2198 imply that the C locale is always coerced. It is only coerced if
2199 if the LC_CTYPE locale is "C". */
2200 if (config->coerce_c_locale != 0) {
Victor Stinner144f1e22018-09-17 18:01:39 -07002201 /* The C locale enables the C locale coercion (PEP 538) */
2202 if (_Py_LegacyLocaleDetected()) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002203 config->coerce_c_locale = 1;
Victor Stinner144f1e22018-09-17 18:01:39 -07002204 }
Victor Stinnerdf738d52018-11-30 12:19:48 +01002205 else {
2206 config->coerce_c_locale = 0;
2207 }
Victor Stinner144f1e22018-09-17 18:01:39 -07002208 }
2209
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002210#ifndef MS_WINDOWS
2211 if (config->utf8_mode < 0) {
2212 /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */
2213 const char *ctype_loc = setlocale(LC_CTYPE, NULL);
2214 if (ctype_loc != NULL
2215 && (strcmp(ctype_loc, "C") == 0
2216 || strcmp(ctype_loc, "POSIX") == 0))
2217 {
2218 config->utf8_mode = 1;
2219 }
2220 }
2221#endif
Victor Stinner9cfc0022017-12-20 19:36:46 +01002222}
2223
2224
Victor Stinner8ded5b82018-01-24 17:03:28 +01002225static _PyInitError
2226config_init_module_search_paths(_PyCoreConfig *config)
2227{
2228 assert(config->module_search_paths == NULL);
2229 assert(config->nmodule_search_path < 0);
2230
2231 config->nmodule_search_path = 0;
2232
2233 const wchar_t *sys_path = Py_GetPath();
2234 const wchar_t delim = DELIM;
2235 const wchar_t *p = sys_path;
2236 while (1) {
2237 p = wcschr(sys_path, delim);
2238 if (p == NULL) {
2239 p = sys_path + wcslen(sys_path); /* End of string */
2240 }
2241
2242 size_t path_len = (p - sys_path);
2243 wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t));
2244 if (path == NULL) {
2245 return _Py_INIT_NO_MEMORY();
2246 }
2247 memcpy(path, sys_path, path_len * sizeof(wchar_t));
2248 path[path_len] = L'\0';
2249
2250 _PyInitError err = wstrlist_append(&config->nmodule_search_path,
2251 &config->module_search_paths,
2252 path);
2253 PyMem_RawFree(path);
2254 if (_Py_INIT_FAILED(err)) {
2255 return err;
2256 }
2257
2258 if (*p == '\0') {
2259 break;
2260 }
2261 sys_path = p + 1;
2262 }
2263 return _Py_INIT_OK();
2264}
2265
2266
2267static _PyInitError
2268config_init_path_config(_PyCoreConfig *config)
2269{
2270 _PyInitError err = _PyPathConfig_Init(config);
2271 if (_Py_INIT_FAILED(err)) {
2272 return err;
2273 }
2274
2275 if (config->nmodule_search_path < 0) {
2276 err = config_init_module_search_paths(config);
2277 if (_Py_INIT_FAILED(err)) {
2278 return err;
2279 }
2280 }
2281
2282 if (config->executable == NULL) {
2283 config->executable = _PyMem_RawWcsdup(Py_GetProgramFullPath());
2284 if (config->executable == NULL) {
2285 return _Py_INIT_NO_MEMORY();
2286 }
2287 }
2288
2289 if (config->prefix == NULL) {
2290 config->prefix = _PyMem_RawWcsdup(Py_GetPrefix());
2291 if (config->prefix == NULL) {
2292 return _Py_INIT_NO_MEMORY();
2293 }
2294 }
2295
2296 if (config->exec_prefix == NULL) {
2297 config->exec_prefix = _PyMem_RawWcsdup(Py_GetExecPrefix());
2298 if (config->exec_prefix == NULL) {
2299 return _Py_INIT_NO_MEMORY();
2300 }
2301 }
2302
2303 if (config->base_prefix == NULL) {
2304 config->base_prefix = _PyMem_RawWcsdup(config->prefix);
2305 if (config->base_prefix == NULL) {
2306 return _Py_INIT_NO_MEMORY();
2307 }
2308 }
2309
2310 if (config->base_exec_prefix == NULL) {
2311 config->base_exec_prefix = _PyMem_RawWcsdup(config->exec_prefix);
2312 if (config->base_exec_prefix == NULL) {
2313 return _Py_INIT_NO_MEMORY();
2314 }
2315 }
2316
2317 return _Py_INIT_OK();
2318}
2319
Victor Stinnerda273412017-12-15 01:46:02 +01002320/* Read configuration settings from standard locations
2321 *
2322 * This function doesn't make any changes to the interpreter state - it
2323 * merely populates any missing configuration settings. This allows an
2324 * embedding application to completely override a config option by
2325 * setting it before calling this function, or else modify the default
2326 * setting before passing the fully populated config to Py_EndInitialization.
2327 *
2328 * More advanced selective initialization tricks are possible by calling
2329 * this function multiple times with various preconfigured settings.
2330 */
2331
2332_PyInitError
2333_PyCoreConfig_Read(_PyCoreConfig *config)
2334{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002335 _PyInitError err;
2336
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002337 _PyCoreConfig_GetGlobalConfig(config);
2338
2339 assert(config->ignore_environment >= 0);
2340 if (!config->ignore_environment) {
2341 err = config_read_env_vars(config);
2342 if (_Py_INIT_FAILED(err)) {
2343 return err;
2344 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002345 }
2346
Victor Stinner9cfc0022017-12-20 19:36:46 +01002347 /* -X options */
2348 if (config_get_xoption(config, L"showrefcount")) {
2349 config->show_ref_count = 1;
2350 }
2351 if (config_get_xoption(config, L"showalloccount")) {
2352 config->show_alloc_count = 1;
2353 }
2354
2355 err = config_read_complex_options(config);
2356 if (_Py_INIT_FAILED(err)) {
2357 return err;
2358 }
2359
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002360 if (config->utf8_mode < 0) {
2361 err = config_init_utf8_mode(config);
2362 if (_Py_INIT_FAILED(err)) {
2363 return err;
2364 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002365 }
2366
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002367 if (config->home == NULL) {
2368 err = config_init_home(config);
2369 if (_Py_INIT_FAILED(err)) {
2370 return err;
2371 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002372 }
2373
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002374 if (config->program_name == NULL) {
2375 err = config_init_program_name(config);
2376 if (_Py_INIT_FAILED(err)) {
2377 return err;
2378 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002379 }
2380
Steve Dowere8510492018-11-17 20:42:08 -08002381 if (config->executable == NULL) {
2382 err = config_init_executable(config);
2383 if (_Py_INIT_FAILED(err)) {
2384 return err;
2385 }
2386 }
2387
Victor Stinnerdf738d52018-11-30 12:19:48 +01002388 if (config->coerce_c_locale != 0 || config->utf8_mode < 0) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002389 config_init_locale(config);
Victor Stinner94540602017-12-16 04:54:22 +01002390 }
2391
Victor Stinner8ded5b82018-01-24 17:03:28 +01002392 if (!config->_disable_importlib) {
2393 err = config_init_path_config(config);
2394 if (_Py_INIT_FAILED(err)) {
2395 return err;
2396 }
2397 }
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002398
2399 /* default values */
2400 if (config->dev_mode) {
2401 if (config->faulthandler < 0) {
2402 config->faulthandler = 1;
2403 }
2404 if (config->allocator == NULL) {
2405 config->allocator = "debug";
2406 }
2407 }
2408 if (config->install_signal_handlers < 0) {
2409 config->install_signal_handlers = 1;
2410 }
2411 if (config->use_hash_seed < 0) {
2412 config->use_hash_seed = 0;
2413 config->hash_seed = 0;
2414 }
2415 if (config->faulthandler < 0) {
2416 config->faulthandler = 0;
2417 }
2418 if (config->tracemalloc < 0) {
2419 config->tracemalloc = 0;
2420 }
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002421 if (config->coerce_c_locale < 0) {
2422 config->coerce_c_locale = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002423 }
2424 if (config->utf8_mode < 0) {
2425 config->utf8_mode = 0;
2426 }
2427 if (config->argc < 0) {
2428 config->argc = 0;
2429 }
2430
Victor Stinnerda273412017-12-15 01:46:02 +01002431 return _Py_INIT_OK();
2432}
2433
2434
2435void
2436_PyCoreConfig_Clear(_PyCoreConfig *config)
2437{
2438#define CLEAR(ATTR) \
2439 do { \
2440 PyMem_RawFree(ATTR); \
2441 ATTR = NULL; \
2442 } while (0)
Victor Stinner8ded5b82018-01-24 17:03:28 +01002443#define CLEAR_WSTRLIST(LEN, LIST) \
2444 do { \
2445 clear_wstrlist(LEN, LIST); \
2446 LEN = 0; \
2447 LIST = NULL; \
2448 } while (0)
Victor Stinnerda273412017-12-15 01:46:02 +01002449
2450 CLEAR(config->module_search_path_env);
2451 CLEAR(config->home);
2452 CLEAR(config->program_name);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002453 CLEAR(config->program);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002454
Victor Stinner8ded5b82018-01-24 17:03:28 +01002455 CLEAR_WSTRLIST(config->argc, config->argv);
2456 config->argc = -1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002457
Victor Stinner8ded5b82018-01-24 17:03:28 +01002458 CLEAR_WSTRLIST(config->nwarnoption, config->warnoptions);
2459 CLEAR_WSTRLIST(config->nxoption, config->xoptions);
2460 CLEAR_WSTRLIST(config->nmodule_search_path, config->module_search_paths);
2461 config->nmodule_search_path = -1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002462
Victor Stinner8ded5b82018-01-24 17:03:28 +01002463 CLEAR(config->executable);
2464 CLEAR(config->prefix);
2465 CLEAR(config->base_prefix);
2466 CLEAR(config->exec_prefix);
2467 CLEAR(config->base_exec_prefix);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002468#undef CLEAR
Victor Stinner8ded5b82018-01-24 17:03:28 +01002469#undef CLEAR_WSTRLIST
Victor Stinnerda273412017-12-15 01:46:02 +01002470}
2471
2472
2473int
2474_PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
2475{
2476 _PyCoreConfig_Clear(config);
2477
2478#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
Victor Stinnerc4bca952017-12-19 23:48:17 +01002479#define COPY_STR_ATTR(ATTR) \
2480 do { \
2481 if (config2->ATTR != NULL) { \
2482 config->ATTR = _PyMem_RawWcsdup(config2->ATTR); \
2483 if (config->ATTR == NULL) { \
2484 return -1; \
2485 } \
2486 } \
2487 } while (0)
Victor Stinner8ded5b82018-01-24 17:03:28 +01002488#define COPY_WSTRLIST(LEN, LIST) \
2489 do { \
2490 if (config2->LIST != NULL) { \
2491 config->LIST = copy_wstrlist(config2->LEN, config2->LIST); \
2492 if (config->LIST == NULL) { \
2493 return -1; \
2494 } \
2495 } \
2496 config->LEN = config2->LEN; \
2497 } while (0)
Victor Stinnerc4bca952017-12-19 23:48:17 +01002498
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002499 COPY_ATTR(install_signal_handlers);
Victor Stinnerda273412017-12-15 01:46:02 +01002500 COPY_ATTR(ignore_environment);
2501 COPY_ATTR(use_hash_seed);
2502 COPY_ATTR(hash_seed);
2503 COPY_ATTR(_disable_importlib);
2504 COPY_ATTR(allocator);
2505 COPY_ATTR(dev_mode);
2506 COPY_ATTR(faulthandler);
2507 COPY_ATTR(tracemalloc);
2508 COPY_ATTR(import_time);
2509 COPY_ATTR(show_ref_count);
2510 COPY_ATTR(show_alloc_count);
2511 COPY_ATTR(dump_refs);
2512 COPY_ATTR(malloc_stats);
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002513
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002514 COPY_ATTR(coerce_c_locale);
2515 COPY_ATTR(coerce_c_locale_warn);
Victor Stinnerda273412017-12-15 01:46:02 +01002516 COPY_ATTR(utf8_mode);
Victor Stinnerda273412017-12-15 01:46:02 +01002517
2518 COPY_STR_ATTR(module_search_path_env);
2519 COPY_STR_ATTR(home);
2520 COPY_STR_ATTR(program_name);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002521 COPY_STR_ATTR(program);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002522
Victor Stinner8ded5b82018-01-24 17:03:28 +01002523 COPY_WSTRLIST(argc, argv);
2524 COPY_WSTRLIST(nwarnoption, warnoptions);
2525 COPY_WSTRLIST(nxoption, xoptions);
2526 COPY_WSTRLIST(nmodule_search_path, module_search_paths);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002527
Victor Stinner8ded5b82018-01-24 17:03:28 +01002528 COPY_STR_ATTR(executable);
2529 COPY_STR_ATTR(prefix);
2530 COPY_STR_ATTR(base_prefix);
2531 COPY_STR_ATTR(exec_prefix);
2532 COPY_STR_ATTR(base_exec_prefix);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002533
Victor Stinnerc4bca952017-12-19 23:48:17 +01002534#undef COPY_ATTR
Victor Stinnerda273412017-12-15 01:46:02 +01002535#undef COPY_STR_ATTR
Victor Stinner8ded5b82018-01-24 17:03:28 +01002536#undef COPY_WSTRLIST
Victor Stinnerda273412017-12-15 01:46:02 +01002537 return 0;
2538}
2539
2540
Victor Stinner35c28d52018-11-14 02:01:52 +01002541PyObject *
2542_PyCoreConfig_AsDict(const _PyCoreConfig *config)
2543{
2544 PyObject *dict, *obj;
2545
2546 dict = PyDict_New();
2547 if (dict == NULL) {
2548 return NULL;
2549 }
2550
Victor Stinner35c28d52018-11-14 02:01:52 +01002551#define SET_ITEM(KEY, EXPR) \
2552 do { \
2553 obj = (EXPR); \
2554 if (obj == NULL) { \
2555 return NULL; \
2556 } \
2557 int res = PyDict_SetItemString(dict, (KEY), obj); \
2558 Py_DECREF(obj); \
2559 if (res < 0) { \
2560 goto fail; \
2561 } \
2562 } while (0)
Victor Stinner9ee1d422018-11-14 18:58:01 +01002563#define FROM_STRING(STR) \
2564 ((STR != NULL) ? \
2565 PyUnicode_FromString(STR) \
2566 : (Py_INCREF(Py_None), Py_None))
2567#define SET_ITEM_INT(ATTR) \
2568 SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR))
2569#define SET_ITEM_UINT(ATTR) \
2570 SET_ITEM(#ATTR, PyLong_FromUnsignedLong(config->ATTR))
2571#define SET_ITEM_STR(ATTR) \
2572 SET_ITEM(#ATTR, FROM_STRING(config->ATTR))
2573#define FROM_WSTRING(STR) \
2574 ((STR != NULL) ? \
2575 PyUnicode_FromWideChar(STR, -1) \
2576 : (Py_INCREF(Py_None), Py_None))
2577#define SET_ITEM_WSTR(ATTR) \
2578 SET_ITEM(#ATTR, FROM_WSTRING(config->ATTR))
2579#define SET_ITEM_WSTRLIST(NOPTION, OPTIONS) \
2580 SET_ITEM(#OPTIONS, _Py_wstrlist_as_pylist(config->NOPTION, config->OPTIONS))
Victor Stinner35c28d52018-11-14 02:01:52 +01002581
Victor Stinner9ee1d422018-11-14 18:58:01 +01002582 SET_ITEM_INT(install_signal_handlers);
2583 SET_ITEM_INT(ignore_environment);
2584 SET_ITEM_INT(use_hash_seed);
2585 SET_ITEM_UINT(hash_seed);
2586 SET_ITEM_STR(allocator);
2587 SET_ITEM_INT(dev_mode);
2588 SET_ITEM_INT(faulthandler);
2589 SET_ITEM_INT(tracemalloc);
2590 SET_ITEM_INT(import_time);
2591 SET_ITEM_INT(show_ref_count);
2592 SET_ITEM_INT(show_alloc_count);
2593 SET_ITEM_INT(dump_refs);
2594 SET_ITEM_INT(malloc_stats);
2595 SET_ITEM_INT(coerce_c_locale);
2596 SET_ITEM_INT(coerce_c_locale_warn);
2597 SET_ITEM_INT(utf8_mode);
2598 SET_ITEM_WSTR(program_name);
2599 SET_ITEM_WSTRLIST(argc, argv);
2600 SET_ITEM_WSTR(program);
2601 SET_ITEM_WSTRLIST(nxoption, xoptions);
2602 SET_ITEM_WSTRLIST(nwarnoption, warnoptions);
2603 SET_ITEM_WSTR(module_search_path_env);
2604 SET_ITEM_WSTR(home);
2605 SET_ITEM_WSTRLIST(nmodule_search_path, module_search_paths);
2606 SET_ITEM_WSTR(executable);
2607 SET_ITEM_WSTR(prefix);
2608 SET_ITEM_WSTR(base_prefix);
2609 SET_ITEM_WSTR(exec_prefix);
2610 SET_ITEM_WSTR(base_exec_prefix);
2611 SET_ITEM_INT(_disable_importlib);
Victor Stinner35c28d52018-11-14 02:01:52 +01002612
2613 return dict;
2614
2615fail:
2616 Py_DECREF(dict);
2617 return NULL;
2618
2619#undef FROM_STRING
2620#undef FROM_WSTRING
2621#undef SET_ITEM
Victor Stinner9ee1d422018-11-14 18:58:01 +01002622#undef SET_ITEM_INT
2623#undef SET_ITEM_UINT
2624#undef SET_ITEM_STR
2625#undef SET_ITEM_WSTR
2626#undef SET_ITEM_WSTRLIST
Victor Stinner35c28d52018-11-14 02:01:52 +01002627}
2628
2629
Victor Stinnerda273412017-12-15 01:46:02 +01002630void
2631_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
2632{
2633 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002634 Py_CLEAR(config->executable);
2635 Py_CLEAR(config->prefix);
2636 Py_CLEAR(config->base_prefix);
2637 Py_CLEAR(config->exec_prefix);
2638 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002639 Py_CLEAR(config->warnoptions);
2640 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002641 Py_CLEAR(config->module_search_path);
Victor Stinnerda273412017-12-15 01:46:02 +01002642}
2643
2644
2645static PyObject*
2646config_copy_attr(PyObject *obj)
2647{
2648 if (PyUnicode_Check(obj)) {
2649 Py_INCREF(obj);
2650 return obj;
2651 }
2652 else if (PyList_Check(obj)) {
2653 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
2654 }
2655 else if (PyDict_Check(obj)) {
2656 /* The dict type is used for xoptions. Make the assumption that keys
2657 and values are immutables */
2658 return PyDict_Copy(obj);
2659 }
2660 else {
2661 PyErr_Format(PyExc_TypeError,
2662 "cannot copy config attribute of type %.200s",
2663 Py_TYPE(obj)->tp_name);
2664 return NULL;
2665 }
2666}
2667
2668
2669int
2670_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
2671 const _PyMainInterpreterConfig *config2)
2672{
2673 _PyMainInterpreterConfig_Clear(config);
2674
Victor Stinner88cbea42018-11-14 02:45:25 +01002675#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
Victor Stinner9ee1d422018-11-14 18:58:01 +01002676#define COPY_OBJ_ATTR(ATTR) \
Victor Stinnerda273412017-12-15 01:46:02 +01002677 do { \
Victor Stinner9ee1d422018-11-14 18:58:01 +01002678 if (config2->ATTR != NULL) { \
2679 config->ATTR = config_copy_attr(config2->ATTR); \
2680 if (config->ATTR == NULL) { \
Victor Stinnerda273412017-12-15 01:46:02 +01002681 return -1; \
2682 } \
2683 } \
2684 } while (0)
2685
Victor Stinner88cbea42018-11-14 02:45:25 +01002686 COPY_ATTR(install_signal_handlers);
2687 COPY_OBJ_ATTR(argv);
2688 COPY_OBJ_ATTR(executable);
2689 COPY_OBJ_ATTR(prefix);
2690 COPY_OBJ_ATTR(base_prefix);
2691 COPY_OBJ_ATTR(exec_prefix);
2692 COPY_OBJ_ATTR(base_exec_prefix);
2693 COPY_OBJ_ATTR(warnoptions);
2694 COPY_OBJ_ATTR(xoptions);
2695 COPY_OBJ_ATTR(module_search_path);
Victor Stinnerda273412017-12-15 01:46:02 +01002696#undef COPY_ATTR
Victor Stinner88cbea42018-11-14 02:45:25 +01002697#undef COPY_OBJ_ATTR
Victor Stinnerda273412017-12-15 01:46:02 +01002698 return 0;
2699}
2700
2701
Victor Stinner35c28d52018-11-14 02:01:52 +01002702PyObject*
2703_PyMainInterpreterConfig_AsDict(const _PyMainInterpreterConfig *config)
2704{
2705 PyObject *dict, *obj;
2706 int res;
2707
2708 dict = PyDict_New();
2709 if (dict == NULL) {
2710 return NULL;
2711 }
2712
Victor Stinner9ee1d422018-11-14 18:58:01 +01002713#define SET_ITEM_INT(ATTR) \
2714 do { \
2715 obj = PyLong_FromLong(config->ATTR); \
2716 if (obj == NULL) { \
2717 goto fail; \
2718 } \
2719 res = PyDict_SetItemString(dict, #ATTR, obj); \
2720 Py_DECREF(obj); \
2721 if (res < 0) { \
2722 goto fail; \
2723 } \
2724 } while (0)
Victor Stinner35c28d52018-11-14 02:01:52 +01002725
Victor Stinner9ee1d422018-11-14 18:58:01 +01002726#define SET_ITEM_OBJ(ATTR) \
2727 do { \
2728 obj = config->ATTR; \
2729 if (obj == NULL) { \
2730 obj = Py_None; \
2731 } \
2732 res = PyDict_SetItemString(dict, #ATTR, obj); \
2733 if (res < 0) { \
2734 goto fail; \
2735 } \
2736 } while (0)
Victor Stinner35c28d52018-11-14 02:01:52 +01002737
Victor Stinner9ee1d422018-11-14 18:58:01 +01002738 SET_ITEM_INT(install_signal_handlers);
2739 SET_ITEM_OBJ(argv);
2740 SET_ITEM_OBJ(executable);
2741 SET_ITEM_OBJ(prefix);
2742 SET_ITEM_OBJ(base_prefix);
2743 SET_ITEM_OBJ(exec_prefix);
2744 SET_ITEM_OBJ(base_exec_prefix);
2745 SET_ITEM_OBJ(warnoptions);
2746 SET_ITEM_OBJ(xoptions);
2747 SET_ITEM_OBJ(module_search_path);
Victor Stinner35c28d52018-11-14 02:01:52 +01002748
2749 return dict;
2750
2751fail:
2752 Py_DECREF(dict);
2753 return NULL;
2754
Victor Stinner9ee1d422018-11-14 18:58:01 +01002755#undef SET_ITEM_OBJ
Victor Stinner35c28d52018-11-14 02:01:52 +01002756}
Victor Stinnerda273412017-12-15 01:46:02 +01002757
2758
Victor Stinner41264f12017-12-15 02:05:29 +01002759_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01002760_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
2761 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002762{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002763 if (main_config->install_signal_handlers < 0) {
2764 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002765 }
2766
Victor Stinner9cfc0022017-12-20 19:36:46 +01002767 if (main_config->xoptions == NULL) {
2768 main_config->xoptions = config_create_xoptions_dict(config);
2769 if (main_config->xoptions == NULL) {
2770 return _Py_INIT_NO_MEMORY();
2771 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002772 }
2773
Victor Stinner8ded5b82018-01-24 17:03:28 +01002774#define COPY_WSTR(ATTR) \
2775 do { \
2776 if (main_config->ATTR == NULL) { \
2777 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
2778 if (main_config->ATTR == NULL) { \
2779 return _Py_INIT_NO_MEMORY(); \
2780 } \
2781 } \
2782 } while (0)
2783#define COPY_WSTRLIST(ATTR, LEN, LIST) \
2784 do { \
2785 if (ATTR == NULL) { \
Victor Stinner35c28d52018-11-14 02:01:52 +01002786 ATTR = _Py_wstrlist_as_pylist(LEN, LIST); \
Victor Stinner8ded5b82018-01-24 17:03:28 +01002787 if (ATTR == NULL) { \
2788 return _Py_INIT_NO_MEMORY(); \
2789 } \
2790 } \
2791 } while (0)
2792
2793 COPY_WSTRLIST(main_config->warnoptions,
2794 config->nwarnoption, config->warnoptions);
2795 if (config->argc >= 0) {
2796 COPY_WSTRLIST(main_config->argv,
2797 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002798 }
2799
Victor Stinner8ded5b82018-01-24 17:03:28 +01002800 if (!config->_disable_importlib) {
2801 COPY_WSTR(executable);
2802 COPY_WSTR(prefix);
2803 COPY_WSTR(base_prefix);
2804 COPY_WSTR(exec_prefix);
2805 COPY_WSTR(base_exec_prefix);
2806
2807 COPY_WSTRLIST(main_config->module_search_path,
2808 config->nmodule_search_path, config->module_search_paths);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002809 }
Victor Stinner41264f12017-12-15 02:05:29 +01002810
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002811 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01002812#undef COPY_WSTR
2813#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002814}
2815
2816
2817static int
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002818pymain_init_python_main(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002819{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002820 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002821
Victor Stinner9cfc0022017-12-20 19:36:46 +01002822 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002823 err = _PyMainInterpreterConfig_Read(&main_config, &interp->core_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002824 if (!_Py_INIT_FAILED(err)) {
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002825 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002826 }
2827 _PyMainInterpreterConfig_Clear(&main_config);
2828
2829 if (_Py_INIT_FAILED(err)) {
2830 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002831 return -1;
2832 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002833 return 0;
2834}
Victor Stinnera7368ac2017-11-15 18:11:45 -08002835
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002836
2837static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002838pymain_init_sys_path(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002839{
Victor Stinnerca719ac2017-12-20 18:00:19 +01002840 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002841 /* If filename is a package (ex: directory or ZIP file) which contains
2842 __main__.py, main_importer_path is set to filename and will be
2843 prepended to sys.path by pymain_run_main_from_importer(). Otherwise,
2844 main_importer_path is set to NULL. */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002845 pymain->main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01002846 }
2847
Victor Stinner19760862017-12-20 01:41:59 +01002848 PyObject *path0;
Victor Stinnerddc163d2018-09-24 05:03:01 -07002849 if (pymain_compute_path0(pymain, config, &path0) < 0) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002850 return -1;
2851 }
Victor Stinner19760862017-12-20 01:41:59 +01002852
Victor Stinner19760862017-12-20 01:41:59 +01002853 if (path0 != NULL) {
2854 if (pymain_update_sys_path(pymain, path0) < 0) {
2855 Py_DECREF(path0);
2856 return -1;
2857 }
2858 Py_DECREF(path0);
2859 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002860 return 0;
2861}
2862
2863
2864static void
2865pymain_run_python(_PyMain *pymain)
2866{
Victor Stinner19760862017-12-20 01:41:59 +01002867 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08002868
2869 pymain_header(pymain);
2870 pymain_import_readline(pymain);
2871
Victor Stinnerca719ac2017-12-20 18:00:19 +01002872 if (pymain->command) {
2873 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002874 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002875 else if (pymain->module) {
2876 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002877 }
2878 else {
Victor Stinner19760862017-12-20 01:41:59 +01002879 pymain_run_filename(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002880 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002881
Victor Stinner19760862017-12-20 01:41:59 +01002882 pymain_repl(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002883}
2884
2885
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002886static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002887pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
2888 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002889{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002890 pymain->err = _PyRuntime_Initialize();
2891 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002892 return -1;
2893 }
2894
Victor Stinnerddc163d2018-09-24 05:03:01 -07002895 int res = pymain_read_conf(pymain, config, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002896 if (res < 0) {
2897 return -1;
2898 }
2899 if (res > 0) {
2900 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01002901 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002902 }
2903
Victor Stinner94540602017-12-16 04:54:22 +01002904 if (cmdline->print_help) {
Victor Stinnerddc163d2018-09-24 05:03:01 -07002905 pymain_usage(0, config->program);
Victor Stinner19760862017-12-20 01:41:59 +01002906 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002907 }
2908
2909 if (cmdline->print_version) {
2910 printf("Python %s\n",
2911 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01002912 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002913 }
2914
Victor Stinnerc4bca952017-12-19 23:48:17 +01002915 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002916 orig_argv = copy_wstrlist(pymain->argc, cmdline->argv);
2917 if (orig_argv == NULL) {
2918 pymain->err = _Py_INIT_NO_MEMORY();
2919 return -1;
2920 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01002921 orig_argc = pymain->argc;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002922
Victor Stinnerddc163d2018-09-24 05:03:01 -07002923 _PyInitError err = config_init_warnoptions(config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002924 if (_Py_INIT_FAILED(err)) {
2925 pymain->err = err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002926 return -1;
2927 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002928 return 0;
2929}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00002930
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002931
Victor Stinnerca719ac2017-12-20 18:00:19 +01002932/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
2933 LC_CTYPE locale and Py_DecodeLocale().
2934
2935 Configuration:
2936
2937 * Command line arguments
2938 * Environment variables
2939 * Py_xxx global configuration variables
2940
Victor Stinnerddc163d2018-09-24 05:03:01 -07002941 _PyCmdline is a temporary structure used to prioritize these
Victor Stinnerca719ac2017-12-20 18:00:19 +01002942 variables. */
2943static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002944pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerca719ac2017-12-20 18:00:19 +01002945{
Victor Stinner31e99082017-12-20 23:41:38 +01002946 /* Force default allocator, since pymain_free() and pymain_clear_config()
2947 must use the same allocator than this function. */
2948 PyMemAllocatorEx old_alloc;
2949 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2950#ifdef Py_DEBUG
2951 PyMemAllocatorEx default_alloc;
2952 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
2953#endif
2954
Victor Stinnerddc163d2018-09-24 05:03:01 -07002955 _PyCmdline cmdline;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002956 memset(&cmdline, 0, sizeof(cmdline));
2957
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002958 cmdline_get_global_config(&cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002959
Victor Stinnerddc163d2018-09-24 05:03:01 -07002960 int res = pymain_cmdline_impl(pymain, config, &cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002961
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002962 cmdline_set_global_config(&cmdline);
Victor Stinnerddc163d2018-09-24 05:03:01 -07002963 _PyCoreConfig_SetGlobalConfig(config);
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002964 if (Py_IsolatedFlag) {
2965 Py_IgnoreEnvironmentFlag = 1;
2966 Py_NoUserSiteDirectory = 1;
2967 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002968
2969 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01002970
2971#ifdef Py_DEBUG
2972 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
2973 PyMemAllocatorEx cur_alloc;
2974 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
2975 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
2976#endif
2977 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002978 return res;
2979}
2980
2981
Victor Stinner94540602017-12-16 04:54:22 +01002982static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002983pymain_init(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +01002984{
Victor Stinnerddc163d2018-09-24 05:03:01 -07002985 _PyCoreConfig local_config = _PyCoreConfig_INIT;
2986 _PyCoreConfig *config = &local_config;
Victor Stinner94540602017-12-16 04:54:22 +01002987
Victor Stinnerddc163d2018-09-24 05:03:01 -07002988 /* 754 requires that FP exceptions run in "no stop" mode by default,
2989 * and until C vendors implement C99's ways to control FP exceptions,
2990 * Python requires non-stop mode. Alas, some platforms enable FP
2991 * exceptions by default. Here we disable them.
2992 */
2993#ifdef __FreeBSD__
2994 fedisableexcept(FE_OVERFLOW);
2995#endif
2996
2997 config->_disable_importlib = 0;
2998 config->install_signal_handlers = 1;
2999 _PyCoreConfig_GetGlobalConfig(config);
3000
3001 int res = pymain_cmdline(pymain, config);
Victor Stinner19760862017-12-20 01:41:59 +01003002 if (res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01003003 _Py_FatalInitError(pymain->err);
3004 }
Victor Stinner19760862017-12-20 01:41:59 +01003005 if (res == 1) {
Victor Stinnerddc163d2018-09-24 05:03:01 -07003006 pymain_clear_config(&local_config);
3007 return res;
Victor Stinner19760862017-12-20 01:41:59 +01003008 }
3009
Victor Stinner9cfc0022017-12-20 19:36:46 +01003010 pymain_init_stdio(pymain);
3011
Victor Stinner0c90d6f2018-08-05 12:31:59 +02003012 PyInterpreterState *interp;
Victor Stinnerddc163d2018-09-24 05:03:01 -07003013 pymain->err = _Py_InitializeCore(&interp, config);
Victor Stinner0c90d6f2018-08-05 12:31:59 +02003014 if (_Py_INIT_FAILED(pymain->err)) {
3015 _Py_FatalInitError(pymain->err);
Victor Stinner19760862017-12-20 01:41:59 +01003016 }
3017
Victor Stinnerddc163d2018-09-24 05:03:01 -07003018 pymain_clear_config(&local_config);
3019 config = &interp->core_config;
3020
Victor Stinner0c90d6f2018-08-05 12:31:59 +02003021 if (pymain_init_python_main(pymain, interp) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01003022 _Py_FatalInitError(pymain->err);
3023 }
3024
Victor Stinnerddc163d2018-09-24 05:03:01 -07003025 if (pymain_init_sys_path(pymain, config) < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01003026 _Py_FatalInitError(pymain->err);
3027 }
Victor Stinnerddc163d2018-09-24 05:03:01 -07003028 return 0;
3029}
3030
3031
3032static int
3033pymain_main(_PyMain *pymain)
3034{
3035 int res = pymain_init(pymain);
3036 if (res == 1) {
3037 goto done;
3038 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01003039
Victor Stinner19760862017-12-20 01:41:59 +01003040 pymain_run_python(pymain);
3041
3042 if (Py_FinalizeEx() < 0) {
3043 /* Value unlikely to be confused with a non-error exit status or
3044 other special meaning */
3045 pymain->status = 120;
3046 }
3047
3048done:
Victor Stinner94540602017-12-16 04:54:22 +01003049 pymain_free(pymain);
3050
Victor Stinner94540602017-12-16 04:54:22 +01003051 return pymain->status;
3052}
3053
3054
Victor Stinnerf7e5b562017-11-15 15:48:08 -08003055int
3056Py_Main(int argc, wchar_t **argv)
3057{
3058 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01003059 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08003060 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01003061 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08003062
Victor Stinner95cc3ee2018-09-19 12:01:52 -07003063 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00003064}
3065
Victor Stinner94540602017-12-16 04:54:22 +01003066
3067int
3068_Py_UnixMain(int argc, char **argv)
3069{
3070 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01003071 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01003072 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01003073 pymain.bytes_argv = argv;
3074
Victor Stinner95cc3ee2018-09-19 12:01:52 -07003075 return pymain_main(&pymain);
Victor Stinner94540602017-12-16 04:54:22 +01003076}
3077
3078
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003079/* this is gonna seem *real weird*, but if you put some other code between
3080 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
3081 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00003082
Guido van Rossum667d7041995-08-04 04:20:48 +00003083/* Make the *original* argc/argv available to other modules.
3084 This is rare, but it is needed by the secureware extension. */
3085
3086void
Martin v. Löwis790465f2008-04-05 20:41:37 +00003087Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00003088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 *argc = orig_argc;
3090 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00003091}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003092
3093#ifdef __cplusplus
3094}
3095#endif