blob: 3a6cf31fc860b88a7ebfa2d7cc572565633340d2 [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\
Cheryl Sabella186b6062018-02-24 22:04:40 -050099-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"
Carl Meyerb193fa92018-06-15 22:40:56 -0600147"PYTHONDEVMODE: enable the development mode.\n"
148"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
Guido van Rossum667d7041995-08-04 04:20:48 +0000149
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800150static void
Victor Stinnera7368ac2017-11-15 18:11:45 -0800151pymain_usage(int error, const wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000152{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800153 FILE *f = error ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 fprintf(f, usage_line, program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800156 if (error)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 fprintf(f, "Try `python -h' for more information.\n");
158 else {
159 fputs(usage_1, f);
160 fputs(usage_2, f);
161 fputs(usage_3, f);
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200162 fprintf(f, usage_4, (wint_t)DELIM);
163 fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100164 fputs(usage_6, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000166}
167
Victor Stinnera7368ac2017-11-15 18:11:45 -0800168
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200169static const char*
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200170config_get_env_var(const _PyCoreConfig *config, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -0800171{
Victor Stinnerd1457752018-07-26 16:04:56 +0200172 assert(config->use_environment >= 0);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200173
Victor Stinnerd1457752018-07-26 16:04:56 +0200174 if (!config->use_environment) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200175 return NULL;
176 }
177
178 const char *var = getenv(name);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800179 if (var && var[0] != '\0') {
180 return var;
181 }
182 else {
183 return NULL;
184 }
185}
186
187
Victor Stinnerca719ac2017-12-20 18:00:19 +0100188static int
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200189config_get_env_var_dup(const _PyCoreConfig *config, wchar_t **dest, wchar_t *wname, char *name)
Victor Stinnerca719ac2017-12-20 18:00:19 +0100190{
Victor Stinnerd1457752018-07-26 16:04:56 +0200191 assert(config->use_environment >= 0);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200192
Victor Stinnerd1457752018-07-26 16:04:56 +0200193 if (!config->use_environment) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100194 *dest = NULL;
195 return 0;
196 }
197
198#ifdef MS_WINDOWS
199 const wchar_t *var = _wgetenv(wname);
200 if (!var || var[0] == '\0') {
201 *dest = NULL;
202 return 0;
203 }
204
205 wchar_t *copy = _PyMem_RawWcsdup(var);
206 if (copy == NULL) {
207 return -1;
208 }
209
210 *dest = copy;
211#else
212 const char *var = getenv(name);
213 if (!var || var[0] == '\0') {
214 *dest = NULL;
215 return 0;
216 }
217
218 size_t len;
219 wchar_t *wvar = Py_DecodeLocale(var, &len);
220 if (!wvar) {
221 if (len == (size_t)-2) {
222 return -2;
223 }
224 else {
225 return -1;
226 }
227 }
228 *dest = wvar;
229#endif
230 return 0;
231}
232
233
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800234static void
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800235pymain_run_interactive_hook(void)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200236{
237 PyObject *sys, *hook, *result;
238 sys = PyImport_ImportModule("sys");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800239 if (sys == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200240 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800241 }
242
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200243 hook = PyObject_GetAttrString(sys, "__interactivehook__");
244 Py_DECREF(sys);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800245 if (hook == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200246 PyErr_Clear();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800247 return;
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200248 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800249
250 result = _PyObject_CallNoArg(hook);
251 Py_DECREF(hook);
252 if (result == NULL) {
253 goto error;
254 }
255 Py_DECREF(result);
256
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200257 return;
258
259error:
260 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
261 PyErr_Print();
262 PyErr_Clear();
263}
264
Thomas Woutersa9773292006-04-21 09:43:23 +0000265
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800266static int
Victor Stinnerc4bca952017-12-19 23:48:17 +0100267pymain_run_module(const wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 PyObject *module, *runpy, *runmodule, *runargs, *result;
270 runpy = PyImport_ImportModule("runpy");
271 if (runpy == NULL) {
272 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200273 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 return -1;
275 }
276 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
277 if (runmodule == NULL) {
278 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200279 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_DECREF(runpy);
281 return -1;
282 }
283 module = PyUnicode_FromWideChar(modname, wcslen(modname));
284 if (module == NULL) {
285 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200286 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 Py_DECREF(runpy);
288 Py_DECREF(runmodule);
289 return -1;
290 }
291 runargs = Py_BuildValue("(Oi)", module, set_argv0);
292 if (runargs == NULL) {
293 fprintf(stderr,
294 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200295 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 Py_DECREF(runpy);
297 Py_DECREF(runmodule);
298 Py_DECREF(module);
299 return -1;
300 }
301 result = PyObject_Call(runmodule, runargs, NULL);
302 if (result == NULL) {
303 PyErr_Print();
304 }
305 Py_DECREF(runpy);
306 Py_DECREF(runmodule);
307 Py_DECREF(module);
308 Py_DECREF(runargs);
309 if (result == NULL) {
310 return -1;
311 }
312 Py_DECREF(result);
313 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000314}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000315
Nick Coghland2977a32017-03-12 20:38:32 +1000316static PyObject *
Victor Stinnerc4bca952017-12-19 23:48:17 +0100317pymain_get_importer(const wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000318{
Nick Coghland2977a32017-03-12 20:38:32 +1000319 PyObject *sys_path0 = NULL, *importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000320
Nick Coghland2977a32017-03-12 20:38:32 +1000321 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800322 if (sys_path0 == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000323 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800324 }
Victor Stinner4726e402010-10-06 23:24:57 +0000325
Nick Coghland2977a32017-03-12 20:38:32 +1000326 importer = PyImport_GetImporter(sys_path0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800327 if (importer == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000328 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800329 }
Victor Stinner4726e402010-10-06 23:24:57 +0000330
Brett Cannonaa936422012-04-27 15:30:58 -0400331 if (importer == Py_None) {
Nick Coghland2977a32017-03-12 20:38:32 +1000332 Py_DECREF(sys_path0);
Victor Stinner4726e402010-10-06 23:24:57 +0000333 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000334 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800336
Victor Stinner4726e402010-10-06 23:24:57 +0000337 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000338 return sys_path0;
Victor Stinner4726e402010-10-06 23:24:57 +0000339
Nick Coghland2977a32017-03-12 20:38:32 +1000340error:
341 Py_XDECREF(sys_path0);
342 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
343 PyErr_Print();
344 PyErr_Clear();
345 return NULL;
346}
347
348
349static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800350pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
Victor Stinnera62207c2010-08-07 10:57:17 +0000351{
352 PyObject *unicode, *bytes;
353 int ret;
354
355 unicode = PyUnicode_FromWideChar(command, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800356 if (unicode == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000357 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800358 }
359
Victor Stinnera62207c2010-08-07 10:57:17 +0000360 bytes = PyUnicode_AsUTF8String(unicode);
361 Py_DECREF(unicode);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800362 if (bytes == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000363 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800364 }
365
Victor Stinnera62207c2010-08-07 10:57:17 +0000366 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
367 Py_DECREF(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800368 return (ret != 0);
Victor Stinnera62207c2010-08-07 10:57:17 +0000369
370error:
Victor Stinner398356b2010-08-18 22:23:22 +0000371 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000372 PyErr_Print();
373 return 1;
374}
375
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800376
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000377static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800378pymain_run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf)
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000379{
380 PyObject *unicode, *bytes = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200381 const char *filename_str;
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000382 int run;
383
384 /* call pending calls like signal handlers (SIGINT) */
385 if (Py_MakePendingCalls() == -1) {
386 PyErr_Print();
387 return 1;
388 }
389
390 if (filename) {
391 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
392 if (unicode != NULL) {
Victor Stinnere0f32682010-10-17 19:34:51 +0000393 bytes = PyUnicode_EncodeFSDefault(unicode);
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000394 Py_DECREF(unicode);
395 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800396 if (bytes != NULL) {
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000397 filename_str = PyBytes_AsString(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800398 }
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000399 else {
400 PyErr_Clear();
Victor Stinnere0f32682010-10-17 19:34:51 +0000401 filename_str = "<encoding error>";
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000402 }
403 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800404 else {
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000405 filename_str = "<stdin>";
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800406 }
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000407
408 run = PyRun_AnyFileExFlags(fp, filename_str, filename != NULL, p_cf);
409 Py_XDECREF(bytes);
410 return run != 0;
411}
412
Christian Heimes9cd17752007-11-18 19:35:23 +0000413
Guido van Rossum667d7041995-08-04 04:20:48 +0000414/* Main program */
415
Eric Snow6b4be192017-05-22 21:36:03 -0700416typedef struct {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100417 wchar_t **argv;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200418 int nwarnoption; /* Number of -W command line options */
419 wchar_t **warnoptions; /* Command line -W options */
420 int nenv_warnoption; /* Number of PYTHONWARNINGS environment variables */
421 wchar_t **env_warnoptions; /* PYTHONWARNINGS environment variables */
Eric Snow6b4be192017-05-22 21:36:03 -0700422 int print_help; /* -h, -? options */
423 int print_version; /* -V option */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200424} _PyCmdline;
Eric Snow6b4be192017-05-22 21:36:03 -0700425
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800426/* Structure used by Py_Main() to pass data to subfunctions */
427typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100428 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800429 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100430 int use_bytes_argv;
431 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100432 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100433
434 /* Exit status or "exit code": result of pymain_main() */
435 int status;
436 /* Error message if a function failed */
437 _PyInitError err;
438
Victor Stinner19760862017-12-20 01:41:59 +0100439 /* non-zero is stdin is a TTY or if -i option is used */
440 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100441 int skip_first_line; /* -x option */
442 wchar_t *filename; /* Trailing arg without -c or -m */
443 wchar_t *command; /* -c argument */
444 wchar_t *module; /* -m argument */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800445} _PyMain;
446
Victor Stinner1dc6e392018-07-25 02:49:17 +0200447#define _PyMain_INIT {.err = _Py_INIT_OK()}
Victor Stinnerd5dda982017-12-13 17:31:16 +0100448/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800449
450
Victor Stinner19760862017-12-20 01:41:59 +0100451/* Non-zero if filename, command (-c) or module (-m) is set
452 on the command line */
453#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100454 (pymain->command != NULL || pymain->filename != NULL \
455 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100456
457
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200458static void
459clear_wstrlist(int len, wchar_t **list)
460{
461 for (int i=0; i < len; i++) {
462 PyMem_RawFree(list[i]);
463 }
464 PyMem_RawFree(list);
465}
466
467
468static wchar_t**
469copy_wstrlist(int len, wchar_t **list)
470{
471 assert((len > 0 && list != NULL) || len == 0);
472 size_t size = len * sizeof(list[0]);
473 wchar_t **list_copy = PyMem_RawMalloc(size);
474 for (int i=0; i < len; i++) {
475 wchar_t* arg = _PyMem_RawWcsdup(list[i]);
476 if (arg == NULL) {
477 clear_wstrlist(i, list);
478 return NULL;
479 }
480 list_copy[i] = arg;
481 }
482 return list_copy;
483}
484
485
Victor Stinnerca719ac2017-12-20 18:00:19 +0100486static wchar_t*
487pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800488{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100489 wchar_t *str2 = _PyMem_RawWcsdup(str);
490 if (str2 == NULL) {
491 pymain->err = _Py_INIT_NO_MEMORY();
492 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800493 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100494 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800495}
496
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100497
Victor Stinnerc4bca952017-12-19 23:48:17 +0100498static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200499pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,
500 _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100501{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100502 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100503
Victor Stinnerca719ac2017-12-20 18:00:19 +0100504 if (pymain->use_bytes_argv) {
505 /* +1 for a the NULL terminator */
506 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
507 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
508 if (argv == NULL) {
509 pymain->err = _Py_INIT_NO_MEMORY();
510 return -1;
511 }
512
513 for (int i = 0; i < pymain->argc; i++) {
514 size_t len;
515 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
516 if (arg == NULL) {
517 clear_wstrlist(i, argv);
518 pymain->err = DECODE_LOCALE_ERR("command line arguments",
519 (Py_ssize_t)len);
520 return -1;
521 }
522 argv[i] = arg;
523 }
524 argv[pymain->argc] = NULL;
525
526 cmdline->argv = argv;
527 }
528 else {
529 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100530 }
531
Victor Stinnerca719ac2017-12-20 18:00:19 +0100532 wchar_t *program;
533 if (pymain->argc >= 1 && cmdline->argv != NULL) {
534 program = cmdline->argv[0];
535 }
536 else {
537 program = L"";
538 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200539 config->program = pymain_wstrdup(pymain, program);
540 if (config->program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100541 return -1;
542 }
543
Victor Stinnerc4bca952017-12-19 23:48:17 +0100544 return 0;
545}
546
547
548static void
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200549_PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config)
550{
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200551#define COPY_FLAG(ATTR, VALUE) \
552 if (config->ATTR == -1) { \
553 config->ATTR = VALUE; \
554 }
555#define COPY_NOT_FLAG(ATTR, VALUE) \
556 if (config->ATTR == -1) { \
557 config->ATTR = !(VALUE); \
558 }
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200559
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200560 COPY_FLAG(utf8_mode, Py_UTF8Mode);
561 COPY_FLAG(isolated, Py_IsolatedFlag);
562 COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
563 COPY_FLAG(inspect, Py_InspectFlag);
564 COPY_FLAG(interactive, Py_InteractiveFlag);
565 COPY_FLAG(optimization_level, Py_OptimizeFlag);
566 COPY_FLAG(debug, Py_DebugFlag);
567 COPY_FLAG(verbose, Py_VerboseFlag);
568 COPY_FLAG(quiet, Py_QuietFlag);
569 COPY_FLAG(unbuffered_stdio, Py_UnbufferedStdioFlag);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200570#ifdef MS_WINDOWS
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200571 COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
572 COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200573#endif
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200574
Victor Stinnerd1457752018-07-26 16:04:56 +0200575 COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200576 COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
577 COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
578 COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
579
580 if (config->_check_hash_pycs_mode == NULL) {
581 config->_check_hash_pycs_mode = _Py_CheckHashBasedPycsMode;
582 }
583
584#undef COPY_FLAG
585#undef COPY_NOT_FLAG
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200586}
587
588
589/* Set Py_xxx global configuration variables from 'config' configuration. */
590void
591_PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config)
592{
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200593#define COPY_FLAG(ATTR, VAR) \
594 if (config->ATTR != -1) { \
595 VAR = config->ATTR; \
596 }
597#define COPY_NOT_FLAG(ATTR, VAR) \
598 if (config->ATTR != -1) { \
599 VAR = !config->ATTR; \
600 }
601
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200602 COPY_FLAG(utf8_mode, Py_UTF8Mode);
603 COPY_FLAG(isolated, Py_IsolatedFlag);
604 COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
605 COPY_FLAG(inspect, Py_InspectFlag);
606 COPY_FLAG(interactive, Py_InteractiveFlag);
607 COPY_FLAG(optimization_level, Py_OptimizeFlag);
608 COPY_FLAG(debug, Py_DebugFlag);
609 COPY_FLAG(verbose, Py_VerboseFlag);
610 COPY_FLAG(quiet, Py_QuietFlag);
611 COPY_FLAG(unbuffered_stdio, Py_UnbufferedStdioFlag);
612#ifdef MS_WINDOWS
613 COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
614 COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
615#endif
616
Victor Stinnerd1457752018-07-26 16:04:56 +0200617 COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200618 COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
619 COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
620 COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
621
622 if (config->_check_hash_pycs_mode != NULL) {
623 _Py_CheckHashBasedPycsMode = config->_check_hash_pycs_mode;
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200624 }
625
626 /* Random or non-zero hash seed */
627 Py_HashRandomizationFlag = (config->use_hash_seed == 0 ||
628 config->hash_seed != 0);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200629
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200630#undef COPY_FLAG
631#undef COPY_NOT_FLAG
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200632}
633
634
635/* Free memory allocated in config, but don't clear all attributes */
636void
637_PyCoreConfig_Clear(_PyCoreConfig *config)
638{
639#define CLEAR(ATTR) \
640 do { \
641 PyMem_RawFree(ATTR); \
642 ATTR = NULL; \
643 } while (0)
644#define CLEAR_WSTRLIST(LEN, LIST) \
645 do { \
646 clear_wstrlist(LEN, LIST); \
647 LEN = 0; \
648 LIST = NULL; \
649 } while (0)
650
651 CLEAR(config->pycache_prefix);
652 CLEAR(config->module_search_path_env);
653 CLEAR(config->home);
654 CLEAR(config->program_name);
655 CLEAR(config->program);
656
657 CLEAR_WSTRLIST(config->argc, config->argv);
658 config->argc = -1;
659
660 CLEAR_WSTRLIST(config->nwarnoption, config->warnoptions);
661 CLEAR_WSTRLIST(config->nxoption, config->xoptions);
662 CLEAR_WSTRLIST(config->nmodule_search_path, config->module_search_paths);
663 config->nmodule_search_path = -1;
664
665 CLEAR(config->executable);
666 CLEAR(config->prefix);
667 CLEAR(config->base_prefix);
668 CLEAR(config->exec_prefix);
669#ifdef MS_WINDOWS
670 CLEAR(config->dll_path);
671#endif
672 CLEAR(config->base_exec_prefix);
673#undef CLEAR
674#undef CLEAR_WSTRLIST
675}
676
677
678int
679_PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
680{
681 _PyCoreConfig_Clear(config);
682
683#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
684#define COPY_STR_ATTR(ATTR) \
685 do { \
686 if (config2->ATTR != NULL) { \
687 config->ATTR = _PyMem_RawWcsdup(config2->ATTR); \
688 if (config->ATTR == NULL) { \
689 return -1; \
690 } \
691 } \
692 } while (0)
693#define COPY_WSTRLIST(LEN, LIST) \
694 do { \
695 if (config2->LIST != NULL) { \
696 config->LIST = copy_wstrlist(config2->LEN, config2->LIST); \
697 if (config->LIST == NULL) { \
698 return -1; \
699 } \
700 } \
701 config->LEN = config2->LEN; \
702 } while (0)
703
Victor Stinnerecf411c2018-07-26 02:37:22 +0200704 COPY_ATTR(install_signal_handlers);
Victor Stinnerd1457752018-07-26 16:04:56 +0200705 COPY_ATTR(use_environment);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200706 COPY_ATTR(use_hash_seed);
707 COPY_ATTR(hash_seed);
708 COPY_ATTR(_install_importlib);
709 COPY_ATTR(allocator);
710 COPY_ATTR(dev_mode);
711 COPY_ATTR(faulthandler);
712 COPY_ATTR(tracemalloc);
713 COPY_ATTR(import_time);
714 COPY_ATTR(show_ref_count);
715 COPY_ATTR(show_alloc_count);
716 COPY_ATTR(dump_refs);
717 COPY_ATTR(malloc_stats);
Victor Stinnerecf411c2018-07-26 02:37:22 +0200718
719 COPY_ATTR(coerce_c_locale);
720 COPY_ATTR(coerce_c_locale_warn);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200721 COPY_ATTR(utf8_mode);
722
723 COPY_STR_ATTR(pycache_prefix);
724 COPY_STR_ATTR(module_search_path_env);
725 COPY_STR_ATTR(home);
726 COPY_STR_ATTR(program_name);
727 COPY_STR_ATTR(program);
728
729 COPY_WSTRLIST(argc, argv);
730 COPY_WSTRLIST(nwarnoption, warnoptions);
731 COPY_WSTRLIST(nxoption, xoptions);
732 COPY_WSTRLIST(nmodule_search_path, module_search_paths);
733
734 COPY_STR_ATTR(executable);
735 COPY_STR_ATTR(prefix);
736 COPY_STR_ATTR(base_prefix);
737 COPY_STR_ATTR(exec_prefix);
738#ifdef MS_WINDOWS
739 COPY_STR_ATTR(dll_path);
740#endif
741 COPY_STR_ATTR(base_exec_prefix);
742
743 COPY_ATTR(isolated);
744 COPY_ATTR(site_import);
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200745 COPY_ATTR(bytes_warning);
746 COPY_ATTR(inspect);
747 COPY_ATTR(interactive);
748 COPY_ATTR(optimization_level);
749 COPY_ATTR(debug);
750 COPY_ATTR(write_bytecode);
751 COPY_ATTR(verbose);
752 COPY_ATTR(quiet);
753 COPY_ATTR(user_site_directory);
754 COPY_ATTR(unbuffered_stdio);
755#ifdef MS_WINDOWS
756 COPY_ATTR(legacy_windows_fs_encoding);
757 COPY_ATTR(legacy_windows_stdio);
758#endif
759 COPY_ATTR(_check_hash_pycs_mode);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200760
761#undef COPY_ATTR
762#undef COPY_STR_ATTR
763#undef COPY_WSTRLIST
764 return 0;
765}
766
767
768static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200769pymain_clear_cmdline(_PyMain *pymain, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100770{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100771 PyMemAllocatorEx old_alloc;
772 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100773
Victor Stinnerca719ac2017-12-20 18:00:19 +0100774 clear_wstrlist(cmdline->nwarnoption, cmdline->warnoptions);
775 cmdline->nwarnoption = 0;
776 cmdline->warnoptions = NULL;
777
778 clear_wstrlist(cmdline->nenv_warnoption, cmdline->env_warnoptions);
779 cmdline->nenv_warnoption = 0;
780 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100781
782 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100783 clear_wstrlist(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100784 }
785 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100786
787 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
788}
789
790
791static void
792pymain_clear_pymain(_PyMain *pymain)
793{
794#define CLEAR(ATTR) \
795 do { \
796 PyMem_RawFree(ATTR); \
797 ATTR = NULL; \
798 } while (0)
799
800 CLEAR(pymain->filename);
801 CLEAR(pymain->command);
802 CLEAR(pymain->module);
803#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100804}
805
Victor Stinnerc4bca952017-12-19 23:48:17 +0100806static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200807pymain_clear_config(_PyCoreConfig *config)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100808{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100809 /* Clear core config with the memory allocator
810 used by pymain_read_conf() */
811 PyMemAllocatorEx old_alloc;
812 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
813
Victor Stinner1dc6e392018-07-25 02:49:17 +0200814 _PyCoreConfig_Clear(config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100815
816 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
817}
818
819
820static void
Victor Stinnerd1457752018-07-26 16:04:56 +0200821pymain_free(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100822{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100823 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100824
Victor Stinnerc4bca952017-12-19 23:48:17 +0100825 /* Free global variables which cannot be freed in Py_Finalize():
826 configuration options set before Py_Initialize() which should
827 remain valid after Py_Finalize(), since
828 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinnerb1147e42018-07-21 02:06:16 +0200829 _PyPathConfig_ClearGlobal();
Victor Stinner94540602017-12-16 04:54:22 +0100830
Victor Stinnerc4bca952017-12-19 23:48:17 +0100831 /* Force the allocator used by pymain_read_conf() */
832 PyMemAllocatorEx old_alloc;
833 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100834
Victor Stinnerca719ac2017-12-20 18:00:19 +0100835 pymain_clear_pymain(pymain);
836
837 clear_wstrlist(orig_argc, orig_argv);
838 orig_argc = 0;
839 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100840
Victor Stinnerc4bca952017-12-19 23:48:17 +0100841 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200842
843#ifdef __INSURE__
844 /* Insure++ is a memory analysis tool that aids in discovering
845 * memory leaks and other memory problems. On Python exit, the
846 * interned string dictionaries are flagged as being in use at exit
847 * (which it is). Under normal circumstances, this is fine because
848 * the memory will be automatically reclaimed by the system. Under
849 * memory debugging, it's a huge source of useless noise, so we
850 * trade off slower shutdown for less distraction in the memory
851 * reports. -baw
852 */
853 _Py_ReleaseInternedUnicodeStrings();
854#endif /* __INSURE__ */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800855}
856
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100857
Eric Snow6b4be192017-05-22 21:36:03 -0700858static int
Victor Stinnerd3b19192018-07-25 10:21:03 +0200859pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
Guido van Rossum667d7041995-08-04 04:20:48 +0000860{
Victor Stinnerd3b19192018-07-25 10:21:03 +0200861 PyObject *sys_path;
862 PyObject *sysdict = interp->sysdict;
863 if (sysdict != NULL) {
864 sys_path = PyDict_GetItemString(sysdict, "path");
865 }
866 else {
867 sys_path = NULL;
868 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800869 if (sys_path == NULL) {
870 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
871 goto error;
872 }
873
Victor Stinnerd3b19192018-07-25 10:21:03 +0200874 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800875 goto error;
876 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200877 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800878
879error:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800880 PyErr_Print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200881 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800882}
883
884
Victor Stinnerb1147e42018-07-21 02:06:16 +0200885_PyInitError
886_Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800887{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200888 if (*len == INT_MAX) {
889 /* len+1 would overflow */
890 return _Py_INIT_NO_MEMORY();
891 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100892 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800893 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100894 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800895 }
896
Victor Stinnerca719ac2017-12-20 18:00:19 +0100897 size_t size = (*len + 1) * sizeof(list[0]);
898 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
899 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800900 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100901 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800902 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100903 list2[*len] = str2;
904 *list = list2;
905 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100906 return _Py_INIT_OK();
907}
908
909
910static int
911pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
912{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200913 _PyInitError err = _Py_wstrlist_append(len, list, str);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100914 if (_Py_INIT_FAILED(err)) {
915 pymain->err = err;
916 return -1;
917 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800918 return 0;
919}
920
921
922/* Parse the command line arguments
923 Return 0 on success.
924 Return 1 if parsing failed.
925 Set pymain->err and return -1 on other errors. */
926static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200927pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
928 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800929{
Antoine Pitrou86838b02012-02-21 19:03:47 +0100930 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800931 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800932 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100933 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800934 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800935 if (c == EOF) {
936 break;
937 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 /* -c is the last option; following arguments
941 that look like options are left for the
942 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800943 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
944 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
945 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100946 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800947 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800948 }
Victor Stinner58d16832018-05-31 15:09:28 +0200949 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 command[len - 2] = '\n';
951 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100952 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 break;
954 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (c == 'm') {
957 /* -m is the last option; following arguments
958 that look like options are left for the
959 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100960 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
961 if (pymain->module == NULL) {
962 return -1;
963 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 break;
965 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800968 case 0:
969 // Handle long option.
970 assert(longindex == 0); // Only one long option now.
971 if (!wcscmp(_PyOS_optarg, L"always")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200972 config->_check_hash_pycs_mode = "always";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800973 } else if (!wcscmp(_PyOS_optarg, L"never")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200974 config->_check_hash_pycs_mode = "never";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800975 } else if (!wcscmp(_PyOS_optarg, L"default")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200976 config->_check_hash_pycs_mode = "default";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800977 } else {
978 fprintf(stderr, "--check-hash-based-pycs must be one of "
979 "'default', 'always', or 'never'\n");
980 return 1;
981 }
982 break;
983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 case 'b':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200985 config->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 case 'd':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200989 config->debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 case 'i':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200993 config->inspect++;
994 config->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000996
Christian Heimesad73a9c2013-08-10 16:36:18 +0200997 case 'I':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200998 config->isolated++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200999 break;
1000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 case 'O':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001004 config->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 break;
Guido van Rossum7614da61997-03-03 19:14:45 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 case 'B':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001008 config->write_bytecode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 break;
Christian Heimes790c8232008-01-07 21:14:23 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 case 's':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001012 config->user_site_directory = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 case 'S':
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001016 config->site_import = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +00001018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 case 'E':
Victor Stinnerd1457752018-07-26 16:04:56 +02001020 config->use_environment = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 case 't':
1024 /* ignored for backwards compatibility */
1025 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 case 'u':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001028 config->unbuffered_stdio = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 break;
Guido van Rossum667d7041995-08-04 04:20:48 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 case 'v':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001032 config->verbose++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 break;
Guido van Rossum667d7041995-08-04 04:20:48 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +01001036 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 case 'h':
1040 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -07001041 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -07001045 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +01001049 if (pymain_wstrlist_append(pymain,
1050 &cmdline->nwarnoption,
1051 &cmdline->warnoptions,
1052 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001053 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001054 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +00001056
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001057 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +01001058 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +01001059 &config->nxoption,
1060 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +01001061 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001062 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001063 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001064 break;
1065
Georg Brandl9d871192010-12-04 10:47:18 +00001066 case 'q':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001067 config->quiet++;
Georg Brandl9d871192010-12-04 10:47:18 +00001068 break;
1069
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001070 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +01001071 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001072 break;
1073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +00001075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001077 /* unknown argument: parsing failed */
1078 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001080 } while (1);
1081
Victor Stinnerca719ac2017-12-20 18:00:19 +01001082 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001083 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +01001084 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001085 {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001086 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
1087 if (pymain->filename == NULL) {
1088 return -1;
1089 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001091
Victor Stinnerd5dda982017-12-13 17:31:16 +01001092 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +01001093 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +01001094
Eric Snow6b4be192017-05-22 21:36:03 -07001095 return 0;
1096}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +00001097
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001098
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001099static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01001100add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +01001101{
1102 PyObject *name, *value;
1103
1104 const wchar_t *name_end = wcschr(s, L'=');
1105 if (!name_end) {
1106 name = PyUnicode_FromWideChar(s, -1);
1107 value = Py_True;
1108 Py_INCREF(value);
1109 }
1110 else {
1111 name = PyUnicode_FromWideChar(s, name_end - s);
1112 value = PyUnicode_FromWideChar(name_end + 1, -1);
1113 }
1114 if (name == NULL || value == NULL) {
1115 goto error;
1116 }
1117 if (PyDict_SetItem(opts, name, value) < 0) {
1118 goto error;
1119 }
1120 Py_DECREF(name);
1121 Py_DECREF(value);
1122 return 0;
1123
1124error:
1125 Py_XDECREF(name);
1126 Py_XDECREF(value);
1127 return -1;
1128}
1129
Victor Stinner9cfc0022017-12-20 19:36:46 +01001130
1131static PyObject*
1132config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001133{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001134 int nxoption = config->nxoption;
1135 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +01001136 PyObject *dict = PyDict_New();
1137 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001138 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +01001139 }
1140
Victor Stinnerca719ac2017-12-20 18:00:19 +01001141 for (int i=0; i < nxoption; i++) {
1142 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +01001143 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +01001144 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001145 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001146 }
1147 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001148
Victor Stinner9cfc0022017-12-20 19:36:46 +01001149 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -07001150}
1151
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001152
Victor Stinner9cfc0022017-12-20 19:36:46 +01001153static _PyInitError
1154config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -07001155{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001156 for (int i = 0; i < len; i++) {
Victor Stinnerb1147e42018-07-21 02:06:16 +02001157 _PyInitError err = _Py_wstrlist_append(&config->nwarnoption,
1158 &config->warnoptions,
1159 options[i]);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001160 if (_Py_INIT_FAILED(err)) {
1161 return err;
Eric Snow6b4be192017-05-22 21:36:03 -07001162 }
Eric Snow6b4be192017-05-22 21:36:03 -07001163 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001164 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001165}
Eric Snow6b4be192017-05-22 21:36:03 -07001166
Victor Stinner747f48e2017-12-12 22:59:48 +01001167
Victor Stinner9cfc0022017-12-20 19:36:46 +01001168static _PyInitError
Victor Stinner1dc6e392018-07-25 02:49:17 +02001169config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +01001170{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001171 _PyInitError err;
1172
1173 assert(config->nwarnoption == 0);
1174
Victor Stinner747f48e2017-12-12 22:59:48 +01001175 /* The priority order for warnings configuration is (highest precedence
1176 * first):
1177 *
1178 * - the BytesWarning filter, if needed ('-b', '-bb')
1179 * - any '-W' command line options; then
1180 * - the 'PYTHONWARNINGS' environment variable; then
1181 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
1182 * - any implicit filters added by _warnings.c/warnings.py
1183 *
1184 * All settings except the last are passed to the warnings module via
1185 * the `sys.warnoptions` list. Since the warnings module works on the basis
1186 * of "the most recently added filter will be checked first", we add
1187 * the lowest precedence entries first so that later entries override them.
1188 */
1189
Victor Stinner9cfc0022017-12-20 19:36:46 +01001190 if (config->dev_mode) {
Victor Stinnerb1147e42018-07-21 02:06:16 +02001191 err = _Py_wstrlist_append(&config->nwarnoption,
1192 &config->warnoptions,
1193 L"default");
Victor Stinner9cfc0022017-12-20 19:36:46 +01001194 if (_Py_INIT_FAILED(err)) {
1195 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001196 }
Victor Stinner747f48e2017-12-12 22:59:48 +01001197 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001198
Victor Stinner9cfc0022017-12-20 19:36:46 +01001199 err = config_add_warnings_optlist(config,
1200 cmdline->nenv_warnoption,
1201 cmdline->env_warnoptions);
1202 if (_Py_INIT_FAILED(err)) {
1203 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001204 }
1205
Victor Stinner9cfc0022017-12-20 19:36:46 +01001206 err = config_add_warnings_optlist(config,
1207 cmdline->nwarnoption,
1208 cmdline->warnoptions);
1209 if (_Py_INIT_FAILED(err)) {
1210 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001211 }
1212
1213 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
1214 * don't even try to emit a warning, so we skip setting the filter in that
1215 * case.
1216 */
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001217 if (config->bytes_warning) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001218 wchar_t *filter;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001219 if (config->bytes_warning> 1) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001220 filter = L"error::BytesWarning";
1221 }
1222 else {
1223 filter = L"default::BytesWarning";
1224 }
Victor Stinnerb1147e42018-07-21 02:06:16 +02001225 err = _Py_wstrlist_append(&config->nwarnoption,
1226 &config->warnoptions,
1227 filter);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001228 if (_Py_INIT_FAILED(err)) {
1229 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001230 }
1231 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001232 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001233}
1234
1235
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001236/* Get warning options from PYTHONWARNINGS environment variable.
1237 Return 0 on success.
1238 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001239static _PyInitError
Victor Stinnerd1457752018-07-26 16:04:56 +02001240cmdline_init_env_warnoptions(_PyMain *pymain, const _PyCoreConfig *config,
1241 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001242{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001243 wchar_t *env;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001244 int res = config_get_env_var_dup(config, &env,
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001245 L"PYTHONWARNINGS", "PYTHONWARNINGS");
Victor Stinnerca719ac2017-12-20 18:00:19 +01001246 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001247 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001248 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001249
Victor Stinnerca719ac2017-12-20 18:00:19 +01001250 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001251 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001252 }
Philip Jenvey0805ca32010-04-07 04:04:10 +00001253
Victor Stinnerca719ac2017-12-20 18:00:19 +01001254
1255 wchar_t *warning, *context = NULL;
1256 for (warning = WCSTOK(env, L",", &context);
1257 warning != NULL;
1258 warning = WCSTOK(NULL, L",", &context))
1259 {
Victor Stinnerb1147e42018-07-21 02:06:16 +02001260 _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption,
1261 &cmdline->env_warnoptions,
1262 warning);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001263 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001264 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001265 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001266 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001268 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001269 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001270}
1271
1272
1273static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001274pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001275{
1276 pymain->stdin_is_interactive = (isatty(fileno(stdin))
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001277 || config->interactive);
Guido van Rossum775af911997-02-14 19:50:32 +00001278
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +00001279#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +00001280 /* don't translate newlines (\r\n <=> \n) */
1281 _setmode(fileno(stdin), O_BINARY);
1282 _setmode(fileno(stdout), O_BINARY);
1283 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +00001284#endif
Victor Stinner89e34362011-01-07 18:47:22 +00001285
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001286 if (config->unbuffered_stdio) {
Guido van Rossum22ffac11998-03-06 15:30:39 +00001287#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
1289 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
1290 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001291#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 setbuf(stdin, (char *)NULL);
1293 setbuf(stdout, (char *)NULL);
1294 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001295#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001297 else if (config->interactive) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +00001298#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 /* Doesn't have to have line-buffered -- use unbuffered */
1300 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
1301 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001302#else /* !MS_WINDOWS */
1303#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
1305 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001306#endif /* HAVE_SETVBUF */
1307#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* Leave stderr alone - it should be unbuffered anyway. */
1309 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001310}
Guido van Rossum667d7041995-08-04 04:20:48 +00001311
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001312
1313/* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
Victor Stinner31a83932017-12-04 13:39:15 +01001314 environment variables on macOS if available. */
1315static _PyInitError
Victor Stinnerca719ac2017-12-20 18:00:19 +01001316config_init_program_name(_PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001317{
Victor Stinner31a83932017-12-04 13:39:15 +01001318 /* If Py_SetProgramName() was called, use its value */
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001319 const wchar_t *program_name = _Py_path_config.program_name;
Victor Stinner31a83932017-12-04 13:39:15 +01001320 if (program_name != NULL) {
1321 config->program_name = _PyMem_RawWcsdup(program_name);
1322 if (config->program_name == NULL) {
1323 return _Py_INIT_NO_MEMORY();
1324 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001325 return _Py_INIT_OK();
Victor Stinner31a83932017-12-04 13:39:15 +01001326 }
1327
Just van Rossum2ac79ef2003-03-05 15:46:54 +00001328#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 /* On MacOS X, when the Python interpreter is embedded in an
1330 application bundle, it gets executed by a bootstrapping script
1331 that does os.execve() with an argv[0] that's different from the
1332 actual Python executable. This is needed to keep the Finder happy,
1333 or rather, to work around Apple's overly strict requirements of
1334 the process name. However, we still need a usable sys.executable,
1335 so the actual executable path is passed in an environment variable.
1336 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
1337 script. */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001338 const char *p = config_get_env_var(config, "PYTHONEXECUTABLE");
Victor Stinner6bf992a2017-12-06 17:26:10 +01001339 if (p != NULL) {
Victor Stinner31a83932017-12-04 13:39:15 +01001340 size_t len;
1341 wchar_t* program_name = Py_DecodeLocale(p, &len);
1342 if (program_name == NULL) {
1343 return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment "
1344 "variable", (Py_ssize_t)len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 }
Victor Stinner31a83932017-12-04 13:39:15 +01001346 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001347 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001348 }
Vinay Sajip90db6612012-07-17 17:33:46 +01001349#ifdef WITH_NEXT_FRAMEWORK
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001350 else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001351 const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
Vinay Sajip90db6612012-07-17 17:33:46 +01001352 if (pyvenv_launcher && *pyvenv_launcher) {
1353 /* Used by Mac/Tools/pythonw.c to forward
1354 * the argv0 of the stub executable
1355 */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001356 size_t len;
Victor Stinner31a83932017-12-04 13:39:15 +01001357 wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len);
1358 if (program_name == NULL) {
1359 return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment "
1360 "variable", (Py_ssize_t)len);
Vinay Sajip90db6612012-07-17 17:33:46 +01001361 }
Victor Stinner31a83932017-12-04 13:39:15 +01001362 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001363 return _Py_INIT_OK();
Vinay Sajip90db6612012-07-17 17:33:46 +01001364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001366#endif /* WITH_NEXT_FRAMEWORK */
1367#endif /* __APPLE__ */
Victor Stinneraf5a8952017-12-02 10:11:32 +01001368
Victor Stinnerca719ac2017-12-20 18:00:19 +01001369 /* Use argv[0] by default, if available */
1370 if (config->program != NULL) {
1371 config->program_name = _PyMem_RawWcsdup(config->program);
1372 if (config->program_name == NULL) {
1373 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001374 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001375 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001376 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001377
1378 /* Last fall back: hardcoded string */
1379#ifdef MS_WINDOWS
1380 const wchar_t *default_program_name = L"python";
1381#else
1382 const wchar_t *default_program_name = L"python3";
1383#endif
1384 config->program_name = _PyMem_RawWcsdup(default_program_name);
1385 if (config->program_name == NULL) {
1386 return _Py_INIT_NO_MEMORY();
1387 }
1388 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001389}
1390
1391
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001392static void
1393pymain_header(_PyMain *pymain)
1394{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001395 if (Py_QuietFlag) {
1396 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 }
Guido van Rossum393661d2001-08-31 17:40:15 +00001398
Victor Stinner19760862017-12-20 01:41:59 +01001399 if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001400 return;
1401 }
1402
1403 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
1404 if (!Py_NoSiteFlag) {
1405 fprintf(stderr, "%s\n", COPYRIGHT);
1406 }
1407}
1408
1409
Victor Stinnerc4bca952017-12-19 23:48:17 +01001410static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001411pymain_init_core_argv(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001412{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001413 /* Copy argv to be able to modify it (to force -c/-m) */
1414 int argc = pymain->argc - _PyOS_optind;
1415 wchar_t **argv;
1416
1417 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +01001418 /* Ensure at least one (empty) argument is seen */
1419 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +01001420 argc = 1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001421 argv = copy_wstrlist(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +01001422 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001423 else {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001424 argv = copy_wstrlist(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001425 }
1426
1427 if (argv == NULL) {
1428 pymain->err = _Py_INIT_NO_MEMORY();
1429 return -1;
1430 }
1431
1432 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001433 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001434 /* Force sys.argv[0] = '-c' */
1435 arg0 = L"-c";
1436 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001437 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001438 /* Force sys.argv[0] = '-m'*/
1439 arg0 = L"-m";
1440 }
1441 if (arg0 != NULL) {
1442 arg0 = _PyMem_RawWcsdup(arg0);
1443 if (arg0 == NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001444 clear_wstrlist(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001445 pymain->err = _Py_INIT_NO_MEMORY();
1446 return -1;
1447 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001448
1449 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001450 PyMem_RawFree(argv[0]);
1451 argv[0] = arg0;
1452 }
1453
Victor Stinner1dc6e392018-07-25 02:49:17 +02001454 config->argc = argc;
1455 config->argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001456 return 0;
1457}
1458
1459
Victor Stinner8ded5b82018-01-24 17:03:28 +01001460static PyObject*
1461wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001462{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001463 assert(list != NULL || len < 1);
1464
1465 PyObject *pylist = PyList_New(len);
1466 if (pylist == NULL) {
1467 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001468 }
1469
Victor Stinner8ded5b82018-01-24 17:03:28 +01001470 for (int i = 0; i < len; i++) {
1471 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001472 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001473 Py_DECREF(pylist);
1474 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001475 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001476 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001477 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001478 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001479}
1480
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001481
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001482static void
1483pymain_import_readline(_PyMain *pymain)
1484{
1485 if (Py_IsolatedFlag) {
1486 return;
1487 }
Victor Stinner19760862017-12-20 01:41:59 +01001488 if (!Py_InspectFlag && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001489 return;
1490 }
1491 if (!isatty(fileno(stdin))) {
1492 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001493 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001494
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001495 PyObject *mod = PyImport_ImportModule("readline");
1496 if (mod == NULL) {
1497 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 }
1499 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001500 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001502}
1503
1504
1505static FILE*
Victor Stinner1dc6e392018-07-25 02:49:17 +02001506pymain_open_filename(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001507{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001508 FILE* fp;
1509
Victor Stinnerca719ac2017-12-20 18:00:19 +01001510 fp = _Py_wfopen(pymain->filename, L"r");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001511 if (fp == NULL) {
1512 char *cfilename_buffer;
1513 const char *cfilename;
1514 int err = errno;
Victor Stinner9dd76202017-12-21 16:20:32 +01001515 cfilename_buffer = _Py_EncodeLocaleRaw(pymain->filename, NULL);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001516 if (cfilename_buffer != NULL)
1517 cfilename = cfilename_buffer;
1518 else
1519 cfilename = "<unprintable file name>";
1520 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinner1dc6e392018-07-25 02:49:17 +02001521 config->program, cfilename, err, strerror(err));
Victor Stinner9dd76202017-12-21 16:20:32 +01001522 PyMem_RawFree(cfilename_buffer);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001523 pymain->status = 2;
1524 return NULL;
1525 }
1526
Victor Stinnerca719ac2017-12-20 18:00:19 +01001527 if (pymain->skip_first_line) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001528 int ch;
1529 /* Push back first newline so line numbers
1530 remain the same */
1531 while ((ch = getc(fp)) != EOF) {
1532 if (ch == '\n') {
1533 (void)ungetc(ch, fp);
1534 break;
1535 }
1536 }
1537 }
1538
1539 struct _Py_stat_struct sb;
1540 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 &&
1541 S_ISDIR(sb.st_mode)) {
1542 fprintf(stderr,
1543 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinner1dc6e392018-07-25 02:49:17 +02001544 config->program, pymain->filename);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001545 fclose(fp);
1546 pymain->status = 1;
1547 return NULL;
1548 }
1549
1550 return fp;
1551}
1552
1553
1554static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001555pymain_run_startup(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001556{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001557 const char *startup = config_get_env_var(config, "PYTHONSTARTUP");
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001558 if (startup == NULL) {
1559 return;
1560 }
1561
1562 FILE *fp = _Py_fopen(startup, "r");
1563 if (fp == NULL) {
1564 int save_errno = errno;
1565 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
1566 errno = save_errno;
1567
1568 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
1569 startup);
1570 PyErr_Print();
1571 PyErr_Clear();
1572 return;
1573 }
1574
1575 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
1576 PyErr_Clear();
1577 fclose(fp);
1578}
1579
1580
1581static void
Victor Stinnerd3b19192018-07-25 10:21:03 +02001582pymain_run_filename(_PyMain *pymain, _PyCoreConfig *config,
1583 PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001584{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001585 if (pymain->filename == NULL && pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001586 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner1dc6e392018-07-25 02:49:17 +02001587 pymain_run_startup(pymain, config, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001588 pymain_run_interactive_hook();
1589 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001590
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001591 FILE *fp;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001592 if (pymain->filename != NULL) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001593 fp = pymain_open_filename(pymain, config);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001594 if (fp == NULL) {
1595 return;
1596 }
1597 }
1598 else {
1599 fp = stdin;
1600 }
1601
Victor Stinnerca719ac2017-12-20 18:00:19 +01001602 pymain->status = pymain_run_file(fp, pymain->filename, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001603}
1604
1605
1606static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001607pymain_repl(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001610 opportunity to set it from Python. */
Victor Stinner1dc6e392018-07-25 02:49:17 +02001611 if (!Py_InspectFlag && config_get_env_var(config, "PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 Py_InspectFlag = 1;
1613 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001614
Victor Stinner19760862017-12-20 01:41:59 +01001615 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001616 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001618
1619 Py_InspectFlag = 0;
1620 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001621
Victor Stinner19760862017-12-20 01:41:59 +01001622 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001623 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001624}
1625
1626
1627/* Parse the command line.
1628 Handle --version and --help options directly.
1629
1630 Return 1 if Python must exit.
1631 Return 0 on success.
1632 Set pymain->err and return -1 on failure. */
1633static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001634pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config,
1635 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001636{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001637 int res = pymain_parse_cmdline_impl(pymain, config, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001638 if (res < 0) {
1639 return -1;
1640 }
1641 if (res) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001642 pymain_usage(1, config->program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001643 pymain->status = 2;
1644 return 1;
1645 }
1646
Victor Stinnerca719ac2017-12-20 18:00:19 +01001647 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001648 /* Backup _PyOS_optind */
1649 _PyOS_optind--;
1650 }
1651
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001652 return 0;
1653}
1654
1655
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001656static const wchar_t*
Victor Stinner9cfc0022017-12-20 19:36:46 +01001657config_get_xoption(_PyCoreConfig *config, wchar_t *name)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001658{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001659 int nxoption = config->nxoption;
1660 wchar_t **xoptions = config->xoptions;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001661 for (int i=0; i < nxoption; i++) {
1662 wchar_t *option = xoptions[i];
Victor Stinnera7368ac2017-11-15 18:11:45 -08001663 size_t len;
1664 wchar_t *sep = wcschr(option, L'=');
1665 if (sep != NULL) {
1666 len = (sep - option);
1667 }
1668 else {
1669 len = wcslen(option);
1670 }
1671 if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
1672 return option;
1673 }
1674 }
1675 return NULL;
1676}
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001677
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001678
Victor Stinnera7368ac2017-11-15 18:11:45 -08001679static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001680pymain_str_to_int(const char *str, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001681{
1682 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001683 const char *endptr = str;
1684 long value = strtol(str, (char **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001685 if (*endptr != '\0' || errno == ERANGE) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001686 return -1;
1687 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001688 if (value < INT_MIN || value > INT_MAX) {
1689 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001690 }
1691
Victor Stinnera7368ac2017-11-15 18:11:45 -08001692 *result = (int)value;
1693 return 0;
1694}
1695
1696
1697static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001698pymain_wstr_to_int(const wchar_t *wstr, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001699{
1700 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001701 const wchar_t *endptr = wstr;
1702 long value = wcstol(wstr, (wchar_t **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001703 if (*endptr != '\0' || errno == ERANGE) {
1704 return -1;
1705 }
1706 if (value < INT_MIN || value > INT_MAX) {
1707 return -1;
1708 }
1709
1710 *result = (int)value;
1711 return 0;
1712}
1713
1714
Victor Stinner9cfc0022017-12-20 19:36:46 +01001715static _PyInitError
1716pymain_init_tracemalloc(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001717{
1718 int nframe;
1719 int valid;
1720
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001721 const char *env = config_get_env_var(config, "PYTHONTRACEMALLOC");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001722 if (env) {
1723 if (!pymain_str_to_int(env, &nframe)) {
Victor Stinner60b04c92018-07-25 19:23:53 +02001724 valid = (nframe >= 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001725 }
1726 else {
1727 valid = 0;
1728 }
1729 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001730 return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number "
1731 "of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001732 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001733 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001734 }
1735
Victor Stinner9cfc0022017-12-20 19:36:46 +01001736 const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001737 if (xoption) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001738 const wchar_t *sep = wcschr(xoption, L'=');
Victor Stinnera7368ac2017-11-15 18:11:45 -08001739 if (sep) {
1740 if (!pymain_wstr_to_int(sep + 1, &nframe)) {
Victor Stinner60b04c92018-07-25 19:23:53 +02001741 valid = (nframe >= 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001742 }
1743 else {
1744 valid = 0;
1745 }
1746 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001747 return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: "
1748 "invalid number of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001749 }
1750 }
1751 else {
1752 /* -X tracemalloc behaves as -X tracemalloc=1 */
1753 nframe = 1;
1754 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001755 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001756 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001757 return _Py_INIT_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001758}
1759
1760
Carl Meyerb193fa92018-06-15 22:40:56 -06001761static _PyInitError
1762pymain_init_pycache_prefix(_PyCoreConfig *config)
1763{
1764 wchar_t *env;
1765
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001766 int res = config_get_env_var_dup(config, &env,
1767 L"PYTHONPYCACHEPREFIX", "PYTHONPYCACHEPREFIX");
Carl Meyerb193fa92018-06-15 22:40:56 -06001768 if (res < 0) {
1769 return DECODE_LOCALE_ERR("PYTHONPYCACHEPREFIX", res);
1770 } else if (env) {
1771 config->pycache_prefix = env;
1772 }
1773
1774 const wchar_t *xoption = config_get_xoption(config, L"pycache_prefix");
1775 if (xoption) {
1776 const wchar_t *sep = wcschr(xoption, L'=');
1777 if (sep && wcslen(sep) > 1) {
1778 config->pycache_prefix = _PyMem_RawWcsdup(sep + 1);
1779 if (config->pycache_prefix == NULL) {
1780 return _Py_INIT_NO_MEMORY();
1781 }
1782 } else {
1783 // -X pycache_prefix= can cancel the env var
1784 config->pycache_prefix = NULL;
1785 }
1786 }
1787
1788 return _Py_INIT_OK();
1789}
1790
1791
Victor Stinnera7368ac2017-11-15 18:11:45 -08001792static void
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001793get_env_flag(_PyCoreConfig *config, int *flag, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001794{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001795 const char *var = config_get_env_var(config, name);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001796 if (!var) {
1797 return;
1798 }
1799 int value;
1800 if (pymain_str_to_int(var, &value) < 0 || value < 0) {
1801 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
1802 value = 1;
1803 }
1804 if (*flag < value) {
1805 *flag = value;
1806 }
1807}
1808
1809
Victor Stinner46972b72017-11-24 22:55:40 +01001810static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001811config_init_home(_PyCoreConfig *config)
Victor Stinner1f151112017-11-23 10:43:14 +01001812{
1813 wchar_t *home;
1814
Victor Stinner31a83932017-12-04 13:39:15 +01001815 /* If Py_SetPythonHome() was called, use its value */
1816 home = _Py_path_config.home;
Victor Stinner1f151112017-11-23 10:43:14 +01001817 if (home) {
Victor Stinner46972b72017-11-24 22:55:40 +01001818 config->home = _PyMem_RawWcsdup(home);
1819 if (config->home == NULL) {
1820 return _Py_INIT_NO_MEMORY();
Victor Stinner1f151112017-11-23 10:43:14 +01001821 }
Victor Stinner46972b72017-11-24 22:55:40 +01001822 return _Py_INIT_OK();
Victor Stinner1f151112017-11-23 10:43:14 +01001823 }
1824
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001825 int res = config_get_env_var_dup(config, &home, L"PYTHONHOME", "PYTHONHOME");
Victor Stinner1f151112017-11-23 10:43:14 +01001826 if (res < 0) {
Victor Stinner46972b72017-11-24 22:55:40 +01001827 return DECODE_LOCALE_ERR("PYTHONHOME", res);
Victor Stinner1f151112017-11-23 10:43:14 +01001828 }
Victor Stinner46972b72017-11-24 22:55:40 +01001829 config->home = home;
1830 return _Py_INIT_OK();
Victor Stinnerd4341102017-11-23 00:12:09 +01001831}
1832
1833
Victor Stinner358e5e12017-12-15 00:51:22 +01001834static _PyInitError
1835config_init_hash_seed(_PyCoreConfig *config)
1836{
1837 if (config->use_hash_seed < 0) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001838 const char *seed_text = config_get_env_var(config, "PYTHONHASHSEED");
Victor Stinner358e5e12017-12-15 00:51:22 +01001839 int use_hash_seed;
1840 unsigned long hash_seed;
1841 if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
1842 return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
1843 "or an integer in range [0; 4294967295]");
1844 }
1845 config->use_hash_seed = use_hash_seed;
1846 config->hash_seed = hash_seed;
1847 }
1848 return _Py_INIT_OK();
1849}
1850
1851
Victor Stinner9cfc0022017-12-20 19:36:46 +01001852static _PyInitError
1853config_init_utf8_mode(_PyCoreConfig *config)
Victor Stinner91106cd2017-12-13 12:29:09 +01001854{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001855 const wchar_t *xopt = config_get_xoption(config, L"utf8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001856 if (xopt) {
1857 wchar_t *sep = wcschr(xopt, L'=');
1858 if (sep) {
1859 xopt = sep + 1;
1860 if (wcscmp(xopt, L"1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001861 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001862 }
1863 else if (wcscmp(xopt, L"0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001864 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001865 }
1866 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001867 return _Py_INIT_USER_ERR("invalid -X utf8 option value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001868 }
1869 }
1870 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001871 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001872 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001873 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001874 }
1875
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001876 const char *opt = config_get_env_var(config, "PYTHONUTF8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001877 if (opt) {
1878 if (strcmp(opt, "1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001879 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001880 }
1881 else if (strcmp(opt, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001882 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001883 }
1884 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001885 return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
1886 "variable value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001887 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001888 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001889 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001890
1891 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001892}
Victor Stinner46972b72017-11-24 22:55:40 +01001893
1894
Victor Stinner9cfc0022017-12-20 19:36:46 +01001895static _PyInitError
1896config_read_env_vars(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001897{
Victor Stinnerd1457752018-07-26 16:04:56 +02001898 assert(config->use_environment > 0);
Victor Stinnerecf411c2018-07-26 02:37:22 +02001899
1900 /* Get environment variables */
1901 get_env_flag(config, &config->debug, "PYTHONDEBUG");
1902 get_env_flag(config, &config->verbose, "PYTHONVERBOSE");
1903 get_env_flag(config, &config->optimization_level, "PYTHONOPTIMIZE");
1904 get_env_flag(config, &config->inspect, "PYTHONINSPECT");
1905
1906 int dont_write_bytecode = 0;
1907 get_env_flag(config, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
1908 if (dont_write_bytecode) {
1909 config->write_bytecode = 0;
1910 }
1911
1912 int no_user_site_directory = 0;
1913 get_env_flag(config, &no_user_site_directory, "PYTHONNOUSERSITE");
1914 if (no_user_site_directory) {
1915 config->user_site_directory = 0;
1916 }
1917
1918 get_env_flag(config, &config->unbuffered_stdio, "PYTHONUNBUFFERED");
1919#ifdef MS_WINDOWS
1920 get_env_flag(config, &config->legacy_windows_fs_encoding,
1921 "PYTHONLEGACYWINDOWSFSENCODING");
1922 get_env_flag(config, &config->legacy_windows_stdio,
1923 "PYTHONLEGACYWINDOWSSTDIO");
1924#endif
1925
1926 if (config->allocator == NULL) {
1927 config->allocator = config_get_env_var(config, "PYTHONMALLOC");
1928 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001929
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001930 if (config_get_env_var(config, "PYTHONDUMPREFS")) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001931 config->dump_refs = 1;
1932 }
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001933 if (config_get_env_var(config, "PYTHONMALLOCSTATS")) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001934 config->malloc_stats = 1;
Victor Stinner31a83932017-12-04 13:39:15 +01001935 }
1936
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001937 const char *env = config_get_env_var(config, "PYTHONCOERCECLOCALE");
Victor Stinner94540602017-12-16 04:54:22 +01001938 if (env) {
1939 if (strcmp(env, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001940 config->coerce_c_locale = 0;
Victor Stinner94540602017-12-16 04:54:22 +01001941 }
1942 else if (strcmp(env, "warn") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001943 config->coerce_c_locale_warn = 1;
Victor Stinner94540602017-12-16 04:54:22 +01001944 }
1945 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001946 config->coerce_c_locale = 1;
Victor Stinner94540602017-12-16 04:54:22 +01001947 }
1948 }
1949
Victor Stinner9cfc0022017-12-20 19:36:46 +01001950 wchar_t *path;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001951 int res = config_get_env_var_dup(config, &path, L"PYTHONPATH", "PYTHONPATH");
Victor Stinner9cfc0022017-12-20 19:36:46 +01001952 if (res < 0) {
Carl Meyer48575432018-05-19 16:48:22 -06001953 return DECODE_LOCALE_ERR("PYTHONPATH", res);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001954 }
1955 config->module_search_path_env = path;
1956
1957 _PyInitError err = config_init_hash_seed(config);
1958 if (_Py_INIT_FAILED(err)) {
1959 return err;
1960 }
1961
1962 return _Py_INIT_OK();
1963}
1964
1965
1966static _PyInitError
1967config_read_complex_options(_PyCoreConfig *config)
1968{
1969 /* More complex options configured by env var and -X option */
Victor Stinnerd1457752018-07-26 16:04:56 +02001970 if (config->faulthandler < 0) {
1971 if (config_get_env_var(config, "PYTHONFAULTHANDLER")
1972 || config_get_xoption(config, L"faulthandler")) {
1973 config->faulthandler = 1;
1974 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001975 }
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001976 if (config_get_env_var(config, "PYTHONPROFILEIMPORTTIME")
Victor Stinner9cfc0022017-12-20 19:36:46 +01001977 || config_get_xoption(config, L"importtime")) {
1978 config->import_time = 1;
1979 }
1980 if (config_get_xoption(config, L"dev" ) ||
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001981 config_get_env_var(config, "PYTHONDEVMODE"))
Victor Stinner9cfc0022017-12-20 19:36:46 +01001982 {
1983 config->dev_mode = 1;
Victor Stinner9cfc0022017-12-20 19:36:46 +01001984 }
1985
Victor Stinnerd1457752018-07-26 16:04:56 +02001986 _PyInitError err;
1987 if (config->tracemalloc < 0) {
1988 err = pymain_init_tracemalloc(config);
1989 if (_Py_INIT_FAILED(err)) {
1990 return err;
1991 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001992 }
Carl Meyerb193fa92018-06-15 22:40:56 -06001993
Victor Stinnerd1457752018-07-26 16:04:56 +02001994 if (config->pycache_prefix == NULL) {
1995 err = pymain_init_pycache_prefix(config);
1996 if (_Py_INIT_FAILED(err)) {
1997 return err;
1998 }
Carl Meyerb193fa92018-06-15 22:40:56 -06001999 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002000 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002001}
2002
2003
Victor Stinnera7368ac2017-11-15 18:11:45 -08002004/* Parse command line options and environment variables.
2005 This code must not use Python runtime apart PyMem_Raw memory allocator.
2006
2007 Return 0 on success.
2008 Return 1 if Python is done and must exit.
2009 Set pymain->err and return -1 on error. */
2010static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002011pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
2012 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002013{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002014 _PyInitError err;
2015
Victor Stinner1dc6e392018-07-25 02:49:17 +02002016 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01002017 if (res != 0) {
2018 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002019 }
2020
Victor Stinner1dc6e392018-07-25 02:49:17 +02002021 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01002022 return -1;
2023 }
2024
Victor Stinnerd1457752018-07-26 16:04:56 +02002025 err = _PyCoreConfig_Read(config);
2026 if (_Py_INIT_FAILED(err)) {
2027 pymain->err = err;
2028 return -1;
2029 }
2030
2031 assert(config->use_environment >= 0);
2032 if (config->use_environment) {
Victor Stinnerecf411c2018-07-26 02:37:22 +02002033 err = cmdline_init_env_warnoptions(pymain, config, cmdline);
2034 if (_Py_INIT_FAILED(err)) {
2035 pymain->err = err;
2036 return -1;
2037 }
2038 }
2039
Victor Stinnerd1457752018-07-26 16:04:56 +02002040 err = config_init_warnoptions(config, cmdline);
Victor Stinner31a83932017-12-04 13:39:15 +01002041 if (_Py_INIT_FAILED(err)) {
2042 pymain->err = err;
2043 return -1;
2044 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002045 return 0;
2046}
2047
2048
Victor Stinner19760862017-12-20 01:41:59 +01002049/* Read the configuration, but initialize also the LC_CTYPE locale:
2050 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
Victor Stinnera7368ac2017-11-15 18:11:45 -08002051static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002052pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
2053 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002054{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002055 _PyCoreConfig save_config = _PyCoreConfig_INIT;
2056 char *oldloc = NULL;
Victor Stinner94540602017-12-16 04:54:22 +01002057 int res = -1;
2058
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002059 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
Victor Stinner94540602017-12-16 04:54:22 +01002060 if (oldloc == NULL) {
2061 pymain->err = _Py_INIT_NO_MEMORY();
2062 goto done;
2063 }
2064
2065 /* Reconfigure the locale to the default for this process */
2066 _Py_SetLocaleFromEnv(LC_ALL);
2067
2068 int locale_coerced = 0;
2069 int loops = 0;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002070
2071 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
2072 pymain->err = _Py_INIT_NO_MEMORY();
2073 goto done;
2074 }
Victor Stinner94540602017-12-16 04:54:22 +01002075
2076 while (1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002077 int utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002078 int encoding_changed = 0;
2079
2080 /* Watchdog to prevent an infinite loop */
2081 loops++;
2082 if (loops == 3) {
2083 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
2084 "reading the configuration");
2085 goto done;
2086 }
2087
Victor Stinner1dc6e392018-07-25 02:49:17 +02002088 if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01002089 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01002090 }
2091
Victor Stinner1dc6e392018-07-25 02:49:17 +02002092 int conf_res = pymain_read_conf_impl(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002093 if (conf_res != 0) {
2094 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01002095 goto done;
2096 }
2097
2098 /* The legacy C locale assumes ASCII as the default text encoding, which
2099 * causes problems not only for the CPython runtime, but also other
2100 * components like GNU readline.
2101 *
2102 * Accordingly, when the CLI detects it, it attempts to coerce it to a
2103 * more capable UTF-8 based alternative.
2104 *
2105 * See the documentation of the PYTHONCOERCECLOCALE setting for more
2106 * details.
2107 */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002108 if (config->coerce_c_locale == 1 && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01002109 locale_coerced = 1;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002110 _Py_CoerceLegacyLocale(config);
Victor Stinner94540602017-12-16 04:54:22 +01002111 encoding_changed = 1;
2112 }
2113
2114 if (utf8_mode == -1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002115 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01002116 /* UTF-8 Mode enabled */
2117 encoding_changed = 1;
2118 }
2119 }
2120 else {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002121 if (config->utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01002122 encoding_changed = 1;
2123 }
2124 }
2125
2126 if (!encoding_changed) {
2127 break;
2128 }
2129
Victor Stinnerd1457752018-07-26 16:04:56 +02002130 /* Reset the configuration before reading again the configuration,
2131 just keep UTF-8 Mode value. */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002132 int new_utf8_mode = config->utf8_mode;
2133 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
2134 pymain->err = _Py_INIT_NO_MEMORY();
2135 goto done;
2136 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002137 pymain_clear_cmdline(pymain, cmdline);
Victor Stinner6c5a4b32018-06-16 00:06:28 +02002138 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner1dc6e392018-07-25 02:49:17 +02002139 config->utf8_mode = new_utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002140
2141 /* The encoding changed: read again the configuration
2142 with the new encoding */
2143 }
2144 res = 0;
2145
2146done:
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002147 _PyCoreConfig_Clear(&save_config);
Victor Stinner94540602017-12-16 04:54:22 +01002148 if (oldloc != NULL) {
2149 setlocale(LC_ALL, oldloc);
2150 PyMem_RawFree(oldloc);
2151 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002152 return res;
2153}
2154
Victor Stinner91106cd2017-12-13 12:29:09 +01002155
Victor Stinner9cfc0022017-12-20 19:36:46 +01002156static void
2157config_init_locale(_PyCoreConfig *config)
2158{
2159 if (config->utf8_mode >= 0 && config->coerce_c_locale >= 0) {
2160 return;
2161 }
2162
2163 if (_Py_LegacyLocaleDetected()) {
2164 /* POSIX locale: enable C locale coercion and UTF-8 Mode */
2165 if (config->utf8_mode < 0) {
2166 config->utf8_mode = 1;
2167 }
2168 if (config->coerce_c_locale < 0) {
2169 config->coerce_c_locale = 1;
2170 }
2171 return;
2172 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002173}
2174
2175
Victor Stinnerda273412017-12-15 01:46:02 +01002176/* Read configuration settings from standard locations
2177 *
2178 * This function doesn't make any changes to the interpreter state - it
2179 * merely populates any missing configuration settings. This allows an
2180 * embedding application to completely override a config option by
2181 * setting it before calling this function, or else modify the default
2182 * setting before passing the fully populated config to Py_EndInitialization.
2183 *
2184 * More advanced selective initialization tricks are possible by calling
2185 * this function multiple times with various preconfigured settings.
2186 */
2187
2188_PyInitError
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002189_PyCoreConfig_Read(_PyCoreConfig *config)
Victor Stinnerda273412017-12-15 01:46:02 +01002190{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002191 _PyInitError err;
2192
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002193 _PyCoreConfig_GetGlobalConfig(config);
2194
Victor Stinnerd1457752018-07-26 16:04:56 +02002195 if (config->isolated > 0) {
2196 config->use_environment = 0;
2197 config->user_site_directory = 0;
2198 }
2199
2200#ifdef MS_WINDOWS
2201 if (config->legacy_windows_fs_encoding) {
2202 config->utf8_mode = 0;
2203 }
2204#endif
2205
2206 assert(config->use_environment >= 0);
2207 if (config->use_environment) {
Victor Stinnerecf411c2018-07-26 02:37:22 +02002208 err = config_read_env_vars(config);
2209 if (_Py_INIT_FAILED(err)) {
2210 return err;
2211 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002212 }
2213
Victor Stinner9cfc0022017-12-20 19:36:46 +01002214 /* -X options */
2215 if (config_get_xoption(config, L"showrefcount")) {
2216 config->show_ref_count = 1;
2217 }
2218 if (config_get_xoption(config, L"showalloccount")) {
2219 config->show_alloc_count = 1;
2220 }
2221
2222 err = config_read_complex_options(config);
2223 if (_Py_INIT_FAILED(err)) {
2224 return err;
2225 }
2226
Victor Stinnerd1457752018-07-26 16:04:56 +02002227 if (config->utf8_mode < 0) {
2228 err = config_init_utf8_mode(config);
2229 if (_Py_INIT_FAILED(err)) {
2230 return err;
2231 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002232 }
2233
Victor Stinnerd1457752018-07-26 16:04:56 +02002234 if (config->home == NULL) {
2235 err = config_init_home(config);
2236 if (_Py_INIT_FAILED(err)) {
2237 return err;
2238 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002239 }
2240
Victor Stinner1dc6e392018-07-25 02:49:17 +02002241 if (config->program_name == NULL) {
2242 err = config_init_program_name(config);
2243 if (_Py_INIT_FAILED(err)) {
2244 return err;
2245 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002246 }
2247
Victor Stinner9cfc0022017-12-20 19:36:46 +01002248 config_init_locale(config);
Victor Stinnerda273412017-12-15 01:46:02 +01002249
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002250 if (config->_install_importlib) {
2251 err = _PyCoreConfig_InitPathConfig(config);
Victor Stinner8ded5b82018-01-24 17:03:28 +01002252 if (_Py_INIT_FAILED(err)) {
2253 return err;
2254 }
2255 }
Victor Stinner60b04c92018-07-25 19:23:53 +02002256
Victor Stinnerd1457752018-07-26 16:04:56 +02002257 /* default values */
Victor Stinnerecf411c2018-07-26 02:37:22 +02002258 if (config->dev_mode) {
2259 if (config->faulthandler < 0) {
2260 config->faulthandler = 1;
2261 }
2262 if (config->allocator == NULL) {
2263 config->allocator = "debug";
2264 }
2265 }
Victor Stinnerecf411c2018-07-26 02:37:22 +02002266 if (config->use_hash_seed < 0) {
2267 config->use_hash_seed = 0;
2268 config->hash_seed = 0;
2269 }
2270 if (config->faulthandler < 0) {
2271 config->faulthandler = 0;
2272 }
Victor Stinner60b04c92018-07-25 19:23:53 +02002273 if (config->tracemalloc < 0) {
2274 config->tracemalloc = 0;
2275 }
Victor Stinnerecf411c2018-07-26 02:37:22 +02002276 if (config->coerce_c_locale < 0) {
2277 config->coerce_c_locale = 0;
2278 }
2279 if (config->utf8_mode < 0) {
2280 config->utf8_mode = 0;
Victor Stinner60b04c92018-07-25 19:23:53 +02002281 }
2282
Victor Stinnerda273412017-12-15 01:46:02 +01002283 return _Py_INIT_OK();
2284}
2285
2286
2287void
Victor Stinnerda273412017-12-15 01:46:02 +01002288_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
2289{
2290 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002291 Py_CLEAR(config->executable);
2292 Py_CLEAR(config->prefix);
2293 Py_CLEAR(config->base_prefix);
2294 Py_CLEAR(config->exec_prefix);
2295 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002296 Py_CLEAR(config->warnoptions);
2297 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002298 Py_CLEAR(config->module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06002299 Py_CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002300}
2301
2302
2303static PyObject*
2304config_copy_attr(PyObject *obj)
2305{
2306 if (PyUnicode_Check(obj)) {
2307 Py_INCREF(obj);
2308 return obj;
2309 }
2310 else if (PyList_Check(obj)) {
2311 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
2312 }
2313 else if (PyDict_Check(obj)) {
2314 /* The dict type is used for xoptions. Make the assumption that keys
2315 and values are immutables */
2316 return PyDict_Copy(obj);
2317 }
2318 else {
2319 PyErr_Format(PyExc_TypeError,
2320 "cannot copy config attribute of type %.200s",
2321 Py_TYPE(obj)->tp_name);
2322 return NULL;
2323 }
2324}
2325
2326
2327int
2328_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
2329 const _PyMainInterpreterConfig *config2)
2330{
2331 _PyMainInterpreterConfig_Clear(config);
2332
2333#define COPY_ATTR(ATTR) \
2334 do { \
2335 if (config2->ATTR != NULL) { \
2336 config->ATTR = config_copy_attr(config2->ATTR); \
2337 if (config->ATTR == NULL) { \
2338 return -1; \
2339 } \
2340 } \
2341 } while (0)
2342
2343 COPY_ATTR(argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002344 COPY_ATTR(executable);
2345 COPY_ATTR(prefix);
2346 COPY_ATTR(base_prefix);
2347 COPY_ATTR(exec_prefix);
2348 COPY_ATTR(base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002349 COPY_ATTR(warnoptions);
2350 COPY_ATTR(xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002351 COPY_ATTR(module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06002352 COPY_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002353#undef COPY_ATTR
2354 return 0;
2355}
2356
2357
2358
2359
Victor Stinner41264f12017-12-15 02:05:29 +01002360_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01002361_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
2362 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002363{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002364 if (main_config->install_signal_handlers < 0) {
2365 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002366 }
2367
Victor Stinner9cfc0022017-12-20 19:36:46 +01002368 if (main_config->xoptions == NULL) {
2369 main_config->xoptions = config_create_xoptions_dict(config);
2370 if (main_config->xoptions == NULL) {
2371 return _Py_INIT_NO_MEMORY();
2372 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002373 }
2374
Victor Stinner8ded5b82018-01-24 17:03:28 +01002375#define COPY_WSTR(ATTR) \
2376 do { \
Victor Stinner1dc6e392018-07-25 02:49:17 +02002377 if (main_config->ATTR == NULL && config->ATTR != NULL) { \
Victor Stinner8ded5b82018-01-24 17:03:28 +01002378 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
2379 if (main_config->ATTR == NULL) { \
2380 return _Py_INIT_NO_MEMORY(); \
2381 } \
2382 } \
2383 } while (0)
2384#define COPY_WSTRLIST(ATTR, LEN, LIST) \
2385 do { \
2386 if (ATTR == NULL) { \
2387 ATTR = wstrlist_as_pylist(LEN, LIST); \
2388 if (ATTR == NULL) { \
2389 return _Py_INIT_NO_MEMORY(); \
2390 } \
2391 } \
2392 } while (0)
2393
2394 COPY_WSTRLIST(main_config->warnoptions,
2395 config->nwarnoption, config->warnoptions);
2396 if (config->argc >= 0) {
2397 COPY_WSTRLIST(main_config->argv,
2398 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002399 }
2400
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002401 if (config->_install_importlib) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01002402 COPY_WSTR(executable);
2403 COPY_WSTR(prefix);
2404 COPY_WSTR(base_prefix);
2405 COPY_WSTR(exec_prefix);
2406 COPY_WSTR(base_exec_prefix);
2407
2408 COPY_WSTRLIST(main_config->module_search_path,
2409 config->nmodule_search_path, config->module_search_paths);
Carl Meyerb193fa92018-06-15 22:40:56 -06002410
2411 if (config->pycache_prefix != NULL) {
2412 COPY_WSTR(pycache_prefix);
2413 } else {
2414 main_config->pycache_prefix = NULL;
2415 }
2416
Victor Stinner9cfc0022017-12-20 19:36:46 +01002417 }
Victor Stinner41264f12017-12-15 02:05:29 +01002418
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002419 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01002420#undef COPY_WSTR
2421#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002422}
2423
2424
2425static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002426pymain_init_python_main(_PyMain *pymain, _PyCoreConfig *config,
2427 PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002428{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002429 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002430
Victor Stinner9cfc0022017-12-20 19:36:46 +01002431 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner1dc6e392018-07-25 02:49:17 +02002432 err = _PyMainInterpreterConfig_Read(&main_config, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002433 if (!_Py_INIT_FAILED(err)) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02002434 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002435 }
2436 _PyMainInterpreterConfig_Clear(&main_config);
2437
2438 if (_Py_INIT_FAILED(err)) {
2439 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002440 return -1;
2441 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002442 return 0;
2443}
Victor Stinnera7368ac2017-11-15 18:11:45 -08002444
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002445
2446static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02002447pymain_run_python(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002448{
Victor Stinnerd3b19192018-07-25 10:21:03 +02002449 int res = 0;
2450 _PyCoreConfig *config = &interp->core_config;
2451
2452 PyObject *main_importer_path = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002453 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002454 /* If filename is a package (ex: directory or ZIP file) which contains
2455 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +02002456 prepended to sys.path.
2457
2458 Otherwise, main_importer_path is set to NULL. */
2459 main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01002460 }
2461
Victor Stinnerd3b19192018-07-25 10:21:03 +02002462 if (main_importer_path != NULL) {
2463 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
2464 pymain->status = 1;
2465 goto done;
2466 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01002467 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02002468 else if (!config->isolated) {
2469 PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc,
2470 config->argv);
2471 if (path0 == NULL) {
2472 pymain->err = _Py_INIT_NO_MEMORY();
2473 res = -1;
2474 goto done;
2475 }
Victor Stinner19760862017-12-20 01:41:59 +01002476
Victor Stinnerd3b19192018-07-25 10:21:03 +02002477 if (pymain_sys_path_add_path0(interp, path0) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01002478 Py_DECREF(path0);
Victor Stinnerd3b19192018-07-25 10:21:03 +02002479 pymain->status = 1;
2480 goto done;
Victor Stinner19760862017-12-20 01:41:59 +01002481 }
2482 Py_DECREF(path0);
2483 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002484
Victor Stinner19760862017-12-20 01:41:59 +01002485 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08002486
2487 pymain_header(pymain);
2488 pymain_import_readline(pymain);
2489
Victor Stinnerca719ac2017-12-20 18:00:19 +01002490 if (pymain->command) {
2491 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002492 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002493 else if (pymain->module) {
2494 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002495 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02002496 else if (main_importer_path != NULL) {
2497 int sts = pymain_run_module(L"__main__", 0);
2498 pymain->status = (sts != 0);
2499 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002500 else {
Victor Stinner1dc6e392018-07-25 02:49:17 +02002501 pymain_run_filename(pymain, config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002502 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002503
Victor Stinner1dc6e392018-07-25 02:49:17 +02002504 pymain_repl(pymain, config, &cf);
Victor Stinnerd3b19192018-07-25 10:21:03 +02002505
2506done:
2507 Py_XDECREF(main_importer_path);
2508 return res;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002509}
2510
Victor Stinnera7368ac2017-11-15 18:11:45 -08002511
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002512static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002513pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
2514 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002515{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002516 pymain->err = _PyRuntime_Initialize();
2517 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002518 return -1;
2519 }
2520
Victor Stinner1dc6e392018-07-25 02:49:17 +02002521 int res = pymain_read_conf(pymain, config, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002522 if (res < 0) {
2523 return -1;
2524 }
2525 if (res > 0) {
2526 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01002527 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002528 }
2529
Victor Stinner94540602017-12-16 04:54:22 +01002530 if (cmdline->print_help) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02002531 pymain_usage(0, config->program);
Victor Stinner19760862017-12-20 01:41:59 +01002532 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002533 }
2534
2535 if (cmdline->print_version) {
2536 printf("Python %s\n",
2537 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01002538 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002539 }
2540
Victor Stinnerc4bca952017-12-19 23:48:17 +01002541 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002542 orig_argv = copy_wstrlist(pymain->argc, cmdline->argv);
2543 if (orig_argv == NULL) {
2544 pymain->err = _Py_INIT_NO_MEMORY();
2545 return -1;
2546 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01002547 orig_argc = pymain->argc;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002548 return 0;
2549}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00002550
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002551
Victor Stinnerca719ac2017-12-20 18:00:19 +01002552/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
2553 LC_CTYPE locale and Py_DecodeLocale().
2554
2555 Configuration:
2556
2557 * Command line arguments
2558 * Environment variables
2559 * Py_xxx global configuration variables
2560
Victor Stinner1dc6e392018-07-25 02:49:17 +02002561 _PyCmdline is a temporary structure used to prioritize these
Victor Stinnerca719ac2017-12-20 18:00:19 +01002562 variables. */
2563static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002564pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerca719ac2017-12-20 18:00:19 +01002565{
Victor Stinner31e99082017-12-20 23:41:38 +01002566 /* Force default allocator, since pymain_free() and pymain_clear_config()
2567 must use the same allocator than this function. */
2568 PyMemAllocatorEx old_alloc;
2569 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2570#ifdef Py_DEBUG
2571 PyMemAllocatorEx default_alloc;
2572 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
2573#endif
2574
Victor Stinner1dc6e392018-07-25 02:49:17 +02002575 _PyCmdline cmdline;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002576 memset(&cmdline, 0, sizeof(cmdline));
2577
Victor Stinner1dc6e392018-07-25 02:49:17 +02002578 int res = pymain_cmdline_impl(pymain, config, &cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002579
Victor Stinnerca719ac2017-12-20 18:00:19 +01002580 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01002581
2582#ifdef Py_DEBUG
2583 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
2584 PyMemAllocatorEx cur_alloc;
2585 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
2586 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
2587#endif
2588 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002589 return res;
2590}
2591
2592
Victor Stinner94540602017-12-16 04:54:22 +01002593static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02002594pymain_init(_PyMain *pymain, PyInterpreterState **interp_p)
Victor Stinner94540602017-12-16 04:54:22 +01002595{
Victor Stinner1dc6e392018-07-25 02:49:17 +02002596 /* 754 requires that FP exceptions run in "no stop" mode by default,
2597 * and until C vendors implement C99's ways to control FP exceptions,
2598 * Python requires non-stop mode. Alas, some platforms enable FP
2599 * exceptions by default. Here we disable them.
2600 */
2601#ifdef __FreeBSD__
2602 fedisableexcept(FE_OVERFLOW);
2603#endif
Victor Stinner94540602017-12-16 04:54:22 +01002604
Victor Stinner1dc6e392018-07-25 02:49:17 +02002605 _PyCoreConfig local_config = _PyCoreConfig_INIT;
2606 _PyCoreConfig *config = &local_config;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02002607
Victor Stinner1dc6e392018-07-25 02:49:17 +02002608 _PyCoreConfig_GetGlobalConfig(config);
2609
2610 int cmd_res = pymain_cmdline(pymain, config);
2611 if (cmd_res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01002612 _Py_FatalInitError(pymain->err);
2613 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02002614 if (cmd_res == 1) {
2615 pymain_clear_config(config);
2616 return 1;
Victor Stinner19760862017-12-20 01:41:59 +01002617 }
2618
Victor Stinner1dc6e392018-07-25 02:49:17 +02002619 _PyCoreConfig_SetGlobalConfig(config);
Victor Stinner53b7d4e2018-07-25 01:37:05 +02002620
Victor Stinner1dc6e392018-07-25 02:49:17 +02002621 pymain_init_stdio(pymain, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002622
Victor Stinner1dc6e392018-07-25 02:49:17 +02002623 PyInterpreterState *interp;
2624 pymain->err = _Py_InitializeCore(&interp, config);
2625 if (_Py_INIT_FAILED(pymain->err)) {
2626 _Py_FatalInitError(pymain->err);
2627 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02002628 *interp_p = interp;
Victor Stinner1dc6e392018-07-25 02:49:17 +02002629
2630 pymain_clear_config(config);
2631 config = &interp->core_config;
2632
2633 if (pymain_init_python_main(pymain, config, interp) < 0) {
2634 _Py_FatalInitError(pymain->err);
2635 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02002636 return 0;
2637}
2638
2639
2640static int
2641pymain_main(_PyMain *pymain)
2642{
Victor Stinnerd3b19192018-07-25 10:21:03 +02002643 PyInterpreterState *interp;
2644 int res = pymain_init(pymain, &interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +02002645 if (res != 1) {
Victor Stinnerd3b19192018-07-25 10:21:03 +02002646 if (pymain_run_python(pymain, interp) < 0) {
2647 _Py_FatalInitError(pymain->err);
2648 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02002649
2650 if (Py_FinalizeEx() < 0) {
2651 /* Value unlikely to be confused with a non-error exit status or
2652 other special meaning */
2653 pymain->status = 120;
Victor Stinnerfb47bca2018-07-20 17:34:23 +02002654 }
Victor Stinner19760862017-12-20 01:41:59 +01002655 }
2656
Victor Stinner94540602017-12-16 04:54:22 +01002657 pymain_free(pymain);
2658
Victor Stinner94540602017-12-16 04:54:22 +01002659 return pymain->status;
2660}
2661
2662
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002663int
2664Py_Main(int argc, wchar_t **argv)
2665{
2666 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002667 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002668 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002669 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002670
Victor Stinner94540602017-12-16 04:54:22 +01002671 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00002672}
2673
Victor Stinner94540602017-12-16 04:54:22 +01002674
2675int
2676_Py_UnixMain(int argc, char **argv)
2677{
2678 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01002679 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002680 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01002681 pymain.bytes_argv = argv;
2682
2683 return pymain_main(&pymain);
2684}
2685
2686
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002687/* this is gonna seem *real weird*, but if you put some other code between
2688 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
2689 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00002690
Guido van Rossum667d7041995-08-04 04:20:48 +00002691/* Make the *original* argc/argv available to other modules.
2692 This is rare, but it is needed by the secureware extension. */
2693
2694void
Martin v. Löwis790465f2008-04-05 20:41:37 +00002695Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00002696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 *argc = orig_argc;
2698 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00002699}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002700
2701#ifdef __cplusplus
2702}
2703#endif