blob: da3441f3c96e047b3e5f14f6c1731391959536ef [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 Petersone425bd72017-12-14 23:48:12 -08005#include "internal/pygetopt.h"
Victor Stinnerf7e5b562017-11-15 15:48:08 -08006#include "internal/pystate.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00007
Antoine Pitrou5651eaa2008-09-06 20:46:58 +00008#include <locale.h>
9
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000010#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010011# include <windows.h>
12# ifdef HAVE_IO_H
13# include <io.h>
14# endif
15# ifdef HAVE_FCNTL_H
16# include <fcntl.h>
17# endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000018#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000019
Martin v. Löwis945362c2007-08-30 14:57:25 +000020#ifdef _MSC_VER
Victor Stinner6efcb6d2017-12-18 23:42:55 +010021# include <crtdbg.h>
22#endif
23
24#ifdef __FreeBSD__
25# include <fenv.h>
Martin v. Löwis945362c2007-08-30 14:57:25 +000026#endif
27
Jesus Ceaab70e2a2012-10-05 01:48:08 +020028#if defined(MS_WINDOWS)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010029# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
Guido van Rossuma075ce11997-12-05 21:56:45 +000030#else
Victor Stinner6efcb6d2017-12-18 23:42:55 +010031# define PYTHONHOMEHELP "<prefix>/lib/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000032#endif
33
Guido van Rossuma22865e2000-09-05 04:41:18 +000034#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000035 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
36 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000037
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038#ifdef __cplusplus
39extern "C" {
40#endif
41
Victor Stinner46972b72017-11-24 22:55:40 +010042#define DECODE_LOCALE_ERR(NAME, LEN) \
43 (((LEN) == -2) \
Victor Stinner94540602017-12-16 04:54:22 +010044 ? _Py_INIT_USER_ERR("cannot decode " NAME) \
Victor Stinner46972b72017-11-24 22:55:40 +010045 : _Py_INIT_NO_MEMORY())
46
47
Victor Stinnerca719ac2017-12-20 18:00:19 +010048#ifdef MS_WINDOWS
49#define WCSTOK wcstok_s
50#else
51#define WCSTOK wcstok
52#endif
53
Guido van Rossumac56b031996-07-21 02:33:38 +000054/* For Py_GetArgcArgv(); set by main() */
Victor Stinner94540602017-12-16 04:54:22 +010055static wchar_t **orig_argv = NULL;
56static int orig_argc = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +000057
Guido van Rossumbceccf52001-04-10 22:07:43 +000058/* command line options */
Christian Heimesad73a9c2013-08-10 16:36:18 +020059#define BASE_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000060
Guido van Rossumbceccf52001-04-10 22:07:43 +000061#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000062
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080063static const _PyOS_LongOption longoptions[] = {
64 {L"check-hash-based-pycs", 1, 0},
65 {NULL, 0, 0},
66};
67
Guido van Rossum667d7041995-08-04 04:20:48 +000068/* Short usage message (with %s for argv0) */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020069static const char usage_line[] =
Martin v. Löwis790465f2008-04-05 20:41:37 +000070"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000071
72/* Long usage message, split into parts < 512 bytes */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020073static const char usage_1[] = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000074Options and arguments (and corresponding environment variables):\n\
Christian Heimes2ab34442008-09-03 20:31:07 +000075-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
76 and comparing bytes/bytearray with str. (-bb: issue errors)\n\
Xiang Zhang0710d752017-03-11 13:02:52 +080077-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000078-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000079-d : debug output from parser; also PYTHONDEBUG=x\n\
Christian Heimes790c8232008-01-07 21:14:23 +000080-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000081-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000082";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020083static const char usage_2[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000084-i : inspect interactively after running script; forces a prompt even\n\
85 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Christian Heimesad73a9c2013-08-10 16:36:18 +020086-I : isolate Python from the user's environment (implies -E and -s)\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000087-m mod : run library module as a script (terminates option list)\n\
Cheryl Sabella186b6062018-02-24 22:04:40 -050088-O : remove assert and __debug__-dependent statements; add .opt-1 before\n\
89 .pyc extension; also PYTHONOPTIMIZE=x\n\
90-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
91 .pyc extension\n\
Georg Brandl9d871192010-12-04 10:47:18 +000092-q : don't print version and copyright messages on interactive startup\n\
Christian Heimes8dc226f2008-05-06 23:45:46 +000093-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000094-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000095";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020096static const char usage_3[] = "\
Berker Peksag7f580972017-10-13 15:16:31 +030097-u : force the stdout and stderr streams to be unbuffered;\n\
98 this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000099-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
100 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101-V : print the Python version number and exit (also --version)\n\
INADA Naoki0e175a62016-11-21 20:57:14 +0900102 when given twice, print more information about the build\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000103-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenvey0805ca32010-04-07 04:04:10 +0000104 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000105-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000106-X opt : set implementation-specific option\n\
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800107--check-hash-based-pycs always|default|never:\n\
108 control how Python invalidates hash-based .pyc files\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000109";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200110static const char usage_4[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +0000111file : program read from script file\n\
112- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000113arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000114Other environment variables:\n\
115PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200116PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000117 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +0000118";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200119static const char usage_5[] =
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200120"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
Victor Stinner9802b392010-08-19 11:36:43 +0000121" The default module search path uses %s.\n"
122"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
123"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
Victor Stinner34be8072016-03-14 12:04:26 +0100124"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
125static const char usage_6[] =
126"PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
127" to seed the hashes of str, bytes and datetime objects. It can also be\n"
128" set to an integer in the range [0,4294967295] to get hash values with a\n"
129" predictable seed.\n"
130"PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
131" on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000132" hooks.\n"
Stéphane Wirtel7d1017d2017-06-12 13:30:33 +0200133"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000134" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
Victor Stinner5e3806f2017-11-30 11:40:24 +0100135" locale coercion and locale compatibility warnings on stderr.\n"
Stéphane Wirtelb7fd7382018-07-29 12:27:16 +0200136"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
137" debugger. It can be set to the callable of your debugger of choice.\n"
Carl Meyerb193fa92018-06-15 22:40:56 -0600138"PYTHONDEVMODE: enable the development mode.\n"
139"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
Guido van Rossum667d7041995-08-04 04:20:48 +0000140
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800141static void
Victor Stinnera7368ac2017-11-15 18:11:45 -0800142pymain_usage(int error, const wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000143{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800144 FILE *f = error ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 fprintf(f, usage_line, program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800147 if (error)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 fprintf(f, "Try `python -h' for more information.\n");
149 else {
150 fputs(usage_1, f);
151 fputs(usage_2, f);
152 fputs(usage_3, f);
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200153 fprintf(f, usage_4, (wint_t)DELIM);
154 fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100155 fputs(usage_6, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000157}
158
Victor Stinnera7368ac2017-11-15 18:11:45 -0800159
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800160static void
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800161pymain_run_interactive_hook(void)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200162{
163 PyObject *sys, *hook, *result;
164 sys = PyImport_ImportModule("sys");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800165 if (sys == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200166 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800167 }
168
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200169 hook = PyObject_GetAttrString(sys, "__interactivehook__");
170 Py_DECREF(sys);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800171 if (hook == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200172 PyErr_Clear();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800173 return;
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200174 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800175
176 result = _PyObject_CallNoArg(hook);
177 Py_DECREF(hook);
178 if (result == NULL) {
179 goto error;
180 }
181 Py_DECREF(result);
182
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200183 return;
184
185error:
186 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
187 PyErr_Print();
188 PyErr_Clear();
189}
190
Thomas Woutersa9773292006-04-21 09:43:23 +0000191
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800192static int
Victor Stinnerc4bca952017-12-19 23:48:17 +0100193pymain_run_module(const wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 PyObject *module, *runpy, *runmodule, *runargs, *result;
196 runpy = PyImport_ImportModule("runpy");
197 if (runpy == NULL) {
198 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200199 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 return -1;
201 }
202 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
203 if (runmodule == NULL) {
204 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200205 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 Py_DECREF(runpy);
207 return -1;
208 }
209 module = PyUnicode_FromWideChar(modname, wcslen(modname));
210 if (module == NULL) {
211 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200212 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 Py_DECREF(runpy);
214 Py_DECREF(runmodule);
215 return -1;
216 }
217 runargs = Py_BuildValue("(Oi)", module, set_argv0);
218 if (runargs == NULL) {
219 fprintf(stderr,
220 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200221 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 Py_DECREF(runpy);
223 Py_DECREF(runmodule);
224 Py_DECREF(module);
225 return -1;
226 }
227 result = PyObject_Call(runmodule, runargs, NULL);
228 if (result == NULL) {
229 PyErr_Print();
230 }
231 Py_DECREF(runpy);
232 Py_DECREF(runmodule);
233 Py_DECREF(module);
234 Py_DECREF(runargs);
235 if (result == NULL) {
236 return -1;
237 }
238 Py_DECREF(result);
239 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000240}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000241
Nick Coghland2977a32017-03-12 20:38:32 +1000242static PyObject *
Victor Stinnerc4bca952017-12-19 23:48:17 +0100243pymain_get_importer(const wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000244{
Nick Coghland2977a32017-03-12 20:38:32 +1000245 PyObject *sys_path0 = NULL, *importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000246
Nick Coghland2977a32017-03-12 20:38:32 +1000247 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800248 if (sys_path0 == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000249 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800250 }
Victor Stinner4726e402010-10-06 23:24:57 +0000251
Nick Coghland2977a32017-03-12 20:38:32 +1000252 importer = PyImport_GetImporter(sys_path0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800253 if (importer == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000254 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800255 }
Victor Stinner4726e402010-10-06 23:24:57 +0000256
Brett Cannonaa936422012-04-27 15:30:58 -0400257 if (importer == Py_None) {
Nick Coghland2977a32017-03-12 20:38:32 +1000258 Py_DECREF(sys_path0);
Victor Stinner4726e402010-10-06 23:24:57 +0000259 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000260 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800262
Victor Stinner4726e402010-10-06 23:24:57 +0000263 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000264 return sys_path0;
Victor Stinner4726e402010-10-06 23:24:57 +0000265
Nick Coghland2977a32017-03-12 20:38:32 +1000266error:
267 Py_XDECREF(sys_path0);
268 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
269 PyErr_Print();
270 PyErr_Clear();
271 return NULL;
272}
273
274
275static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800276pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
Victor Stinnera62207c2010-08-07 10:57:17 +0000277{
278 PyObject *unicode, *bytes;
279 int ret;
280
281 unicode = PyUnicode_FromWideChar(command, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800282 if (unicode == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000283 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800284 }
285
Victor Stinnera62207c2010-08-07 10:57:17 +0000286 bytes = PyUnicode_AsUTF8String(unicode);
287 Py_DECREF(unicode);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800288 if (bytes == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000289 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800290 }
291
Victor Stinnera62207c2010-08-07 10:57:17 +0000292 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
293 Py_DECREF(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800294 return (ret != 0);
Victor Stinnera62207c2010-08-07 10:57:17 +0000295
296error:
Victor Stinner398356b2010-08-18 22:23:22 +0000297 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000298 PyErr_Print();
299 return 1;
300}
301
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800302
Guido van Rossum667d7041995-08-04 04:20:48 +0000303/* Main program */
304
Eric Snow6b4be192017-05-22 21:36:03 -0700305typedef struct {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100306 wchar_t **argv;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200307 int nwarnoption; /* Number of -W command line options */
308 wchar_t **warnoptions; /* Command line -W options */
309 int nenv_warnoption; /* Number of PYTHONWARNINGS environment variables */
310 wchar_t **env_warnoptions; /* PYTHONWARNINGS environment variables */
Eric Snow6b4be192017-05-22 21:36:03 -0700311 int print_help; /* -h, -? options */
312 int print_version; /* -V option */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200313} _PyCmdline;
Eric Snow6b4be192017-05-22 21:36:03 -0700314
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800315/* Structure used by Py_Main() to pass data to subfunctions */
316typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100317 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800318 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100319 int use_bytes_argv;
320 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100321 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100322
323 /* Exit status or "exit code": result of pymain_main() */
324 int status;
325 /* Error message if a function failed */
326 _PyInitError err;
327
Victor Stinner19760862017-12-20 01:41:59 +0100328 /* non-zero is stdin is a TTY or if -i option is used */
329 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100330 int skip_first_line; /* -x option */
331 wchar_t *filename; /* Trailing arg without -c or -m */
332 wchar_t *command; /* -c argument */
333 wchar_t *module; /* -m argument */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800334} _PyMain;
335
Victor Stinner1dc6e392018-07-25 02:49:17 +0200336#define _PyMain_INIT {.err = _Py_INIT_OK()}
Victor Stinnerd5dda982017-12-13 17:31:16 +0100337/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800338
339
Victor Stinner19760862017-12-20 01:41:59 +0100340/* Non-zero if filename, command (-c) or module (-m) is set
341 on the command line */
342#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100343 (pymain->command != NULL || pymain->filename != NULL \
344 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100345
346
Victor Stinnerca719ac2017-12-20 18:00:19 +0100347static wchar_t*
348pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800349{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100350 wchar_t *str2 = _PyMem_RawWcsdup(str);
351 if (str2 == NULL) {
352 pymain->err = _Py_INIT_NO_MEMORY();
353 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800354 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100355 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800356}
357
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100358
Victor Stinnerc4bca952017-12-19 23:48:17 +0100359static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200360pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,
361 _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100362{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100363 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100364
Victor Stinnerca719ac2017-12-20 18:00:19 +0100365 if (pymain->use_bytes_argv) {
366 /* +1 for a the NULL terminator */
367 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
368 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
369 if (argv == NULL) {
370 pymain->err = _Py_INIT_NO_MEMORY();
371 return -1;
372 }
373
374 for (int i = 0; i < pymain->argc; i++) {
375 size_t len;
376 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
377 if (arg == NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +0200378 _Py_wstrlist_clear(i, argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100379 pymain->err = DECODE_LOCALE_ERR("command line arguments",
380 (Py_ssize_t)len);
381 return -1;
382 }
383 argv[i] = arg;
384 }
385 argv[pymain->argc] = NULL;
386
387 cmdline->argv = argv;
388 }
389 else {
390 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100391 }
392
Victor Stinnerca719ac2017-12-20 18:00:19 +0100393 wchar_t *program;
394 if (pymain->argc >= 1 && cmdline->argv != NULL) {
395 program = cmdline->argv[0];
396 }
397 else {
398 program = L"";
399 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200400 config->program = pymain_wstrdup(pymain, program);
401 if (config->program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100402 return -1;
403 }
404
Victor Stinnerc4bca952017-12-19 23:48:17 +0100405 return 0;
406}
407
408
409static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200410pymain_clear_cmdline(_PyMain *pymain, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100411{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100412 PyMemAllocatorEx old_alloc;
413 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100414
Victor Stinner6c785c02018-08-01 17:56:14 +0200415 _Py_wstrlist_clear(cmdline->nwarnoption, cmdline->warnoptions);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100416 cmdline->nwarnoption = 0;
417 cmdline->warnoptions = NULL;
418
Victor Stinner6c785c02018-08-01 17:56:14 +0200419 _Py_wstrlist_clear(cmdline->nenv_warnoption, cmdline->env_warnoptions);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100420 cmdline->nenv_warnoption = 0;
421 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100422
423 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +0200424 _Py_wstrlist_clear(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100425 }
426 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100427
428 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
429}
430
431
432static void
433pymain_clear_pymain(_PyMain *pymain)
434{
435#define CLEAR(ATTR) \
436 do { \
437 PyMem_RawFree(ATTR); \
438 ATTR = NULL; \
439 } while (0)
440
441 CLEAR(pymain->filename);
442 CLEAR(pymain->command);
443 CLEAR(pymain->module);
444#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100445}
446
Victor Stinnerc4bca952017-12-19 23:48:17 +0100447static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200448pymain_clear_config(_PyCoreConfig *config)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100449{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100450 /* Clear core config with the memory allocator
451 used by pymain_read_conf() */
452 PyMemAllocatorEx old_alloc;
453 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
454
Victor Stinner1dc6e392018-07-25 02:49:17 +0200455 _PyCoreConfig_Clear(config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100456
457 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
458}
459
460
461static void
Victor Stinnerd1457752018-07-26 16:04:56 +0200462pymain_free(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100463{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100464 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100465
Victor Stinnerc4bca952017-12-19 23:48:17 +0100466 /* Free global variables which cannot be freed in Py_Finalize():
467 configuration options set before Py_Initialize() which should
468 remain valid after Py_Finalize(), since
469 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinnerb1147e42018-07-21 02:06:16 +0200470 _PyPathConfig_ClearGlobal();
Victor Stinner94540602017-12-16 04:54:22 +0100471
Victor Stinnerc4bca952017-12-19 23:48:17 +0100472 /* Force the allocator used by pymain_read_conf() */
473 PyMemAllocatorEx old_alloc;
474 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100475
Victor Stinnerca719ac2017-12-20 18:00:19 +0100476 pymain_clear_pymain(pymain);
477
Victor Stinner6c785c02018-08-01 17:56:14 +0200478 _Py_wstrlist_clear(orig_argc, orig_argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100479 orig_argc = 0;
480 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100481
Victor Stinnerc4bca952017-12-19 23:48:17 +0100482 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200483
484#ifdef __INSURE__
485 /* Insure++ is a memory analysis tool that aids in discovering
486 * memory leaks and other memory problems. On Python exit, the
487 * interned string dictionaries are flagged as being in use at exit
488 * (which it is). Under normal circumstances, this is fine because
489 * the memory will be automatically reclaimed by the system. Under
490 * memory debugging, it's a huge source of useless noise, so we
491 * trade off slower shutdown for less distraction in the memory
492 * reports. -baw
493 */
494 _Py_ReleaseInternedUnicodeStrings();
495#endif /* __INSURE__ */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800496}
497
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100498
Eric Snow6b4be192017-05-22 21:36:03 -0700499static int
Victor Stinnerd3b19192018-07-25 10:21:03 +0200500pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
Guido van Rossum667d7041995-08-04 04:20:48 +0000501{
Victor Stinnerd3b19192018-07-25 10:21:03 +0200502 PyObject *sys_path;
503 PyObject *sysdict = interp->sysdict;
504 if (sysdict != NULL) {
505 sys_path = PyDict_GetItemString(sysdict, "path");
506 }
507 else {
508 sys_path = NULL;
509 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800510 if (sys_path == NULL) {
511 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
512 goto error;
513 }
514
Victor Stinnerd3b19192018-07-25 10:21:03 +0200515 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800516 goto error;
517 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200518 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800519
520error:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800521 PyErr_Print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200522 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800523}
524
525
Victor Stinnerb1147e42018-07-21 02:06:16 +0200526_PyInitError
527_Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800528{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200529 if (*len == INT_MAX) {
530 /* len+1 would overflow */
531 return _Py_INIT_NO_MEMORY();
532 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100533 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800534 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100535 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800536 }
537
Victor Stinnerca719ac2017-12-20 18:00:19 +0100538 size_t size = (*len + 1) * sizeof(list[0]);
539 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
540 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800541 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100542 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800543 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100544 list2[*len] = str2;
545 *list = list2;
546 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100547 return _Py_INIT_OK();
548}
549
550
551static int
552pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
553{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200554 _PyInitError err = _Py_wstrlist_append(len, list, str);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100555 if (_Py_INIT_FAILED(err)) {
556 pymain->err = err;
557 return -1;
558 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800559 return 0;
560}
561
562
563/* Parse the command line arguments
564 Return 0 on success.
565 Return 1 if parsing failed.
566 Set pymain->err and return -1 on other errors. */
567static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200568pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
569 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800570{
Antoine Pitrou86838b02012-02-21 19:03:47 +0100571 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800572 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800573 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100574 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800575 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800576 if (c == EOF) {
577 break;
578 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 /* -c is the last option; following arguments
582 that look like options are left for the
583 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800584 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
585 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
586 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100587 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800588 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800589 }
Victor Stinner58d16832018-05-31 15:09:28 +0200590 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 command[len - 2] = '\n';
592 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100593 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 break;
595 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 if (c == 'm') {
598 /* -m is the last option; following arguments
599 that look like options are left for the
600 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100601 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
602 if (pymain->module == NULL) {
603 return -1;
604 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 break;
606 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800609 case 0:
610 // Handle long option.
611 assert(longindex == 0); // Only one long option now.
612 if (!wcscmp(_PyOS_optarg, L"always")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200613 config->_check_hash_pycs_mode = "always";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800614 } else if (!wcscmp(_PyOS_optarg, L"never")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200615 config->_check_hash_pycs_mode = "never";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800616 } else if (!wcscmp(_PyOS_optarg, L"default")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200617 config->_check_hash_pycs_mode = "default";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800618 } else {
619 fprintf(stderr, "--check-hash-based-pycs must be one of "
620 "'default', 'always', or 'never'\n");
621 return 1;
622 }
623 break;
624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 case 'b':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200626 config->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 case 'd':
Victor Stinner98512272018-08-01 03:07:00 +0200630 config->parser_debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 case 'i':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200634 config->inspect++;
635 config->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000637
Christian Heimesad73a9c2013-08-10 16:36:18 +0200638 case 'I':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200639 config->isolated++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200640 break;
641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 case 'O':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200645 config->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 case 'B':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200649 config->write_bytecode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 case 's':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200653 config->user_site_directory = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 case 'S':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200657 config->site_import = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 case 'E':
Victor Stinnerd1457752018-07-26 16:04:56 +0200661 config->use_environment = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 case 't':
665 /* ignored for backwards compatibility */
666 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 case 'u':
Victor Stinner98512272018-08-01 03:07:00 +0200669 config->buffered_stdio = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 case 'v':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200673 config->verbose++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100677 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 case 'h':
681 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -0700682 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -0700686 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100690 if (pymain_wstrlist_append(pymain,
691 &cmdline->nwarnoption,
692 &cmdline->warnoptions,
693 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800694 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800695 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000697
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000698 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100699 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +0100700 &config->nxoption,
701 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +0100702 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800703 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800704 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000705 break;
706
Georg Brandl9d871192010-12-04 10:47:18 +0000707 case 'q':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200708 config->quiet++;
Georg Brandl9d871192010-12-04 10:47:18 +0000709 break;
710
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100711 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100712 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100713 break;
714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800718 /* unknown argument: parsing failed */
719 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800721 } while (1);
722
Victor Stinnerca719ac2017-12-20 18:00:19 +0100723 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800724 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +0100725 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800726 {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100727 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
728 if (pymain->filename == NULL) {
729 return -1;
730 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000732
Victor Stinnerd5dda982017-12-13 17:31:16 +0100733 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100734 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +0100735
Eric Snow6b4be192017-05-22 21:36:03 -0700736 return 0;
737}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000738
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800739
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800740static int
Victor Stinner9cfc0022017-12-20 19:36:46 +0100741add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +0100742{
743 PyObject *name, *value;
744
745 const wchar_t *name_end = wcschr(s, L'=');
746 if (!name_end) {
747 name = PyUnicode_FromWideChar(s, -1);
748 value = Py_True;
749 Py_INCREF(value);
750 }
751 else {
752 name = PyUnicode_FromWideChar(s, name_end - s);
753 value = PyUnicode_FromWideChar(name_end + 1, -1);
754 }
755 if (name == NULL || value == NULL) {
756 goto error;
757 }
758 if (PyDict_SetItem(opts, name, value) < 0) {
759 goto error;
760 }
761 Py_DECREF(name);
762 Py_DECREF(value);
763 return 0;
764
765error:
766 Py_XDECREF(name);
767 Py_XDECREF(value);
768 return -1;
769}
770
Victor Stinner9cfc0022017-12-20 19:36:46 +0100771
772static PyObject*
773config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800774{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100775 int nxoption = config->nxoption;
776 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +0100777 PyObject *dict = PyDict_New();
778 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100779 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +0100780 }
781
Victor Stinnerca719ac2017-12-20 18:00:19 +0100782 for (int i=0; i < nxoption; i++) {
783 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +0100784 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +0100785 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100786 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800787 }
788 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100789
Victor Stinner9cfc0022017-12-20 19:36:46 +0100790 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -0700791}
792
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800793
Victor Stinner9cfc0022017-12-20 19:36:46 +0100794static _PyInitError
795config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -0700796{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100797 for (int i = 0; i < len; i++) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200798 _PyInitError err = _Py_wstrlist_append(&config->nwarnoption,
799 &config->warnoptions,
800 options[i]);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100801 if (_Py_INIT_FAILED(err)) {
802 return err;
Eric Snow6b4be192017-05-22 21:36:03 -0700803 }
Eric Snow6b4be192017-05-22 21:36:03 -0700804 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100805 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800806}
Eric Snow6b4be192017-05-22 21:36:03 -0700807
Victor Stinner747f48e2017-12-12 22:59:48 +0100808
Victor Stinner9cfc0022017-12-20 19:36:46 +0100809static _PyInitError
Victor Stinner1dc6e392018-07-25 02:49:17 +0200810config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +0100811{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100812 _PyInitError err;
813
814 assert(config->nwarnoption == 0);
815
Victor Stinner747f48e2017-12-12 22:59:48 +0100816 /* The priority order for warnings configuration is (highest precedence
817 * first):
818 *
819 * - the BytesWarning filter, if needed ('-b', '-bb')
820 * - any '-W' command line options; then
821 * - the 'PYTHONWARNINGS' environment variable; then
822 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
823 * - any implicit filters added by _warnings.c/warnings.py
824 *
825 * All settings except the last are passed to the warnings module via
826 * the `sys.warnoptions` list. Since the warnings module works on the basis
827 * of "the most recently added filter will be checked first", we add
828 * the lowest precedence entries first so that later entries override them.
829 */
830
Victor Stinner9cfc0022017-12-20 19:36:46 +0100831 if (config->dev_mode) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200832 err = _Py_wstrlist_append(&config->nwarnoption,
833 &config->warnoptions,
834 L"default");
Victor Stinner9cfc0022017-12-20 19:36:46 +0100835 if (_Py_INIT_FAILED(err)) {
836 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100837 }
Victor Stinner747f48e2017-12-12 22:59:48 +0100838 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100839
Victor Stinner9cfc0022017-12-20 19:36:46 +0100840 err = config_add_warnings_optlist(config,
841 cmdline->nenv_warnoption,
842 cmdline->env_warnoptions);
843 if (_Py_INIT_FAILED(err)) {
844 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100845 }
846
Victor Stinner9cfc0022017-12-20 19:36:46 +0100847 err = config_add_warnings_optlist(config,
848 cmdline->nwarnoption,
849 cmdline->warnoptions);
850 if (_Py_INIT_FAILED(err)) {
851 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100852 }
853
854 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
855 * don't even try to emit a warning, so we skip setting the filter in that
856 * case.
857 */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200858 if (config->bytes_warning) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100859 wchar_t *filter;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200860 if (config->bytes_warning> 1) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100861 filter = L"error::BytesWarning";
862 }
863 else {
864 filter = L"default::BytesWarning";
865 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200866 err = _Py_wstrlist_append(&config->nwarnoption,
867 &config->warnoptions,
868 filter);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100869 if (_Py_INIT_FAILED(err)) {
870 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100871 }
872 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100873 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +0100874}
875
876
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800877/* Get warning options from PYTHONWARNINGS environment variable.
878 Return 0 on success.
879 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +0100880static _PyInitError
Victor Stinnerd1457752018-07-26 16:04:56 +0200881cmdline_init_env_warnoptions(_PyMain *pymain, const _PyCoreConfig *config,
882 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800883{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100884 wchar_t *env;
Victor Stinner6c785c02018-08-01 17:56:14 +0200885 int res = _PyCoreConfig_GetEnvDup(config, &env,
886 L"PYTHONWARNINGS", "PYTHONWARNINGS");
Victor Stinnerca719ac2017-12-20 18:00:19 +0100887 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100888 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100889 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800890
Victor Stinnerca719ac2017-12-20 18:00:19 +0100891 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100892 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +0100893 }
Philip Jenvey0805ca32010-04-07 04:04:10 +0000894
Victor Stinnerca719ac2017-12-20 18:00:19 +0100895
896 wchar_t *warning, *context = NULL;
897 for (warning = WCSTOK(env, L",", &context);
898 warning != NULL;
899 warning = WCSTOK(NULL, L",", &context))
900 {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200901 _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption,
902 &cmdline->env_warnoptions,
903 warning);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100904 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100905 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100906 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800907 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100909 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100910 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800911}
912
913
914static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200915pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800916{
917 pymain->stdin_is_interactive = (isatty(fileno(stdin))
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200918 || config->interactive);
Guido van Rossum775af911997-02-14 19:50:32 +0000919
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000920#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +0000921 /* don't translate newlines (\r\n <=> \n) */
922 _setmode(fileno(stdin), O_BINARY);
923 _setmode(fileno(stdout), O_BINARY);
924 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000925#endif
Victor Stinner89e34362011-01-07 18:47:22 +0000926
Victor Stinner98512272018-08-01 03:07:00 +0200927 if (!config->buffered_stdio) {
Guido van Rossum22ffac11998-03-06 15:30:39 +0000928#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
930 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
931 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000932#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 setbuf(stdin, (char *)NULL);
934 setbuf(stdout, (char *)NULL);
935 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000936#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200938 else if (config->interactive) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000939#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 /* Doesn't have to have line-buffered -- use unbuffered */
941 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
942 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000943#else /* !MS_WINDOWS */
944#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
946 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000947#endif /* HAVE_SETVBUF */
948#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 /* Leave stderr alone - it should be unbuffered anyway. */
950 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800951}
Guido van Rossum667d7041995-08-04 04:20:48 +0000952
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800953
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800954static void
955pymain_header(_PyMain *pymain)
956{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800957 if (Py_QuietFlag) {
958 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000960
Victor Stinner19760862017-12-20 01:41:59 +0100961 if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800962 return;
963 }
964
965 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
966 if (!Py_NoSiteFlag) {
967 fprintf(stderr, "%s\n", COPYRIGHT);
968 }
969}
970
971
Victor Stinnerc4bca952017-12-19 23:48:17 +0100972static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200973pymain_init_core_argv(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100974{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100975 /* Copy argv to be able to modify it (to force -c/-m) */
976 int argc = pymain->argc - _PyOS_optind;
977 wchar_t **argv;
978
979 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100980 /* Ensure at least one (empty) argument is seen */
981 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +0100982 argc = 1;
Victor Stinner6c785c02018-08-01 17:56:14 +0200983 argv = _Py_wstrlist_copy(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +0100984 }
Victor Stinnerc4bca952017-12-19 23:48:17 +0100985 else {
Victor Stinner6c785c02018-08-01 17:56:14 +0200986 argv = _Py_wstrlist_copy(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100987 }
988
989 if (argv == NULL) {
990 pymain->err = _Py_INIT_NO_MEMORY();
991 return -1;
992 }
993
994 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100995 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100996 /* Force sys.argv[0] = '-c' */
997 arg0 = L"-c";
998 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100999 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001000 /* Force sys.argv[0] = '-m'*/
1001 arg0 = L"-m";
1002 }
1003 if (arg0 != NULL) {
1004 arg0 = _PyMem_RawWcsdup(arg0);
1005 if (arg0 == NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +02001006 _Py_wstrlist_clear(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001007 pymain->err = _Py_INIT_NO_MEMORY();
1008 return -1;
1009 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001010
1011 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001012 PyMem_RawFree(argv[0]);
1013 argv[0] = arg0;
1014 }
1015
Victor Stinner1dc6e392018-07-25 02:49:17 +02001016 config->argc = argc;
1017 config->argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001018 return 0;
1019}
1020
1021
Victor Stinner8ded5b82018-01-24 17:03:28 +01001022static PyObject*
1023wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001024{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001025 assert(list != NULL || len < 1);
1026
1027 PyObject *pylist = PyList_New(len);
1028 if (pylist == NULL) {
1029 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001030 }
1031
Victor Stinner8ded5b82018-01-24 17:03:28 +01001032 for (int i = 0; i < len; i++) {
1033 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001034 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001035 Py_DECREF(pylist);
1036 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001037 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001038 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001039 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001040 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001041}
1042
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001043
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001044static void
1045pymain_import_readline(_PyMain *pymain)
1046{
1047 if (Py_IsolatedFlag) {
1048 return;
1049 }
Victor Stinner19760862017-12-20 01:41:59 +01001050 if (!Py_InspectFlag && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001051 return;
1052 }
1053 if (!isatty(fileno(stdin))) {
1054 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001055 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001056
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001057 PyObject *mod = PyImport_ImportModule("readline");
1058 if (mod == NULL) {
1059 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 }
1061 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001062 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001064}
1065
1066
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001067static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001068pymain_run_startup(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001069{
Victor Stinner6c785c02018-08-01 17:56:14 +02001070 const char *startup = _PyCoreConfig_GetEnv(config, "PYTHONSTARTUP");
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001071 if (startup == NULL) {
1072 return;
1073 }
1074
1075 FILE *fp = _Py_fopen(startup, "r");
1076 if (fp == NULL) {
1077 int save_errno = errno;
1078 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
1079 errno = save_errno;
1080
1081 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
1082 startup);
1083 PyErr_Print();
1084 PyErr_Clear();
1085 return;
1086 }
1087
1088 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
1089 PyErr_Clear();
1090 fclose(fp);
1091}
1092
1093
1094static void
Victor Stinner72ec3192018-08-02 19:34:20 +02001095pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001096{
Victor Stinner72ec3192018-08-02 19:34:20 +02001097 const wchar_t *filename = pymain->filename;
1098 FILE *fp = _Py_wfopen(filename, L"r");
1099 if (fp == NULL) {
1100 char *cfilename_buffer;
1101 const char *cfilename;
1102 int err = errno;
1103 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
1104 if (cfilename_buffer != NULL)
1105 cfilename = cfilename_buffer;
1106 else
1107 cfilename = "<unprintable file name>";
1108 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
1109 config->program, cfilename, err, strerror(err));
1110 PyMem_RawFree(cfilename_buffer);
1111 pymain->status = 2;
1112 return;
1113 }
1114
1115 if (pymain->skip_first_line) {
1116 int ch;
1117 /* Push back first newline so line numbers remain the same */
1118 while ((ch = getc(fp)) != EOF) {
1119 if (ch == '\n') {
1120 (void)ungetc(ch, fp);
1121 break;
1122 }
1123 }
1124 }
1125
1126 struct _Py_stat_struct sb;
1127 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
1128 fprintf(stderr,
1129 "%ls: '%ls' is a directory, cannot continue\n",
1130 config->program, filename);
1131 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001132 fclose(fp);
1133 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001134 }
1135
1136 /* call pending calls like signal handlers (SIGINT) */
1137 if (Py_MakePendingCalls() == -1) {
1138 PyErr_Print();
1139 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001140 fclose(fp);
1141 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001142 }
1143
1144 PyObject *unicode, *bytes = NULL;
1145 const char *filename_str;
1146
1147 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
1148 if (unicode != NULL) {
1149 bytes = PyUnicode_EncodeFSDefault(unicode);
1150 Py_DECREF(unicode);
1151 }
1152 if (bytes != NULL) {
1153 filename_str = PyBytes_AsString(bytes);
1154 }
1155 else {
1156 PyErr_Clear();
1157 filename_str = "<filename encoding error>";
1158 }
1159
Victor Stinnerd8078622018-08-03 23:54:06 +02001160 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
1161 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
Victor Stinner72ec3192018-08-02 19:34:20 +02001162 Py_XDECREF(bytes);
1163 pymain->status = (run != 0);
Victor Stinner72ec3192018-08-02 19:34:20 +02001164}
1165
1166
1167static void
1168pymain_run_stdin(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
1169{
1170 if (pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001171 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinnera4d20b22018-08-01 02:57:45 +02001172 config->inspect = 0;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001173 pymain_run_startup(pymain, config, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001174 pymain_run_interactive_hook();
1175 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001176
Victor Stinner72ec3192018-08-02 19:34:20 +02001177 /* call pending calls like signal handlers (SIGINT) */
1178 if (Py_MakePendingCalls() == -1) {
1179 PyErr_Print();
1180 pymain->status = 1;
1181 return;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001182 }
1183
Victor Stinner72ec3192018-08-02 19:34:20 +02001184 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
1185 pymain->status = (run != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001186}
1187
1188
1189static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001190pymain_repl(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001193 opportunity to set it from Python. */
Victor Stinner6c785c02018-08-01 17:56:14 +02001194 if (!Py_InspectFlag && _PyCoreConfig_GetEnv(config, "PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 Py_InspectFlag = 1;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001196 config->inspect = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001198
Victor Stinner19760862017-12-20 01:41:59 +01001199 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001200 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001202
1203 Py_InspectFlag = 0;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001204 config->inspect = 0;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001205 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001206
Victor Stinner19760862017-12-20 01:41:59 +01001207 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001208 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001209}
1210
1211
1212/* Parse the command line.
1213 Handle --version and --help options directly.
1214
1215 Return 1 if Python must exit.
1216 Return 0 on success.
1217 Set pymain->err and return -1 on failure. */
1218static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001219pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config,
1220 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001221{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001222 int res = pymain_parse_cmdline_impl(pymain, config, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001223 if (res < 0) {
1224 return -1;
1225 }
1226 if (res) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001227 pymain_usage(1, config->program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001228 pymain->status = 2;
1229 return 1;
1230 }
1231
Victor Stinnerca719ac2017-12-20 18:00:19 +01001232 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001233 /* Backup _PyOS_optind */
1234 _PyOS_optind--;
1235 }
1236
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001237 return 0;
1238}
1239
1240
Victor Stinnera7368ac2017-11-15 18:11:45 -08001241/* Parse command line options and environment variables.
1242 This code must not use Python runtime apart PyMem_Raw memory allocator.
1243
1244 Return 0 on success.
1245 Return 1 if Python is done and must exit.
1246 Set pymain->err and return -1 on error. */
1247static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001248pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
1249 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001250{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001251 _PyInitError err;
1252
Victor Stinner1dc6e392018-07-25 02:49:17 +02001253 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01001254 if (res != 0) {
1255 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001256 }
1257
Victor Stinner1dc6e392018-07-25 02:49:17 +02001258 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001259 return -1;
1260 }
1261
Victor Stinnerd1457752018-07-26 16:04:56 +02001262 err = _PyCoreConfig_Read(config);
1263 if (_Py_INIT_FAILED(err)) {
1264 pymain->err = err;
1265 return -1;
1266 }
1267
1268 assert(config->use_environment >= 0);
1269 if (config->use_environment) {
Victor Stinnerecf411c2018-07-26 02:37:22 +02001270 err = cmdline_init_env_warnoptions(pymain, config, cmdline);
1271 if (_Py_INIT_FAILED(err)) {
1272 pymain->err = err;
1273 return -1;
1274 }
1275 }
1276
Victor Stinnerd1457752018-07-26 16:04:56 +02001277 err = config_init_warnoptions(config, cmdline);
Victor Stinner31a83932017-12-04 13:39:15 +01001278 if (_Py_INIT_FAILED(err)) {
1279 pymain->err = err;
1280 return -1;
1281 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001282 return 0;
1283}
1284
1285
Victor Stinner19760862017-12-20 01:41:59 +01001286/* Read the configuration, but initialize also the LC_CTYPE locale:
1287 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
Victor Stinnera7368ac2017-11-15 18:11:45 -08001288static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001289pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
1290 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001291{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001292 _PyCoreConfig save_config = _PyCoreConfig_INIT;
1293 char *oldloc = NULL;
Victor Stinner94540602017-12-16 04:54:22 +01001294 int res = -1;
1295
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001296 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
Victor Stinner94540602017-12-16 04:54:22 +01001297 if (oldloc == NULL) {
1298 pymain->err = _Py_INIT_NO_MEMORY();
1299 goto done;
1300 }
1301
1302 /* Reconfigure the locale to the default for this process */
1303 _Py_SetLocaleFromEnv(LC_ALL);
1304
1305 int locale_coerced = 0;
1306 int loops = 0;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001307
1308 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
1309 pymain->err = _Py_INIT_NO_MEMORY();
1310 goto done;
1311 }
Victor Stinner94540602017-12-16 04:54:22 +01001312
1313 while (1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001314 int utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01001315 int encoding_changed = 0;
1316
1317 /* Watchdog to prevent an infinite loop */
1318 loops++;
1319 if (loops == 3) {
1320 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
1321 "reading the configuration");
1322 goto done;
1323 }
1324
Victor Stinner1dc6e392018-07-25 02:49:17 +02001325 if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001326 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01001327 }
1328
Victor Stinner1dc6e392018-07-25 02:49:17 +02001329 int conf_res = pymain_read_conf_impl(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001330 if (conf_res != 0) {
1331 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01001332 goto done;
1333 }
1334
1335 /* The legacy C locale assumes ASCII as the default text encoding, which
1336 * causes problems not only for the CPython runtime, but also other
1337 * components like GNU readline.
1338 *
1339 * Accordingly, when the CLI detects it, it attempts to coerce it to a
1340 * more capable UTF-8 based alternative.
1341 *
1342 * See the documentation of the PYTHONCOERCECLOCALE setting for more
1343 * details.
1344 */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001345 if (config->coerce_c_locale == 1 && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01001346 locale_coerced = 1;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001347 _Py_CoerceLegacyLocale(config);
Victor Stinner94540602017-12-16 04:54:22 +01001348 encoding_changed = 1;
1349 }
1350
1351 if (utf8_mode == -1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001352 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01001353 /* UTF-8 Mode enabled */
1354 encoding_changed = 1;
1355 }
1356 }
1357 else {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001358 if (config->utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01001359 encoding_changed = 1;
1360 }
1361 }
1362
1363 if (!encoding_changed) {
1364 break;
1365 }
1366
Victor Stinnerd1457752018-07-26 16:04:56 +02001367 /* Reset the configuration before reading again the configuration,
1368 just keep UTF-8 Mode value. */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001369 int new_utf8_mode = config->utf8_mode;
1370 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
1371 pymain->err = _Py_INIT_NO_MEMORY();
1372 goto done;
1373 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001374 pymain_clear_cmdline(pymain, cmdline);
Victor Stinner6c5a4b32018-06-16 00:06:28 +02001375 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner1dc6e392018-07-25 02:49:17 +02001376 config->utf8_mode = new_utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01001377
1378 /* The encoding changed: read again the configuration
1379 with the new encoding */
1380 }
1381 res = 0;
1382
1383done:
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001384 _PyCoreConfig_Clear(&save_config);
Victor Stinner94540602017-12-16 04:54:22 +01001385 if (oldloc != NULL) {
1386 setlocale(LC_ALL, oldloc);
1387 PyMem_RawFree(oldloc);
1388 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001389 return res;
1390}
1391
Victor Stinner91106cd2017-12-13 12:29:09 +01001392
Victor Stinnerda273412017-12-15 01:46:02 +01001393void
Victor Stinnerda273412017-12-15 01:46:02 +01001394_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
1395{
1396 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01001397 Py_CLEAR(config->executable);
1398 Py_CLEAR(config->prefix);
1399 Py_CLEAR(config->base_prefix);
1400 Py_CLEAR(config->exec_prefix);
1401 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001402 Py_CLEAR(config->warnoptions);
1403 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01001404 Py_CLEAR(config->module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06001405 Py_CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001406}
1407
1408
1409static PyObject*
1410config_copy_attr(PyObject *obj)
1411{
1412 if (PyUnicode_Check(obj)) {
1413 Py_INCREF(obj);
1414 return obj;
1415 }
1416 else if (PyList_Check(obj)) {
1417 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
1418 }
1419 else if (PyDict_Check(obj)) {
1420 /* The dict type is used for xoptions. Make the assumption that keys
1421 and values are immutables */
1422 return PyDict_Copy(obj);
1423 }
1424 else {
1425 PyErr_Format(PyExc_TypeError,
1426 "cannot copy config attribute of type %.200s",
1427 Py_TYPE(obj)->tp_name);
1428 return NULL;
1429 }
1430}
1431
1432
1433int
1434_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
1435 const _PyMainInterpreterConfig *config2)
1436{
1437 _PyMainInterpreterConfig_Clear(config);
1438
1439#define COPY_ATTR(ATTR) \
1440 do { \
1441 if (config2->ATTR != NULL) { \
1442 config->ATTR = config_copy_attr(config2->ATTR); \
1443 if (config->ATTR == NULL) { \
1444 return -1; \
1445 } \
1446 } \
1447 } while (0)
1448
1449 COPY_ATTR(argv);
Victor Stinner41264f12017-12-15 02:05:29 +01001450 COPY_ATTR(executable);
1451 COPY_ATTR(prefix);
1452 COPY_ATTR(base_prefix);
1453 COPY_ATTR(exec_prefix);
1454 COPY_ATTR(base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001455 COPY_ATTR(warnoptions);
1456 COPY_ATTR(xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01001457 COPY_ATTR(module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06001458 COPY_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001459#undef COPY_ATTR
1460 return 0;
1461}
1462
1463
Victor Stinner41264f12017-12-15 02:05:29 +01001464_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01001465_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
1466 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001467{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001468 if (main_config->install_signal_handlers < 0) {
1469 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001470 }
1471
Victor Stinner9cfc0022017-12-20 19:36:46 +01001472 if (main_config->xoptions == NULL) {
1473 main_config->xoptions = config_create_xoptions_dict(config);
1474 if (main_config->xoptions == NULL) {
1475 return _Py_INIT_NO_MEMORY();
1476 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001477 }
1478
Victor Stinner8ded5b82018-01-24 17:03:28 +01001479#define COPY_WSTR(ATTR) \
1480 do { \
Victor Stinner1dc6e392018-07-25 02:49:17 +02001481 if (main_config->ATTR == NULL && config->ATTR != NULL) { \
Victor Stinner8ded5b82018-01-24 17:03:28 +01001482 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
1483 if (main_config->ATTR == NULL) { \
1484 return _Py_INIT_NO_MEMORY(); \
1485 } \
1486 } \
1487 } while (0)
1488#define COPY_WSTRLIST(ATTR, LEN, LIST) \
1489 do { \
1490 if (ATTR == NULL) { \
1491 ATTR = wstrlist_as_pylist(LEN, LIST); \
1492 if (ATTR == NULL) { \
1493 return _Py_INIT_NO_MEMORY(); \
1494 } \
1495 } \
1496 } while (0)
1497
1498 COPY_WSTRLIST(main_config->warnoptions,
1499 config->nwarnoption, config->warnoptions);
1500 if (config->argc >= 0) {
1501 COPY_WSTRLIST(main_config->argv,
1502 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001503 }
1504
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001505 if (config->_install_importlib) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001506 COPY_WSTR(executable);
1507 COPY_WSTR(prefix);
1508 COPY_WSTR(base_prefix);
1509 COPY_WSTR(exec_prefix);
1510 COPY_WSTR(base_exec_prefix);
1511
1512 COPY_WSTRLIST(main_config->module_search_path,
1513 config->nmodule_search_path, config->module_search_paths);
Carl Meyerb193fa92018-06-15 22:40:56 -06001514
1515 if (config->pycache_prefix != NULL) {
1516 COPY_WSTR(pycache_prefix);
1517 } else {
1518 main_config->pycache_prefix = NULL;
1519 }
1520
Victor Stinner9cfc0022017-12-20 19:36:46 +01001521 }
Victor Stinner41264f12017-12-15 02:05:29 +01001522
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001523 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01001524#undef COPY_WSTR
1525#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001526}
1527
1528
1529static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001530pymain_init_python_main(_PyMain *pymain, _PyCoreConfig *config,
1531 PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001532{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001533 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001534
Victor Stinner9cfc0022017-12-20 19:36:46 +01001535 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001536 err = _PyMainInterpreterConfig_Read(&main_config, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001537 if (!_Py_INIT_FAILED(err)) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001538 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001539 }
1540 _PyMainInterpreterConfig_Clear(&main_config);
1541
1542 if (_Py_INIT_FAILED(err)) {
1543 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001544 return -1;
1545 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001546 return 0;
1547}
Victor Stinnera7368ac2017-11-15 18:11:45 -08001548
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001549
1550static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02001551pymain_run_python(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001552{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001553 int res = 0;
1554 _PyCoreConfig *config = &interp->core_config;
1555
1556 PyObject *main_importer_path = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001557 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01001558 /* If filename is a package (ex: directory or ZIP file) which contains
1559 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +02001560 prepended to sys.path.
1561
1562 Otherwise, main_importer_path is set to NULL. */
1563 main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01001564 }
1565
Victor Stinnerd3b19192018-07-25 10:21:03 +02001566 if (main_importer_path != NULL) {
1567 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
1568 pymain->status = 1;
1569 goto done;
1570 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001571 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001572 else if (!config->isolated) {
1573 PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc,
1574 config->argv);
1575 if (path0 == NULL) {
1576 pymain->err = _Py_INIT_NO_MEMORY();
1577 res = -1;
1578 goto done;
1579 }
Victor Stinner19760862017-12-20 01:41:59 +01001580
Victor Stinnerd3b19192018-07-25 10:21:03 +02001581 if (pymain_sys_path_add_path0(interp, path0) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001582 Py_DECREF(path0);
Victor Stinnerd3b19192018-07-25 10:21:03 +02001583 pymain->status = 1;
1584 goto done;
Victor Stinner19760862017-12-20 01:41:59 +01001585 }
1586 Py_DECREF(path0);
1587 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001588
Victor Stinner19760862017-12-20 01:41:59 +01001589 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08001590
1591 pymain_header(pymain);
1592 pymain_import_readline(pymain);
1593
Victor Stinnerca719ac2017-12-20 18:00:19 +01001594 if (pymain->command) {
1595 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001596 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001597 else if (pymain->module) {
1598 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001599 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001600 else if (main_importer_path != NULL) {
1601 int sts = pymain_run_module(L"__main__", 0);
1602 pymain->status = (sts != 0);
1603 }
Victor Stinner72ec3192018-08-02 19:34:20 +02001604 else if (pymain->filename != NULL) {
1605 pymain_run_file(pymain, config, &cf);
1606 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001607 else {
Victor Stinner72ec3192018-08-02 19:34:20 +02001608 pymain_run_stdin(pymain, config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001609 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001610
Victor Stinner1dc6e392018-07-25 02:49:17 +02001611 pymain_repl(pymain, config, &cf);
Victor Stinnerd3b19192018-07-25 10:21:03 +02001612
1613done:
1614 Py_XDECREF(main_importer_path);
1615 return res;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001616}
1617
Victor Stinnera7368ac2017-11-15 18:11:45 -08001618
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001619static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001620pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
1621 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001622{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001623 pymain->err = _PyRuntime_Initialize();
1624 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001625 return -1;
1626 }
1627
Victor Stinner1dc6e392018-07-25 02:49:17 +02001628 int res = pymain_read_conf(pymain, config, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001629 if (res < 0) {
1630 return -1;
1631 }
1632 if (res > 0) {
1633 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01001634 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001635 }
1636
Victor Stinner94540602017-12-16 04:54:22 +01001637 if (cmdline->print_help) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001638 pymain_usage(0, config->program);
Victor Stinner19760862017-12-20 01:41:59 +01001639 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01001640 }
1641
1642 if (cmdline->print_version) {
1643 printf("Python %s\n",
1644 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01001645 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01001646 }
1647
Victor Stinnerc4bca952017-12-19 23:48:17 +01001648 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinner6c785c02018-08-01 17:56:14 +02001649 orig_argv = _Py_wstrlist_copy(pymain->argc, cmdline->argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001650 if (orig_argv == NULL) {
1651 pymain->err = _Py_INIT_NO_MEMORY();
1652 return -1;
1653 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001654 orig_argc = pymain->argc;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001655 return 0;
1656}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00001657
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001658
Victor Stinnerca719ac2017-12-20 18:00:19 +01001659/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
1660 LC_CTYPE locale and Py_DecodeLocale().
1661
1662 Configuration:
1663
1664 * Command line arguments
1665 * Environment variables
1666 * Py_xxx global configuration variables
1667
Victor Stinner1dc6e392018-07-25 02:49:17 +02001668 _PyCmdline is a temporary structure used to prioritize these
Victor Stinnerca719ac2017-12-20 18:00:19 +01001669 variables. */
1670static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001671pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerca719ac2017-12-20 18:00:19 +01001672{
Victor Stinner31e99082017-12-20 23:41:38 +01001673 /* Force default allocator, since pymain_free() and pymain_clear_config()
1674 must use the same allocator than this function. */
1675 PyMemAllocatorEx old_alloc;
1676 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
1677#ifdef Py_DEBUG
1678 PyMemAllocatorEx default_alloc;
1679 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
1680#endif
1681
Victor Stinner1dc6e392018-07-25 02:49:17 +02001682 _PyCmdline cmdline;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001683 memset(&cmdline, 0, sizeof(cmdline));
1684
Victor Stinner1dc6e392018-07-25 02:49:17 +02001685 int res = pymain_cmdline_impl(pymain, config, &cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001686
Victor Stinnerca719ac2017-12-20 18:00:19 +01001687 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01001688
1689#ifdef Py_DEBUG
1690 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
1691 PyMemAllocatorEx cur_alloc;
1692 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
1693 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
1694#endif
1695 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001696 return res;
1697}
1698
1699
Victor Stinner94540602017-12-16 04:54:22 +01001700static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02001701pymain_init(_PyMain *pymain, PyInterpreterState **interp_p)
Victor Stinner94540602017-12-16 04:54:22 +01001702{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001703 /* 754 requires that FP exceptions run in "no stop" mode by default,
1704 * and until C vendors implement C99's ways to control FP exceptions,
1705 * Python requires non-stop mode. Alas, some platforms enable FP
1706 * exceptions by default. Here we disable them.
1707 */
1708#ifdef __FreeBSD__
1709 fedisableexcept(FE_OVERFLOW);
1710#endif
Victor Stinner94540602017-12-16 04:54:22 +01001711
Victor Stinner1dc6e392018-07-25 02:49:17 +02001712 _PyCoreConfig local_config = _PyCoreConfig_INIT;
1713 _PyCoreConfig *config = &local_config;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001714
Victor Stinner1dc6e392018-07-25 02:49:17 +02001715 _PyCoreConfig_GetGlobalConfig(config);
1716
1717 int cmd_res = pymain_cmdline(pymain, config);
1718 if (cmd_res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01001719 _Py_FatalInitError(pymain->err);
1720 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001721 if (cmd_res == 1) {
1722 pymain_clear_config(config);
1723 return 1;
Victor Stinner19760862017-12-20 01:41:59 +01001724 }
1725
Victor Stinner1dc6e392018-07-25 02:49:17 +02001726 _PyCoreConfig_SetGlobalConfig(config);
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001727
Victor Stinner1dc6e392018-07-25 02:49:17 +02001728 pymain_init_stdio(pymain, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001729
Victor Stinner1dc6e392018-07-25 02:49:17 +02001730 PyInterpreterState *interp;
1731 pymain->err = _Py_InitializeCore(&interp, config);
1732 if (_Py_INIT_FAILED(pymain->err)) {
1733 _Py_FatalInitError(pymain->err);
1734 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001735 *interp_p = interp;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001736
1737 pymain_clear_config(config);
1738 config = &interp->core_config;
1739
1740 if (pymain_init_python_main(pymain, config, interp) < 0) {
1741 _Py_FatalInitError(pymain->err);
1742 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001743 return 0;
1744}
1745
1746
1747static int
1748pymain_main(_PyMain *pymain)
1749{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001750 PyInterpreterState *interp;
1751 int res = pymain_init(pymain, &interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +02001752 if (res != 1) {
Victor Stinnerd3b19192018-07-25 10:21:03 +02001753 if (pymain_run_python(pymain, interp) < 0) {
1754 _Py_FatalInitError(pymain->err);
1755 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001756
1757 if (Py_FinalizeEx() < 0) {
1758 /* Value unlikely to be confused with a non-error exit status or
1759 other special meaning */
1760 pymain->status = 120;
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001761 }
Victor Stinner19760862017-12-20 01:41:59 +01001762 }
1763
Victor Stinner94540602017-12-16 04:54:22 +01001764 pymain_free(pymain);
1765
Victor Stinner94540602017-12-16 04:54:22 +01001766 return pymain->status;
1767}
1768
1769
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001770int
1771Py_Main(int argc, wchar_t **argv)
1772{
1773 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001774 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001775 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001776 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001777
Victor Stinner94540602017-12-16 04:54:22 +01001778 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00001779}
1780
Victor Stinner94540602017-12-16 04:54:22 +01001781
1782int
1783_Py_UnixMain(int argc, char **argv)
1784{
1785 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001786 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001787 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01001788 pymain.bytes_argv = argv;
1789
1790 return pymain_main(&pymain);
1791}
1792
1793
Skip Montanaro786ea6b2004-03-01 15:44:05 +00001794/* this is gonna seem *real weird*, but if you put some other code between
1795 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
1796 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00001797
Guido van Rossum667d7041995-08-04 04:20:48 +00001798/* Make the *original* argc/argv available to other modules.
1799 This is rare, but it is needed by the secureware extension. */
1800
1801void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001802Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00001803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 *argc = orig_argc;
1805 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00001806}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001807
1808#ifdef __cplusplus
1809}
1810#endif