blob: c847d1acedb737e4e913a0f9414a55c7af3e3de8 [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_getopt.h"
Victor Stinnera1c249c2018-11-01 03:15:58 +01006#include "pycore_lifecycle.h"
7#include "pycore_mem.h"
8#include "pycore_pathconfig.h"
Victor Stinner27e2d1f2018-11-01 00:52:28 +01009#include "pycore_state.h"
Guido van Rossum667d7041995-08-04 04:20:48 +000010
Antoine Pitrou5651eaa2008-09-06 20:46:58 +000011#include <locale.h>
12
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000013#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010014# include <windows.h>
15# ifdef HAVE_IO_H
16# include <io.h>
17# endif
18# ifdef HAVE_FCNTL_H
19# include <fcntl.h>
20# endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000021#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000022
Martin v. Löwis945362c2007-08-30 14:57:25 +000023#ifdef _MSC_VER
Victor Stinner6efcb6d2017-12-18 23:42:55 +010024# include <crtdbg.h>
25#endif
26
27#ifdef __FreeBSD__
28# include <fenv.h>
Martin v. Löwis945362c2007-08-30 14:57:25 +000029#endif
30
Jesus Ceaab70e2a2012-10-05 01:48:08 +020031#if defined(MS_WINDOWS)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010032# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
Guido van Rossuma075ce11997-12-05 21:56:45 +000033#else
Victor Stinner6efcb6d2017-12-18 23:42:55 +010034# define PYTHONHOMEHELP "<prefix>/lib/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000035#endif
36
Guido van Rossuma22865e2000-09-05 04:41:18 +000037#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000038 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
39 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000040
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041#ifdef __cplusplus
42extern "C" {
43#endif
44
Victor Stinner46972b72017-11-24 22:55:40 +010045#define DECODE_LOCALE_ERR(NAME, LEN) \
46 (((LEN) == -2) \
Victor Stinner94540602017-12-16 04:54:22 +010047 ? _Py_INIT_USER_ERR("cannot decode " NAME) \
Victor Stinner46972b72017-11-24 22:55:40 +010048 : _Py_INIT_NO_MEMORY())
49
50
Victor Stinnerca719ac2017-12-20 18:00:19 +010051#ifdef MS_WINDOWS
52#define WCSTOK wcstok_s
53#else
54#define WCSTOK wcstok
55#endif
56
Guido van Rossumac56b031996-07-21 02:33:38 +000057/* For Py_GetArgcArgv(); set by main() */
Victor Stinner94540602017-12-16 04:54:22 +010058static wchar_t **orig_argv = NULL;
59static int orig_argc = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +000060
Guido van Rossumbceccf52001-04-10 22:07:43 +000061/* command line options */
Christian Heimesad73a9c2013-08-10 16:36:18 +020062#define BASE_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000063
Guido van Rossumbceccf52001-04-10 22:07:43 +000064#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000065
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080066static const _PyOS_LongOption longoptions[] = {
67 {L"check-hash-based-pycs", 1, 0},
68 {NULL, 0, 0},
69};
70
Guido van Rossum667d7041995-08-04 04:20:48 +000071/* Short usage message (with %s for argv0) */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020072static const char usage_line[] =
Martin v. Löwis790465f2008-04-05 20:41:37 +000073"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000074
75/* Long usage message, split into parts < 512 bytes */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020076static const char usage_1[] = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000077Options and arguments (and corresponding environment variables):\n\
Christian Heimes2ab34442008-09-03 20:31:07 +000078-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
79 and comparing bytes/bytearray with str. (-bb: issue errors)\n\
Xiang Zhang0710d752017-03-11 13:02:52 +080080-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000081-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000082-d : debug output from parser; also PYTHONDEBUG=x\n\
Christian Heimes790c8232008-01-07 21:14:23 +000083-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000084-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000085";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020086static const char usage_2[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000087-i : inspect interactively after running script; forces a prompt even\n\
88 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Christian Heimesad73a9c2013-08-10 16:36:18 +020089-I : isolate Python from the user's environment (implies -E and -s)\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000090-m mod : run library module as a script (terminates option list)\n\
Cheryl Sabella186b6062018-02-24 22:04:40 -050091-O : remove assert and __debug__-dependent statements; add .opt-1 before\n\
92 .pyc extension; also PYTHONOPTIMIZE=x\n\
93-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
94 .pyc extension\n\
Georg Brandl9d871192010-12-04 10:47:18 +000095-q : don't print version and copyright messages on interactive startup\n\
Christian Heimes8dc226f2008-05-06 23:45:46 +000096-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000097-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000098";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020099static const char usage_3[] = "\
Berker Peksag7f580972017-10-13 15:16:31 +0300100-u : force the stdout and stderr streams to be unbuffered;\n\
101 this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000102-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
103 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000104-V : print the Python version number and exit (also --version)\n\
INADA Naoki0e175a62016-11-21 20:57:14 +0900105 when given twice, print more information about the build\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000106-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenvey0805ca32010-04-07 04:04:10 +0000107 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000108-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000109-X opt : set implementation-specific option\n\
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800110--check-hash-based-pycs always|default|never:\n\
111 control how Python invalidates hash-based .pyc files\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000112";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200113static const char usage_4[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +0000114file : program read from script file\n\
115- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000116arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000117Other environment variables:\n\
118PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200119PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000120 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +0000121";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200122static const char usage_5[] =
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200123"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
Victor Stinner9802b392010-08-19 11:36:43 +0000124" The default module search path uses %s.\n"
125"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
126"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
Victor Stinner34be8072016-03-14 12:04:26 +0100127"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
128static const char usage_6[] =
129"PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
130" to seed the hashes of str, bytes and datetime objects. It can also be\n"
131" set to an integer in the range [0,4294967295] to get hash values with a\n"
132" predictable seed.\n"
133"PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
134" on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000135" hooks.\n"
Stéphane Wirtel7d1017d2017-06-12 13:30:33 +0200136"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000137" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
Victor Stinner5e3806f2017-11-30 11:40:24 +0100138" locale coercion and locale compatibility warnings on stderr.\n"
Stéphane Wirtelb7fd7382018-07-29 12:27:16 +0200139"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
140" debugger. It can be set to the callable of your debugger of choice.\n"
Carl Meyerb193fa92018-06-15 22:40:56 -0600141"PYTHONDEVMODE: enable the development mode.\n"
142"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
Guido van Rossum667d7041995-08-04 04:20:48 +0000143
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800144static void
Victor Stinnera7368ac2017-11-15 18:11:45 -0800145pymain_usage(int error, const wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000146{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800147 FILE *f = error ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 fprintf(f, usage_line, program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800150 if (error)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 fprintf(f, "Try `python -h' for more information.\n");
152 else {
153 fputs(usage_1, f);
154 fputs(usage_2, f);
155 fputs(usage_3, f);
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200156 fprintf(f, usage_4, (wint_t)DELIM);
157 fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100158 fputs(usage_6, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000160}
161
Victor Stinnera7368ac2017-11-15 18:11:45 -0800162
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800163static void
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800164pymain_run_interactive_hook(void)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200165{
166 PyObject *sys, *hook, *result;
167 sys = PyImport_ImportModule("sys");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800168 if (sys == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200169 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800170 }
171
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200172 hook = PyObject_GetAttrString(sys, "__interactivehook__");
173 Py_DECREF(sys);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800174 if (hook == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200175 PyErr_Clear();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800176 return;
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200177 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800178
179 result = _PyObject_CallNoArg(hook);
180 Py_DECREF(hook);
181 if (result == NULL) {
182 goto error;
183 }
184 Py_DECREF(result);
185
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200186 return;
187
188error:
189 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
190 PyErr_Print();
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200191}
192
Thomas Woutersa9773292006-04-21 09:43:23 +0000193
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800194static int
Victor Stinnerc4bca952017-12-19 23:48:17 +0100195pymain_run_module(const wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 PyObject *module, *runpy, *runmodule, *runargs, *result;
198 runpy = PyImport_ImportModule("runpy");
199 if (runpy == NULL) {
200 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200201 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return -1;
203 }
204 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
205 if (runmodule == NULL) {
206 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200207 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 Py_DECREF(runpy);
209 return -1;
210 }
211 module = PyUnicode_FromWideChar(modname, wcslen(modname));
212 if (module == NULL) {
213 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200214 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_DECREF(runpy);
216 Py_DECREF(runmodule);
217 return -1;
218 }
219 runargs = Py_BuildValue("(Oi)", module, set_argv0);
220 if (runargs == NULL) {
221 fprintf(stderr,
222 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200223 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 Py_DECREF(runpy);
225 Py_DECREF(runmodule);
226 Py_DECREF(module);
227 return -1;
228 }
229 result = PyObject_Call(runmodule, runargs, NULL);
230 if (result == NULL) {
231 PyErr_Print();
232 }
233 Py_DECREF(runpy);
234 Py_DECREF(runmodule);
235 Py_DECREF(module);
236 Py_DECREF(runargs);
237 if (result == NULL) {
238 return -1;
239 }
240 Py_DECREF(result);
241 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000242}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000243
Nick Coghland2977a32017-03-12 20:38:32 +1000244static PyObject *
Victor Stinnerc4bca952017-12-19 23:48:17 +0100245pymain_get_importer(const wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000246{
Nick Coghland2977a32017-03-12 20:38:32 +1000247 PyObject *sys_path0 = NULL, *importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000248
Nick Coghland2977a32017-03-12 20:38:32 +1000249 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800250 if (sys_path0 == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000251 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800252 }
Victor Stinner4726e402010-10-06 23:24:57 +0000253
Nick Coghland2977a32017-03-12 20:38:32 +1000254 importer = PyImport_GetImporter(sys_path0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800255 if (importer == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000256 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800257 }
Victor Stinner4726e402010-10-06 23:24:57 +0000258
Brett Cannonaa936422012-04-27 15:30:58 -0400259 if (importer == Py_None) {
Nick Coghland2977a32017-03-12 20:38:32 +1000260 Py_DECREF(sys_path0);
Victor Stinner4726e402010-10-06 23:24:57 +0000261 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000262 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800264
Victor Stinner4726e402010-10-06 23:24:57 +0000265 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000266 return sys_path0;
Victor Stinner4726e402010-10-06 23:24:57 +0000267
Nick Coghland2977a32017-03-12 20:38:32 +1000268error:
269 Py_XDECREF(sys_path0);
270 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
271 PyErr_Print();
Nick Coghland2977a32017-03-12 20:38:32 +1000272 return NULL;
273}
274
275
276static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800277pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
Victor Stinnera62207c2010-08-07 10:57:17 +0000278{
279 PyObject *unicode, *bytes;
280 int ret;
281
282 unicode = PyUnicode_FromWideChar(command, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800283 if (unicode == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000284 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800285 }
286
Victor Stinnera62207c2010-08-07 10:57:17 +0000287 bytes = PyUnicode_AsUTF8String(unicode);
288 Py_DECREF(unicode);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800289 if (bytes == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000290 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800291 }
292
Victor Stinnera62207c2010-08-07 10:57:17 +0000293 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
294 Py_DECREF(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800295 return (ret != 0);
Victor Stinnera62207c2010-08-07 10:57:17 +0000296
297error:
Victor Stinner398356b2010-08-18 22:23:22 +0000298 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000299 PyErr_Print();
300 return 1;
301}
302
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800303
Guido van Rossum667d7041995-08-04 04:20:48 +0000304/* Main program */
305
Eric Snow6b4be192017-05-22 21:36:03 -0700306typedef struct {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100307 wchar_t **argv;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200308 int nwarnoption; /* Number of -W command line options */
309 wchar_t **warnoptions; /* Command line -W options */
310 int nenv_warnoption; /* Number of PYTHONWARNINGS environment variables */
311 wchar_t **env_warnoptions; /* PYTHONWARNINGS environment variables */
Eric Snow6b4be192017-05-22 21:36:03 -0700312 int print_help; /* -h, -? options */
313 int print_version; /* -V option */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200314} _PyCmdline;
Eric Snow6b4be192017-05-22 21:36:03 -0700315
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800316/* Structure used by Py_Main() to pass data to subfunctions */
317typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100318 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800319 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100320 int use_bytes_argv;
321 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100322 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100323
324 /* Exit status or "exit code": result of pymain_main() */
325 int status;
326 /* Error message if a function failed */
327 _PyInitError err;
328
Victor Stinner19760862017-12-20 01:41:59 +0100329 /* non-zero is stdin is a TTY or if -i option is used */
330 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100331 int skip_first_line; /* -x option */
332 wchar_t *filename; /* Trailing arg without -c or -m */
333 wchar_t *command; /* -c argument */
334 wchar_t *module; /* -m argument */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800335} _PyMain;
336
Victor Stinner1dc6e392018-07-25 02:49:17 +0200337#define _PyMain_INIT {.err = _Py_INIT_OK()}
Victor Stinnerd5dda982017-12-13 17:31:16 +0100338/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800339
340
Victor Stinner19760862017-12-20 01:41:59 +0100341/* Non-zero if filename, command (-c) or module (-m) is set
342 on the command line */
343#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100344 (pymain->command != NULL || pymain->filename != NULL \
345 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100346
347
Victor Stinnerca719ac2017-12-20 18:00:19 +0100348static wchar_t*
349pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800350{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100351 wchar_t *str2 = _PyMem_RawWcsdup(str);
352 if (str2 == NULL) {
353 pymain->err = _Py_INIT_NO_MEMORY();
354 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800355 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100356 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800357}
358
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100359
Victor Stinnerc4bca952017-12-19 23:48:17 +0100360static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200361pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,
362 _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100363{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100364 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100365
Victor Stinnerca719ac2017-12-20 18:00:19 +0100366 if (pymain->use_bytes_argv) {
367 /* +1 for a the NULL terminator */
368 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
369 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
370 if (argv == NULL) {
371 pymain->err = _Py_INIT_NO_MEMORY();
372 return -1;
373 }
374
375 for (int i = 0; i < pymain->argc; i++) {
376 size_t len;
377 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
378 if (arg == NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +0200379 _Py_wstrlist_clear(i, argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100380 pymain->err = DECODE_LOCALE_ERR("command line arguments",
381 (Py_ssize_t)len);
382 return -1;
383 }
384 argv[i] = arg;
385 }
386 argv[pymain->argc] = NULL;
387
388 cmdline->argv = argv;
389 }
390 else {
391 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100392 }
393
Victor Stinnerca719ac2017-12-20 18:00:19 +0100394 wchar_t *program;
395 if (pymain->argc >= 1 && cmdline->argv != NULL) {
396 program = cmdline->argv[0];
397 }
398 else {
399 program = L"";
400 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200401 config->program = pymain_wstrdup(pymain, program);
402 if (config->program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100403 return -1;
404 }
405
Victor Stinnerc4bca952017-12-19 23:48:17 +0100406 return 0;
407}
408
409
410static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200411pymain_clear_cmdline(_PyMain *pymain, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100412{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100413 PyMemAllocatorEx old_alloc;
414 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100415
Victor Stinner6c785c02018-08-01 17:56:14 +0200416 _Py_wstrlist_clear(cmdline->nwarnoption, cmdline->warnoptions);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100417 cmdline->nwarnoption = 0;
418 cmdline->warnoptions = NULL;
419
Victor Stinner6c785c02018-08-01 17:56:14 +0200420 _Py_wstrlist_clear(cmdline->nenv_warnoption, cmdline->env_warnoptions);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100421 cmdline->nenv_warnoption = 0;
422 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100423
424 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +0200425 _Py_wstrlist_clear(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100426 }
427 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100428
429 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
430}
431
432
433static void
434pymain_clear_pymain(_PyMain *pymain)
435{
436#define CLEAR(ATTR) \
437 do { \
438 PyMem_RawFree(ATTR); \
439 ATTR = NULL; \
440 } while (0)
441
442 CLEAR(pymain->filename);
443 CLEAR(pymain->command);
444 CLEAR(pymain->module);
445#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100446}
447
Victor Stinnerc4bca952017-12-19 23:48:17 +0100448static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200449pymain_clear_config(_PyCoreConfig *config)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100450{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100451 /* Clear core config with the memory allocator
452 used by pymain_read_conf() */
453 PyMemAllocatorEx old_alloc;
454 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
455
Victor Stinner1dc6e392018-07-25 02:49:17 +0200456 _PyCoreConfig_Clear(config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100457
458 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
459}
460
461
462static void
Victor Stinnerd1457752018-07-26 16:04:56 +0200463pymain_free(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100464{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100465 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100466
Victor Stinnerc4bca952017-12-19 23:48:17 +0100467 /* Free global variables which cannot be freed in Py_Finalize():
468 configuration options set before Py_Initialize() which should
469 remain valid after Py_Finalize(), since
470 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinnerb1147e42018-07-21 02:06:16 +0200471 _PyPathConfig_ClearGlobal();
Victor Stinner124b9eb2018-08-29 01:29:06 +0200472 _Py_ClearStandardStreamEncoding();
Victor Stinner94540602017-12-16 04:54:22 +0100473
Victor Stinnerc4bca952017-12-19 23:48:17 +0100474 /* Force the allocator used by pymain_read_conf() */
475 PyMemAllocatorEx old_alloc;
476 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100477
Victor Stinnerca719ac2017-12-20 18:00:19 +0100478 pymain_clear_pymain(pymain);
479
Victor Stinner6c785c02018-08-01 17:56:14 +0200480 _Py_wstrlist_clear(orig_argc, orig_argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100481 orig_argc = 0;
482 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100483
Victor Stinnerc4bca952017-12-19 23:48:17 +0100484 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200485
486#ifdef __INSURE__
487 /* Insure++ is a memory analysis tool that aids in discovering
488 * memory leaks and other memory problems. On Python exit, the
489 * interned string dictionaries are flagged as being in use at exit
490 * (which it is). Under normal circumstances, this is fine because
491 * the memory will be automatically reclaimed by the system. Under
492 * memory debugging, it's a huge source of useless noise, so we
493 * trade off slower shutdown for less distraction in the memory
494 * reports. -baw
495 */
496 _Py_ReleaseInternedUnicodeStrings();
497#endif /* __INSURE__ */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800498}
499
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100500
Eric Snow6b4be192017-05-22 21:36:03 -0700501static int
Victor Stinnerd3b19192018-07-25 10:21:03 +0200502pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
Guido van Rossum667d7041995-08-04 04:20:48 +0000503{
Victor Stinnerd3b19192018-07-25 10:21:03 +0200504 PyObject *sys_path;
505 PyObject *sysdict = interp->sysdict;
506 if (sysdict != NULL) {
507 sys_path = PyDict_GetItemString(sysdict, "path");
508 }
509 else {
510 sys_path = NULL;
511 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800512 if (sys_path == NULL) {
513 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
514 goto error;
515 }
516
Victor Stinnerd3b19192018-07-25 10:21:03 +0200517 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800518 goto error;
519 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200520 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800521
522error:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800523 PyErr_Print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200524 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800525}
526
527
Victor Stinnerb1147e42018-07-21 02:06:16 +0200528_PyInitError
529_Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800530{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200531 if (*len == INT_MAX) {
532 /* len+1 would overflow */
533 return _Py_INIT_NO_MEMORY();
534 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100535 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800536 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100537 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800538 }
539
Victor Stinnerca719ac2017-12-20 18:00:19 +0100540 size_t size = (*len + 1) * sizeof(list[0]);
541 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
542 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800543 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100544 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800545 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100546 list2[*len] = str2;
547 *list = list2;
548 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100549 return _Py_INIT_OK();
550}
551
552
553static int
554pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
555{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200556 _PyInitError err = _Py_wstrlist_append(len, list, str);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100557 if (_Py_INIT_FAILED(err)) {
558 pymain->err = err;
559 return -1;
560 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800561 return 0;
562}
563
564
565/* Parse the command line arguments
566 Return 0 on success.
567 Return 1 if parsing failed.
568 Set pymain->err and return -1 on other errors. */
569static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200570pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
571 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800572{
Antoine Pitrou86838b02012-02-21 19:03:47 +0100573 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800574 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800575 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100576 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800577 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800578 if (c == EOF) {
579 break;
580 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 /* -c is the last option; following arguments
584 that look like options are left for the
585 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800586 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
587 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
588 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100589 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800590 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800591 }
Victor Stinner58d16832018-05-31 15:09:28 +0200592 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 command[len - 2] = '\n';
594 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100595 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 break;
597 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 if (c == 'm') {
600 /* -m is the last option; following arguments
601 that look like options are left for the
602 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100603 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
604 if (pymain->module == NULL) {
605 return -1;
606 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 break;
608 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800611 case 0:
612 // Handle long option.
613 assert(longindex == 0); // Only one long option now.
614 if (!wcscmp(_PyOS_optarg, L"always")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200615 config->_check_hash_pycs_mode = "always";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800616 } else if (!wcscmp(_PyOS_optarg, L"never")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200617 config->_check_hash_pycs_mode = "never";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800618 } else if (!wcscmp(_PyOS_optarg, L"default")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200619 config->_check_hash_pycs_mode = "default";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800620 } else {
621 fprintf(stderr, "--check-hash-based-pycs must be one of "
622 "'default', 'always', or 'never'\n");
623 return 1;
624 }
625 break;
626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 case 'b':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200628 config->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 case 'd':
Victor Stinner98512272018-08-01 03:07:00 +0200632 config->parser_debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 case 'i':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200636 config->inspect++;
637 config->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000639
Christian Heimesad73a9c2013-08-10 16:36:18 +0200640 case 'I':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200641 config->isolated++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200642 break;
643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 case 'O':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200647 config->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 case 'B':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200651 config->write_bytecode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 case 's':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200655 config->user_site_directory = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 case 'S':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200659 config->site_import = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 case 'E':
Victor Stinnerd1457752018-07-26 16:04:56 +0200663 config->use_environment = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 case 't':
667 /* ignored for backwards compatibility */
668 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 case 'u':
Victor Stinner98512272018-08-01 03:07:00 +0200671 config->buffered_stdio = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 case 'v':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200675 config->verbose++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100679 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 case 'h':
683 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -0700684 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -0700688 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100692 if (pymain_wstrlist_append(pymain,
693 &cmdline->nwarnoption,
694 &cmdline->warnoptions,
695 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800696 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800697 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000699
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000700 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100701 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +0100702 &config->nxoption,
703 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +0100704 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800705 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800706 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000707 break;
708
Georg Brandl9d871192010-12-04 10:47:18 +0000709 case 'q':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200710 config->quiet++;
Georg Brandl9d871192010-12-04 10:47:18 +0000711 break;
712
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100713 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100714 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100715 break;
716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800720 /* unknown argument: parsing failed */
721 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800723 } while (1);
724
Victor Stinnerca719ac2017-12-20 18:00:19 +0100725 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800726 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +0100727 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800728 {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100729 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
730 if (pymain->filename == NULL) {
731 return -1;
732 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000734
Victor Stinnerd5dda982017-12-13 17:31:16 +0100735 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100736 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +0100737
Eric Snow6b4be192017-05-22 21:36:03 -0700738 return 0;
739}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000740
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800741
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800742static int
Victor Stinner9cfc0022017-12-20 19:36:46 +0100743add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +0100744{
745 PyObject *name, *value;
746
747 const wchar_t *name_end = wcschr(s, L'=');
748 if (!name_end) {
749 name = PyUnicode_FromWideChar(s, -1);
750 value = Py_True;
751 Py_INCREF(value);
752 }
753 else {
754 name = PyUnicode_FromWideChar(s, name_end - s);
755 value = PyUnicode_FromWideChar(name_end + 1, -1);
756 }
757 if (name == NULL || value == NULL) {
758 goto error;
759 }
760 if (PyDict_SetItem(opts, name, value) < 0) {
761 goto error;
762 }
763 Py_DECREF(name);
764 Py_DECREF(value);
765 return 0;
766
767error:
768 Py_XDECREF(name);
769 Py_XDECREF(value);
770 return -1;
771}
772
Victor Stinner9cfc0022017-12-20 19:36:46 +0100773
774static PyObject*
775config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800776{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100777 int nxoption = config->nxoption;
778 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +0100779 PyObject *dict = PyDict_New();
780 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100781 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +0100782 }
783
Victor Stinnerca719ac2017-12-20 18:00:19 +0100784 for (int i=0; i < nxoption; i++) {
785 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +0100786 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +0100787 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100788 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800789 }
790 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100791
Victor Stinner9cfc0022017-12-20 19:36:46 +0100792 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -0700793}
794
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800795
Victor Stinner9cfc0022017-12-20 19:36:46 +0100796static _PyInitError
797config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -0700798{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100799 for (int i = 0; i < len; i++) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200800 _PyInitError err = _Py_wstrlist_append(&config->nwarnoption,
801 &config->warnoptions,
802 options[i]);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100803 if (_Py_INIT_FAILED(err)) {
804 return err;
Eric Snow6b4be192017-05-22 21:36:03 -0700805 }
Eric Snow6b4be192017-05-22 21:36:03 -0700806 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100807 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800808}
Eric Snow6b4be192017-05-22 21:36:03 -0700809
Victor Stinner747f48e2017-12-12 22:59:48 +0100810
Victor Stinner9cfc0022017-12-20 19:36:46 +0100811static _PyInitError
Victor Stinner1dc6e392018-07-25 02:49:17 +0200812config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +0100813{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100814 _PyInitError err;
815
816 assert(config->nwarnoption == 0);
817
Victor Stinner747f48e2017-12-12 22:59:48 +0100818 /* The priority order for warnings configuration is (highest precedence
819 * first):
820 *
821 * - the BytesWarning filter, if needed ('-b', '-bb')
822 * - any '-W' command line options; then
823 * - the 'PYTHONWARNINGS' environment variable; then
824 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
825 * - any implicit filters added by _warnings.c/warnings.py
826 *
827 * All settings except the last are passed to the warnings module via
828 * the `sys.warnoptions` list. Since the warnings module works on the basis
829 * of "the most recently added filter will be checked first", we add
830 * the lowest precedence entries first so that later entries override them.
831 */
832
Victor Stinner9cfc0022017-12-20 19:36:46 +0100833 if (config->dev_mode) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200834 err = _Py_wstrlist_append(&config->nwarnoption,
835 &config->warnoptions,
836 L"default");
Victor Stinner9cfc0022017-12-20 19:36:46 +0100837 if (_Py_INIT_FAILED(err)) {
838 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100839 }
Victor Stinner747f48e2017-12-12 22:59:48 +0100840 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100841
Victor Stinner9cfc0022017-12-20 19:36:46 +0100842 err = config_add_warnings_optlist(config,
843 cmdline->nenv_warnoption,
844 cmdline->env_warnoptions);
845 if (_Py_INIT_FAILED(err)) {
846 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100847 }
848
Victor Stinner9cfc0022017-12-20 19:36:46 +0100849 err = config_add_warnings_optlist(config,
850 cmdline->nwarnoption,
851 cmdline->warnoptions);
852 if (_Py_INIT_FAILED(err)) {
853 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100854 }
855
856 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
857 * don't even try to emit a warning, so we skip setting the filter in that
858 * case.
859 */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200860 if (config->bytes_warning) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100861 wchar_t *filter;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200862 if (config->bytes_warning> 1) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100863 filter = L"error::BytesWarning";
864 }
865 else {
866 filter = L"default::BytesWarning";
867 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200868 err = _Py_wstrlist_append(&config->nwarnoption,
869 &config->warnoptions,
870 filter);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100871 if (_Py_INIT_FAILED(err)) {
872 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100873 }
874 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100875 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +0100876}
877
878
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800879/* Get warning options from PYTHONWARNINGS environment variable.
880 Return 0 on success.
881 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +0100882static _PyInitError
Victor Stinnerd1457752018-07-26 16:04:56 +0200883cmdline_init_env_warnoptions(_PyMain *pymain, const _PyCoreConfig *config,
884 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800885{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100886 wchar_t *env;
Victor Stinner6c785c02018-08-01 17:56:14 +0200887 int res = _PyCoreConfig_GetEnvDup(config, &env,
888 L"PYTHONWARNINGS", "PYTHONWARNINGS");
Victor Stinnerca719ac2017-12-20 18:00:19 +0100889 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100890 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100891 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800892
Victor Stinnerca719ac2017-12-20 18:00:19 +0100893 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100894 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +0100895 }
Philip Jenvey0805ca32010-04-07 04:04:10 +0000896
Victor Stinnerca719ac2017-12-20 18:00:19 +0100897
898 wchar_t *warning, *context = NULL;
899 for (warning = WCSTOK(env, L",", &context);
900 warning != NULL;
901 warning = WCSTOK(NULL, L",", &context))
902 {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200903 _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption,
904 &cmdline->env_warnoptions,
905 warning);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100906 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100907 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100908 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800909 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100911 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100912 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800913}
914
915
916static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200917pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800918{
919 pymain->stdin_is_interactive = (isatty(fileno(stdin))
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200920 || config->interactive);
Guido van Rossum775af911997-02-14 19:50:32 +0000921
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000922#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +0000923 /* don't translate newlines (\r\n <=> \n) */
924 _setmode(fileno(stdin), O_BINARY);
925 _setmode(fileno(stdout), O_BINARY);
926 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000927#endif
Victor Stinner89e34362011-01-07 18:47:22 +0000928
Victor Stinner98512272018-08-01 03:07:00 +0200929 if (!config->buffered_stdio) {
Guido van Rossum22ffac11998-03-06 15:30:39 +0000930#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
932 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
933 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000934#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 setbuf(stdin, (char *)NULL);
936 setbuf(stdout, (char *)NULL);
937 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000938#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200940 else if (config->interactive) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000941#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 /* Doesn't have to have line-buffered -- use unbuffered */
943 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
944 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000945#else /* !MS_WINDOWS */
946#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
948 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000949#endif /* HAVE_SETVBUF */
950#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 /* Leave stderr alone - it should be unbuffered anyway. */
952 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800953}
Guido van Rossum667d7041995-08-04 04:20:48 +0000954
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800955
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800956static void
Victor Stinnerfbca9082018-08-30 00:50:45 +0200957pymain_header(_PyMain *pymain, const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800958{
Victor Stinnerfbca9082018-08-30 00:50:45 +0200959 if (config->quiet) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800960 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000962
Victor Stinnerfbca9082018-08-30 00:50:45 +0200963 if (!config->verbose && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800964 return;
965 }
966
967 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
Victor Stinnerfbca9082018-08-30 00:50:45 +0200968 if (config->site_import) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800969 fprintf(stderr, "%s\n", COPYRIGHT);
970 }
971}
972
973
Victor Stinnerc4bca952017-12-19 23:48:17 +0100974static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200975pymain_init_core_argv(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100976{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100977 /* Copy argv to be able to modify it (to force -c/-m) */
978 int argc = pymain->argc - _PyOS_optind;
979 wchar_t **argv;
980
981 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100982 /* Ensure at least one (empty) argument is seen */
983 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +0100984 argc = 1;
Victor Stinner6c785c02018-08-01 17:56:14 +0200985 argv = _Py_wstrlist_copy(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +0100986 }
Victor Stinnerc4bca952017-12-19 23:48:17 +0100987 else {
Victor Stinner6c785c02018-08-01 17:56:14 +0200988 argv = _Py_wstrlist_copy(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100989 }
990
991 if (argv == NULL) {
992 pymain->err = _Py_INIT_NO_MEMORY();
993 return -1;
994 }
995
996 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100997 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100998 /* Force sys.argv[0] = '-c' */
999 arg0 = L"-c";
1000 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001001 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001002 /* Force sys.argv[0] = '-m'*/
1003 arg0 = L"-m";
1004 }
1005 if (arg0 != NULL) {
1006 arg0 = _PyMem_RawWcsdup(arg0);
1007 if (arg0 == NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +02001008 _Py_wstrlist_clear(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001009 pymain->err = _Py_INIT_NO_MEMORY();
1010 return -1;
1011 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001012
1013 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001014 PyMem_RawFree(argv[0]);
1015 argv[0] = arg0;
1016 }
1017
Victor Stinner1dc6e392018-07-25 02:49:17 +02001018 config->argc = argc;
1019 config->argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001020 return 0;
1021}
1022
1023
Victor Stinner2094c2b2018-09-03 17:06:39 +02001024PyObject*
1025_Py_wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001026{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001027 assert(list != NULL || len < 1);
1028
1029 PyObject *pylist = PyList_New(len);
1030 if (pylist == NULL) {
1031 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001032 }
1033
Victor Stinner8ded5b82018-01-24 17:03:28 +01001034 for (int i = 0; i < len; i++) {
1035 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001036 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001037 Py_DECREF(pylist);
1038 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001039 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001040 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001041 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001042 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001043}
1044
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001045
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001046static void
Victor Stinnerfbca9082018-08-30 00:50:45 +02001047pymain_import_readline(_PyMain *pymain, const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001048{
Victor Stinnerfbca9082018-08-30 00:50:45 +02001049 if (config->isolated) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001050 return;
1051 }
Victor Stinnerfbca9082018-08-30 00:50:45 +02001052 if (!config->inspect && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001053 return;
1054 }
1055 if (!isatty(fileno(stdin))) {
1056 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001057 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001058
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001059 PyObject *mod = PyImport_ImportModule("readline");
1060 if (mod == NULL) {
1061 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 }
1063 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001064 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001066}
1067
1068
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001069static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001070pymain_run_startup(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001071{
Victor Stinner6c785c02018-08-01 17:56:14 +02001072 const char *startup = _PyCoreConfig_GetEnv(config, "PYTHONSTARTUP");
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001073 if (startup == NULL) {
1074 return;
1075 }
1076
1077 FILE *fp = _Py_fopen(startup, "r");
1078 if (fp == NULL) {
1079 int save_errno = errno;
1080 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
1081 errno = save_errno;
1082
1083 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
1084 startup);
1085 PyErr_Print();
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001086 return;
1087 }
1088
1089 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
1090 PyErr_Clear();
1091 fclose(fp);
1092}
1093
1094
1095static void
Victor Stinner72ec3192018-08-02 19:34:20 +02001096pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001097{
Victor Stinner72ec3192018-08-02 19:34:20 +02001098 const wchar_t *filename = pymain->filename;
1099 FILE *fp = _Py_wfopen(filename, L"r");
1100 if (fp == NULL) {
1101 char *cfilename_buffer;
1102 const char *cfilename;
1103 int err = errno;
1104 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
1105 if (cfilename_buffer != NULL)
1106 cfilename = cfilename_buffer;
1107 else
1108 cfilename = "<unprintable file name>";
1109 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
1110 config->program, cfilename, err, strerror(err));
1111 PyMem_RawFree(cfilename_buffer);
1112 pymain->status = 2;
1113 return;
1114 }
1115
1116 if (pymain->skip_first_line) {
1117 int ch;
1118 /* Push back first newline so line numbers remain the same */
1119 while ((ch = getc(fp)) != EOF) {
1120 if (ch == '\n') {
1121 (void)ungetc(ch, fp);
1122 break;
1123 }
1124 }
1125 }
1126
1127 struct _Py_stat_struct sb;
1128 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
1129 fprintf(stderr,
1130 "%ls: '%ls' is a directory, cannot continue\n",
1131 config->program, filename);
1132 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001133 fclose(fp);
1134 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001135 }
1136
1137 /* call pending calls like signal handlers (SIGINT) */
1138 if (Py_MakePendingCalls() == -1) {
1139 PyErr_Print();
1140 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001141 fclose(fp);
1142 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001143 }
1144
1145 PyObject *unicode, *bytes = NULL;
1146 const char *filename_str;
1147
1148 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
1149 if (unicode != NULL) {
1150 bytes = PyUnicode_EncodeFSDefault(unicode);
1151 Py_DECREF(unicode);
1152 }
1153 if (bytes != NULL) {
1154 filename_str = PyBytes_AsString(bytes);
1155 }
1156 else {
1157 PyErr_Clear();
1158 filename_str = "<filename encoding error>";
1159 }
1160
Victor Stinnerd8078622018-08-03 23:54:06 +02001161 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
1162 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
Victor Stinner72ec3192018-08-02 19:34:20 +02001163 Py_XDECREF(bytes);
1164 pymain->status = (run != 0);
Victor Stinner72ec3192018-08-02 19:34:20 +02001165}
1166
1167
1168static void
1169pymain_run_stdin(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
1170{
1171 if (pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001172 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinnera4d20b22018-08-01 02:57:45 +02001173 config->inspect = 0;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001174 pymain_run_startup(pymain, config, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001175 pymain_run_interactive_hook();
1176 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001177
Victor Stinner72ec3192018-08-02 19:34:20 +02001178 /* call pending calls like signal handlers (SIGINT) */
1179 if (Py_MakePendingCalls() == -1) {
1180 PyErr_Print();
1181 pymain->status = 1;
1182 return;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001183 }
1184
Victor Stinner72ec3192018-08-02 19:34:20 +02001185 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
1186 pymain->status = (run != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001187}
1188
1189
1190static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001191pymain_repl(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001194 opportunity to set it from Python. */
Victor Stinner6c785c02018-08-01 17:56:14 +02001195 if (!Py_InspectFlag && _PyCoreConfig_GetEnv(config, "PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 Py_InspectFlag = 1;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001197 config->inspect = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001199
Victor Stinner19760862017-12-20 01:41:59 +01001200 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001201 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001203
1204 Py_InspectFlag = 0;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001205 config->inspect = 0;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001206 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001207
Victor Stinner19760862017-12-20 01:41:59 +01001208 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001209 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001210}
1211
1212
1213/* Parse the command line.
1214 Handle --version and --help options directly.
1215
1216 Return 1 if Python must exit.
1217 Return 0 on success.
1218 Set pymain->err and return -1 on failure. */
1219static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001220pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config,
1221 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001222{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001223 int res = pymain_parse_cmdline_impl(pymain, config, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001224 if (res < 0) {
1225 return -1;
1226 }
1227 if (res) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001228 pymain_usage(1, config->program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001229 pymain->status = 2;
1230 return 1;
1231 }
1232
Victor Stinnerca719ac2017-12-20 18:00:19 +01001233 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001234 /* Backup _PyOS_optind */
1235 _PyOS_optind--;
1236 }
1237
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001238 return 0;
1239}
1240
1241
Victor Stinnera7368ac2017-11-15 18:11:45 -08001242/* Parse command line options and environment variables.
1243 This code must not use Python runtime apart PyMem_Raw memory allocator.
1244
1245 Return 0 on success.
1246 Return 1 if Python is done and must exit.
1247 Set pymain->err and return -1 on error. */
1248static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001249pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
1250 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001251{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001252 _PyInitError err;
1253
Victor Stinner1dc6e392018-07-25 02:49:17 +02001254 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01001255 if (res != 0) {
1256 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001257 }
1258
Victor Stinner1dc6e392018-07-25 02:49:17 +02001259 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001260 return -1;
1261 }
1262
Victor Stinnerd1457752018-07-26 16:04:56 +02001263 err = _PyCoreConfig_Read(config);
1264 if (_Py_INIT_FAILED(err)) {
1265 pymain->err = err;
1266 return -1;
1267 }
1268
Victor Stinnerd1457752018-07-26 16:04:56 +02001269 if (config->use_environment) {
Victor Stinnerecf411c2018-07-26 02:37:22 +02001270 err = cmdline_init_env_warnoptions(pymain, config, cmdline);
1271 if (_Py_INIT_FAILED(err)) {
1272 pymain->err = err;
1273 return -1;
1274 }
1275 }
1276
Victor Stinnerd1457752018-07-26 16:04:56 +02001277 err = config_init_warnoptions(config, cmdline);
Victor Stinner31a83932017-12-04 13:39:15 +01001278 if (_Py_INIT_FAILED(err)) {
1279 pymain->err = err;
1280 return -1;
1281 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001282 return 0;
1283}
1284
1285
Victor Stinner177d9212018-08-29 11:25:15 +02001286/* Read the configuration and initialize the LC_CTYPE locale:
1287 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538). */
Victor Stinnera7368ac2017-11-15 18:11:45 -08001288static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001289pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
1290 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001291{
Victor Stinner89487f52018-08-23 12:23:46 +02001292 int init_utf8_mode = Py_UTF8Mode;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001293#ifdef MS_WINDOWS
1294 int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
1295#endif
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001296 _PyCoreConfig save_config = _PyCoreConfig_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001297 int res = -1;
Victor Stinner94540602017-12-16 04:54:22 +01001298 int locale_coerced = 0;
1299 int loops = 0;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001300
1301 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
1302 pymain->err = _Py_INIT_NO_MEMORY();
1303 goto done;
1304 }
Victor Stinner94540602017-12-16 04:54:22 +01001305
Victor Stinner73b00be2018-09-03 17:32:31 +02001306 /* Set LC_CTYPE to the user preferred locale */
1307 _Py_SetLocaleFromEnv(LC_CTYPE);
1308
Victor Stinner94540602017-12-16 04:54:22 +01001309 while (1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001310 int utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01001311 int encoding_changed = 0;
1312
1313 /* Watchdog to prevent an infinite loop */
1314 loops++;
1315 if (loops == 3) {
1316 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
1317 "reading the configuration");
1318 goto done;
1319 }
1320
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001321 /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
1322 on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
Victor Stinner89487f52018-08-23 12:23:46 +02001323 Py_UTF8Mode = config->utf8_mode;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001324#ifdef MS_WINDOWS
1325 Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
1326#endif
Victor Stinner89487f52018-08-23 12:23:46 +02001327
Victor Stinner1dc6e392018-07-25 02:49:17 +02001328 if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001329 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01001330 }
1331
Victor Stinner1dc6e392018-07-25 02:49:17 +02001332 int conf_res = pymain_read_conf_impl(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001333 if (conf_res != 0) {
1334 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01001335 goto done;
1336 }
1337
1338 /* The legacy C locale assumes ASCII as the default text encoding, which
1339 * causes problems not only for the CPython runtime, but also other
1340 * components like GNU readline.
1341 *
1342 * Accordingly, when the CLI detects it, it attempts to coerce it to a
1343 * more capable UTF-8 based alternative.
1344 *
1345 * See the documentation of the PYTHONCOERCECLOCALE setting for more
1346 * details.
1347 */
Victor Stinner06e76082018-09-19 14:56:36 -07001348 if (config->coerce_c_locale && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01001349 locale_coerced = 1;
Victor Stinner06e76082018-09-19 14:56:36 -07001350 _Py_CoerceLegacyLocale(config->coerce_c_locale_warn);
Victor Stinner94540602017-12-16 04:54:22 +01001351 encoding_changed = 1;
1352 }
1353
1354 if (utf8_mode == -1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001355 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01001356 /* UTF-8 Mode enabled */
1357 encoding_changed = 1;
1358 }
1359 }
1360 else {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001361 if (config->utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01001362 encoding_changed = 1;
1363 }
1364 }
1365
1366 if (!encoding_changed) {
1367 break;
1368 }
1369
Victor Stinnerd1457752018-07-26 16:04:56 +02001370 /* Reset the configuration before reading again the configuration,
1371 just keep UTF-8 Mode value. */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001372 int new_utf8_mode = config->utf8_mode;
Victor Stinner06e76082018-09-19 14:56:36 -07001373 int new_coerce_c_locale = config->coerce_c_locale;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001374 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
1375 pymain->err = _Py_INIT_NO_MEMORY();
1376 goto done;
1377 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001378 pymain_clear_cmdline(pymain, cmdline);
Victor Stinner6c5a4b32018-06-16 00:06:28 +02001379 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner1dc6e392018-07-25 02:49:17 +02001380 config->utf8_mode = new_utf8_mode;
Victor Stinner06e76082018-09-19 14:56:36 -07001381 config->coerce_c_locale = new_coerce_c_locale;
Victor Stinner94540602017-12-16 04:54:22 +01001382
1383 /* The encoding changed: read again the configuration
1384 with the new encoding */
1385 }
1386 res = 0;
1387
1388done:
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001389 _PyCoreConfig_Clear(&save_config);
Victor Stinner89487f52018-08-23 12:23:46 +02001390 Py_UTF8Mode = init_utf8_mode ;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001391#ifdef MS_WINDOWS
1392 Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;
1393#endif
Victor Stinnera7368ac2017-11-15 18:11:45 -08001394 return res;
1395}
1396
Victor Stinner91106cd2017-12-13 12:29:09 +01001397
Victor Stinnerda273412017-12-15 01:46:02 +01001398void
Victor Stinnerda273412017-12-15 01:46:02 +01001399_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
1400{
1401 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01001402 Py_CLEAR(config->executable);
1403 Py_CLEAR(config->prefix);
1404 Py_CLEAR(config->base_prefix);
1405 Py_CLEAR(config->exec_prefix);
1406 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001407 Py_CLEAR(config->warnoptions);
1408 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01001409 Py_CLEAR(config->module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06001410 Py_CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001411}
1412
1413
1414static PyObject*
1415config_copy_attr(PyObject *obj)
1416{
1417 if (PyUnicode_Check(obj)) {
1418 Py_INCREF(obj);
1419 return obj;
1420 }
1421 else if (PyList_Check(obj)) {
1422 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
1423 }
1424 else if (PyDict_Check(obj)) {
1425 /* The dict type is used for xoptions. Make the assumption that keys
1426 and values are immutables */
1427 return PyDict_Copy(obj);
1428 }
1429 else {
1430 PyErr_Format(PyExc_TypeError,
1431 "cannot copy config attribute of type %.200s",
1432 Py_TYPE(obj)->tp_name);
1433 return NULL;
1434 }
1435}
1436
1437
1438int
1439_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
1440 const _PyMainInterpreterConfig *config2)
1441{
1442 _PyMainInterpreterConfig_Clear(config);
1443
1444#define COPY_ATTR(ATTR) \
1445 do { \
1446 if (config2->ATTR != NULL) { \
1447 config->ATTR = config_copy_attr(config2->ATTR); \
1448 if (config->ATTR == NULL) { \
1449 return -1; \
1450 } \
1451 } \
1452 } while (0)
1453
1454 COPY_ATTR(argv);
Victor Stinner41264f12017-12-15 02:05:29 +01001455 COPY_ATTR(executable);
1456 COPY_ATTR(prefix);
1457 COPY_ATTR(base_prefix);
1458 COPY_ATTR(exec_prefix);
1459 COPY_ATTR(base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001460 COPY_ATTR(warnoptions);
1461 COPY_ATTR(xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01001462 COPY_ATTR(module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06001463 COPY_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001464#undef COPY_ATTR
1465 return 0;
1466}
1467
1468
Victor Stinner41264f12017-12-15 02:05:29 +01001469_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01001470_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
1471 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001472{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001473 if (main_config->install_signal_handlers < 0) {
1474 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001475 }
1476
Victor Stinner9cfc0022017-12-20 19:36:46 +01001477 if (main_config->xoptions == NULL) {
1478 main_config->xoptions = config_create_xoptions_dict(config);
1479 if (main_config->xoptions == NULL) {
1480 return _Py_INIT_NO_MEMORY();
1481 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001482 }
1483
Victor Stinner8ded5b82018-01-24 17:03:28 +01001484#define COPY_WSTR(ATTR) \
1485 do { \
Victor Stinner1dc6e392018-07-25 02:49:17 +02001486 if (main_config->ATTR == NULL && config->ATTR != NULL) { \
Victor Stinner8ded5b82018-01-24 17:03:28 +01001487 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
1488 if (main_config->ATTR == NULL) { \
1489 return _Py_INIT_NO_MEMORY(); \
1490 } \
1491 } \
1492 } while (0)
1493#define COPY_WSTRLIST(ATTR, LEN, LIST) \
1494 do { \
1495 if (ATTR == NULL) { \
Victor Stinner2094c2b2018-09-03 17:06:39 +02001496 ATTR = _Py_wstrlist_as_pylist(LEN, LIST); \
Victor Stinner8ded5b82018-01-24 17:03:28 +01001497 if (ATTR == NULL) { \
1498 return _Py_INIT_NO_MEMORY(); \
1499 } \
1500 } \
1501 } while (0)
1502
1503 COPY_WSTRLIST(main_config->warnoptions,
1504 config->nwarnoption, config->warnoptions);
1505 if (config->argc >= 0) {
1506 COPY_WSTRLIST(main_config->argv,
1507 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001508 }
1509
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001510 if (config->_install_importlib) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001511 COPY_WSTR(executable);
1512 COPY_WSTR(prefix);
1513 COPY_WSTR(base_prefix);
1514 COPY_WSTR(exec_prefix);
1515 COPY_WSTR(base_exec_prefix);
1516
1517 COPY_WSTRLIST(main_config->module_search_path,
1518 config->nmodule_search_path, config->module_search_paths);
Carl Meyerb193fa92018-06-15 22:40:56 -06001519
1520 if (config->pycache_prefix != NULL) {
1521 COPY_WSTR(pycache_prefix);
1522 } else {
1523 main_config->pycache_prefix = NULL;
1524 }
1525
Victor Stinner9cfc0022017-12-20 19:36:46 +01001526 }
Victor Stinner41264f12017-12-15 02:05:29 +01001527
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001528 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01001529#undef COPY_WSTR
1530#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001531}
1532
1533
1534static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001535pymain_init_python_main(_PyMain *pymain, _PyCoreConfig *config,
1536 PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001537{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001538 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001539
Victor Stinner9cfc0022017-12-20 19:36:46 +01001540 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001541 err = _PyMainInterpreterConfig_Read(&main_config, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001542 if (!_Py_INIT_FAILED(err)) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001543 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001544 }
1545 _PyMainInterpreterConfig_Clear(&main_config);
1546
1547 if (_Py_INIT_FAILED(err)) {
1548 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001549 return -1;
1550 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001551 return 0;
1552}
Victor Stinnera7368ac2017-11-15 18:11:45 -08001553
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001554
1555static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02001556pymain_run_python(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001557{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001558 int res = 0;
1559 _PyCoreConfig *config = &interp->core_config;
1560
1561 PyObject *main_importer_path = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001562 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01001563 /* If filename is a package (ex: directory or ZIP file) which contains
1564 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +02001565 prepended to sys.path.
1566
1567 Otherwise, main_importer_path is set to NULL. */
1568 main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01001569 }
1570
Victor Stinnerd3b19192018-07-25 10:21:03 +02001571 if (main_importer_path != NULL) {
1572 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
1573 pymain->status = 1;
1574 goto done;
1575 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001576 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001577 else if (!config->isolated) {
1578 PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc,
1579 config->argv);
1580 if (path0 == NULL) {
1581 pymain->err = _Py_INIT_NO_MEMORY();
1582 res = -1;
1583 goto done;
1584 }
Victor Stinner19760862017-12-20 01:41:59 +01001585
Victor Stinnerd3b19192018-07-25 10:21:03 +02001586 if (pymain_sys_path_add_path0(interp, path0) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001587 Py_DECREF(path0);
Victor Stinnerd3b19192018-07-25 10:21:03 +02001588 pymain->status = 1;
1589 goto done;
Victor Stinner19760862017-12-20 01:41:59 +01001590 }
1591 Py_DECREF(path0);
1592 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001593
Victor Stinner19760862017-12-20 01:41:59 +01001594 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08001595
Victor Stinnerfbca9082018-08-30 00:50:45 +02001596 pymain_header(pymain, config);
1597 pymain_import_readline(pymain, config);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001598
Victor Stinnerca719ac2017-12-20 18:00:19 +01001599 if (pymain->command) {
1600 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001601 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001602 else if (pymain->module) {
1603 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001604 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001605 else if (main_importer_path != NULL) {
1606 int sts = pymain_run_module(L"__main__", 0);
1607 pymain->status = (sts != 0);
1608 }
Victor Stinner72ec3192018-08-02 19:34:20 +02001609 else if (pymain->filename != NULL) {
1610 pymain_run_file(pymain, config, &cf);
1611 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001612 else {
Victor Stinner72ec3192018-08-02 19:34:20 +02001613 pymain_run_stdin(pymain, config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001614 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001615
Victor Stinner1dc6e392018-07-25 02:49:17 +02001616 pymain_repl(pymain, config, &cf);
Victor Stinnerd3b19192018-07-25 10:21:03 +02001617
1618done:
1619 Py_XDECREF(main_importer_path);
1620 return res;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001621}
1622
Victor Stinnera7368ac2017-11-15 18:11:45 -08001623
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001624static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001625pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
1626 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001627{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001628 pymain->err = _PyRuntime_Initialize();
1629 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001630 return -1;
1631 }
1632
Victor Stinner1dc6e392018-07-25 02:49:17 +02001633 int res = pymain_read_conf(pymain, config, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001634 if (res < 0) {
1635 return -1;
1636 }
1637 if (res > 0) {
1638 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01001639 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001640 }
1641
Victor Stinner94540602017-12-16 04:54:22 +01001642 if (cmdline->print_help) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001643 pymain_usage(0, config->program);
Victor Stinner19760862017-12-20 01:41:59 +01001644 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01001645 }
1646
1647 if (cmdline->print_version) {
1648 printf("Python %s\n",
1649 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01001650 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01001651 }
1652
Victor Stinnerc4bca952017-12-19 23:48:17 +01001653 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinner6c785c02018-08-01 17:56:14 +02001654 orig_argv = _Py_wstrlist_copy(pymain->argc, cmdline->argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001655 if (orig_argv == NULL) {
1656 pymain->err = _Py_INIT_NO_MEMORY();
1657 return -1;
1658 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001659 orig_argc = pymain->argc;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001660 return 0;
1661}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00001662
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001663
Victor Stinnerca719ac2017-12-20 18:00:19 +01001664/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
1665 LC_CTYPE locale and Py_DecodeLocale().
1666
1667 Configuration:
1668
1669 * Command line arguments
1670 * Environment variables
1671 * Py_xxx global configuration variables
1672
Victor Stinner1dc6e392018-07-25 02:49:17 +02001673 _PyCmdline is a temporary structure used to prioritize these
Victor Stinnerca719ac2017-12-20 18:00:19 +01001674 variables. */
1675static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001676pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerca719ac2017-12-20 18:00:19 +01001677{
Victor Stinner31e99082017-12-20 23:41:38 +01001678 /* Force default allocator, since pymain_free() and pymain_clear_config()
1679 must use the same allocator than this function. */
1680 PyMemAllocatorEx old_alloc;
1681 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
1682#ifdef Py_DEBUG
1683 PyMemAllocatorEx default_alloc;
1684 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
1685#endif
1686
Victor Stinner1dc6e392018-07-25 02:49:17 +02001687 _PyCmdline cmdline;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001688 memset(&cmdline, 0, sizeof(cmdline));
1689
Victor Stinner1dc6e392018-07-25 02:49:17 +02001690 int res = pymain_cmdline_impl(pymain, config, &cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001691
Victor Stinnerca719ac2017-12-20 18:00:19 +01001692 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01001693
1694#ifdef Py_DEBUG
1695 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
1696 PyMemAllocatorEx cur_alloc;
1697 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
1698 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
1699#endif
1700 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001701 return res;
1702}
1703
1704
Victor Stinner94540602017-12-16 04:54:22 +01001705static int
Victor Stinner06e76082018-09-19 14:56:36 -07001706pymain_init(_PyMain *pymain, PyInterpreterState **interp_p)
Victor Stinner94540602017-12-16 04:54:22 +01001707{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001708 /* 754 requires that FP exceptions run in "no stop" mode by default,
1709 * and until C vendors implement C99's ways to control FP exceptions,
1710 * Python requires non-stop mode. Alas, some platforms enable FP
1711 * exceptions by default. Here we disable them.
1712 */
1713#ifdef __FreeBSD__
1714 fedisableexcept(FE_OVERFLOW);
1715#endif
Victor Stinner94540602017-12-16 04:54:22 +01001716
Victor Stinner1dc6e392018-07-25 02:49:17 +02001717 _PyCoreConfig local_config = _PyCoreConfig_INIT;
1718 _PyCoreConfig *config = &local_config;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001719
Victor Stinner1dc6e392018-07-25 02:49:17 +02001720 _PyCoreConfig_GetGlobalConfig(config);
1721
1722 int cmd_res = pymain_cmdline(pymain, config);
1723 if (cmd_res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01001724 _Py_FatalInitError(pymain->err);
1725 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001726 if (cmd_res == 1) {
1727 pymain_clear_config(config);
1728 return 1;
Victor Stinner19760862017-12-20 01:41:59 +01001729 }
1730
Victor Stinner1dc6e392018-07-25 02:49:17 +02001731 _PyCoreConfig_SetGlobalConfig(config);
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001732
Victor Stinner1dc6e392018-07-25 02:49:17 +02001733 pymain_init_stdio(pymain, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001734
Victor Stinner1dc6e392018-07-25 02:49:17 +02001735 PyInterpreterState *interp;
1736 pymain->err = _Py_InitializeCore(&interp, config);
1737 if (_Py_INIT_FAILED(pymain->err)) {
1738 _Py_FatalInitError(pymain->err);
1739 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001740 *interp_p = interp;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001741
1742 pymain_clear_config(config);
1743 config = &interp->core_config;
1744
1745 if (pymain_init_python_main(pymain, config, interp) < 0) {
1746 _Py_FatalInitError(pymain->err);
1747 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001748 return 0;
1749}
1750
1751
1752static int
Victor Stinner06e76082018-09-19 14:56:36 -07001753pymain_main(_PyMain *pymain)
Victor Stinner1dc6e392018-07-25 02:49:17 +02001754{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001755 PyInterpreterState *interp;
Victor Stinner06e76082018-09-19 14:56:36 -07001756 int res = pymain_init(pymain, &interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +02001757 if (res != 1) {
Victor Stinnerd3b19192018-07-25 10:21:03 +02001758 if (pymain_run_python(pymain, interp) < 0) {
1759 _Py_FatalInitError(pymain->err);
1760 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001761
1762 if (Py_FinalizeEx() < 0) {
1763 /* Value unlikely to be confused with a non-error exit status or
1764 other special meaning */
1765 pymain->status = 120;
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001766 }
Victor Stinner19760862017-12-20 01:41:59 +01001767 }
1768
Victor Stinner94540602017-12-16 04:54:22 +01001769 pymain_free(pymain);
1770
Victor Stinner94540602017-12-16 04:54:22 +01001771 return pymain->status;
1772}
1773
1774
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001775int
1776Py_Main(int argc, wchar_t **argv)
1777{
1778 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001779 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001780 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001781 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001782
Victor Stinner06e76082018-09-19 14:56:36 -07001783 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00001784}
1785
Victor Stinner94540602017-12-16 04:54:22 +01001786
1787int
1788_Py_UnixMain(int argc, char **argv)
1789{
1790 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001791 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001792 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01001793 pymain.bytes_argv = argv;
1794
Victor Stinner06e76082018-09-19 14:56:36 -07001795 return pymain_main(&pymain);
Victor Stinner94540602017-12-16 04:54:22 +01001796}
1797
1798
Skip Montanaro786ea6b2004-03-01 15:44:05 +00001799/* this is gonna seem *real weird*, but if you put some other code between
1800 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
1801 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00001802
Guido van Rossum667d7041995-08-04 04:20:48 +00001803/* Make the *original* argc/argv available to other modules.
1804 This is rare, but it is needed by the secureware extension. */
1805
1806void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001807Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00001808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 *argc = orig_argc;
1810 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00001811}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001812
1813#ifdef __cplusplus
1814}
1815#endif