blob: 1918f4f9b8ff02490772b50b81a811956c7de3c8 [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"
Victor Stinner27e2d1f2018-11-01 00:52:28 +01005#include "pycore_mem.h"
6#include "pycore_getopt.h"
7#include "pycore_state.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00008
Antoine Pitrou5651eaa2008-09-06 20:46:58 +00009#include <locale.h>
10
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000011#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010012# include <windows.h>
13# ifdef HAVE_IO_H
14# include <io.h>
15# endif
16# ifdef HAVE_FCNTL_H
17# include <fcntl.h>
18# endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000019#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000020
Martin v. Löwis945362c2007-08-30 14:57:25 +000021#ifdef _MSC_VER
Victor Stinner6efcb6d2017-12-18 23:42:55 +010022# include <crtdbg.h>
23#endif
24
25#ifdef __FreeBSD__
26# include <fenv.h>
Martin v. Löwis945362c2007-08-30 14:57:25 +000027#endif
28
Jesus Ceaab70e2a2012-10-05 01:48:08 +020029#if defined(MS_WINDOWS)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010030# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
Guido van Rossuma075ce11997-12-05 21:56:45 +000031#else
Victor Stinner6efcb6d2017-12-18 23:42:55 +010032# define PYTHONHOMEHELP "<prefix>/lib/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000033#endif
34
Guido van Rossuma22865e2000-09-05 04:41:18 +000035#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000036 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
37 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000038
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039#ifdef __cplusplus
40extern "C" {
41#endif
42
Victor Stinner46972b72017-11-24 22:55:40 +010043#define DECODE_LOCALE_ERR(NAME, LEN) \
44 (((LEN) == -2) \
Victor Stinner94540602017-12-16 04:54:22 +010045 ? _Py_INIT_USER_ERR("cannot decode " NAME) \
Victor Stinner46972b72017-11-24 22:55:40 +010046 : _Py_INIT_NO_MEMORY())
47
48
Victor Stinnerca719ac2017-12-20 18:00:19 +010049#ifdef MS_WINDOWS
50#define WCSTOK wcstok_s
51#else
52#define WCSTOK wcstok
53#endif
54
Guido van Rossumac56b031996-07-21 02:33:38 +000055/* For Py_GetArgcArgv(); set by main() */
Victor Stinner94540602017-12-16 04:54:22 +010056static wchar_t **orig_argv = NULL;
57static int orig_argc = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +000058
Guido van Rossumbceccf52001-04-10 22:07:43 +000059/* command line options */
Christian Heimesad73a9c2013-08-10 16:36:18 +020060#define BASE_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000061
Guido van Rossumbceccf52001-04-10 22:07:43 +000062#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000063
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080064static const _PyOS_LongOption longoptions[] = {
65 {L"check-hash-based-pycs", 1, 0},
66 {NULL, 0, 0},
67};
68
Guido van Rossum667d7041995-08-04 04:20:48 +000069/* Short usage message (with %s for argv0) */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020070static const char usage_line[] =
Martin v. Löwis790465f2008-04-05 20:41:37 +000071"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000072
73/* Long usage message, split into parts < 512 bytes */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020074static const char usage_1[] = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000075Options and arguments (and corresponding environment variables):\n\
Christian Heimes2ab34442008-09-03 20:31:07 +000076-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
77 and comparing bytes/bytearray with str. (-bb: issue errors)\n\
Xiang Zhang0710d752017-03-11 13:02:52 +080078-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000079-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000080-d : debug output from parser; also PYTHONDEBUG=x\n\
Christian Heimes790c8232008-01-07 21:14:23 +000081-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000083";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020084static const char usage_2[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000085-i : inspect interactively after running script; forces a prompt even\n\
86 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Christian Heimesad73a9c2013-08-10 16:36:18 +020087-I : isolate Python from the user's environment (implies -E and -s)\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000088-m mod : run library module as a script (terminates option list)\n\
Cheryl Sabella186b6062018-02-24 22:04:40 -050089-O : remove assert and __debug__-dependent statements; add .opt-1 before\n\
90 .pyc extension; also PYTHONOPTIMIZE=x\n\
91-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
92 .pyc extension\n\
Georg Brandl9d871192010-12-04 10:47:18 +000093-q : don't print version and copyright messages on interactive startup\n\
Christian Heimes8dc226f2008-05-06 23:45:46 +000094-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000095-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000096";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020097static const char usage_3[] = "\
Berker Peksag7f580972017-10-13 15:16:31 +030098-u : force the stdout and stderr streams to be unbuffered;\n\
99 this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000100-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
101 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000102-V : print the Python version number and exit (also --version)\n\
INADA Naoki0e175a62016-11-21 20:57:14 +0900103 when given twice, print more information about the build\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000104-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenvey0805ca32010-04-07 04:04:10 +0000105 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000106-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000107-X opt : set implementation-specific option\n\
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800108--check-hash-based-pycs always|default|never:\n\
109 control how Python invalidates hash-based .pyc files\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000110";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200111static const char usage_4[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +0000112file : program read from script file\n\
113- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000115Other environment variables:\n\
116PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200117PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000118 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +0000119";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200120static const char usage_5[] =
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200121"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
Victor Stinner9802b392010-08-19 11:36:43 +0000122" The default module search path uses %s.\n"
123"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
124"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
Victor Stinner34be8072016-03-14 12:04:26 +0100125"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
126static const char usage_6[] =
127"PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
128" to seed the hashes of str, bytes and datetime objects. It can also be\n"
129" set to an integer in the range [0,4294967295] to get hash values with a\n"
130" predictable seed.\n"
131"PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
132" on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000133" hooks.\n"
Stéphane Wirtel7d1017d2017-06-12 13:30:33 +0200134"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000135" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
Victor Stinner5e3806f2017-11-30 11:40:24 +0100136" locale coercion and locale compatibility warnings on stderr.\n"
Stéphane Wirtelb7fd7382018-07-29 12:27:16 +0200137"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
138" debugger. It can be set to the callable of your debugger of choice.\n"
Carl Meyerb193fa92018-06-15 22:40:56 -0600139"PYTHONDEVMODE: enable the development mode.\n"
140"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
Guido van Rossum667d7041995-08-04 04:20:48 +0000141
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800142static void
Victor Stinnera7368ac2017-11-15 18:11:45 -0800143pymain_usage(int error, const wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000144{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800145 FILE *f = error ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 fprintf(f, usage_line, program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800148 if (error)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 fprintf(f, "Try `python -h' for more information.\n");
150 else {
151 fputs(usage_1, f);
152 fputs(usage_2, f);
153 fputs(usage_3, f);
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200154 fprintf(f, usage_4, (wint_t)DELIM);
155 fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100156 fputs(usage_6, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000158}
159
Victor Stinnera7368ac2017-11-15 18:11:45 -0800160
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800161static void
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800162pymain_run_interactive_hook(void)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200163{
164 PyObject *sys, *hook, *result;
165 sys = PyImport_ImportModule("sys");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800166 if (sys == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200167 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800168 }
169
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200170 hook = PyObject_GetAttrString(sys, "__interactivehook__");
171 Py_DECREF(sys);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800172 if (hook == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200173 PyErr_Clear();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800174 return;
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200175 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800176
177 result = _PyObject_CallNoArg(hook);
178 Py_DECREF(hook);
179 if (result == NULL) {
180 goto error;
181 }
182 Py_DECREF(result);
183
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200184 return;
185
186error:
187 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
188 PyErr_Print();
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200189}
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();
Nick Coghland2977a32017-03-12 20:38:32 +1000270 return NULL;
271}
272
273
274static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800275pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
Victor Stinnera62207c2010-08-07 10:57:17 +0000276{
277 PyObject *unicode, *bytes;
278 int ret;
279
280 unicode = PyUnicode_FromWideChar(command, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800281 if (unicode == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000282 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800283 }
284
Victor Stinnera62207c2010-08-07 10:57:17 +0000285 bytes = PyUnicode_AsUTF8String(unicode);
286 Py_DECREF(unicode);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800287 if (bytes == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000288 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800289 }
290
Victor Stinnera62207c2010-08-07 10:57:17 +0000291 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
292 Py_DECREF(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800293 return (ret != 0);
Victor Stinnera62207c2010-08-07 10:57:17 +0000294
295error:
Victor Stinner398356b2010-08-18 22:23:22 +0000296 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000297 PyErr_Print();
298 return 1;
299}
300
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800301
Guido van Rossum667d7041995-08-04 04:20:48 +0000302/* Main program */
303
Eric Snow6b4be192017-05-22 21:36:03 -0700304typedef struct {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100305 wchar_t **argv;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200306 int nwarnoption; /* Number of -W command line options */
307 wchar_t **warnoptions; /* Command line -W options */
308 int nenv_warnoption; /* Number of PYTHONWARNINGS environment variables */
309 wchar_t **env_warnoptions; /* PYTHONWARNINGS environment variables */
Eric Snow6b4be192017-05-22 21:36:03 -0700310 int print_help; /* -h, -? options */
311 int print_version; /* -V option */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200312} _PyCmdline;
Eric Snow6b4be192017-05-22 21:36:03 -0700313
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800314/* Structure used by Py_Main() to pass data to subfunctions */
315typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100316 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800317 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100318 int use_bytes_argv;
319 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100320 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100321
322 /* Exit status or "exit code": result of pymain_main() */
323 int status;
324 /* Error message if a function failed */
325 _PyInitError err;
326
Victor Stinner19760862017-12-20 01:41:59 +0100327 /* non-zero is stdin is a TTY or if -i option is used */
328 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100329 int skip_first_line; /* -x option */
330 wchar_t *filename; /* Trailing arg without -c or -m */
331 wchar_t *command; /* -c argument */
332 wchar_t *module; /* -m argument */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800333} _PyMain;
334
Victor Stinner1dc6e392018-07-25 02:49:17 +0200335#define _PyMain_INIT {.err = _Py_INIT_OK()}
Victor Stinnerd5dda982017-12-13 17:31:16 +0100336/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800337
338
Victor Stinner19760862017-12-20 01:41:59 +0100339/* Non-zero if filename, command (-c) or module (-m) is set
340 on the command line */
341#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100342 (pymain->command != NULL || pymain->filename != NULL \
343 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100344
345
Victor Stinnerca719ac2017-12-20 18:00:19 +0100346static wchar_t*
347pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800348{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100349 wchar_t *str2 = _PyMem_RawWcsdup(str);
350 if (str2 == NULL) {
351 pymain->err = _Py_INIT_NO_MEMORY();
352 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800353 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100354 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800355}
356
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100357
Victor Stinnerc4bca952017-12-19 23:48:17 +0100358static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200359pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,
360 _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100361{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100362 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100363
Victor Stinnerca719ac2017-12-20 18:00:19 +0100364 if (pymain->use_bytes_argv) {
365 /* +1 for a the NULL terminator */
366 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
367 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
368 if (argv == NULL) {
369 pymain->err = _Py_INIT_NO_MEMORY();
370 return -1;
371 }
372
373 for (int i = 0; i < pymain->argc; i++) {
374 size_t len;
375 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
376 if (arg == NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +0200377 _Py_wstrlist_clear(i, argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100378 pymain->err = DECODE_LOCALE_ERR("command line arguments",
379 (Py_ssize_t)len);
380 return -1;
381 }
382 argv[i] = arg;
383 }
384 argv[pymain->argc] = NULL;
385
386 cmdline->argv = argv;
387 }
388 else {
389 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100390 }
391
Victor Stinnerca719ac2017-12-20 18:00:19 +0100392 wchar_t *program;
393 if (pymain->argc >= 1 && cmdline->argv != NULL) {
394 program = cmdline->argv[0];
395 }
396 else {
397 program = L"";
398 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200399 config->program = pymain_wstrdup(pymain, program);
400 if (config->program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100401 return -1;
402 }
403
Victor Stinnerc4bca952017-12-19 23:48:17 +0100404 return 0;
405}
406
407
408static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200409pymain_clear_cmdline(_PyMain *pymain, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100410{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100411 PyMemAllocatorEx old_alloc;
412 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100413
Victor Stinner6c785c02018-08-01 17:56:14 +0200414 _Py_wstrlist_clear(cmdline->nwarnoption, cmdline->warnoptions);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100415 cmdline->nwarnoption = 0;
416 cmdline->warnoptions = NULL;
417
Victor Stinner6c785c02018-08-01 17:56:14 +0200418 _Py_wstrlist_clear(cmdline->nenv_warnoption, cmdline->env_warnoptions);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100419 cmdline->nenv_warnoption = 0;
420 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100421
422 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +0200423 _Py_wstrlist_clear(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100424 }
425 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100426
427 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
428}
429
430
431static void
432pymain_clear_pymain(_PyMain *pymain)
433{
434#define CLEAR(ATTR) \
435 do { \
436 PyMem_RawFree(ATTR); \
437 ATTR = NULL; \
438 } while (0)
439
440 CLEAR(pymain->filename);
441 CLEAR(pymain->command);
442 CLEAR(pymain->module);
443#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100444}
445
Victor Stinnerc4bca952017-12-19 23:48:17 +0100446static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200447pymain_clear_config(_PyCoreConfig *config)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100448{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100449 /* Clear core config with the memory allocator
450 used by pymain_read_conf() */
451 PyMemAllocatorEx old_alloc;
452 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
453
Victor Stinner1dc6e392018-07-25 02:49:17 +0200454 _PyCoreConfig_Clear(config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100455
456 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
457}
458
459
460static void
Victor Stinnerd1457752018-07-26 16:04:56 +0200461pymain_free(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100462{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100463 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100464
Victor Stinnerc4bca952017-12-19 23:48:17 +0100465 /* Free global variables which cannot be freed in Py_Finalize():
466 configuration options set before Py_Initialize() which should
467 remain valid after Py_Finalize(), since
468 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinnerb1147e42018-07-21 02:06:16 +0200469 _PyPathConfig_ClearGlobal();
Victor Stinner124b9eb2018-08-29 01:29:06 +0200470 _Py_ClearStandardStreamEncoding();
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
Victor Stinnerfbca9082018-08-30 00:50:45 +0200955pymain_header(_PyMain *pymain, const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800956{
Victor Stinnerfbca9082018-08-30 00:50:45 +0200957 if (config->quiet) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800958 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000960
Victor Stinnerfbca9082018-08-30 00:50:45 +0200961 if (!config->verbose && (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());
Victor Stinnerfbca9082018-08-30 00:50:45 +0200966 if (config->site_import) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800967 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 Stinner2094c2b2018-09-03 17:06:39 +02001022PyObject*
1023_Py_wstrlist_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
Victor Stinnerfbca9082018-08-30 00:50:45 +02001045pymain_import_readline(_PyMain *pymain, const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001046{
Victor Stinnerfbca9082018-08-30 00:50:45 +02001047 if (config->isolated) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001048 return;
1049 }
Victor Stinnerfbca9082018-08-30 00:50:45 +02001050 if (!config->inspect && 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();
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001084 return;
1085 }
1086
1087 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
1088 PyErr_Clear();
1089 fclose(fp);
1090}
1091
1092
1093static void
Victor Stinner72ec3192018-08-02 19:34:20 +02001094pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001095{
Victor Stinner72ec3192018-08-02 19:34:20 +02001096 const wchar_t *filename = pymain->filename;
1097 FILE *fp = _Py_wfopen(filename, L"r");
1098 if (fp == NULL) {
1099 char *cfilename_buffer;
1100 const char *cfilename;
1101 int err = errno;
1102 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
1103 if (cfilename_buffer != NULL)
1104 cfilename = cfilename_buffer;
1105 else
1106 cfilename = "<unprintable file name>";
1107 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
1108 config->program, cfilename, err, strerror(err));
1109 PyMem_RawFree(cfilename_buffer);
1110 pymain->status = 2;
1111 return;
1112 }
1113
1114 if (pymain->skip_first_line) {
1115 int ch;
1116 /* Push back first newline so line numbers remain the same */
1117 while ((ch = getc(fp)) != EOF) {
1118 if (ch == '\n') {
1119 (void)ungetc(ch, fp);
1120 break;
1121 }
1122 }
1123 }
1124
1125 struct _Py_stat_struct sb;
1126 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
1127 fprintf(stderr,
1128 "%ls: '%ls' is a directory, cannot continue\n",
1129 config->program, filename);
1130 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001131 fclose(fp);
1132 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001133 }
1134
1135 /* call pending calls like signal handlers (SIGINT) */
1136 if (Py_MakePendingCalls() == -1) {
1137 PyErr_Print();
1138 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001139 fclose(fp);
1140 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001141 }
1142
1143 PyObject *unicode, *bytes = NULL;
1144 const char *filename_str;
1145
1146 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
1147 if (unicode != NULL) {
1148 bytes = PyUnicode_EncodeFSDefault(unicode);
1149 Py_DECREF(unicode);
1150 }
1151 if (bytes != NULL) {
1152 filename_str = PyBytes_AsString(bytes);
1153 }
1154 else {
1155 PyErr_Clear();
1156 filename_str = "<filename encoding error>";
1157 }
1158
Victor Stinnerd8078622018-08-03 23:54:06 +02001159 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
1160 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
Victor Stinner72ec3192018-08-02 19:34:20 +02001161 Py_XDECREF(bytes);
1162 pymain->status = (run != 0);
Victor Stinner72ec3192018-08-02 19:34:20 +02001163}
1164
1165
1166static void
1167pymain_run_stdin(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
1168{
1169 if (pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001170 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinnera4d20b22018-08-01 02:57:45 +02001171 config->inspect = 0;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001172 pymain_run_startup(pymain, config, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001173 pymain_run_interactive_hook();
1174 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001175
Victor Stinner72ec3192018-08-02 19:34:20 +02001176 /* call pending calls like signal handlers (SIGINT) */
1177 if (Py_MakePendingCalls() == -1) {
1178 PyErr_Print();
1179 pymain->status = 1;
1180 return;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001181 }
1182
Victor Stinner72ec3192018-08-02 19:34:20 +02001183 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
1184 pymain->status = (run != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001185}
1186
1187
1188static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001189pymain_repl(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001192 opportunity to set it from Python. */
Victor Stinner6c785c02018-08-01 17:56:14 +02001193 if (!Py_InspectFlag && _PyCoreConfig_GetEnv(config, "PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 Py_InspectFlag = 1;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001195 config->inspect = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001197
Victor Stinner19760862017-12-20 01:41:59 +01001198 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001199 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001201
1202 Py_InspectFlag = 0;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001203 config->inspect = 0;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001204 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001205
Victor Stinner19760862017-12-20 01:41:59 +01001206 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001207 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001208}
1209
1210
1211/* Parse the command line.
1212 Handle --version and --help options directly.
1213
1214 Return 1 if Python must exit.
1215 Return 0 on success.
1216 Set pymain->err and return -1 on failure. */
1217static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001218pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config,
1219 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001220{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001221 int res = pymain_parse_cmdline_impl(pymain, config, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001222 if (res < 0) {
1223 return -1;
1224 }
1225 if (res) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001226 pymain_usage(1, config->program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001227 pymain->status = 2;
1228 return 1;
1229 }
1230
Victor Stinnerca719ac2017-12-20 18:00:19 +01001231 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001232 /* Backup _PyOS_optind */
1233 _PyOS_optind--;
1234 }
1235
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001236 return 0;
1237}
1238
1239
Victor Stinnera7368ac2017-11-15 18:11:45 -08001240/* Parse command line options and environment variables.
1241 This code must not use Python runtime apart PyMem_Raw memory allocator.
1242
1243 Return 0 on success.
1244 Return 1 if Python is done and must exit.
1245 Set pymain->err and return -1 on error. */
1246static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001247pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
1248 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001249{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001250 _PyInitError err;
1251
Victor Stinner1dc6e392018-07-25 02:49:17 +02001252 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01001253 if (res != 0) {
1254 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001255 }
1256
Victor Stinner1dc6e392018-07-25 02:49:17 +02001257 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001258 return -1;
1259 }
1260
Victor Stinnerd1457752018-07-26 16:04:56 +02001261 err = _PyCoreConfig_Read(config);
1262 if (_Py_INIT_FAILED(err)) {
1263 pymain->err = err;
1264 return -1;
1265 }
1266
Victor Stinnerd1457752018-07-26 16:04:56 +02001267 if (config->use_environment) {
Victor Stinnerecf411c2018-07-26 02:37:22 +02001268 err = cmdline_init_env_warnoptions(pymain, config, cmdline);
1269 if (_Py_INIT_FAILED(err)) {
1270 pymain->err = err;
1271 return -1;
1272 }
1273 }
1274
Victor Stinnerd1457752018-07-26 16:04:56 +02001275 err = config_init_warnoptions(config, cmdline);
Victor Stinner31a83932017-12-04 13:39:15 +01001276 if (_Py_INIT_FAILED(err)) {
1277 pymain->err = err;
1278 return -1;
1279 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001280 return 0;
1281}
1282
1283
Victor Stinner177d9212018-08-29 11:25:15 +02001284/* Read the configuration and initialize the LC_CTYPE locale:
1285 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538). */
Victor Stinnera7368ac2017-11-15 18:11:45 -08001286static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001287pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
1288 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001289{
Victor Stinner89487f52018-08-23 12:23:46 +02001290 int init_utf8_mode = Py_UTF8Mode;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001291#ifdef MS_WINDOWS
1292 int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
1293#endif
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001294 _PyCoreConfig save_config = _PyCoreConfig_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001295 int res = -1;
Victor Stinner94540602017-12-16 04:54:22 +01001296 int locale_coerced = 0;
1297 int loops = 0;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001298
1299 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
1300 pymain->err = _Py_INIT_NO_MEMORY();
1301 goto done;
1302 }
Victor Stinner94540602017-12-16 04:54:22 +01001303
Victor Stinner73b00be2018-09-03 17:32:31 +02001304 /* Set LC_CTYPE to the user preferred locale */
1305 _Py_SetLocaleFromEnv(LC_CTYPE);
1306
Victor Stinner94540602017-12-16 04:54:22 +01001307 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 Stinner06e76082018-09-19 14:56:36 -07001346 if (config->coerce_c_locale && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01001347 locale_coerced = 1;
Victor Stinner06e76082018-09-19 14:56:36 -07001348 _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 Stinner06e76082018-09-19 14:56:36 -07001371 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 Stinner06e76082018-09-19 14:56:36 -07001379 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) { \
Victor Stinner2094c2b2018-09-03 17:06:39 +02001494 ATTR = _Py_wstrlist_as_pylist(LEN, LIST); \
Victor Stinner8ded5b82018-01-24 17:03:28 +01001495 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 Stinner06e76082018-09-19 14:56:36 -07001704pymain_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
Victor Stinner06e76082018-09-19 14:56:36 -07001751pymain_main(_PyMain *pymain)
Victor Stinner1dc6e392018-07-25 02:49:17 +02001752{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001753 PyInterpreterState *interp;
Victor Stinner06e76082018-09-19 14:56:36 -07001754 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 Stinner06e76082018-09-19 14:56:36 -07001781 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
Victor Stinner06e76082018-09-19 14:56:36 -07001793 return pymain_main(&pymain);
Victor Stinner94540602017-12-16 04:54:22 +01001794}
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