blob: 1ab555b42de49fa4a092a926b2140109f456d197 [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();
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200188}
189
Thomas Woutersa9773292006-04-21 09:43:23 +0000190
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800191static int
Victor Stinnerc4bca952017-12-19 23:48:17 +0100192pymain_run_module(const wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 PyObject *module, *runpy, *runmodule, *runargs, *result;
195 runpy = PyImport_ImportModule("runpy");
196 if (runpy == NULL) {
197 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200198 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 return -1;
200 }
201 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
202 if (runmodule == NULL) {
203 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200204 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 Py_DECREF(runpy);
206 return -1;
207 }
208 module = PyUnicode_FromWideChar(modname, wcslen(modname));
209 if (module == NULL) {
210 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200211 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 Py_DECREF(runpy);
213 Py_DECREF(runmodule);
214 return -1;
215 }
216 runargs = Py_BuildValue("(Oi)", module, set_argv0);
217 if (runargs == NULL) {
218 fprintf(stderr,
219 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200220 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 Py_DECREF(runpy);
222 Py_DECREF(runmodule);
223 Py_DECREF(module);
224 return -1;
225 }
226 result = PyObject_Call(runmodule, runargs, NULL);
227 if (result == NULL) {
228 PyErr_Print();
229 }
230 Py_DECREF(runpy);
231 Py_DECREF(runmodule);
232 Py_DECREF(module);
233 Py_DECREF(runargs);
234 if (result == NULL) {
235 return -1;
236 }
237 Py_DECREF(result);
238 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000239}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000240
Nick Coghland2977a32017-03-12 20:38:32 +1000241static PyObject *
Victor Stinnerc4bca952017-12-19 23:48:17 +0100242pymain_get_importer(const wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000243{
Nick Coghland2977a32017-03-12 20:38:32 +1000244 PyObject *sys_path0 = NULL, *importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000245
Nick Coghland2977a32017-03-12 20:38:32 +1000246 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800247 if (sys_path0 == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000248 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800249 }
Victor Stinner4726e402010-10-06 23:24:57 +0000250
Nick Coghland2977a32017-03-12 20:38:32 +1000251 importer = PyImport_GetImporter(sys_path0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800252 if (importer == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000253 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800254 }
Victor Stinner4726e402010-10-06 23:24:57 +0000255
Brett Cannonaa936422012-04-27 15:30:58 -0400256 if (importer == Py_None) {
Nick Coghland2977a32017-03-12 20:38:32 +1000257 Py_DECREF(sys_path0);
Victor Stinner4726e402010-10-06 23:24:57 +0000258 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000259 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800261
Victor Stinner4726e402010-10-06 23:24:57 +0000262 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000263 return sys_path0;
Victor Stinner4726e402010-10-06 23:24:57 +0000264
Nick Coghland2977a32017-03-12 20:38:32 +1000265error:
266 Py_XDECREF(sys_path0);
267 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
268 PyErr_Print();
Nick Coghland2977a32017-03-12 20:38:32 +1000269 return NULL;
270}
271
272
273static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800274pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
Victor Stinnera62207c2010-08-07 10:57:17 +0000275{
276 PyObject *unicode, *bytes;
277 int ret;
278
279 unicode = PyUnicode_FromWideChar(command, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800280 if (unicode == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000281 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800282 }
283
Victor Stinnera62207c2010-08-07 10:57:17 +0000284 bytes = PyUnicode_AsUTF8String(unicode);
285 Py_DECREF(unicode);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800286 if (bytes == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000287 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800288 }
289
Victor Stinnera62207c2010-08-07 10:57:17 +0000290 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
291 Py_DECREF(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800292 return (ret != 0);
Victor Stinnera62207c2010-08-07 10:57:17 +0000293
294error:
Victor Stinner398356b2010-08-18 22:23:22 +0000295 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000296 PyErr_Print();
297 return 1;
298}
299
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800300
Guido van Rossum667d7041995-08-04 04:20:48 +0000301/* Main program */
302
Eric Snow6b4be192017-05-22 21:36:03 -0700303typedef struct {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100304 wchar_t **argv;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200305 int nwarnoption; /* Number of -W command line options */
306 wchar_t **warnoptions; /* Command line -W options */
307 int nenv_warnoption; /* Number of PYTHONWARNINGS environment variables */
308 wchar_t **env_warnoptions; /* PYTHONWARNINGS environment variables */
Eric Snow6b4be192017-05-22 21:36:03 -0700309 int print_help; /* -h, -? options */
310 int print_version; /* -V option */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200311} _PyCmdline;
Eric Snow6b4be192017-05-22 21:36:03 -0700312
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800313/* Structure used by Py_Main() to pass data to subfunctions */
314typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100315 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800316 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100317 int use_bytes_argv;
318 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100319 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100320
321 /* Exit status or "exit code": result of pymain_main() */
322 int status;
323 /* Error message if a function failed */
324 _PyInitError err;
325
Victor Stinner19760862017-12-20 01:41:59 +0100326 /* non-zero is stdin is a TTY or if -i option is used */
327 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100328 int skip_first_line; /* -x option */
329 wchar_t *filename; /* Trailing arg without -c or -m */
330 wchar_t *command; /* -c argument */
331 wchar_t *module; /* -m argument */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800332} _PyMain;
333
Victor Stinner1dc6e392018-07-25 02:49:17 +0200334#define _PyMain_INIT {.err = _Py_INIT_OK()}
Victor Stinnerd5dda982017-12-13 17:31:16 +0100335/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800336
337
Victor Stinner19760862017-12-20 01:41:59 +0100338/* Non-zero if filename, command (-c) or module (-m) is set
339 on the command line */
340#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100341 (pymain->command != NULL || pymain->filename != NULL \
342 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100343
344
Victor Stinnerca719ac2017-12-20 18:00:19 +0100345static wchar_t*
346pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800347{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100348 wchar_t *str2 = _PyMem_RawWcsdup(str);
349 if (str2 == NULL) {
350 pymain->err = _Py_INIT_NO_MEMORY();
351 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800352 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100353 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800354}
355
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100356
Victor Stinnerc4bca952017-12-19 23:48:17 +0100357static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200358pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,
359 _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100360{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100361 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100362
Victor Stinnerca719ac2017-12-20 18:00:19 +0100363 if (pymain->use_bytes_argv) {
364 /* +1 for a the NULL terminator */
365 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
366 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
367 if (argv == NULL) {
368 pymain->err = _Py_INIT_NO_MEMORY();
369 return -1;
370 }
371
372 for (int i = 0; i < pymain->argc; i++) {
373 size_t len;
374 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
375 if (arg == NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +0200376 _Py_wstrlist_clear(i, argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100377 pymain->err = DECODE_LOCALE_ERR("command line arguments",
378 (Py_ssize_t)len);
379 return -1;
380 }
381 argv[i] = arg;
382 }
383 argv[pymain->argc] = NULL;
384
385 cmdline->argv = argv;
386 }
387 else {
388 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100389 }
390
Victor Stinnerca719ac2017-12-20 18:00:19 +0100391 wchar_t *program;
392 if (pymain->argc >= 1 && cmdline->argv != NULL) {
393 program = cmdline->argv[0];
394 }
395 else {
396 program = L"";
397 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200398 config->program = pymain_wstrdup(pymain, program);
399 if (config->program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100400 return -1;
401 }
402
Victor Stinnerc4bca952017-12-19 23:48:17 +0100403 return 0;
404}
405
406
407static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200408pymain_clear_cmdline(_PyMain *pymain, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100409{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100410 PyMemAllocatorEx old_alloc;
411 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100412
Victor Stinner6c785c02018-08-01 17:56:14 +0200413 _Py_wstrlist_clear(cmdline->nwarnoption, cmdline->warnoptions);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100414 cmdline->nwarnoption = 0;
415 cmdline->warnoptions = NULL;
416
Victor Stinner6c785c02018-08-01 17:56:14 +0200417 _Py_wstrlist_clear(cmdline->nenv_warnoption, cmdline->env_warnoptions);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100418 cmdline->nenv_warnoption = 0;
419 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100420
421 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +0200422 _Py_wstrlist_clear(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100423 }
424 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100425
426 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
427}
428
429
430static void
431pymain_clear_pymain(_PyMain *pymain)
432{
433#define CLEAR(ATTR) \
434 do { \
435 PyMem_RawFree(ATTR); \
436 ATTR = NULL; \
437 } while (0)
438
439 CLEAR(pymain->filename);
440 CLEAR(pymain->command);
441 CLEAR(pymain->module);
442#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100443}
444
Victor Stinnerc4bca952017-12-19 23:48:17 +0100445static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200446pymain_clear_config(_PyCoreConfig *config)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100447{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100448 /* Clear core config with the memory allocator
449 used by pymain_read_conf() */
450 PyMemAllocatorEx old_alloc;
451 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
452
Victor Stinner1dc6e392018-07-25 02:49:17 +0200453 _PyCoreConfig_Clear(config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100454
455 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
456}
457
458
459static void
Victor Stinnerd1457752018-07-26 16:04:56 +0200460pymain_free(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100461{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100462 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100463
Victor Stinnerc4bca952017-12-19 23:48:17 +0100464 /* Free global variables which cannot be freed in Py_Finalize():
465 configuration options set before Py_Initialize() which should
466 remain valid after Py_Finalize(), since
467 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinnerb1147e42018-07-21 02:06:16 +0200468 _PyPathConfig_ClearGlobal();
Victor Stinner124b9eb2018-08-29 01:29:06 +0200469 _Py_ClearStandardStreamEncoding();
Victor Stinner94540602017-12-16 04:54:22 +0100470
Victor Stinnerc4bca952017-12-19 23:48:17 +0100471 /* Force the allocator used by pymain_read_conf() */
472 PyMemAllocatorEx old_alloc;
473 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100474
Victor Stinnerca719ac2017-12-20 18:00:19 +0100475 pymain_clear_pymain(pymain);
476
Victor Stinner6c785c02018-08-01 17:56:14 +0200477 _Py_wstrlist_clear(orig_argc, orig_argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100478 orig_argc = 0;
479 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100480
Victor Stinnerc4bca952017-12-19 23:48:17 +0100481 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200482
483#ifdef __INSURE__
484 /* Insure++ is a memory analysis tool that aids in discovering
485 * memory leaks and other memory problems. On Python exit, the
486 * interned string dictionaries are flagged as being in use at exit
487 * (which it is). Under normal circumstances, this is fine because
488 * the memory will be automatically reclaimed by the system. Under
489 * memory debugging, it's a huge source of useless noise, so we
490 * trade off slower shutdown for less distraction in the memory
491 * reports. -baw
492 */
493 _Py_ReleaseInternedUnicodeStrings();
494#endif /* __INSURE__ */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800495}
496
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100497
Eric Snow6b4be192017-05-22 21:36:03 -0700498static int
Victor Stinnerd3b19192018-07-25 10:21:03 +0200499pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
Guido van Rossum667d7041995-08-04 04:20:48 +0000500{
Victor Stinnerd3b19192018-07-25 10:21:03 +0200501 PyObject *sys_path;
502 PyObject *sysdict = interp->sysdict;
503 if (sysdict != NULL) {
504 sys_path = PyDict_GetItemString(sysdict, "path");
505 }
506 else {
507 sys_path = NULL;
508 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800509 if (sys_path == NULL) {
510 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
511 goto error;
512 }
513
Victor Stinnerd3b19192018-07-25 10:21:03 +0200514 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800515 goto error;
516 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200517 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800518
519error:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800520 PyErr_Print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200521 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800522}
523
524
Victor Stinnerb1147e42018-07-21 02:06:16 +0200525_PyInitError
526_Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800527{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200528 if (*len == INT_MAX) {
529 /* len+1 would overflow */
530 return _Py_INIT_NO_MEMORY();
531 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100532 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800533 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100534 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800535 }
536
Victor Stinnerca719ac2017-12-20 18:00:19 +0100537 size_t size = (*len + 1) * sizeof(list[0]);
538 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
539 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800540 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100541 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800542 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100543 list2[*len] = str2;
544 *list = list2;
545 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100546 return _Py_INIT_OK();
547}
548
549
550static int
551pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
552{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200553 _PyInitError err = _Py_wstrlist_append(len, list, str);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100554 if (_Py_INIT_FAILED(err)) {
555 pymain->err = err;
556 return -1;
557 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800558 return 0;
559}
560
561
562/* Parse the command line arguments
563 Return 0 on success.
564 Return 1 if parsing failed.
565 Set pymain->err and return -1 on other errors. */
566static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200567pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
568 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800569{
Antoine Pitrou86838b02012-02-21 19:03:47 +0100570 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800571 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800572 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100573 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800574 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800575 if (c == EOF) {
576 break;
577 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 /* -c is the last option; following arguments
581 that look like options are left for the
582 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800583 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
584 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
585 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100586 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800587 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800588 }
Victor Stinner58d16832018-05-31 15:09:28 +0200589 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 command[len - 2] = '\n';
591 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100592 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 break;
594 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 if (c == 'm') {
597 /* -m is the last option; following arguments
598 that look like options are left for the
599 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100600 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
601 if (pymain->module == NULL) {
602 return -1;
603 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 break;
605 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800608 case 0:
609 // Handle long option.
610 assert(longindex == 0); // Only one long option now.
611 if (!wcscmp(_PyOS_optarg, L"always")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200612 config->_check_hash_pycs_mode = "always";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800613 } else if (!wcscmp(_PyOS_optarg, L"never")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200614 config->_check_hash_pycs_mode = "never";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800615 } else if (!wcscmp(_PyOS_optarg, L"default")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200616 config->_check_hash_pycs_mode = "default";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800617 } else {
618 fprintf(stderr, "--check-hash-based-pycs must be one of "
619 "'default', 'always', or 'never'\n");
620 return 1;
621 }
622 break;
623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 case 'b':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200625 config->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 case 'd':
Victor Stinner98512272018-08-01 03:07:00 +0200629 config->parser_debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 case 'i':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200633 config->inspect++;
634 config->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000636
Christian Heimesad73a9c2013-08-10 16:36:18 +0200637 case 'I':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200638 config->isolated++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200639 break;
640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 case 'O':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200644 config->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 case 'B':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200648 config->write_bytecode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 case 's':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200652 config->user_site_directory = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 case 'S':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200656 config->site_import = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 case 'E':
Victor Stinnerd1457752018-07-26 16:04:56 +0200660 config->use_environment = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 case 't':
664 /* ignored for backwards compatibility */
665 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 case 'u':
Victor Stinner98512272018-08-01 03:07:00 +0200668 config->buffered_stdio = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 case 'v':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200672 config->verbose++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100676 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 case 'h':
680 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -0700681 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -0700685 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100689 if (pymain_wstrlist_append(pymain,
690 &cmdline->nwarnoption,
691 &cmdline->warnoptions,
692 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800693 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800694 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000696
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000697 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100698 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +0100699 &config->nxoption,
700 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +0100701 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800702 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800703 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000704 break;
705
Georg Brandl9d871192010-12-04 10:47:18 +0000706 case 'q':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200707 config->quiet++;
Georg Brandl9d871192010-12-04 10:47:18 +0000708 break;
709
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100710 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100711 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100712 break;
713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800717 /* unknown argument: parsing failed */
718 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800720 } while (1);
721
Victor Stinnerca719ac2017-12-20 18:00:19 +0100722 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800723 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +0100724 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800725 {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100726 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
727 if (pymain->filename == NULL) {
728 return -1;
729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000731
Victor Stinnerd5dda982017-12-13 17:31:16 +0100732 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100733 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +0100734
Eric Snow6b4be192017-05-22 21:36:03 -0700735 return 0;
736}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000737
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800738
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800739static int
Victor Stinner9cfc0022017-12-20 19:36:46 +0100740add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +0100741{
742 PyObject *name, *value;
743
744 const wchar_t *name_end = wcschr(s, L'=');
745 if (!name_end) {
746 name = PyUnicode_FromWideChar(s, -1);
747 value = Py_True;
748 Py_INCREF(value);
749 }
750 else {
751 name = PyUnicode_FromWideChar(s, name_end - s);
752 value = PyUnicode_FromWideChar(name_end + 1, -1);
753 }
754 if (name == NULL || value == NULL) {
755 goto error;
756 }
757 if (PyDict_SetItem(opts, name, value) < 0) {
758 goto error;
759 }
760 Py_DECREF(name);
761 Py_DECREF(value);
762 return 0;
763
764error:
765 Py_XDECREF(name);
766 Py_XDECREF(value);
767 return -1;
768}
769
Victor Stinner9cfc0022017-12-20 19:36:46 +0100770
771static PyObject*
772config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800773{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100774 int nxoption = config->nxoption;
775 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +0100776 PyObject *dict = PyDict_New();
777 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100778 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +0100779 }
780
Victor Stinnerca719ac2017-12-20 18:00:19 +0100781 for (int i=0; i < nxoption; i++) {
782 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +0100783 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +0100784 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100785 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800786 }
787 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100788
Victor Stinner9cfc0022017-12-20 19:36:46 +0100789 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -0700790}
791
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800792
Victor Stinner9cfc0022017-12-20 19:36:46 +0100793static _PyInitError
794config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -0700795{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100796 for (int i = 0; i < len; i++) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200797 _PyInitError err = _Py_wstrlist_append(&config->nwarnoption,
798 &config->warnoptions,
799 options[i]);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100800 if (_Py_INIT_FAILED(err)) {
801 return err;
Eric Snow6b4be192017-05-22 21:36:03 -0700802 }
Eric Snow6b4be192017-05-22 21:36:03 -0700803 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100804 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800805}
Eric Snow6b4be192017-05-22 21:36:03 -0700806
Victor Stinner747f48e2017-12-12 22:59:48 +0100807
Victor Stinner9cfc0022017-12-20 19:36:46 +0100808static _PyInitError
Victor Stinner1dc6e392018-07-25 02:49:17 +0200809config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +0100810{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100811 _PyInitError err;
812
813 assert(config->nwarnoption == 0);
814
Victor Stinner747f48e2017-12-12 22:59:48 +0100815 /* The priority order for warnings configuration is (highest precedence
816 * first):
817 *
818 * - the BytesWarning filter, if needed ('-b', '-bb')
819 * - any '-W' command line options; then
820 * - the 'PYTHONWARNINGS' environment variable; then
821 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
822 * - any implicit filters added by _warnings.c/warnings.py
823 *
824 * All settings except the last are passed to the warnings module via
825 * the `sys.warnoptions` list. Since the warnings module works on the basis
826 * of "the most recently added filter will be checked first", we add
827 * the lowest precedence entries first so that later entries override them.
828 */
829
Victor Stinner9cfc0022017-12-20 19:36:46 +0100830 if (config->dev_mode) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200831 err = _Py_wstrlist_append(&config->nwarnoption,
832 &config->warnoptions,
833 L"default");
Victor Stinner9cfc0022017-12-20 19:36:46 +0100834 if (_Py_INIT_FAILED(err)) {
835 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100836 }
Victor Stinner747f48e2017-12-12 22:59:48 +0100837 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100838
Victor Stinner9cfc0022017-12-20 19:36:46 +0100839 err = config_add_warnings_optlist(config,
840 cmdline->nenv_warnoption,
841 cmdline->env_warnoptions);
842 if (_Py_INIT_FAILED(err)) {
843 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100844 }
845
Victor Stinner9cfc0022017-12-20 19:36:46 +0100846 err = config_add_warnings_optlist(config,
847 cmdline->nwarnoption,
848 cmdline->warnoptions);
849 if (_Py_INIT_FAILED(err)) {
850 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100851 }
852
853 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
854 * don't even try to emit a warning, so we skip setting the filter in that
855 * case.
856 */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200857 if (config->bytes_warning) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100858 wchar_t *filter;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200859 if (config->bytes_warning> 1) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100860 filter = L"error::BytesWarning";
861 }
862 else {
863 filter = L"default::BytesWarning";
864 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200865 err = _Py_wstrlist_append(&config->nwarnoption,
866 &config->warnoptions,
867 filter);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100868 if (_Py_INIT_FAILED(err)) {
869 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100870 }
871 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100872 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +0100873}
874
875
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800876/* Get warning options from PYTHONWARNINGS environment variable.
877 Return 0 on success.
878 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +0100879static _PyInitError
Victor Stinnerd1457752018-07-26 16:04:56 +0200880cmdline_init_env_warnoptions(_PyMain *pymain, const _PyCoreConfig *config,
881 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800882{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100883 wchar_t *env;
Victor Stinner6c785c02018-08-01 17:56:14 +0200884 int res = _PyCoreConfig_GetEnvDup(config, &env,
885 L"PYTHONWARNINGS", "PYTHONWARNINGS");
Victor Stinnerca719ac2017-12-20 18:00:19 +0100886 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100887 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100888 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800889
Victor Stinnerca719ac2017-12-20 18:00:19 +0100890 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100891 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +0100892 }
Philip Jenvey0805ca32010-04-07 04:04:10 +0000893
Victor Stinnerca719ac2017-12-20 18:00:19 +0100894
895 wchar_t *warning, *context = NULL;
896 for (warning = WCSTOK(env, L",", &context);
897 warning != NULL;
898 warning = WCSTOK(NULL, L",", &context))
899 {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200900 _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption,
901 &cmdline->env_warnoptions,
902 warning);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100903 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100904 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100905 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800906 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100908 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100909 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800910}
911
912
913static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200914pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800915{
916 pymain->stdin_is_interactive = (isatty(fileno(stdin))
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200917 || config->interactive);
Guido van Rossum775af911997-02-14 19:50:32 +0000918
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000919#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +0000920 /* don't translate newlines (\r\n <=> \n) */
921 _setmode(fileno(stdin), O_BINARY);
922 _setmode(fileno(stdout), O_BINARY);
923 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000924#endif
Victor Stinner89e34362011-01-07 18:47:22 +0000925
Victor Stinner98512272018-08-01 03:07:00 +0200926 if (!config->buffered_stdio) {
Guido van Rossum22ffac11998-03-06 15:30:39 +0000927#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
929 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
930 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000931#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 setbuf(stdin, (char *)NULL);
933 setbuf(stdout, (char *)NULL);
934 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000935#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200937 else if (config->interactive) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000938#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 /* Doesn't have to have line-buffered -- use unbuffered */
940 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
941 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000942#else /* !MS_WINDOWS */
943#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
945 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000946#endif /* HAVE_SETVBUF */
947#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 /* Leave stderr alone - it should be unbuffered anyway. */
949 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800950}
Guido van Rossum667d7041995-08-04 04:20:48 +0000951
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800952
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800953static void
Victor Stinnerfbca9082018-08-30 00:50:45 +0200954pymain_header(_PyMain *pymain, const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800955{
Victor Stinnerfbca9082018-08-30 00:50:45 +0200956 if (config->quiet) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800957 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000959
Victor Stinnerfbca9082018-08-30 00:50:45 +0200960 if (!config->verbose && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800961 return;
962 }
963
964 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
Victor Stinnerfbca9082018-08-30 00:50:45 +0200965 if (config->site_import) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800966 fprintf(stderr, "%s\n", COPYRIGHT);
967 }
968}
969
970
Victor Stinnerc4bca952017-12-19 23:48:17 +0100971static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200972pymain_init_core_argv(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100973{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100974 /* Copy argv to be able to modify it (to force -c/-m) */
975 int argc = pymain->argc - _PyOS_optind;
976 wchar_t **argv;
977
978 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100979 /* Ensure at least one (empty) argument is seen */
980 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +0100981 argc = 1;
Victor Stinner6c785c02018-08-01 17:56:14 +0200982 argv = _Py_wstrlist_copy(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +0100983 }
Victor Stinnerc4bca952017-12-19 23:48:17 +0100984 else {
Victor Stinner6c785c02018-08-01 17:56:14 +0200985 argv = _Py_wstrlist_copy(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100986 }
987
988 if (argv == NULL) {
989 pymain->err = _Py_INIT_NO_MEMORY();
990 return -1;
991 }
992
993 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100994 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100995 /* Force sys.argv[0] = '-c' */
996 arg0 = L"-c";
997 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100998 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100999 /* Force sys.argv[0] = '-m'*/
1000 arg0 = L"-m";
1001 }
1002 if (arg0 != NULL) {
1003 arg0 = _PyMem_RawWcsdup(arg0);
1004 if (arg0 == NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +02001005 _Py_wstrlist_clear(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001006 pymain->err = _Py_INIT_NO_MEMORY();
1007 return -1;
1008 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001009
1010 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001011 PyMem_RawFree(argv[0]);
1012 argv[0] = arg0;
1013 }
1014
Victor Stinner1dc6e392018-07-25 02:49:17 +02001015 config->argc = argc;
1016 config->argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001017 return 0;
1018}
1019
1020
Victor Stinner8ded5b82018-01-24 17:03:28 +01001021static PyObject*
1022wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001023{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001024 assert(list != NULL || len < 1);
1025
1026 PyObject *pylist = PyList_New(len);
1027 if (pylist == NULL) {
1028 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001029 }
1030
Victor Stinner8ded5b82018-01-24 17:03:28 +01001031 for (int i = 0; i < len; i++) {
1032 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001033 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001034 Py_DECREF(pylist);
1035 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001036 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001037 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001038 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001039 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001040}
1041
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001042
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001043static void
Victor Stinnerfbca9082018-08-30 00:50:45 +02001044pymain_import_readline(_PyMain *pymain, const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001045{
Victor Stinnerfbca9082018-08-30 00:50:45 +02001046 if (config->isolated) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001047 return;
1048 }
Victor Stinnerfbca9082018-08-30 00:50:45 +02001049 if (!config->inspect && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001050 return;
1051 }
1052 if (!isatty(fileno(stdin))) {
1053 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001054 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001055
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001056 PyObject *mod = PyImport_ImportModule("readline");
1057 if (mod == NULL) {
1058 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 }
1060 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001061 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001063}
1064
1065
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001066static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001067pymain_run_startup(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001068{
Victor Stinner6c785c02018-08-01 17:56:14 +02001069 const char *startup = _PyCoreConfig_GetEnv(config, "PYTHONSTARTUP");
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001070 if (startup == NULL) {
1071 return;
1072 }
1073
1074 FILE *fp = _Py_fopen(startup, "r");
1075 if (fp == NULL) {
1076 int save_errno = errno;
1077 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
1078 errno = save_errno;
1079
1080 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
1081 startup);
1082 PyErr_Print();
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001083 return;
1084 }
1085
1086 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
1087 PyErr_Clear();
1088 fclose(fp);
1089}
1090
1091
1092static void
Victor Stinner72ec3192018-08-02 19:34:20 +02001093pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001094{
Victor Stinner72ec3192018-08-02 19:34:20 +02001095 const wchar_t *filename = pymain->filename;
1096 FILE *fp = _Py_wfopen(filename, L"r");
1097 if (fp == NULL) {
1098 char *cfilename_buffer;
1099 const char *cfilename;
1100 int err = errno;
1101 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
1102 if (cfilename_buffer != NULL)
1103 cfilename = cfilename_buffer;
1104 else
1105 cfilename = "<unprintable file name>";
1106 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
1107 config->program, cfilename, err, strerror(err));
1108 PyMem_RawFree(cfilename_buffer);
1109 pymain->status = 2;
1110 return;
1111 }
1112
1113 if (pymain->skip_first_line) {
1114 int ch;
1115 /* Push back first newline so line numbers remain the same */
1116 while ((ch = getc(fp)) != EOF) {
1117 if (ch == '\n') {
1118 (void)ungetc(ch, fp);
1119 break;
1120 }
1121 }
1122 }
1123
1124 struct _Py_stat_struct sb;
1125 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
1126 fprintf(stderr,
1127 "%ls: '%ls' is a directory, cannot continue\n",
1128 config->program, filename);
1129 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001130 fclose(fp);
1131 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001132 }
1133
1134 /* call pending calls like signal handlers (SIGINT) */
1135 if (Py_MakePendingCalls() == -1) {
1136 PyErr_Print();
1137 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001138 fclose(fp);
1139 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001140 }
1141
1142 PyObject *unicode, *bytes = NULL;
1143 const char *filename_str;
1144
1145 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
1146 if (unicode != NULL) {
1147 bytes = PyUnicode_EncodeFSDefault(unicode);
1148 Py_DECREF(unicode);
1149 }
1150 if (bytes != NULL) {
1151 filename_str = PyBytes_AsString(bytes);
1152 }
1153 else {
1154 PyErr_Clear();
1155 filename_str = "<filename encoding error>";
1156 }
1157
Victor Stinnerd8078622018-08-03 23:54:06 +02001158 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
1159 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
Victor Stinner72ec3192018-08-02 19:34:20 +02001160 Py_XDECREF(bytes);
1161 pymain->status = (run != 0);
Victor Stinner72ec3192018-08-02 19:34:20 +02001162}
1163
1164
1165static void
1166pymain_run_stdin(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
1167{
1168 if (pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001169 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinnera4d20b22018-08-01 02:57:45 +02001170 config->inspect = 0;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001171 pymain_run_startup(pymain, config, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001172 pymain_run_interactive_hook();
1173 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001174
Victor Stinner72ec3192018-08-02 19:34:20 +02001175 /* call pending calls like signal handlers (SIGINT) */
1176 if (Py_MakePendingCalls() == -1) {
1177 PyErr_Print();
1178 pymain->status = 1;
1179 return;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001180 }
1181
Victor Stinner72ec3192018-08-02 19:34:20 +02001182 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
1183 pymain->status = (run != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001184}
1185
1186
1187static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001188pymain_repl(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001191 opportunity to set it from Python. */
Victor Stinner6c785c02018-08-01 17:56:14 +02001192 if (!Py_InspectFlag && _PyCoreConfig_GetEnv(config, "PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 Py_InspectFlag = 1;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001194 config->inspect = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001196
Victor Stinner19760862017-12-20 01:41:59 +01001197 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001198 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001200
1201 Py_InspectFlag = 0;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001202 config->inspect = 0;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001203 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001204
Victor Stinner19760862017-12-20 01:41:59 +01001205 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001206 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001207}
1208
1209
1210/* Parse the command line.
1211 Handle --version and --help options directly.
1212
1213 Return 1 if Python must exit.
1214 Return 0 on success.
1215 Set pymain->err and return -1 on failure. */
1216static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001217pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config,
1218 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001219{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001220 int res = pymain_parse_cmdline_impl(pymain, config, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001221 if (res < 0) {
1222 return -1;
1223 }
1224 if (res) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001225 pymain_usage(1, config->program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001226 pymain->status = 2;
1227 return 1;
1228 }
1229
Victor Stinnerca719ac2017-12-20 18:00:19 +01001230 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001231 /* Backup _PyOS_optind */
1232 _PyOS_optind--;
1233 }
1234
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001235 return 0;
1236}
1237
1238
Victor Stinnera7368ac2017-11-15 18:11:45 -08001239/* Parse command line options and environment variables.
1240 This code must not use Python runtime apart PyMem_Raw memory allocator.
1241
1242 Return 0 on success.
1243 Return 1 if Python is done and must exit.
1244 Set pymain->err and return -1 on error. */
1245static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001246pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
1247 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001248{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001249 _PyInitError err;
1250
Victor Stinner1dc6e392018-07-25 02:49:17 +02001251 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01001252 if (res != 0) {
1253 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001254 }
1255
Victor Stinner1dc6e392018-07-25 02:49:17 +02001256 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001257 return -1;
1258 }
1259
Victor Stinnerd1457752018-07-26 16:04:56 +02001260 err = _PyCoreConfig_Read(config);
1261 if (_Py_INIT_FAILED(err)) {
1262 pymain->err = err;
1263 return -1;
1264 }
1265
Victor Stinnerd1457752018-07-26 16:04:56 +02001266 if (config->use_environment) {
Victor Stinnerecf411c2018-07-26 02:37:22 +02001267 err = cmdline_init_env_warnoptions(pymain, config, cmdline);
1268 if (_Py_INIT_FAILED(err)) {
1269 pymain->err = err;
1270 return -1;
1271 }
1272 }
1273
Victor Stinnerd1457752018-07-26 16:04:56 +02001274 err = config_init_warnoptions(config, cmdline);
Victor Stinner31a83932017-12-04 13:39:15 +01001275 if (_Py_INIT_FAILED(err)) {
1276 pymain->err = err;
1277 return -1;
1278 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001279 return 0;
1280}
1281
1282
Victor Stinner177d9212018-08-29 11:25:15 +02001283/* Read the configuration and initialize the LC_CTYPE locale:
1284 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538). */
Victor Stinnera7368ac2017-11-15 18:11:45 -08001285static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001286pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
1287 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001288{
Victor Stinner89487f52018-08-23 12:23:46 +02001289 int init_utf8_mode = Py_UTF8Mode;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001290#ifdef MS_WINDOWS
1291 int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
1292#endif
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001293 _PyCoreConfig save_config = _PyCoreConfig_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001294 int res = -1;
1295
Victor Stinner177d9212018-08-29 11:25:15 +02001296 /* Set LC_CTYPE to the user preferred locale */
1297 _Py_SetLocaleFromEnv(LC_CTYPE);
Victor Stinner94540602017-12-16 04:54:22 +01001298
1299 int locale_coerced = 0;
1300 int loops = 0;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001301
1302 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
1303 pymain->err = _Py_INIT_NO_MEMORY();
1304 goto done;
1305 }
Victor Stinner94540602017-12-16 04:54:22 +01001306
1307 while (1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001308 int utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01001309 int encoding_changed = 0;
1310
1311 /* Watchdog to prevent an infinite loop */
1312 loops++;
1313 if (loops == 3) {
1314 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
1315 "reading the configuration");
1316 goto done;
1317 }
1318
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001319 /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
1320 on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
Victor Stinner89487f52018-08-23 12:23:46 +02001321 Py_UTF8Mode = config->utf8_mode;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001322#ifdef MS_WINDOWS
1323 Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
1324#endif
Victor Stinner89487f52018-08-23 12:23:46 +02001325
Victor Stinner1dc6e392018-07-25 02:49:17 +02001326 if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001327 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01001328 }
1329
Victor Stinner1dc6e392018-07-25 02:49:17 +02001330 int conf_res = pymain_read_conf_impl(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001331 if (conf_res != 0) {
1332 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01001333 goto done;
1334 }
1335
1336 /* The legacy C locale assumes ASCII as the default text encoding, which
1337 * causes problems not only for the CPython runtime, but also other
1338 * components like GNU readline.
1339 *
1340 * Accordingly, when the CLI detects it, it attempts to coerce it to a
1341 * more capable UTF-8 based alternative.
1342 *
1343 * See the documentation of the PYTHONCOERCECLOCALE setting for more
1344 * details.
1345 */
Victor Stinner2c8ddcf2018-08-29 00:16:53 +02001346 if (config->coerce_c_locale && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01001347 locale_coerced = 1;
Victor Stinnerb2457ef2018-08-29 13:25:36 +02001348 _Py_CoerceLegacyLocale(config->coerce_c_locale_warn);
Victor Stinner94540602017-12-16 04:54:22 +01001349 encoding_changed = 1;
1350 }
1351
1352 if (utf8_mode == -1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001353 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01001354 /* UTF-8 Mode enabled */
1355 encoding_changed = 1;
1356 }
1357 }
1358 else {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001359 if (config->utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01001360 encoding_changed = 1;
1361 }
1362 }
1363
1364 if (!encoding_changed) {
1365 break;
1366 }
1367
Victor Stinnerd1457752018-07-26 16:04:56 +02001368 /* Reset the configuration before reading again the configuration,
1369 just keep UTF-8 Mode value. */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001370 int new_utf8_mode = config->utf8_mode;
Victor Stinner2c8ddcf2018-08-29 00:16:53 +02001371 int new_coerce_c_locale = config->coerce_c_locale;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001372 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
1373 pymain->err = _Py_INIT_NO_MEMORY();
1374 goto done;
1375 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001376 pymain_clear_cmdline(pymain, cmdline);
Victor Stinner6c5a4b32018-06-16 00:06:28 +02001377 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner1dc6e392018-07-25 02:49:17 +02001378 config->utf8_mode = new_utf8_mode;
Victor Stinner2c8ddcf2018-08-29 00:16:53 +02001379 config->coerce_c_locale = new_coerce_c_locale;
Victor Stinner94540602017-12-16 04:54:22 +01001380
1381 /* The encoding changed: read again the configuration
1382 with the new encoding */
1383 }
1384 res = 0;
1385
1386done:
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001387 _PyCoreConfig_Clear(&save_config);
Victor Stinner89487f52018-08-23 12:23:46 +02001388 Py_UTF8Mode = init_utf8_mode ;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001389#ifdef MS_WINDOWS
1390 Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;
1391#endif
Victor Stinnera7368ac2017-11-15 18:11:45 -08001392 return res;
1393}
1394
Victor Stinner91106cd2017-12-13 12:29:09 +01001395
Victor Stinnerda273412017-12-15 01:46:02 +01001396void
Victor Stinnerda273412017-12-15 01:46:02 +01001397_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
1398{
1399 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01001400 Py_CLEAR(config->executable);
1401 Py_CLEAR(config->prefix);
1402 Py_CLEAR(config->base_prefix);
1403 Py_CLEAR(config->exec_prefix);
1404 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001405 Py_CLEAR(config->warnoptions);
1406 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01001407 Py_CLEAR(config->module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06001408 Py_CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001409}
1410
1411
1412static PyObject*
1413config_copy_attr(PyObject *obj)
1414{
1415 if (PyUnicode_Check(obj)) {
1416 Py_INCREF(obj);
1417 return obj;
1418 }
1419 else if (PyList_Check(obj)) {
1420 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
1421 }
1422 else if (PyDict_Check(obj)) {
1423 /* The dict type is used for xoptions. Make the assumption that keys
1424 and values are immutables */
1425 return PyDict_Copy(obj);
1426 }
1427 else {
1428 PyErr_Format(PyExc_TypeError,
1429 "cannot copy config attribute of type %.200s",
1430 Py_TYPE(obj)->tp_name);
1431 return NULL;
1432 }
1433}
1434
1435
1436int
1437_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
1438 const _PyMainInterpreterConfig *config2)
1439{
1440 _PyMainInterpreterConfig_Clear(config);
1441
1442#define COPY_ATTR(ATTR) \
1443 do { \
1444 if (config2->ATTR != NULL) { \
1445 config->ATTR = config_copy_attr(config2->ATTR); \
1446 if (config->ATTR == NULL) { \
1447 return -1; \
1448 } \
1449 } \
1450 } while (0)
1451
1452 COPY_ATTR(argv);
Victor Stinner41264f12017-12-15 02:05:29 +01001453 COPY_ATTR(executable);
1454 COPY_ATTR(prefix);
1455 COPY_ATTR(base_prefix);
1456 COPY_ATTR(exec_prefix);
1457 COPY_ATTR(base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001458 COPY_ATTR(warnoptions);
1459 COPY_ATTR(xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01001460 COPY_ATTR(module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06001461 COPY_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001462#undef COPY_ATTR
1463 return 0;
1464}
1465
1466
Victor Stinner41264f12017-12-15 02:05:29 +01001467_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01001468_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
1469 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001470{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001471 if (main_config->install_signal_handlers < 0) {
1472 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001473 }
1474
Victor Stinner9cfc0022017-12-20 19:36:46 +01001475 if (main_config->xoptions == NULL) {
1476 main_config->xoptions = config_create_xoptions_dict(config);
1477 if (main_config->xoptions == NULL) {
1478 return _Py_INIT_NO_MEMORY();
1479 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001480 }
1481
Victor Stinner8ded5b82018-01-24 17:03:28 +01001482#define COPY_WSTR(ATTR) \
1483 do { \
Victor Stinner1dc6e392018-07-25 02:49:17 +02001484 if (main_config->ATTR == NULL && config->ATTR != NULL) { \
Victor Stinner8ded5b82018-01-24 17:03:28 +01001485 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
1486 if (main_config->ATTR == NULL) { \
1487 return _Py_INIT_NO_MEMORY(); \
1488 } \
1489 } \
1490 } while (0)
1491#define COPY_WSTRLIST(ATTR, LEN, LIST) \
1492 do { \
1493 if (ATTR == NULL) { \
1494 ATTR = wstrlist_as_pylist(LEN, LIST); \
1495 if (ATTR == NULL) { \
1496 return _Py_INIT_NO_MEMORY(); \
1497 } \
1498 } \
1499 } while (0)
1500
1501 COPY_WSTRLIST(main_config->warnoptions,
1502 config->nwarnoption, config->warnoptions);
1503 if (config->argc >= 0) {
1504 COPY_WSTRLIST(main_config->argv,
1505 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001506 }
1507
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001508 if (config->_install_importlib) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001509 COPY_WSTR(executable);
1510 COPY_WSTR(prefix);
1511 COPY_WSTR(base_prefix);
1512 COPY_WSTR(exec_prefix);
1513 COPY_WSTR(base_exec_prefix);
1514
1515 COPY_WSTRLIST(main_config->module_search_path,
1516 config->nmodule_search_path, config->module_search_paths);
Carl Meyerb193fa92018-06-15 22:40:56 -06001517
1518 if (config->pycache_prefix != NULL) {
1519 COPY_WSTR(pycache_prefix);
1520 } else {
1521 main_config->pycache_prefix = NULL;
1522 }
1523
Victor Stinner9cfc0022017-12-20 19:36:46 +01001524 }
Victor Stinner41264f12017-12-15 02:05:29 +01001525
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001526 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01001527#undef COPY_WSTR
1528#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001529}
1530
1531
1532static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001533pymain_init_python_main(_PyMain *pymain, _PyCoreConfig *config,
1534 PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001535{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001536 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001537
Victor Stinner9cfc0022017-12-20 19:36:46 +01001538 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001539 err = _PyMainInterpreterConfig_Read(&main_config, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001540 if (!_Py_INIT_FAILED(err)) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001541 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001542 }
1543 _PyMainInterpreterConfig_Clear(&main_config);
1544
1545 if (_Py_INIT_FAILED(err)) {
1546 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001547 return -1;
1548 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001549 return 0;
1550}
Victor Stinnera7368ac2017-11-15 18:11:45 -08001551
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001552
1553static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02001554pymain_run_python(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001555{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001556 int res = 0;
1557 _PyCoreConfig *config = &interp->core_config;
1558
1559 PyObject *main_importer_path = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001560 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01001561 /* If filename is a package (ex: directory or ZIP file) which contains
1562 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +02001563 prepended to sys.path.
1564
1565 Otherwise, main_importer_path is set to NULL. */
1566 main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01001567 }
1568
Victor Stinnerd3b19192018-07-25 10:21:03 +02001569 if (main_importer_path != NULL) {
1570 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
1571 pymain->status = 1;
1572 goto done;
1573 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001574 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001575 else if (!config->isolated) {
1576 PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc,
1577 config->argv);
1578 if (path0 == NULL) {
1579 pymain->err = _Py_INIT_NO_MEMORY();
1580 res = -1;
1581 goto done;
1582 }
Victor Stinner19760862017-12-20 01:41:59 +01001583
Victor Stinnerd3b19192018-07-25 10:21:03 +02001584 if (pymain_sys_path_add_path0(interp, path0) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001585 Py_DECREF(path0);
Victor Stinnerd3b19192018-07-25 10:21:03 +02001586 pymain->status = 1;
1587 goto done;
Victor Stinner19760862017-12-20 01:41:59 +01001588 }
1589 Py_DECREF(path0);
1590 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001591
Victor Stinner19760862017-12-20 01:41:59 +01001592 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08001593
Victor Stinnerfbca9082018-08-30 00:50:45 +02001594 pymain_header(pymain, config);
1595 pymain_import_readline(pymain, config);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001596
Victor Stinnerca719ac2017-12-20 18:00:19 +01001597 if (pymain->command) {
1598 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001599 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001600 else if (pymain->module) {
1601 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001602 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001603 else if (main_importer_path != NULL) {
1604 int sts = pymain_run_module(L"__main__", 0);
1605 pymain->status = (sts != 0);
1606 }
Victor Stinner72ec3192018-08-02 19:34:20 +02001607 else if (pymain->filename != NULL) {
1608 pymain_run_file(pymain, config, &cf);
1609 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001610 else {
Victor Stinner72ec3192018-08-02 19:34:20 +02001611 pymain_run_stdin(pymain, config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001612 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001613
Victor Stinner1dc6e392018-07-25 02:49:17 +02001614 pymain_repl(pymain, config, &cf);
Victor Stinnerd3b19192018-07-25 10:21:03 +02001615
1616done:
1617 Py_XDECREF(main_importer_path);
1618 return res;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001619}
1620
Victor Stinnera7368ac2017-11-15 18:11:45 -08001621
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001622static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001623pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
1624 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001625{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001626 pymain->err = _PyRuntime_Initialize();
1627 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001628 return -1;
1629 }
1630
Victor Stinner1dc6e392018-07-25 02:49:17 +02001631 int res = pymain_read_conf(pymain, config, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001632 if (res < 0) {
1633 return -1;
1634 }
1635 if (res > 0) {
1636 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01001637 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001638 }
1639
Victor Stinner94540602017-12-16 04:54:22 +01001640 if (cmdline->print_help) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001641 pymain_usage(0, config->program);
Victor Stinner19760862017-12-20 01:41:59 +01001642 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01001643 }
1644
1645 if (cmdline->print_version) {
1646 printf("Python %s\n",
1647 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01001648 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01001649 }
1650
Victor Stinnerc4bca952017-12-19 23:48:17 +01001651 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinner6c785c02018-08-01 17:56:14 +02001652 orig_argv = _Py_wstrlist_copy(pymain->argc, cmdline->argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001653 if (orig_argv == NULL) {
1654 pymain->err = _Py_INIT_NO_MEMORY();
1655 return -1;
1656 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001657 orig_argc = pymain->argc;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001658 return 0;
1659}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00001660
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001661
Victor Stinnerca719ac2017-12-20 18:00:19 +01001662/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
1663 LC_CTYPE locale and Py_DecodeLocale().
1664
1665 Configuration:
1666
1667 * Command line arguments
1668 * Environment variables
1669 * Py_xxx global configuration variables
1670
Victor Stinner1dc6e392018-07-25 02:49:17 +02001671 _PyCmdline is a temporary structure used to prioritize these
Victor Stinnerca719ac2017-12-20 18:00:19 +01001672 variables. */
1673static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001674pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerca719ac2017-12-20 18:00:19 +01001675{
Victor Stinner31e99082017-12-20 23:41:38 +01001676 /* Force default allocator, since pymain_free() and pymain_clear_config()
1677 must use the same allocator than this function. */
1678 PyMemAllocatorEx old_alloc;
1679 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
1680#ifdef Py_DEBUG
1681 PyMemAllocatorEx default_alloc;
1682 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
1683#endif
1684
Victor Stinner1dc6e392018-07-25 02:49:17 +02001685 _PyCmdline cmdline;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001686 memset(&cmdline, 0, sizeof(cmdline));
1687
Victor Stinner1dc6e392018-07-25 02:49:17 +02001688 int res = pymain_cmdline_impl(pymain, config, &cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001689
Victor Stinnerca719ac2017-12-20 18:00:19 +01001690 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01001691
1692#ifdef Py_DEBUG
1693 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
1694 PyMemAllocatorEx cur_alloc;
1695 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
1696 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
1697#endif
1698 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001699 return res;
1700}
1701
1702
Victor Stinner94540602017-12-16 04:54:22 +01001703static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02001704pymain_init(_PyMain *pymain, PyInterpreterState **interp_p)
Victor Stinner94540602017-12-16 04:54:22 +01001705{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001706 /* 754 requires that FP exceptions run in "no stop" mode by default,
1707 * and until C vendors implement C99's ways to control FP exceptions,
1708 * Python requires non-stop mode. Alas, some platforms enable FP
1709 * exceptions by default. Here we disable them.
1710 */
1711#ifdef __FreeBSD__
1712 fedisableexcept(FE_OVERFLOW);
1713#endif
Victor Stinner94540602017-12-16 04:54:22 +01001714
Victor Stinner1dc6e392018-07-25 02:49:17 +02001715 _PyCoreConfig local_config = _PyCoreConfig_INIT;
1716 _PyCoreConfig *config = &local_config;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001717
Victor Stinner1dc6e392018-07-25 02:49:17 +02001718 _PyCoreConfig_GetGlobalConfig(config);
1719
1720 int cmd_res = pymain_cmdline(pymain, config);
1721 if (cmd_res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01001722 _Py_FatalInitError(pymain->err);
1723 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001724 if (cmd_res == 1) {
1725 pymain_clear_config(config);
1726 return 1;
Victor Stinner19760862017-12-20 01:41:59 +01001727 }
1728
Victor Stinner1dc6e392018-07-25 02:49:17 +02001729 _PyCoreConfig_SetGlobalConfig(config);
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001730
Victor Stinner1dc6e392018-07-25 02:49:17 +02001731 pymain_init_stdio(pymain, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001732
Victor Stinner1dc6e392018-07-25 02:49:17 +02001733 PyInterpreterState *interp;
1734 pymain->err = _Py_InitializeCore(&interp, config);
1735 if (_Py_INIT_FAILED(pymain->err)) {
1736 _Py_FatalInitError(pymain->err);
1737 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001738 *interp_p = interp;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001739
1740 pymain_clear_config(config);
1741 config = &interp->core_config;
1742
1743 if (pymain_init_python_main(pymain, config, interp) < 0) {
1744 _Py_FatalInitError(pymain->err);
1745 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001746 return 0;
1747}
1748
1749
1750static int
1751pymain_main(_PyMain *pymain)
1752{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001753 PyInterpreterState *interp;
1754 int res = pymain_init(pymain, &interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +02001755 if (res != 1) {
Victor Stinnerd3b19192018-07-25 10:21:03 +02001756 if (pymain_run_python(pymain, interp) < 0) {
1757 _Py_FatalInitError(pymain->err);
1758 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001759
1760 if (Py_FinalizeEx() < 0) {
1761 /* Value unlikely to be confused with a non-error exit status or
1762 other special meaning */
1763 pymain->status = 120;
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001764 }
Victor Stinner19760862017-12-20 01:41:59 +01001765 }
1766
Victor Stinner94540602017-12-16 04:54:22 +01001767 pymain_free(pymain);
1768
Victor Stinner94540602017-12-16 04:54:22 +01001769 return pymain->status;
1770}
1771
1772
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001773int
1774Py_Main(int argc, wchar_t **argv)
1775{
1776 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001777 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001778 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001779 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001780
Victor Stinner94540602017-12-16 04:54:22 +01001781 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00001782}
1783
Victor Stinner94540602017-12-16 04:54:22 +01001784
1785int
1786_Py_UnixMain(int argc, char **argv)
1787{
1788 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001789 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001790 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01001791 pymain.bytes_argv = argv;
1792
1793 return pymain_main(&pymain);
1794}
1795
1796
Skip Montanaro786ea6b2004-03-01 15:44:05 +00001797/* this is gonna seem *real weird*, but if you put some other code between
1798 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
1799 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00001800
Guido van Rossum667d7041995-08-04 04:20:48 +00001801/* Make the *original* argc/argv available to other modules.
1802 This is rare, but it is needed by the secureware extension. */
1803
1804void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001805Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00001806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 *argc = orig_argc;
1808 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00001809}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001810
1811#ifdef __cplusplus
1812}
1813#endif