blob: af2c191b9b9b321abe79189200fb87909a297e70 [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 Stinnerc4bca952017-12-19 23:48:17 +0100663 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100664}
665
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100666
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800667static void
668pymain_free(_PyMain *pymain)
669{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100670 pymain_free_python(pymain);
671 pymain_free_raw(pymain);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800672}
673
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100674
Eric Snow6b4be192017-05-22 21:36:03 -0700675static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800676pymain_run_main_from_importer(_PyMain *pymain)
Guido van Rossum667d7041995-08-04 04:20:48 +0000677{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800678 /* Assume sys_path0 has already been checked by pymain_get_importer(),
679 * so put it in sys.path[0] and import __main__ */
Victor Stinner11a247d2017-12-13 21:05:57 +0100680 PyObject *sys_path = PySys_GetObject("path");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800681 if (sys_path == NULL) {
682 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
683 goto error;
684 }
685
Victor Stinner11a247d2017-12-13 21:05:57 +0100686 if (PyList_Insert(sys_path, 0, pymain->main_importer_path)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800687 goto error;
688 }
689
Victor Stinner11a247d2017-12-13 21:05:57 +0100690 int sts = pymain_run_module(L"__main__", 0);
691 return (sts != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800692
693error:
694 Py_CLEAR(pymain->main_importer_path);
695 PyErr_Print();
696 return 1;
697}
698
699
Victor Stinner9cfc0022017-12-20 19:36:46 +0100700static _PyInitError
701wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800702{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100703 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800704 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100705 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800706 }
707
Victor Stinnerca719ac2017-12-20 18:00:19 +0100708 size_t size = (*len + 1) * sizeof(list[0]);
709 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
710 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800711 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100712 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800713 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100714 list2[*len] = str2;
715 *list = list2;
716 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100717 return _Py_INIT_OK();
718}
719
720
721static int
722pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
723{
724 _PyInitError err = wstrlist_append(len, list, str);
725 if (_Py_INIT_FAILED(err)) {
726 pymain->err = err;
727 return -1;
728 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800729 return 0;
730}
731
732
733/* Parse the command line arguments
734 Return 0 on success.
735 Return 1 if parsing failed.
736 Set pymain->err and return -1 on other errors. */
737static int
Victor Stinnerddc163d2018-09-24 05:03:01 -0700738pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
739 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800740{
Antoine Pitrou86838b02012-02-21 19:03:47 +0100741 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800742 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800743 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100744 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800745 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800746 if (c == EOF) {
747 break;
748 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 /* -c is the last option; following arguments
752 that look like options are left for the
753 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800754 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
755 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
756 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100757 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800758 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800759 }
Miss Islington (bot)c6de46e2018-05-31 06:43:21 -0700760 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 command[len - 2] = '\n';
762 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100763 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 break;
765 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 if (c == 'm') {
768 /* -m is the last option; following arguments
769 that look like options are left for the
770 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100771 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
772 if (pymain->module == NULL) {
773 return -1;
774 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 break;
776 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800779 case 0:
780 // Handle long option.
781 assert(longindex == 0); // Only one long option now.
782 if (!wcscmp(_PyOS_optarg, L"always")) {
783 cmdline->check_hash_pycs_mode = "always";
784 } else if (!wcscmp(_PyOS_optarg, L"never")) {
785 cmdline->check_hash_pycs_mode = "never";
786 } else if (!wcscmp(_PyOS_optarg, L"default")) {
787 cmdline->check_hash_pycs_mode = "default";
788 } else {
789 fprintf(stderr, "--check-hash-based-pycs must be one of "
790 "'default', 'always', or 'never'\n");
791 return 1;
792 }
793 break;
794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 case 'b':
Eric Snow6b4be192017-05-22 21:36:03 -0700796 cmdline->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 case 'd':
Eric Snow6b4be192017-05-22 21:36:03 -0700800 cmdline->debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 case 'i':
Eric Snow6b4be192017-05-22 21:36:03 -0700804 cmdline->inspect++;
805 cmdline->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000807
Christian Heimesad73a9c2013-08-10 16:36:18 +0200808 case 'I':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100809 config->ignore_environment++;
Eric Snow6b4be192017-05-22 21:36:03 -0700810 cmdline->isolated++;
811 cmdline->no_user_site_directory++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200812 break;
813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 case 'O':
Eric Snow6b4be192017-05-22 21:36:03 -0700817 cmdline->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 case 'B':
Eric Snow6b4be192017-05-22 21:36:03 -0700821 cmdline->dont_write_bytecode++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 case 's':
Eric Snow6b4be192017-05-22 21:36:03 -0700825 cmdline->no_user_site_directory++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 case 'S':
Eric Snow6b4be192017-05-22 21:36:03 -0700829 cmdline->no_site_import++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 case 'E':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100833 config->ignore_environment++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 case 't':
837 /* ignored for backwards compatibility */
838 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 case 'u':
Eric Snow6b4be192017-05-22 21:36:03 -0700841 cmdline->use_unbuffered_io = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 case 'v':
Eric Snow6b4be192017-05-22 21:36:03 -0700845 cmdline->verbosity++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100849 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 case 'h':
853 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -0700854 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -0700858 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100862 if (pymain_wstrlist_append(pymain,
863 &cmdline->nwarnoption,
864 &cmdline->warnoptions,
865 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800866 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000869
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000870 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100871 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +0100872 &config->nxoption,
873 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +0100874 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800875 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800876 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000877 break;
878
Georg Brandl9d871192010-12-04 10:47:18 +0000879 case 'q':
Eric Snow6b4be192017-05-22 21:36:03 -0700880 cmdline->quiet_flag++;
Georg Brandl9d871192010-12-04 10:47:18 +0000881 break;
882
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100883 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100884 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100885 break;
886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800890 /* unknown argument: parsing failed */
891 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800893 } while (1);
894
Victor Stinnerca719ac2017-12-20 18:00:19 +0100895 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800896 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +0100897 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800898 {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100899 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
900 if (pymain->filename == NULL) {
901 return -1;
902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000904
Victor Stinnerd5dda982017-12-13 17:31:16 +0100905 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100906 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +0100907
Eric Snow6b4be192017-05-22 21:36:03 -0700908 return 0;
909}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000910
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800911
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800912static int
Victor Stinner9cfc0022017-12-20 19:36:46 +0100913add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +0100914{
915 PyObject *name, *value;
916
917 const wchar_t *name_end = wcschr(s, L'=');
918 if (!name_end) {
919 name = PyUnicode_FromWideChar(s, -1);
920 value = Py_True;
921 Py_INCREF(value);
922 }
923 else {
924 name = PyUnicode_FromWideChar(s, name_end - s);
925 value = PyUnicode_FromWideChar(name_end + 1, -1);
926 }
927 if (name == NULL || value == NULL) {
928 goto error;
929 }
930 if (PyDict_SetItem(opts, name, value) < 0) {
931 goto error;
932 }
933 Py_DECREF(name);
934 Py_DECREF(value);
935 return 0;
936
937error:
938 Py_XDECREF(name);
939 Py_XDECREF(value);
940 return -1;
941}
942
Victor Stinner9cfc0022017-12-20 19:36:46 +0100943
944static PyObject*
945config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800946{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100947 int nxoption = config->nxoption;
948 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +0100949 PyObject *dict = PyDict_New();
950 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100951 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +0100952 }
953
Victor Stinnerca719ac2017-12-20 18:00:19 +0100954 for (int i=0; i < nxoption; i++) {
955 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +0100956 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +0100957 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100958 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800959 }
960 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100961
Victor Stinner9cfc0022017-12-20 19:36:46 +0100962 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -0700963}
964
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800965
Victor Stinner9cfc0022017-12-20 19:36:46 +0100966static _PyInitError
967config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -0700968{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100969 for (int i = 0; i < len; i++) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100970 _PyInitError err = wstrlist_append(&config->nwarnoption,
971 &config->warnoptions,
972 options[i]);
973 if (_Py_INIT_FAILED(err)) {
974 return err;
Eric Snow6b4be192017-05-22 21:36:03 -0700975 }
Eric Snow6b4be192017-05-22 21:36:03 -0700976 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100977 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800978}
Eric Snow6b4be192017-05-22 21:36:03 -0700979
Victor Stinner747f48e2017-12-12 22:59:48 +0100980
Victor Stinner9cfc0022017-12-20 19:36:46 +0100981static _PyInitError
Victor Stinnerddc163d2018-09-24 05:03:01 -0700982config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +0100983{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100984 _PyInitError err;
985
986 assert(config->nwarnoption == 0);
987
Victor Stinner747f48e2017-12-12 22:59:48 +0100988 /* The priority order for warnings configuration is (highest precedence
989 * first):
990 *
991 * - the BytesWarning filter, if needed ('-b', '-bb')
992 * - any '-W' command line options; then
993 * - the 'PYTHONWARNINGS' environment variable; then
994 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
995 * - any implicit filters added by _warnings.c/warnings.py
996 *
997 * All settings except the last are passed to the warnings module via
998 * the `sys.warnoptions` list. Since the warnings module works on the basis
999 * of "the most recently added filter will be checked first", we add
1000 * the lowest precedence entries first so that later entries override them.
1001 */
1002
Victor Stinner9cfc0022017-12-20 19:36:46 +01001003 if (config->dev_mode) {
1004 err = wstrlist_append(&config->nwarnoption,
1005 &config->warnoptions,
1006 L"default");
1007 if (_Py_INIT_FAILED(err)) {
1008 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001009 }
Victor Stinner747f48e2017-12-12 22:59:48 +01001010 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001011
Victor Stinner9cfc0022017-12-20 19:36:46 +01001012 err = config_add_warnings_optlist(config,
1013 cmdline->nenv_warnoption,
1014 cmdline->env_warnoptions);
1015 if (_Py_INIT_FAILED(err)) {
1016 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001017 }
1018
Victor Stinner9cfc0022017-12-20 19:36:46 +01001019 err = config_add_warnings_optlist(config,
1020 cmdline->nwarnoption,
1021 cmdline->warnoptions);
1022 if (_Py_INIT_FAILED(err)) {
1023 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001024 }
1025
1026 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
1027 * don't even try to emit a warning, so we skip setting the filter in that
1028 * case.
1029 */
1030 if (cmdline->bytes_warning) {
1031 wchar_t *filter;
1032 if (cmdline->bytes_warning> 1) {
1033 filter = L"error::BytesWarning";
1034 }
1035 else {
1036 filter = L"default::BytesWarning";
1037 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001038 err = wstrlist_append(&config->nwarnoption,
1039 &config->warnoptions,
1040 filter);
1041 if (_Py_INIT_FAILED(err)) {
1042 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001043 }
1044 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001045 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001046}
1047
1048
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001049/* Get warning options from PYTHONWARNINGS environment variable.
1050 Return 0 on success.
1051 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001052static _PyInitError
Victor Stinnerddc163d2018-09-24 05:03:01 -07001053cmdline_init_env_warnoptions(_PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001054{
1055 if (Py_IgnoreEnvironmentFlag) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001056 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +00001058
Victor Stinnerca719ac2017-12-20 18:00:19 +01001059 wchar_t *env;
1060 int res = config_get_env_var_dup(&env, L"PYTHONWARNINGS", "PYTHONWARNINGS");
1061 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001062 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001063 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001064
Victor Stinnerca719ac2017-12-20 18:00:19 +01001065 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001066 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001067 }
Philip Jenvey0805ca32010-04-07 04:04:10 +00001068
Victor Stinnerca719ac2017-12-20 18:00:19 +01001069
1070 wchar_t *warning, *context = NULL;
1071 for (warning = WCSTOK(env, L",", &context);
1072 warning != NULL;
1073 warning = WCSTOK(NULL, L",", &context))
1074 {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001075 _PyInitError err = wstrlist_append(&cmdline->nenv_warnoption,
1076 &cmdline->env_warnoptions,
1077 warning);
1078 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001079 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001080 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001081 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001083 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001084 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001085}
1086
1087
1088static void
1089pymain_init_stdio(_PyMain *pymain)
1090{
1091 pymain->stdin_is_interactive = (isatty(fileno(stdin))
1092 || Py_InteractiveFlag);
Guido van Rossum775af911997-02-14 19:50:32 +00001093
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +00001094#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +00001095 /* don't translate newlines (\r\n <=> \n) */
1096 _setmode(fileno(stdin), O_BINARY);
1097 _setmode(fileno(stdout), O_BINARY);
1098 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +00001099#endif
Victor Stinner89e34362011-01-07 18:47:22 +00001100
1101 if (Py_UnbufferedStdioFlag) {
Guido van Rossum22ffac11998-03-06 15:30:39 +00001102#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
1104 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
1105 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001106#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 setbuf(stdin, (char *)NULL);
1108 setbuf(stdout, (char *)NULL);
1109 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001110#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 }
1112 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +00001113#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 /* Doesn't have to have line-buffered -- use unbuffered */
1115 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
1116 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001117#else /* !MS_WINDOWS */
1118#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
1120 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001121#endif /* HAVE_SETVBUF */
1122#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 /* Leave stderr alone - it should be unbuffered anyway. */
1124 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001125}
Guido van Rossum667d7041995-08-04 04:20:48 +00001126
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001127
1128/* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
Victor Stinner31a83932017-12-04 13:39:15 +01001129 environment variables on macOS if available. */
1130static _PyInitError
Victor Stinnerca719ac2017-12-20 18:00:19 +01001131config_init_program_name(_PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001132{
Victor Stinner31a83932017-12-04 13:39:15 +01001133 assert(config->program_name == NULL);
1134
1135 /* If Py_SetProgramName() was called, use its value */
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001136 const wchar_t *program_name = _Py_path_config.program_name;
Victor Stinner31a83932017-12-04 13:39:15 +01001137 if (program_name != NULL) {
1138 config->program_name = _PyMem_RawWcsdup(program_name);
1139 if (config->program_name == NULL) {
1140 return _Py_INIT_NO_MEMORY();
1141 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001142 return _Py_INIT_OK();
Victor Stinner31a83932017-12-04 13:39:15 +01001143 }
1144
Just van Rossum2ac79ef2003-03-05 15:46:54 +00001145#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 /* On MacOS X, when the Python interpreter is embedded in an
1147 application bundle, it gets executed by a bootstrapping script
1148 that does os.execve() with an argv[0] that's different from the
1149 actual Python executable. This is needed to keep the Finder happy,
1150 or rather, to work around Apple's overly strict requirements of
1151 the process name. However, we still need a usable sys.executable,
1152 so the actual executable path is passed in an environment variable.
1153 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
1154 script. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001155 const char *p = config_get_env_var("PYTHONEXECUTABLE");
Victor Stinner6bf992a2017-12-06 17:26:10 +01001156 if (p != NULL) {
Victor Stinner31a83932017-12-04 13:39:15 +01001157 size_t len;
1158 wchar_t* program_name = Py_DecodeLocale(p, &len);
1159 if (program_name == NULL) {
1160 return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment "
1161 "variable", (Py_ssize_t)len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 }
Victor Stinner31a83932017-12-04 13:39:15 +01001163 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001164 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001165 }
Vinay Sajip90db6612012-07-17 17:33:46 +01001166#ifdef WITH_NEXT_FRAMEWORK
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001167 else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001168 const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
Vinay Sajip90db6612012-07-17 17:33:46 +01001169 if (pyvenv_launcher && *pyvenv_launcher) {
1170 /* Used by Mac/Tools/pythonw.c to forward
1171 * the argv0 of the stub executable
1172 */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001173 size_t len;
Victor Stinner31a83932017-12-04 13:39:15 +01001174 wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len);
1175 if (program_name == NULL) {
1176 return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment "
1177 "variable", (Py_ssize_t)len);
Vinay Sajip90db6612012-07-17 17:33:46 +01001178 }
Victor Stinner31a83932017-12-04 13:39:15 +01001179 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001180 return _Py_INIT_OK();
Vinay Sajip90db6612012-07-17 17:33:46 +01001181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001183#endif /* WITH_NEXT_FRAMEWORK */
1184#endif /* __APPLE__ */
Victor Stinneraf5a8952017-12-02 10:11:32 +01001185
Victor Stinnerca719ac2017-12-20 18:00:19 +01001186 /* Use argv[0] by default, if available */
1187 if (config->program != NULL) {
1188 config->program_name = _PyMem_RawWcsdup(config->program);
1189 if (config->program_name == NULL) {
1190 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001191 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001192 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001193 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001194
1195 /* Last fall back: hardcoded string */
1196#ifdef MS_WINDOWS
1197 const wchar_t *default_program_name = L"python";
1198#else
1199 const wchar_t *default_program_name = L"python3";
1200#endif
1201 config->program_name = _PyMem_RawWcsdup(default_program_name);
1202 if (config->program_name == NULL) {
1203 return _Py_INIT_NO_MEMORY();
1204 }
1205 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001206}
1207
1208
Steve Dowere8510492018-11-17 20:42:08 -08001209static _PyInitError
1210config_init_executable(_PyCoreConfig *config)
1211{
1212 assert(config->executable == NULL);
1213
1214 /* If Py_SetProgramFullPath() was called, use its value */
1215 const wchar_t *program_full_path = _Py_path_config.program_full_path;
1216 if (program_full_path != NULL) {
1217 config->executable = _PyMem_RawWcsdup(program_full_path);
1218 if (config->executable == NULL) {
1219 return _Py_INIT_NO_MEMORY();
1220 }
1221 return _Py_INIT_OK();
1222 }
1223
1224 return _Py_INIT_OK();
1225}
1226
1227
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001228static void
1229pymain_header(_PyMain *pymain)
1230{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001231 if (Py_QuietFlag) {
1232 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 }
Guido van Rossum393661d2001-08-31 17:40:15 +00001234
Victor Stinner19760862017-12-20 01:41:59 +01001235 if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001236 return;
1237 }
1238
1239 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
1240 if (!Py_NoSiteFlag) {
1241 fprintf(stderr, "%s\n", COPYRIGHT);
1242 }
1243}
1244
1245
Victor Stinnerc4bca952017-12-19 23:48:17 +01001246static wchar_t**
Victor Stinnerca719ac2017-12-20 18:00:19 +01001247copy_wstrlist(int len, wchar_t **list)
Victor Stinner11a247d2017-12-13 21:05:57 +01001248{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001249 assert((len > 0 && list != NULL) || len == 0);
1250 size_t size = len * sizeof(list[0]);
1251 wchar_t **list_copy = PyMem_RawMalloc(size);
Alexey Izbyshev388bd4b2018-08-25 19:46:58 +03001252 if (list_copy == NULL) {
1253 return NULL;
1254 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001255 for (int i=0; i < len; i++) {
1256 wchar_t* arg = _PyMem_RawWcsdup(list[i]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001257 if (arg == NULL) {
Alexey Izbyshev388bd4b2018-08-25 19:46:58 +03001258 clear_wstrlist(i, list_copy);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001259 return NULL;
1260 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001261 list_copy[i] = arg;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001262 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001263 return list_copy;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001264}
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001265
Victor Stinnerc4bca952017-12-19 23:48:17 +01001266
1267static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07001268pymain_init_core_argv(_PyMain *pymain, _PyCoreConfig *config,
1269 _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001270{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001271 /* Copy argv to be able to modify it (to force -c/-m) */
1272 int argc = pymain->argc - _PyOS_optind;
1273 wchar_t **argv;
1274
1275 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +01001276 /* Ensure at least one (empty) argument is seen */
1277 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +01001278 argc = 1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001279 argv = copy_wstrlist(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +01001280 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001281 else {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001282 argv = copy_wstrlist(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001283 }
1284
1285 if (argv == NULL) {
1286 pymain->err = _Py_INIT_NO_MEMORY();
1287 return -1;
1288 }
1289
1290 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001291 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001292 /* Force sys.argv[0] = '-c' */
1293 arg0 = L"-c";
1294 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001295 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001296 /* Force sys.argv[0] = '-m'*/
1297 arg0 = L"-m";
1298 }
1299 if (arg0 != NULL) {
1300 arg0 = _PyMem_RawWcsdup(arg0);
1301 if (arg0 == NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001302 clear_wstrlist(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001303 pymain->err = _Py_INIT_NO_MEMORY();
1304 return -1;
1305 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001306
1307 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001308 PyMem_RawFree(argv[0]);
1309 argv[0] = arg0;
1310 }
1311
Victor Stinnerddc163d2018-09-24 05:03:01 -07001312 config->argc = argc;
1313 config->argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001314 return 0;
1315}
1316
1317
Victor Stinner8ded5b82018-01-24 17:03:28 +01001318static PyObject*
Victor Stinner35c28d52018-11-14 02:01:52 +01001319_Py_wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001320{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001321 assert(list != NULL || len < 1);
1322
1323 PyObject *pylist = PyList_New(len);
1324 if (pylist == NULL) {
1325 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001326 }
1327
Victor Stinner8ded5b82018-01-24 17:03:28 +01001328 for (int i = 0; i < len; i++) {
1329 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001330 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001331 Py_DECREF(pylist);
1332 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001333 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001334 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001335 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001336 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001337}
1338
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001339
Victor Stinner11a247d2017-12-13 21:05:57 +01001340static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07001341pymain_compute_path0(_PyMain *pymain, _PyCoreConfig *config, PyObject **path0)
Victor Stinner11a247d2017-12-13 21:05:57 +01001342{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001343 if (pymain->main_importer_path != NULL) {
1344 /* Let pymain_run_main_from_importer() adjust sys.path[0] later */
Victor Stinner19760862017-12-20 01:41:59 +01001345 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001346 return 0;
1347 }
1348
1349 if (Py_IsolatedFlag) {
Victor Stinner19760862017-12-20 01:41:59 +01001350 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001351 return 0;
1352 }
1353
Victor Stinnerddc163d2018-09-24 05:03:01 -07001354 *path0 = _PyPathConfig_ComputeArgv0(config->argc, config->argv);
Victor Stinner19760862017-12-20 01:41:59 +01001355 if (*path0 == NULL) {
1356 pymain->err = _Py_INIT_NO_MEMORY();
1357 return -1;
1358 }
1359 return 0;
1360}
1361
1362
1363static int
1364pymain_update_sys_path(_PyMain *pymain, PyObject *path0)
1365{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001366 /* Prepend argv[0] to sys.path.
1367 If argv[0] is a symlink, use the real path. */
1368 PyObject *sys_path = PySys_GetObject("path");
1369 if (sys_path == NULL) {
1370 pymain->err = _Py_INIT_ERR("can't get sys.path");
Victor Stinnerd5dda982017-12-13 17:31:16 +01001371 return -1;
1372 }
1373
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001374 /* Prepend path0 to sys.path */
1375 if (PyList_Insert(sys_path, 0, path0) < 0) {
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001376 pymain->err = _Py_INIT_ERR("sys.path.insert(0, path0) failed");
1377 return -1;
1378 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001379 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001380}
1381
1382
Victor Stinner35c28d52018-11-14 02:01:52 +01001383PyObject *
1384_Py_GetGlobalVariablesAsDict(void)
1385{
1386 PyObject *dict, *obj;
1387
1388 dict = PyDict_New();
1389 if (dict == NULL) {
1390 return NULL;
1391 }
1392
1393#define SET_ITEM(KEY, EXPR) \
1394 do { \
1395 obj = (EXPR); \
1396 if (obj == NULL) { \
1397 return NULL; \
1398 } \
1399 int res = PyDict_SetItemString(dict, (KEY), obj); \
1400 Py_DECREF(obj); \
1401 if (res < 0) { \
1402 goto fail; \
1403 } \
1404 } while (0)
1405#define SET_ITEM_INT(VAR) \
1406 SET_ITEM(#VAR, PyLong_FromLong(VAR))
1407#define FROM_STRING(STR) \
1408 ((STR != NULL) ? \
1409 PyUnicode_FromString(STR) \
1410 : (Py_INCREF(Py_None), Py_None))
1411#define SET_ITEM_STR(VAR) \
1412 SET_ITEM(#VAR, FROM_STRING(VAR))
1413
1414 SET_ITEM_STR(Py_FileSystemDefaultEncoding);
1415 SET_ITEM_INT(Py_HasFileSystemDefaultEncoding);
1416 SET_ITEM_STR(Py_FileSystemDefaultEncodeErrors);
1417
1418 SET_ITEM_INT(Py_UTF8Mode);
1419 SET_ITEM_INT(Py_DebugFlag);
1420 SET_ITEM_INT(Py_VerboseFlag);
1421 SET_ITEM_INT(Py_QuietFlag);
1422 SET_ITEM_INT(Py_InteractiveFlag);
1423 SET_ITEM_INT(Py_InspectFlag);
1424
1425 SET_ITEM_INT(Py_OptimizeFlag);
1426 SET_ITEM_INT(Py_NoSiteFlag);
1427 SET_ITEM_INT(Py_BytesWarningFlag);
1428 SET_ITEM_INT(Py_FrozenFlag);
1429 SET_ITEM_INT(Py_IgnoreEnvironmentFlag);
1430 SET_ITEM_INT(Py_DontWriteBytecodeFlag);
1431 SET_ITEM_INT(Py_NoUserSiteDirectory);
1432 SET_ITEM_INT(Py_UnbufferedStdioFlag);
1433 SET_ITEM_INT(Py_HashRandomizationFlag);
1434 SET_ITEM_INT(Py_IsolatedFlag);
1435
1436#ifdef MS_WINDOWS
1437 SET_ITEM_INT(Py_LegacyWindowsFSEncodingFlag);
1438 SET_ITEM_INT(Py_LegacyWindowsStdioFlag);
1439#endif
1440
1441 return dict;
1442
1443fail:
1444 Py_DECREF(dict);
1445 return NULL;
1446
1447#undef FROM_STRING
1448#undef SET_ITEM
1449#undef SET_ITEM_INT
1450#undef SET_ITEM_STR
1451}
1452
1453
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001454void
1455_PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config)
1456{
1457#define COPY_FLAG(ATTR, VALUE) \
1458 if (config->ATTR == -1) { \
1459 config->ATTR = VALUE; \
1460 }
1461
1462 COPY_FLAG(ignore_environment, Py_IgnoreEnvironmentFlag);
1463 COPY_FLAG(utf8_mode, Py_UTF8Mode);
1464
1465#undef COPY_FLAG
1466}
1467
1468
Victor Stinner6bf992a2017-12-06 17:26:10 +01001469/* Get Py_xxx global configuration variables */
1470static void
Victor Stinnerddc163d2018-09-24 05:03:01 -07001471cmdline_get_global_config(_PyCmdline *cmdline)
Victor Stinner6bf992a2017-12-06 17:26:10 +01001472{
Victor Stinner91106cd2017-12-13 12:29:09 +01001473 cmdline->bytes_warning = Py_BytesWarningFlag;
1474 cmdline->debug = Py_DebugFlag;
1475 cmdline->inspect = Py_InspectFlag;
1476 cmdline->interactive = Py_InteractiveFlag;
1477 cmdline->isolated = Py_IsolatedFlag;
1478 cmdline->optimization_level = Py_OptimizeFlag;
1479 cmdline->dont_write_bytecode = Py_DontWriteBytecodeFlag;
1480 cmdline->no_user_site_directory = Py_NoUserSiteDirectory;
1481 cmdline->no_site_import = Py_NoSiteFlag;
1482 cmdline->use_unbuffered_io = Py_UnbufferedStdioFlag;
1483 cmdline->verbosity = Py_VerboseFlag;
1484 cmdline->quiet_flag = Py_QuietFlag;
1485#ifdef MS_WINDOWS
1486 cmdline->legacy_windows_fs_encoding = Py_LegacyWindowsFSEncodingFlag;
1487 cmdline->legacy_windows_stdio = Py_LegacyWindowsStdioFlag;
1488#endif
1489 cmdline->check_hash_pycs_mode = _Py_CheckHashBasedPycsMode ;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001490}
Victor Stinner91106cd2017-12-13 12:29:09 +01001491
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001492
1493void
1494_PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config)
1495{
1496 Py_IgnoreEnvironmentFlag = config->ignore_environment;
1497 Py_UTF8Mode = config->utf8_mode;
1498
1499 /* Random or non-zero hash seed */
1500 Py_HashRandomizationFlag = (config->use_hash_seed == 0 ||
1501 config->hash_seed != 0);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001502}
1503
1504
Victor Stinner19760862017-12-20 01:41:59 +01001505/* Set Py_xxx global configuration variables */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001506static void
Victor Stinnerddc163d2018-09-24 05:03:01 -07001507cmdline_set_global_config(_PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001508{
Victor Stinner91106cd2017-12-13 12:29:09 +01001509 Py_BytesWarningFlag = cmdline->bytes_warning;
1510 Py_DebugFlag = cmdline->debug;
1511 Py_InspectFlag = cmdline->inspect;
1512 Py_InteractiveFlag = cmdline->interactive;
1513 Py_IsolatedFlag = cmdline->isolated;
1514 Py_OptimizeFlag = cmdline->optimization_level;
1515 Py_DontWriteBytecodeFlag = cmdline->dont_write_bytecode;
1516 Py_NoUserSiteDirectory = cmdline->no_user_site_directory;
1517 Py_NoSiteFlag = cmdline->no_site_import;
1518 Py_UnbufferedStdioFlag = cmdline->use_unbuffered_io;
1519 Py_VerboseFlag = cmdline->verbosity;
1520 Py_QuietFlag = cmdline->quiet_flag;
1521 _Py_CheckHashBasedPycsMode = cmdline->check_hash_pycs_mode;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001522#ifdef MS_WINDOWS
Victor Stinner91106cd2017-12-13 12:29:09 +01001523 Py_LegacyWindowsFSEncodingFlag = cmdline->legacy_windows_fs_encoding;
1524 Py_LegacyWindowsStdioFlag = cmdline->legacy_windows_stdio;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001525#endif
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001526}
1527
1528
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001529static void
1530pymain_import_readline(_PyMain *pymain)
1531{
1532 if (Py_IsolatedFlag) {
1533 return;
1534 }
Victor Stinner19760862017-12-20 01:41:59 +01001535 if (!Py_InspectFlag && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001536 return;
1537 }
1538 if (!isatty(fileno(stdin))) {
1539 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001540 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001541
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001542 PyObject *mod = PyImport_ImportModule("readline");
1543 if (mod == NULL) {
1544 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 }
1546 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001547 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001549}
1550
1551
1552static FILE*
1553pymain_open_filename(_PyMain *pymain)
1554{
Victor Stinnerddc163d2018-09-24 05:03:01 -07001555 const _PyCoreConfig *config = &_PyGILState_GetInterpreterStateUnsafe()->core_config;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001556 FILE* fp;
1557
Victor Stinnerca719ac2017-12-20 18:00:19 +01001558 fp = _Py_wfopen(pymain->filename, L"r");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001559 if (fp == NULL) {
1560 char *cfilename_buffer;
1561 const char *cfilename;
1562 int err = errno;
Victor Stinner9dd76202017-12-21 16:20:32 +01001563 cfilename_buffer = _Py_EncodeLocaleRaw(pymain->filename, NULL);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001564 if (cfilename_buffer != NULL)
1565 cfilename = cfilename_buffer;
1566 else
1567 cfilename = "<unprintable file name>";
1568 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinnerddc163d2018-09-24 05:03:01 -07001569 config->program, cfilename, err, strerror(err));
Victor Stinner9dd76202017-12-21 16:20:32 +01001570 PyMem_RawFree(cfilename_buffer);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001571 pymain->status = 2;
1572 return NULL;
1573 }
1574
Victor Stinnerca719ac2017-12-20 18:00:19 +01001575 if (pymain->skip_first_line) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001576 int ch;
1577 /* Push back first newline so line numbers
1578 remain the same */
1579 while ((ch = getc(fp)) != EOF) {
1580 if (ch == '\n') {
1581 (void)ungetc(ch, fp);
1582 break;
1583 }
1584 }
1585 }
1586
1587 struct _Py_stat_struct sb;
1588 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 &&
1589 S_ISDIR(sb.st_mode)) {
1590 fprintf(stderr,
1591 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinnerddc163d2018-09-24 05:03:01 -07001592 config->program, pymain->filename);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001593 fclose(fp);
1594 pymain->status = 1;
1595 return NULL;
1596 }
1597
1598 return fp;
1599}
1600
1601
1602static void
Victor Stinner19760862017-12-20 01:41:59 +01001603pymain_run_filename(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001604{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001605 if (pymain->filename == NULL && pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001606 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner19760862017-12-20 01:41:59 +01001607 pymain_run_startup(cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001608 pymain_run_interactive_hook();
1609 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001610
1611 if (pymain->main_importer_path != NULL) {
1612 pymain->status = pymain_run_main_from_importer(pymain);
1613 return;
1614 }
1615
1616 FILE *fp;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001617 if (pymain->filename != NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001618 fp = pymain_open_filename(pymain);
1619 if (fp == NULL) {
1620 return;
1621 }
1622 }
1623 else {
1624 fp = stdin;
1625 }
1626
Victor Stinnerca719ac2017-12-20 18:00:19 +01001627 pymain->status = pymain_run_file(fp, pymain->filename, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001628}
1629
1630
1631static void
Victor Stinner19760862017-12-20 01:41:59 +01001632pymain_repl(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001635 opportunity to set it from Python. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001636 if (!Py_InspectFlag && config_get_env_var("PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 Py_InspectFlag = 1;
1638 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001639
Victor Stinner19760862017-12-20 01:41:59 +01001640 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001641 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001643
1644 Py_InspectFlag = 0;
1645 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001646
Victor Stinner19760862017-12-20 01:41:59 +01001647 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001648 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001649}
1650
1651
1652/* Parse the command line.
1653 Handle --version and --help options directly.
1654
1655 Return 1 if Python must exit.
1656 Return 0 on success.
1657 Set pymain->err and return -1 on failure. */
1658static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07001659pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config,
1660 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001661{
Victor Stinnerddc163d2018-09-24 05:03:01 -07001662 int res = pymain_parse_cmdline_impl(pymain, config, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001663 if (res < 0) {
1664 return -1;
1665 }
1666 if (res) {
Victor Stinnerddc163d2018-09-24 05:03:01 -07001667 pymain_usage(1, config->program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001668 pymain->status = 2;
1669 return 1;
1670 }
1671
Victor Stinnerca719ac2017-12-20 18:00:19 +01001672 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001673 /* Backup _PyOS_optind */
1674 _PyOS_optind--;
1675 }
1676
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001677 return 0;
1678}
1679
1680
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001681static const wchar_t*
Victor Stinner9cfc0022017-12-20 19:36:46 +01001682config_get_xoption(_PyCoreConfig *config, wchar_t *name)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001683{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001684 int nxoption = config->nxoption;
1685 wchar_t **xoptions = config->xoptions;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001686 for (int i=0; i < nxoption; i++) {
1687 wchar_t *option = xoptions[i];
Victor Stinnera7368ac2017-11-15 18:11:45 -08001688 size_t len;
1689 wchar_t *sep = wcschr(option, L'=');
1690 if (sep != NULL) {
1691 len = (sep - option);
1692 }
1693 else {
1694 len = wcslen(option);
1695 }
1696 if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
1697 return option;
1698 }
1699 }
1700 return NULL;
1701}
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001702
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001703
Victor Stinnera7368ac2017-11-15 18:11:45 -08001704static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001705pymain_str_to_int(const char *str, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001706{
1707 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001708 const char *endptr = str;
1709 long value = strtol(str, (char **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001710 if (*endptr != '\0' || errno == ERANGE) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001711 return -1;
1712 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001713 if (value < INT_MIN || value > INT_MAX) {
1714 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001715 }
1716
Victor Stinnera7368ac2017-11-15 18:11:45 -08001717 *result = (int)value;
1718 return 0;
1719}
1720
1721
1722static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001723pymain_wstr_to_int(const wchar_t *wstr, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001724{
1725 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001726 const wchar_t *endptr = wstr;
1727 long value = wcstol(wstr, (wchar_t **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001728 if (*endptr != '\0' || errno == ERANGE) {
1729 return -1;
1730 }
1731 if (value < INT_MIN || value > INT_MAX) {
1732 return -1;
1733 }
1734
1735 *result = (int)value;
1736 return 0;
1737}
1738
1739
Victor Stinner9cfc0022017-12-20 19:36:46 +01001740static _PyInitError
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001741config_init_tracemalloc(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001742{
1743 int nframe;
1744 int valid;
1745
Victor Stinner9cfc0022017-12-20 19:36:46 +01001746 const char *env = config_get_env_var("PYTHONTRACEMALLOC");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001747 if (env) {
1748 if (!pymain_str_to_int(env, &nframe)) {
1749 valid = (nframe >= 1);
1750 }
1751 else {
1752 valid = 0;
1753 }
1754 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001755 return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number "
1756 "of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001757 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001758 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001759 }
1760
Victor Stinner9cfc0022017-12-20 19:36:46 +01001761 const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001762 if (xoption) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001763 const wchar_t *sep = wcschr(xoption, L'=');
Victor Stinnera7368ac2017-11-15 18:11:45 -08001764 if (sep) {
1765 if (!pymain_wstr_to_int(sep + 1, &nframe)) {
1766 valid = (nframe >= 1);
1767 }
1768 else {
1769 valid = 0;
1770 }
1771 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001772 return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: "
1773 "invalid number of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001774 }
1775 }
1776 else {
1777 /* -X tracemalloc behaves as -X tracemalloc=1 */
1778 nframe = 1;
1779 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001780 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001781 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001782 return _Py_INIT_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001783}
1784
1785
1786static void
Victor Stinner9cfc0022017-12-20 19:36:46 +01001787get_env_flag(int *flag, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001788{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001789 const char *var = config_get_env_var(name);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001790 if (!var) {
1791 return;
1792 }
1793 int value;
1794 if (pymain_str_to_int(var, &value) < 0 || value < 0) {
1795 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
1796 value = 1;
1797 }
1798 if (*flag < value) {
1799 *flag = value;
1800 }
1801}
1802
1803
1804static void
Victor Stinnerddc163d2018-09-24 05:03:01 -07001805cmdline_get_env_flags(_PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001806{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001807 get_env_flag(&cmdline->debug, "PYTHONDEBUG");
1808 get_env_flag(&cmdline->verbosity, "PYTHONVERBOSE");
1809 get_env_flag(&cmdline->optimization_level, "PYTHONOPTIMIZE");
1810 get_env_flag(&cmdline->inspect, "PYTHONINSPECT");
1811 get_env_flag(&cmdline->dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
1812 get_env_flag(&cmdline->no_user_site_directory, "PYTHONNOUSERSITE");
1813 get_env_flag(&cmdline->use_unbuffered_io, "PYTHONUNBUFFERED");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001814#ifdef MS_WINDOWS
Victor Stinner9cfc0022017-12-20 19:36:46 +01001815 get_env_flag(&cmdline->legacy_windows_fs_encoding,
1816 "PYTHONLEGACYWINDOWSFSENCODING");
1817 get_env_flag(&cmdline->legacy_windows_stdio,
1818 "PYTHONLEGACYWINDOWSSTDIO");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001819#endif
1820}
1821
1822
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001823/* Set global variable variables from environment variables */
1824void
1825_Py_Initialize_ReadEnvVarsNoAlloc(void)
1826{
Victor Stinnerddc163d2018-09-24 05:03:01 -07001827 _PyCmdline cmdline;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001828 memset(&cmdline, 0, sizeof(cmdline));
1829
1830 cmdline_get_global_config(&cmdline);
1831 if (cmdline.isolated) {
1832 Py_IgnoreEnvironmentFlag = 1;
1833 cmdline.no_user_site_directory = 1;
1834 }
1835 if (!Py_IgnoreEnvironmentFlag) {
1836 cmdline_get_env_flags(&cmdline);
1837 }
1838 cmdline_set_global_config(&cmdline);
1839
1840 /* no need to call pymain_clear_cmdline(), no memory has been allocated */
1841}
1842
1843
Victor Stinner46972b72017-11-24 22:55:40 +01001844static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001845config_init_home(_PyCoreConfig *config)
Victor Stinner1f151112017-11-23 10:43:14 +01001846{
1847 wchar_t *home;
1848
Victor Stinner31a83932017-12-04 13:39:15 +01001849 /* If Py_SetPythonHome() was called, use its value */
1850 home = _Py_path_config.home;
Victor Stinner1f151112017-11-23 10:43:14 +01001851 if (home) {
Victor Stinner46972b72017-11-24 22:55:40 +01001852 config->home = _PyMem_RawWcsdup(home);
1853 if (config->home == NULL) {
1854 return _Py_INIT_NO_MEMORY();
Victor Stinner1f151112017-11-23 10:43:14 +01001855 }
Victor Stinner46972b72017-11-24 22:55:40 +01001856 return _Py_INIT_OK();
Victor Stinner1f151112017-11-23 10:43:14 +01001857 }
1858
Victor Stinner46972b72017-11-24 22:55:40 +01001859 int res = config_get_env_var_dup(&home, L"PYTHONHOME", "PYTHONHOME");
Victor Stinner1f151112017-11-23 10:43:14 +01001860 if (res < 0) {
Victor Stinner46972b72017-11-24 22:55:40 +01001861 return DECODE_LOCALE_ERR("PYTHONHOME", res);
Victor Stinner1f151112017-11-23 10:43:14 +01001862 }
Victor Stinner46972b72017-11-24 22:55:40 +01001863 config->home = home;
1864 return _Py_INIT_OK();
Victor Stinnerd4341102017-11-23 00:12:09 +01001865}
1866
1867
Victor Stinner358e5e12017-12-15 00:51:22 +01001868static _PyInitError
1869config_init_hash_seed(_PyCoreConfig *config)
1870{
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001871 const char *seed_text = config_get_env_var("PYTHONHASHSEED");
1872 int use_hash_seed;
1873 unsigned long hash_seed;
1874 if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
1875 return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
1876 "or an integer in range [0; 4294967295]");
Victor Stinner358e5e12017-12-15 00:51:22 +01001877 }
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001878 config->use_hash_seed = use_hash_seed;
1879 config->hash_seed = hash_seed;
Victor Stinner358e5e12017-12-15 00:51:22 +01001880 return _Py_INIT_OK();
1881}
1882
1883
Victor Stinner9cfc0022017-12-20 19:36:46 +01001884static _PyInitError
1885config_init_utf8_mode(_PyCoreConfig *config)
Victor Stinner91106cd2017-12-13 12:29:09 +01001886{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001887 const wchar_t *xopt = config_get_xoption(config, L"utf8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001888 if (xopt) {
1889 wchar_t *sep = wcschr(xopt, L'=');
1890 if (sep) {
1891 xopt = sep + 1;
1892 if (wcscmp(xopt, L"1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001893 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001894 }
1895 else if (wcscmp(xopt, L"0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001896 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001897 }
1898 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001899 return _Py_INIT_USER_ERR("invalid -X utf8 option value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001900 }
1901 }
1902 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001903 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001904 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001905 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001906 }
1907
Victor Stinner9cfc0022017-12-20 19:36:46 +01001908 const char *opt = config_get_env_var("PYTHONUTF8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001909 if (opt) {
1910 if (strcmp(opt, "1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001911 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001912 }
1913 else if (strcmp(opt, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001914 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001915 }
1916 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001917 return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
1918 "variable value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001919 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001920 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001921 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001922
1923 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001924}
Victor Stinner46972b72017-11-24 22:55:40 +01001925
1926
Victor Stinner9cfc0022017-12-20 19:36:46 +01001927static _PyInitError
1928config_read_env_vars(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001929{
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001930 assert(!config->ignore_environment);
1931
1932 if (config->allocator == NULL) {
1933 config->allocator = config_get_env_var("PYTHONMALLOC");
1934 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001935
Victor Stinner9cfc0022017-12-20 19:36:46 +01001936 if (config_get_env_var("PYTHONDUMPREFS")) {
1937 config->dump_refs = 1;
1938 }
1939 if (config_get_env_var("PYTHONMALLOCSTATS")) {
1940 config->malloc_stats = 1;
Victor Stinner31a83932017-12-04 13:39:15 +01001941 }
1942
Victor Stinner98c49c62018-08-29 01:13:29 +02001943 const char *env = config_get_env_var("PYTHONCOERCECLOCALE");
1944 if (env) {
1945 if (strcmp(env, "0") == 0) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07001946 if (config->coerce_c_locale < 0) {
1947 config->coerce_c_locale = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001948 }
Victor Stinner98c49c62018-08-29 01:13:29 +02001949 }
1950 else if (strcmp(env, "warn") == 0) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07001951 config->coerce_c_locale_warn = 1;
Victor Stinner98c49c62018-08-29 01:13:29 +02001952 }
1953 else {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07001954 if (config->coerce_c_locale < 0) {
1955 config->coerce_c_locale = 1;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001956 }
Victor Stinner94540602017-12-16 04:54:22 +01001957 }
1958 }
1959
Victor Stinner9cfc0022017-12-20 19:36:46 +01001960 wchar_t *path;
1961 int res = config_get_env_var_dup(&path, L"PYTHONPATH", "PYTHONPATH");
1962 if (res < 0) {
Miss Islington (bot)6414da92018-05-19 16:14:42 -07001963 return DECODE_LOCALE_ERR("PYTHONPATH", res);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001964 }
1965 config->module_search_path_env = path;
1966
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001967 if (config->use_hash_seed < 0) {
1968 _PyInitError err = config_init_hash_seed(config);
1969 if (_Py_INIT_FAILED(err)) {
1970 return err;
1971 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001972 }
1973
1974 return _Py_INIT_OK();
1975}
1976
1977
1978static _PyInitError
1979config_read_complex_options(_PyCoreConfig *config)
1980{
1981 /* More complex options configured by env var and -X option */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001982 if (config->faulthandler < 0) {
1983 if (config_get_env_var("PYTHONFAULTHANDLER")
1984 || config_get_xoption(config, L"faulthandler")) {
1985 config->faulthandler = 1;
1986 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001987 }
1988 if (config_get_env_var("PYTHONPROFILEIMPORTTIME")
1989 || config_get_xoption(config, L"importtime")) {
1990 config->import_time = 1;
1991 }
1992 if (config_get_xoption(config, L"dev" ) ||
1993 config_get_env_var("PYTHONDEVMODE"))
1994 {
1995 config->dev_mode = 1;
Victor Stinner9cfc0022017-12-20 19:36:46 +01001996 }
1997
Victor Stinner0c90d6f2018-08-05 12:31:59 +02001998 if (config->tracemalloc < 0) {
1999 _PyInitError err = config_init_tracemalloc(config);
2000 if (_Py_INIT_FAILED(err)) {
2001 return err;
2002 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002003 }
2004 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002005}
2006
2007
Victor Stinnera7368ac2017-11-15 18:11:45 -08002008/* Parse command line options and environment variables.
2009 This code must not use Python runtime apart PyMem_Raw memory allocator.
2010
2011 Return 0 on success.
2012 Return 1 if Python is done and must exit.
2013 Set pymain->err and return -1 on error. */
2014static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002015pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
2016 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002017{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002018 _PyInitError err;
2019
Victor Stinnerddc163d2018-09-24 05:03:01 -07002020 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01002021 if (res != 0) {
2022 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002023 }
2024
Victor Stinner9cfc0022017-12-20 19:36:46 +01002025 /* Set Py_IgnoreEnvironmentFlag for Py_GETENV() */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002026 Py_IgnoreEnvironmentFlag = config->ignore_environment || cmdline->isolated;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002027
2028 /* Get environment variables */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002029 if (!Py_IgnoreEnvironmentFlag) {
2030 cmdline_get_env_flags(cmdline);
2031 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002032
2033 err = cmdline_init_env_warnoptions(cmdline);
2034 if (_Py_INIT_FAILED(err)) {
2035 pymain->err = err;
2036 return -1;
2037 }
2038
2039#ifdef MS_WINDOWS
2040 if (cmdline->legacy_windows_fs_encoding) {
2041 config->utf8_mode = 0;
2042 }
2043#endif
2044
Victor Stinnerddc163d2018-09-24 05:03:01 -07002045 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01002046 return -1;
2047 }
2048
Victor Stinner2b822a02018-01-25 09:18:36 +01002049 /* On Windows, _PyPathConfig_Init() modifies Py_IsolatedFlag and
2050 Py_NoSiteFlag variables if a "._pth" file is found. */
2051 int init_isolated = Py_IsolatedFlag;
2052 int init_no_site = Py_NoSiteFlag;
2053 Py_IsolatedFlag = cmdline->isolated;
2054 Py_NoSiteFlag = cmdline->no_site_import;
Victor Stinner8ded5b82018-01-24 17:03:28 +01002055
Victor Stinner9cfc0022017-12-20 19:36:46 +01002056 err = _PyCoreConfig_Read(config);
Victor Stinner2b822a02018-01-25 09:18:36 +01002057
2058 cmdline->isolated = Py_IsolatedFlag;
2059 cmdline->no_site_import = Py_NoSiteFlag;
2060 Py_IsolatedFlag = init_isolated;
2061 Py_NoSiteFlag = init_no_site;
2062
Victor Stinner31a83932017-12-04 13:39:15 +01002063 if (_Py_INIT_FAILED(err)) {
2064 pymain->err = err;
2065 return -1;
2066 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002067 return 0;
2068}
2069
2070
Victor Stinner19760862017-12-20 01:41:59 +01002071/* Read the configuration, but initialize also the LC_CTYPE locale:
2072 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
Victor Stinnera7368ac2017-11-15 18:11:45 -08002073static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002074pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002075{
Victor Stinner80a0eba2018-08-23 12:41:35 +02002076 int init_utf8_mode = Py_UTF8Mode;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002077 _PyCoreConfig save_config = _PyCoreConfig_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01002078 int res = -1;
2079
Victor Stinner94540602017-12-16 04:54:22 +01002080 char *oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
2081 if (oldloc == NULL) {
2082 pymain->err = _Py_INIT_NO_MEMORY();
2083 goto done;
2084 }
2085
2086 /* Reconfigure the locale to the default for this process */
2087 _Py_SetLocaleFromEnv(LC_ALL);
2088
2089 int locale_coerced = 0;
2090 int loops = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002091 int init_ignore_env = config->ignore_environment;
2092
2093 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
2094 pymain->err = _Py_INIT_NO_MEMORY();
2095 goto done;
2096 }
Victor Stinner94540602017-12-16 04:54:22 +01002097
2098 while (1) {
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002099 int init_utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002100 int encoding_changed = 0;
2101
2102 /* Watchdog to prevent an infinite loop */
2103 loops++;
2104 if (loops == 3) {
2105 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
2106 "reading the configuration");
2107 goto done;
2108 }
2109
Victor Stinner80a0eba2018-08-23 12:41:35 +02002110 /* bpo-34207: Py_DecodeLocale(), Py_EncodeLocale() and similar
2111 functions depend on Py_UTF8Mode. */
2112 Py_UTF8Mode = config->utf8_mode;
2113
Victor Stinnerddc163d2018-09-24 05:03:01 -07002114 if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01002115 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01002116 }
2117
Victor Stinnerddc163d2018-09-24 05:03:01 -07002118 int conf_res = pymain_read_conf_impl(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002119 if (conf_res != 0) {
2120 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01002121 goto done;
2122 }
2123
2124 /* The legacy C locale assumes ASCII as the default text encoding, which
2125 * causes problems not only for the CPython runtime, but also other
2126 * components like GNU readline.
2127 *
2128 * Accordingly, when the CLI detects it, it attempts to coerce it to a
2129 * more capable UTF-8 based alternative.
2130 *
2131 * See the documentation of the PYTHONCOERCECLOCALE setting for more
2132 * details.
2133 */
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002134 if (config->coerce_c_locale && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01002135 locale_coerced = 1;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002136 _Py_CoerceLegacyLocale(config);
Victor Stinner94540602017-12-16 04:54:22 +01002137 encoding_changed = 1;
2138 }
2139
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002140 if (init_utf8_mode == -1) {
2141 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01002142 /* UTF-8 Mode enabled */
2143 encoding_changed = 1;
2144 }
2145 }
2146 else {
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002147 if (config->utf8_mode != init_utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01002148 encoding_changed = 1;
2149 }
2150 }
2151
2152 if (!encoding_changed) {
2153 break;
2154 }
2155
2156 /* Reset the configuration, except UTF-8 Mode. Set Py_UTF8Mode for
2157 Py_DecodeLocale(). Reset Py_IgnoreEnvironmentFlag, modified by
Victor Stinner8ded5b82018-01-24 17:03:28 +01002158 pymain_read_conf_impl(). Reset Py_IsolatedFlag and Py_NoSiteFlag
2159 modified by _PyCoreConfig_Read(). */
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002160 int new_utf8_mode = config->utf8_mode;
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002161 int new_coerce_c_locale = config->coerce_c_locale;
Victor Stinner94540602017-12-16 04:54:22 +01002162 Py_IgnoreEnvironmentFlag = init_ignore_env;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002163 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
2164 pymain->err = _Py_INIT_NO_MEMORY();
2165 goto done;
2166 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002167 pymain_clear_cmdline(pymain, cmdline);
Miss Islington (bot)046da162018-06-15 15:26:29 -07002168 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002169
2170 cmdline_get_global_config(cmdline);
2171 _PyCoreConfig_GetGlobalConfig(config);
2172 config->utf8_mode = new_utf8_mode;
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002173 config->coerce_c_locale = new_coerce_c_locale;
Victor Stinner94540602017-12-16 04:54:22 +01002174
2175 /* The encoding changed: read again the configuration
2176 with the new encoding */
2177 }
2178 res = 0;
2179
2180done:
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002181 _PyCoreConfig_Clear(&save_config);
Victor Stinner94540602017-12-16 04:54:22 +01002182 if (oldloc != NULL) {
2183 setlocale(LC_ALL, oldloc);
2184 PyMem_RawFree(oldloc);
2185 }
Victor Stinner80a0eba2018-08-23 12:41:35 +02002186 Py_UTF8Mode = init_utf8_mode ;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002187 return res;
2188}
2189
Victor Stinner91106cd2017-12-13 12:29:09 +01002190
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002191static void
2192config_init_locale(_PyCoreConfig *config)
Victor Stinner9cfc0022017-12-20 19:36:46 +01002193{
Victor Stinnerdf738d52018-11-30 12:19:48 +01002194 /* Test also if coerce_c_locale equals 1: PYTHONCOERCECLOCALE=1 doesn't
2195 imply that the C locale is always coerced. It is only coerced if
2196 if the LC_CTYPE locale is "C". */
2197 if (config->coerce_c_locale != 0) {
Victor Stinner144f1e22018-09-17 18:01:39 -07002198 /* The C locale enables the C locale coercion (PEP 538) */
2199 if (_Py_LegacyLocaleDetected()) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002200 config->coerce_c_locale = 1;
Victor Stinner144f1e22018-09-17 18:01:39 -07002201 }
Victor Stinnerdf738d52018-11-30 12:19:48 +01002202 else {
2203 config->coerce_c_locale = 0;
2204 }
Victor Stinner144f1e22018-09-17 18:01:39 -07002205 }
2206
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002207#ifndef MS_WINDOWS
2208 if (config->utf8_mode < 0) {
2209 /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */
2210 const char *ctype_loc = setlocale(LC_CTYPE, NULL);
2211 if (ctype_loc != NULL
2212 && (strcmp(ctype_loc, "C") == 0
2213 || strcmp(ctype_loc, "POSIX") == 0))
2214 {
2215 config->utf8_mode = 1;
2216 }
2217 }
2218#endif
Victor Stinner9cfc0022017-12-20 19:36:46 +01002219}
2220
2221
Victor Stinner8ded5b82018-01-24 17:03:28 +01002222static _PyInitError
2223config_init_module_search_paths(_PyCoreConfig *config)
2224{
2225 assert(config->module_search_paths == NULL);
2226 assert(config->nmodule_search_path < 0);
2227
2228 config->nmodule_search_path = 0;
2229
2230 const wchar_t *sys_path = Py_GetPath();
2231 const wchar_t delim = DELIM;
2232 const wchar_t *p = sys_path;
2233 while (1) {
2234 p = wcschr(sys_path, delim);
2235 if (p == NULL) {
2236 p = sys_path + wcslen(sys_path); /* End of string */
2237 }
2238
2239 size_t path_len = (p - sys_path);
2240 wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t));
2241 if (path == NULL) {
2242 return _Py_INIT_NO_MEMORY();
2243 }
2244 memcpy(path, sys_path, path_len * sizeof(wchar_t));
2245 path[path_len] = L'\0';
2246
2247 _PyInitError err = wstrlist_append(&config->nmodule_search_path,
2248 &config->module_search_paths,
2249 path);
2250 PyMem_RawFree(path);
2251 if (_Py_INIT_FAILED(err)) {
2252 return err;
2253 }
2254
2255 if (*p == '\0') {
2256 break;
2257 }
2258 sys_path = p + 1;
2259 }
2260 return _Py_INIT_OK();
2261}
2262
2263
2264static _PyInitError
2265config_init_path_config(_PyCoreConfig *config)
2266{
2267 _PyInitError err = _PyPathConfig_Init(config);
2268 if (_Py_INIT_FAILED(err)) {
2269 return err;
2270 }
2271
2272 if (config->nmodule_search_path < 0) {
2273 err = config_init_module_search_paths(config);
2274 if (_Py_INIT_FAILED(err)) {
2275 return err;
2276 }
2277 }
2278
2279 if (config->executable == NULL) {
2280 config->executable = _PyMem_RawWcsdup(Py_GetProgramFullPath());
2281 if (config->executable == NULL) {
2282 return _Py_INIT_NO_MEMORY();
2283 }
2284 }
2285
2286 if (config->prefix == NULL) {
2287 config->prefix = _PyMem_RawWcsdup(Py_GetPrefix());
2288 if (config->prefix == NULL) {
2289 return _Py_INIT_NO_MEMORY();
2290 }
2291 }
2292
2293 if (config->exec_prefix == NULL) {
2294 config->exec_prefix = _PyMem_RawWcsdup(Py_GetExecPrefix());
2295 if (config->exec_prefix == NULL) {
2296 return _Py_INIT_NO_MEMORY();
2297 }
2298 }
2299
2300 if (config->base_prefix == NULL) {
2301 config->base_prefix = _PyMem_RawWcsdup(config->prefix);
2302 if (config->base_prefix == NULL) {
2303 return _Py_INIT_NO_MEMORY();
2304 }
2305 }
2306
2307 if (config->base_exec_prefix == NULL) {
2308 config->base_exec_prefix = _PyMem_RawWcsdup(config->exec_prefix);
2309 if (config->base_exec_prefix == NULL) {
2310 return _Py_INIT_NO_MEMORY();
2311 }
2312 }
2313
2314 return _Py_INIT_OK();
2315}
2316
Victor Stinnerda273412017-12-15 01:46:02 +01002317/* Read configuration settings from standard locations
2318 *
2319 * This function doesn't make any changes to the interpreter state - it
2320 * merely populates any missing configuration settings. This allows an
2321 * embedding application to completely override a config option by
2322 * setting it before calling this function, or else modify the default
2323 * setting before passing the fully populated config to Py_EndInitialization.
2324 *
2325 * More advanced selective initialization tricks are possible by calling
2326 * this function multiple times with various preconfigured settings.
2327 */
2328
2329_PyInitError
2330_PyCoreConfig_Read(_PyCoreConfig *config)
2331{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002332 _PyInitError err;
2333
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002334 _PyCoreConfig_GetGlobalConfig(config);
2335
2336 assert(config->ignore_environment >= 0);
2337 if (!config->ignore_environment) {
2338 err = config_read_env_vars(config);
2339 if (_Py_INIT_FAILED(err)) {
2340 return err;
2341 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002342 }
2343
Victor Stinner9cfc0022017-12-20 19:36:46 +01002344 /* -X options */
2345 if (config_get_xoption(config, L"showrefcount")) {
2346 config->show_ref_count = 1;
2347 }
2348 if (config_get_xoption(config, L"showalloccount")) {
2349 config->show_alloc_count = 1;
2350 }
2351
2352 err = config_read_complex_options(config);
2353 if (_Py_INIT_FAILED(err)) {
2354 return err;
2355 }
2356
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002357 if (config->utf8_mode < 0) {
2358 err = config_init_utf8_mode(config);
2359 if (_Py_INIT_FAILED(err)) {
2360 return err;
2361 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002362 }
2363
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002364 if (config->home == NULL) {
2365 err = config_init_home(config);
2366 if (_Py_INIT_FAILED(err)) {
2367 return err;
2368 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002369 }
2370
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002371 if (config->program_name == NULL) {
2372 err = config_init_program_name(config);
2373 if (_Py_INIT_FAILED(err)) {
2374 return err;
2375 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002376 }
2377
Steve Dowere8510492018-11-17 20:42:08 -08002378 if (config->executable == NULL) {
2379 err = config_init_executable(config);
2380 if (_Py_INIT_FAILED(err)) {
2381 return err;
2382 }
2383 }
2384
Victor Stinnerdf738d52018-11-30 12:19:48 +01002385 if (config->coerce_c_locale != 0 || config->utf8_mode < 0) {
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002386 config_init_locale(config);
Victor Stinner94540602017-12-16 04:54:22 +01002387 }
2388
Victor Stinner8ded5b82018-01-24 17:03:28 +01002389 if (!config->_disable_importlib) {
2390 err = config_init_path_config(config);
2391 if (_Py_INIT_FAILED(err)) {
2392 return err;
2393 }
2394 }
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002395
2396 /* default values */
2397 if (config->dev_mode) {
2398 if (config->faulthandler < 0) {
2399 config->faulthandler = 1;
2400 }
2401 if (config->allocator == NULL) {
2402 config->allocator = "debug";
2403 }
2404 }
2405 if (config->install_signal_handlers < 0) {
2406 config->install_signal_handlers = 1;
2407 }
2408 if (config->use_hash_seed < 0) {
2409 config->use_hash_seed = 0;
2410 config->hash_seed = 0;
2411 }
2412 if (config->faulthandler < 0) {
2413 config->faulthandler = 0;
2414 }
2415 if (config->tracemalloc < 0) {
2416 config->tracemalloc = 0;
2417 }
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002418 if (config->coerce_c_locale < 0) {
2419 config->coerce_c_locale = 0;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002420 }
2421 if (config->utf8_mode < 0) {
2422 config->utf8_mode = 0;
2423 }
2424 if (config->argc < 0) {
2425 config->argc = 0;
2426 }
2427
Victor Stinnerda273412017-12-15 01:46:02 +01002428 return _Py_INIT_OK();
2429}
2430
2431
2432void
2433_PyCoreConfig_Clear(_PyCoreConfig *config)
2434{
2435#define CLEAR(ATTR) \
2436 do { \
2437 PyMem_RawFree(ATTR); \
2438 ATTR = NULL; \
2439 } while (0)
Victor Stinner8ded5b82018-01-24 17:03:28 +01002440#define CLEAR_WSTRLIST(LEN, LIST) \
2441 do { \
2442 clear_wstrlist(LEN, LIST); \
2443 LEN = 0; \
2444 LIST = NULL; \
2445 } while (0)
Victor Stinnerda273412017-12-15 01:46:02 +01002446
2447 CLEAR(config->module_search_path_env);
2448 CLEAR(config->home);
2449 CLEAR(config->program_name);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002450 CLEAR(config->program);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002451
Victor Stinner8ded5b82018-01-24 17:03:28 +01002452 CLEAR_WSTRLIST(config->argc, config->argv);
2453 config->argc = -1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002454
Victor Stinner8ded5b82018-01-24 17:03:28 +01002455 CLEAR_WSTRLIST(config->nwarnoption, config->warnoptions);
2456 CLEAR_WSTRLIST(config->nxoption, config->xoptions);
2457 CLEAR_WSTRLIST(config->nmodule_search_path, config->module_search_paths);
2458 config->nmodule_search_path = -1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002459
Victor Stinner8ded5b82018-01-24 17:03:28 +01002460 CLEAR(config->executable);
2461 CLEAR(config->prefix);
2462 CLEAR(config->base_prefix);
2463 CLEAR(config->exec_prefix);
2464 CLEAR(config->base_exec_prefix);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002465#undef CLEAR
Victor Stinner8ded5b82018-01-24 17:03:28 +01002466#undef CLEAR_WSTRLIST
Victor Stinnerda273412017-12-15 01:46:02 +01002467}
2468
2469
2470int
2471_PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
2472{
2473 _PyCoreConfig_Clear(config);
2474
2475#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
Victor Stinnerc4bca952017-12-19 23:48:17 +01002476#define COPY_STR_ATTR(ATTR) \
2477 do { \
2478 if (config2->ATTR != NULL) { \
2479 config->ATTR = _PyMem_RawWcsdup(config2->ATTR); \
2480 if (config->ATTR == NULL) { \
2481 return -1; \
2482 } \
2483 } \
2484 } while (0)
Victor Stinner8ded5b82018-01-24 17:03:28 +01002485#define COPY_WSTRLIST(LEN, LIST) \
2486 do { \
2487 if (config2->LIST != NULL) { \
2488 config->LIST = copy_wstrlist(config2->LEN, config2->LIST); \
2489 if (config->LIST == NULL) { \
2490 return -1; \
2491 } \
2492 } \
2493 config->LEN = config2->LEN; \
2494 } while (0)
Victor Stinnerc4bca952017-12-19 23:48:17 +01002495
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002496 COPY_ATTR(install_signal_handlers);
Victor Stinnerda273412017-12-15 01:46:02 +01002497 COPY_ATTR(ignore_environment);
2498 COPY_ATTR(use_hash_seed);
2499 COPY_ATTR(hash_seed);
2500 COPY_ATTR(_disable_importlib);
2501 COPY_ATTR(allocator);
2502 COPY_ATTR(dev_mode);
2503 COPY_ATTR(faulthandler);
2504 COPY_ATTR(tracemalloc);
2505 COPY_ATTR(import_time);
2506 COPY_ATTR(show_ref_count);
2507 COPY_ATTR(show_alloc_count);
2508 COPY_ATTR(dump_refs);
2509 COPY_ATTR(malloc_stats);
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002510
Victor Stinner95cc3ee2018-09-19 12:01:52 -07002511 COPY_ATTR(coerce_c_locale);
2512 COPY_ATTR(coerce_c_locale_warn);
Victor Stinnerda273412017-12-15 01:46:02 +01002513 COPY_ATTR(utf8_mode);
Victor Stinnerda273412017-12-15 01:46:02 +01002514
2515 COPY_STR_ATTR(module_search_path_env);
2516 COPY_STR_ATTR(home);
2517 COPY_STR_ATTR(program_name);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002518 COPY_STR_ATTR(program);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002519
Victor Stinner8ded5b82018-01-24 17:03:28 +01002520 COPY_WSTRLIST(argc, argv);
2521 COPY_WSTRLIST(nwarnoption, warnoptions);
2522 COPY_WSTRLIST(nxoption, xoptions);
2523 COPY_WSTRLIST(nmodule_search_path, module_search_paths);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002524
Victor Stinner8ded5b82018-01-24 17:03:28 +01002525 COPY_STR_ATTR(executable);
2526 COPY_STR_ATTR(prefix);
2527 COPY_STR_ATTR(base_prefix);
2528 COPY_STR_ATTR(exec_prefix);
2529 COPY_STR_ATTR(base_exec_prefix);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002530
Victor Stinnerc4bca952017-12-19 23:48:17 +01002531#undef COPY_ATTR
Victor Stinnerda273412017-12-15 01:46:02 +01002532#undef COPY_STR_ATTR
Victor Stinner8ded5b82018-01-24 17:03:28 +01002533#undef COPY_WSTRLIST
Victor Stinnerda273412017-12-15 01:46:02 +01002534 return 0;
2535}
2536
2537
Victor Stinner35c28d52018-11-14 02:01:52 +01002538PyObject *
2539_PyCoreConfig_AsDict(const _PyCoreConfig *config)
2540{
2541 PyObject *dict, *obj;
2542
2543 dict = PyDict_New();
2544 if (dict == NULL) {
2545 return NULL;
2546 }
2547
Victor Stinner35c28d52018-11-14 02:01:52 +01002548#define SET_ITEM(KEY, EXPR) \
2549 do { \
2550 obj = (EXPR); \
2551 if (obj == NULL) { \
2552 return NULL; \
2553 } \
2554 int res = PyDict_SetItemString(dict, (KEY), obj); \
2555 Py_DECREF(obj); \
2556 if (res < 0) { \
2557 goto fail; \
2558 } \
2559 } while (0)
Victor Stinner9ee1d422018-11-14 18:58:01 +01002560#define FROM_STRING(STR) \
2561 ((STR != NULL) ? \
2562 PyUnicode_FromString(STR) \
2563 : (Py_INCREF(Py_None), Py_None))
2564#define SET_ITEM_INT(ATTR) \
2565 SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR))
2566#define SET_ITEM_UINT(ATTR) \
2567 SET_ITEM(#ATTR, PyLong_FromUnsignedLong(config->ATTR))
2568#define SET_ITEM_STR(ATTR) \
2569 SET_ITEM(#ATTR, FROM_STRING(config->ATTR))
2570#define FROM_WSTRING(STR) \
2571 ((STR != NULL) ? \
2572 PyUnicode_FromWideChar(STR, -1) \
2573 : (Py_INCREF(Py_None), Py_None))
2574#define SET_ITEM_WSTR(ATTR) \
2575 SET_ITEM(#ATTR, FROM_WSTRING(config->ATTR))
2576#define SET_ITEM_WSTRLIST(NOPTION, OPTIONS) \
2577 SET_ITEM(#OPTIONS, _Py_wstrlist_as_pylist(config->NOPTION, config->OPTIONS))
Victor Stinner35c28d52018-11-14 02:01:52 +01002578
Victor Stinner9ee1d422018-11-14 18:58:01 +01002579 SET_ITEM_INT(install_signal_handlers);
2580 SET_ITEM_INT(ignore_environment);
2581 SET_ITEM_INT(use_hash_seed);
2582 SET_ITEM_UINT(hash_seed);
2583 SET_ITEM_STR(allocator);
2584 SET_ITEM_INT(dev_mode);
2585 SET_ITEM_INT(faulthandler);
2586 SET_ITEM_INT(tracemalloc);
2587 SET_ITEM_INT(import_time);
2588 SET_ITEM_INT(show_ref_count);
2589 SET_ITEM_INT(show_alloc_count);
2590 SET_ITEM_INT(dump_refs);
2591 SET_ITEM_INT(malloc_stats);
2592 SET_ITEM_INT(coerce_c_locale);
2593 SET_ITEM_INT(coerce_c_locale_warn);
2594 SET_ITEM_INT(utf8_mode);
2595 SET_ITEM_WSTR(program_name);
2596 SET_ITEM_WSTRLIST(argc, argv);
2597 SET_ITEM_WSTR(program);
2598 SET_ITEM_WSTRLIST(nxoption, xoptions);
2599 SET_ITEM_WSTRLIST(nwarnoption, warnoptions);
2600 SET_ITEM_WSTR(module_search_path_env);
2601 SET_ITEM_WSTR(home);
2602 SET_ITEM_WSTRLIST(nmodule_search_path, module_search_paths);
2603 SET_ITEM_WSTR(executable);
2604 SET_ITEM_WSTR(prefix);
2605 SET_ITEM_WSTR(base_prefix);
2606 SET_ITEM_WSTR(exec_prefix);
2607 SET_ITEM_WSTR(base_exec_prefix);
2608 SET_ITEM_INT(_disable_importlib);
Victor Stinner35c28d52018-11-14 02:01:52 +01002609
2610 return dict;
2611
2612fail:
2613 Py_DECREF(dict);
2614 return NULL;
2615
2616#undef FROM_STRING
2617#undef FROM_WSTRING
2618#undef SET_ITEM
Victor Stinner9ee1d422018-11-14 18:58:01 +01002619#undef SET_ITEM_INT
2620#undef SET_ITEM_UINT
2621#undef SET_ITEM_STR
2622#undef SET_ITEM_WSTR
2623#undef SET_ITEM_WSTRLIST
Victor Stinner35c28d52018-11-14 02:01:52 +01002624}
2625
2626
Victor Stinnerda273412017-12-15 01:46:02 +01002627void
2628_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
2629{
2630 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002631 Py_CLEAR(config->executable);
2632 Py_CLEAR(config->prefix);
2633 Py_CLEAR(config->base_prefix);
2634 Py_CLEAR(config->exec_prefix);
2635 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002636 Py_CLEAR(config->warnoptions);
2637 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002638 Py_CLEAR(config->module_search_path);
Victor Stinnerda273412017-12-15 01:46:02 +01002639}
2640
2641
2642static PyObject*
2643config_copy_attr(PyObject *obj)
2644{
2645 if (PyUnicode_Check(obj)) {
2646 Py_INCREF(obj);
2647 return obj;
2648 }
2649 else if (PyList_Check(obj)) {
2650 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
2651 }
2652 else if (PyDict_Check(obj)) {
2653 /* The dict type is used for xoptions. Make the assumption that keys
2654 and values are immutables */
2655 return PyDict_Copy(obj);
2656 }
2657 else {
2658 PyErr_Format(PyExc_TypeError,
2659 "cannot copy config attribute of type %.200s",
2660 Py_TYPE(obj)->tp_name);
2661 return NULL;
2662 }
2663}
2664
2665
2666int
2667_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
2668 const _PyMainInterpreterConfig *config2)
2669{
2670 _PyMainInterpreterConfig_Clear(config);
2671
Victor Stinner88cbea42018-11-14 02:45:25 +01002672#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
Victor Stinner9ee1d422018-11-14 18:58:01 +01002673#define COPY_OBJ_ATTR(ATTR) \
Victor Stinnerda273412017-12-15 01:46:02 +01002674 do { \
Victor Stinner9ee1d422018-11-14 18:58:01 +01002675 if (config2->ATTR != NULL) { \
2676 config->ATTR = config_copy_attr(config2->ATTR); \
2677 if (config->ATTR == NULL) { \
Victor Stinnerda273412017-12-15 01:46:02 +01002678 return -1; \
2679 } \
2680 } \
2681 } while (0)
2682
Victor Stinner88cbea42018-11-14 02:45:25 +01002683 COPY_ATTR(install_signal_handlers);
2684 COPY_OBJ_ATTR(argv);
2685 COPY_OBJ_ATTR(executable);
2686 COPY_OBJ_ATTR(prefix);
2687 COPY_OBJ_ATTR(base_prefix);
2688 COPY_OBJ_ATTR(exec_prefix);
2689 COPY_OBJ_ATTR(base_exec_prefix);
2690 COPY_OBJ_ATTR(warnoptions);
2691 COPY_OBJ_ATTR(xoptions);
2692 COPY_OBJ_ATTR(module_search_path);
Victor Stinnerda273412017-12-15 01:46:02 +01002693#undef COPY_ATTR
Victor Stinner88cbea42018-11-14 02:45:25 +01002694#undef COPY_OBJ_ATTR
Victor Stinnerda273412017-12-15 01:46:02 +01002695 return 0;
2696}
2697
2698
Victor Stinner35c28d52018-11-14 02:01:52 +01002699PyObject*
2700_PyMainInterpreterConfig_AsDict(const _PyMainInterpreterConfig *config)
2701{
2702 PyObject *dict, *obj;
2703 int res;
2704
2705 dict = PyDict_New();
2706 if (dict == NULL) {
2707 return NULL;
2708 }
2709
Victor Stinner9ee1d422018-11-14 18:58:01 +01002710#define SET_ITEM_INT(ATTR) \
2711 do { \
2712 obj = PyLong_FromLong(config->ATTR); \
2713 if (obj == NULL) { \
2714 goto fail; \
2715 } \
2716 res = PyDict_SetItemString(dict, #ATTR, obj); \
2717 Py_DECREF(obj); \
2718 if (res < 0) { \
2719 goto fail; \
2720 } \
2721 } while (0)
Victor Stinner35c28d52018-11-14 02:01:52 +01002722
Victor Stinner9ee1d422018-11-14 18:58:01 +01002723#define SET_ITEM_OBJ(ATTR) \
2724 do { \
2725 obj = config->ATTR; \
2726 if (obj == NULL) { \
2727 obj = Py_None; \
2728 } \
2729 res = PyDict_SetItemString(dict, #ATTR, obj); \
2730 if (res < 0) { \
2731 goto fail; \
2732 } \
2733 } while (0)
Victor Stinner35c28d52018-11-14 02:01:52 +01002734
Victor Stinner9ee1d422018-11-14 18:58:01 +01002735 SET_ITEM_INT(install_signal_handlers);
2736 SET_ITEM_OBJ(argv);
2737 SET_ITEM_OBJ(executable);
2738 SET_ITEM_OBJ(prefix);
2739 SET_ITEM_OBJ(base_prefix);
2740 SET_ITEM_OBJ(exec_prefix);
2741 SET_ITEM_OBJ(base_exec_prefix);
2742 SET_ITEM_OBJ(warnoptions);
2743 SET_ITEM_OBJ(xoptions);
2744 SET_ITEM_OBJ(module_search_path);
Victor Stinner35c28d52018-11-14 02:01:52 +01002745
2746 return dict;
2747
2748fail:
2749 Py_DECREF(dict);
2750 return NULL;
2751
Victor Stinner9ee1d422018-11-14 18:58:01 +01002752#undef SET_ITEM_OBJ
Victor Stinner35c28d52018-11-14 02:01:52 +01002753}
Victor Stinnerda273412017-12-15 01:46:02 +01002754
2755
Victor Stinner41264f12017-12-15 02:05:29 +01002756_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01002757_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
2758 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002759{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002760 if (main_config->install_signal_handlers < 0) {
2761 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002762 }
2763
Victor Stinner9cfc0022017-12-20 19:36:46 +01002764 if (main_config->xoptions == NULL) {
2765 main_config->xoptions = config_create_xoptions_dict(config);
2766 if (main_config->xoptions == NULL) {
2767 return _Py_INIT_NO_MEMORY();
2768 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002769 }
2770
Victor Stinner8ded5b82018-01-24 17:03:28 +01002771#define COPY_WSTR(ATTR) \
2772 do { \
2773 if (main_config->ATTR == NULL) { \
2774 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
2775 if (main_config->ATTR == NULL) { \
2776 return _Py_INIT_NO_MEMORY(); \
2777 } \
2778 } \
2779 } while (0)
2780#define COPY_WSTRLIST(ATTR, LEN, LIST) \
2781 do { \
2782 if (ATTR == NULL) { \
Victor Stinner35c28d52018-11-14 02:01:52 +01002783 ATTR = _Py_wstrlist_as_pylist(LEN, LIST); \
Victor Stinner8ded5b82018-01-24 17:03:28 +01002784 if (ATTR == NULL) { \
2785 return _Py_INIT_NO_MEMORY(); \
2786 } \
2787 } \
2788 } while (0)
2789
2790 COPY_WSTRLIST(main_config->warnoptions,
2791 config->nwarnoption, config->warnoptions);
2792 if (config->argc >= 0) {
2793 COPY_WSTRLIST(main_config->argv,
2794 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002795 }
2796
Victor Stinner8ded5b82018-01-24 17:03:28 +01002797 if (!config->_disable_importlib) {
2798 COPY_WSTR(executable);
2799 COPY_WSTR(prefix);
2800 COPY_WSTR(base_prefix);
2801 COPY_WSTR(exec_prefix);
2802 COPY_WSTR(base_exec_prefix);
2803
2804 COPY_WSTRLIST(main_config->module_search_path,
2805 config->nmodule_search_path, config->module_search_paths);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002806 }
Victor Stinner41264f12017-12-15 02:05:29 +01002807
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002808 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01002809#undef COPY_WSTR
2810#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002811}
2812
2813
2814static int
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002815pymain_init_python_main(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002816{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002817 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002818
Victor Stinner9cfc0022017-12-20 19:36:46 +01002819 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002820 err = _PyMainInterpreterConfig_Read(&main_config, &interp->core_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002821 if (!_Py_INIT_FAILED(err)) {
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002822 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002823 }
2824 _PyMainInterpreterConfig_Clear(&main_config);
2825
2826 if (_Py_INIT_FAILED(err)) {
2827 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002828 return -1;
2829 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002830 return 0;
2831}
Victor Stinnera7368ac2017-11-15 18:11:45 -08002832
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002833
2834static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002835pymain_init_sys_path(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002836{
Victor Stinnerca719ac2017-12-20 18:00:19 +01002837 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002838 /* If filename is a package (ex: directory or ZIP file) which contains
2839 __main__.py, main_importer_path is set to filename and will be
2840 prepended to sys.path by pymain_run_main_from_importer(). Otherwise,
2841 main_importer_path is set to NULL. */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002842 pymain->main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01002843 }
2844
Victor Stinner19760862017-12-20 01:41:59 +01002845 PyObject *path0;
Victor Stinnerddc163d2018-09-24 05:03:01 -07002846 if (pymain_compute_path0(pymain, config, &path0) < 0) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002847 return -1;
2848 }
Victor Stinner19760862017-12-20 01:41:59 +01002849
Victor Stinner19760862017-12-20 01:41:59 +01002850 if (path0 != NULL) {
2851 if (pymain_update_sys_path(pymain, path0) < 0) {
2852 Py_DECREF(path0);
2853 return -1;
2854 }
2855 Py_DECREF(path0);
2856 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002857 return 0;
2858}
2859
2860
2861static void
2862pymain_run_python(_PyMain *pymain)
2863{
Victor Stinner19760862017-12-20 01:41:59 +01002864 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08002865
2866 pymain_header(pymain);
2867 pymain_import_readline(pymain);
2868
Victor Stinnerca719ac2017-12-20 18:00:19 +01002869 if (pymain->command) {
2870 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002871 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002872 else if (pymain->module) {
2873 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002874 }
2875 else {
Victor Stinner19760862017-12-20 01:41:59 +01002876 pymain_run_filename(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002877 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002878
Victor Stinner19760862017-12-20 01:41:59 +01002879 pymain_repl(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002880}
2881
2882
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002883static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002884pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
2885 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002886{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002887 pymain->err = _PyRuntime_Initialize();
2888 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002889 return -1;
2890 }
2891
Victor Stinnerddc163d2018-09-24 05:03:01 -07002892 int res = pymain_read_conf(pymain, config, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002893 if (res < 0) {
2894 return -1;
2895 }
2896 if (res > 0) {
2897 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01002898 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002899 }
2900
Victor Stinner94540602017-12-16 04:54:22 +01002901 if (cmdline->print_help) {
Victor Stinnerddc163d2018-09-24 05:03:01 -07002902 pymain_usage(0, config->program);
Victor Stinner19760862017-12-20 01:41:59 +01002903 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002904 }
2905
2906 if (cmdline->print_version) {
2907 printf("Python %s\n",
2908 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01002909 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002910 }
2911
Victor Stinnerc4bca952017-12-19 23:48:17 +01002912 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002913 orig_argv = copy_wstrlist(pymain->argc, cmdline->argv);
2914 if (orig_argv == NULL) {
2915 pymain->err = _Py_INIT_NO_MEMORY();
2916 return -1;
2917 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01002918 orig_argc = pymain->argc;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002919
Victor Stinnerddc163d2018-09-24 05:03:01 -07002920 _PyInitError err = config_init_warnoptions(config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002921 if (_Py_INIT_FAILED(err)) {
2922 pymain->err = err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002923 return -1;
2924 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002925 return 0;
2926}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00002927
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002928
Victor Stinnerca719ac2017-12-20 18:00:19 +01002929/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
2930 LC_CTYPE locale and Py_DecodeLocale().
2931
2932 Configuration:
2933
2934 * Command line arguments
2935 * Environment variables
2936 * Py_xxx global configuration variables
2937
Victor Stinnerddc163d2018-09-24 05:03:01 -07002938 _PyCmdline is a temporary structure used to prioritize these
Victor Stinnerca719ac2017-12-20 18:00:19 +01002939 variables. */
2940static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002941pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerca719ac2017-12-20 18:00:19 +01002942{
Victor Stinner31e99082017-12-20 23:41:38 +01002943 /* Force default allocator, since pymain_free() and pymain_clear_config()
2944 must use the same allocator than this function. */
2945 PyMemAllocatorEx old_alloc;
2946 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2947#ifdef Py_DEBUG
2948 PyMemAllocatorEx default_alloc;
2949 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
2950#endif
2951
Victor Stinnerddc163d2018-09-24 05:03:01 -07002952 _PyCmdline cmdline;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002953 memset(&cmdline, 0, sizeof(cmdline));
2954
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002955 cmdline_get_global_config(&cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002956
Victor Stinnerddc163d2018-09-24 05:03:01 -07002957 int res = pymain_cmdline_impl(pymain, config, &cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002958
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002959 cmdline_set_global_config(&cmdline);
Victor Stinnerddc163d2018-09-24 05:03:01 -07002960 _PyCoreConfig_SetGlobalConfig(config);
Victor Stinner0c90d6f2018-08-05 12:31:59 +02002961 if (Py_IsolatedFlag) {
2962 Py_IgnoreEnvironmentFlag = 1;
2963 Py_NoUserSiteDirectory = 1;
2964 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002965
2966 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01002967
2968#ifdef Py_DEBUG
2969 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
2970 PyMemAllocatorEx cur_alloc;
2971 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
2972 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
2973#endif
2974 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002975 return res;
2976}
2977
2978
Victor Stinner94540602017-12-16 04:54:22 +01002979static int
Victor Stinnerddc163d2018-09-24 05:03:01 -07002980pymain_init(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +01002981{
Victor Stinnerddc163d2018-09-24 05:03:01 -07002982 _PyCoreConfig local_config = _PyCoreConfig_INIT;
2983 _PyCoreConfig *config = &local_config;
Victor Stinner94540602017-12-16 04:54:22 +01002984
Victor Stinnerddc163d2018-09-24 05:03:01 -07002985 /* 754 requires that FP exceptions run in "no stop" mode by default,
2986 * and until C vendors implement C99's ways to control FP exceptions,
2987 * Python requires non-stop mode. Alas, some platforms enable FP
2988 * exceptions by default. Here we disable them.
2989 */
2990#ifdef __FreeBSD__
2991 fedisableexcept(FE_OVERFLOW);
2992#endif
2993
2994 config->_disable_importlib = 0;
2995 config->install_signal_handlers = 1;
2996 _PyCoreConfig_GetGlobalConfig(config);
2997
2998 int res = pymain_cmdline(pymain, config);
Victor Stinner19760862017-12-20 01:41:59 +01002999 if (res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01003000 _Py_FatalInitError(pymain->err);
3001 }
Victor Stinner19760862017-12-20 01:41:59 +01003002 if (res == 1) {
Victor Stinnerddc163d2018-09-24 05:03:01 -07003003 pymain_clear_config(&local_config);
3004 return res;
Victor Stinner19760862017-12-20 01:41:59 +01003005 }
3006
Victor Stinner9cfc0022017-12-20 19:36:46 +01003007 pymain_init_stdio(pymain);
3008
Victor Stinner0c90d6f2018-08-05 12:31:59 +02003009 PyInterpreterState *interp;
Victor Stinnerddc163d2018-09-24 05:03:01 -07003010 pymain->err = _Py_InitializeCore(&interp, config);
Victor Stinner0c90d6f2018-08-05 12:31:59 +02003011 if (_Py_INIT_FAILED(pymain->err)) {
3012 _Py_FatalInitError(pymain->err);
Victor Stinner19760862017-12-20 01:41:59 +01003013 }
3014
Victor Stinnerddc163d2018-09-24 05:03:01 -07003015 pymain_clear_config(&local_config);
3016 config = &interp->core_config;
3017
Victor Stinner0c90d6f2018-08-05 12:31:59 +02003018 if (pymain_init_python_main(pymain, interp) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01003019 _Py_FatalInitError(pymain->err);
3020 }
3021
Victor Stinnerddc163d2018-09-24 05:03:01 -07003022 if (pymain_init_sys_path(pymain, config) < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01003023 _Py_FatalInitError(pymain->err);
3024 }
Victor Stinnerddc163d2018-09-24 05:03:01 -07003025 return 0;
3026}
3027
3028
3029static int
3030pymain_main(_PyMain *pymain)
3031{
3032 int res = pymain_init(pymain);
3033 if (res == 1) {
3034 goto done;
3035 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01003036
Victor Stinner19760862017-12-20 01:41:59 +01003037 pymain_run_python(pymain);
3038
3039 if (Py_FinalizeEx() < 0) {
3040 /* Value unlikely to be confused with a non-error exit status or
3041 other special meaning */
3042 pymain->status = 120;
3043 }
3044
3045done:
Victor Stinner94540602017-12-16 04:54:22 +01003046 pymain_free(pymain);
3047
Victor Stinner94540602017-12-16 04:54:22 +01003048 return pymain->status;
3049}
3050
3051
Victor Stinnerf7e5b562017-11-15 15:48:08 -08003052int
3053Py_Main(int argc, wchar_t **argv)
3054{
3055 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01003056 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08003057 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01003058 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08003059
Victor Stinner95cc3ee2018-09-19 12:01:52 -07003060 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00003061}
3062
Victor Stinner94540602017-12-16 04:54:22 +01003063
3064int
3065_Py_UnixMain(int argc, char **argv)
3066{
3067 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01003068 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01003069 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01003070 pymain.bytes_argv = argv;
3071
Victor Stinner95cc3ee2018-09-19 12:01:52 -07003072 return pymain_main(&pymain);
Victor Stinner94540602017-12-16 04:54:22 +01003073}
3074
3075
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003076/* this is gonna seem *real weird*, but if you put some other code between
3077 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
3078 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00003079
Guido van Rossum667d7041995-08-04 04:20:48 +00003080/* Make the *original* argc/argv available to other modules.
3081 This is rare, but it is needed by the secureware extension. */
3082
3083void
Martin v. Löwis790465f2008-04-05 20:41:37 +00003084Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00003085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 *argc = orig_argc;
3087 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00003088}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003089
3090#ifdef __cplusplus
3091}
3092#endif