blob: f6e78890085b6f068a407f30a3854b19355de2e8 [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_pathconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01007#include "pycore_pylifecycle.h"
8#include "pycore_pymem.h"
9#include "pycore_pystate.h"
Guido van Rossum667d7041995-08-04 04:20:48 +000010
Antoine Pitrou5651eaa2008-09-06 20:46:58 +000011#include <locale.h>
Gregory P. Smith38f11cc2019-02-16 12:57:40 -080012#ifdef HAVE_SIGNAL_H
13#include <signal.h>
14#endif
15#include <stdio.h>
16#if defined(HAVE_GETPID) && defined(HAVE_UNISTD_H)
17#include <unistd.h>
18#endif
Antoine Pitrou5651eaa2008-09-06 20:46:58 +000019
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000020#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010021# include <windows.h>
22# ifdef HAVE_IO_H
23# include <io.h>
24# endif
25# ifdef HAVE_FCNTL_H
26# include <fcntl.h>
27# endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000028#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000029
Martin v. Löwis945362c2007-08-30 14:57:25 +000030#ifdef _MSC_VER
Victor Stinner6efcb6d2017-12-18 23:42:55 +010031# include <crtdbg.h>
32#endif
33
34#ifdef __FreeBSD__
35# include <fenv.h>
Martin v. Löwis945362c2007-08-30 14:57:25 +000036#endif
37
Jesus Ceaab70e2a2012-10-05 01:48:08 +020038#if defined(MS_WINDOWS)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010039# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
Guido van Rossuma075ce11997-12-05 21:56:45 +000040#else
Victor Stinner6efcb6d2017-12-18 23:42:55 +010041# define PYTHONHOMEHELP "<prefix>/lib/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000042#endif
43
Guido van Rossuma22865e2000-09-05 04:41:18 +000044#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000045 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
46 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000047
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000048#ifdef __cplusplus
49extern "C" {
50#endif
51
Victor Stinner46972b72017-11-24 22:55:40 +010052#define DECODE_LOCALE_ERR(NAME, LEN) \
53 (((LEN) == -2) \
Victor Stinner94540602017-12-16 04:54:22 +010054 ? _Py_INIT_USER_ERR("cannot decode " NAME) \
Victor Stinner46972b72017-11-24 22:55:40 +010055 : _Py_INIT_NO_MEMORY())
56
57
Victor Stinnerca719ac2017-12-20 18:00:19 +010058#ifdef MS_WINDOWS
59#define WCSTOK wcstok_s
60#else
61#define WCSTOK wcstok
62#endif
63
Guido van Rossumac56b031996-07-21 02:33:38 +000064/* For Py_GetArgcArgv(); set by main() */
Victor Stinner94540602017-12-16 04:54:22 +010065static wchar_t **orig_argv = NULL;
66static int orig_argc = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +000067
Guido van Rossumbceccf52001-04-10 22:07:43 +000068/* command line options */
Christian Heimesad73a9c2013-08-10 16:36:18 +020069#define BASE_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000070
Guido van Rossumbceccf52001-04-10 22:07:43 +000071#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000072
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080073static const _PyOS_LongOption longoptions[] = {
74 {L"check-hash-based-pycs", 1, 0},
75 {NULL, 0, 0},
76};
77
Guido van Rossum667d7041995-08-04 04:20:48 +000078/* Short usage message (with %s for argv0) */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020079static const char usage_line[] =
Martin v. Löwis790465f2008-04-05 20:41:37 +000080"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000081
82/* Long usage message, split into parts < 512 bytes */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020083static const char usage_1[] = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000084Options and arguments (and corresponding environment variables):\n\
Christian Heimes2ab34442008-09-03 20:31:07 +000085-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
86 and comparing bytes/bytearray with str. (-bb: issue errors)\n\
Xiang Zhang0710d752017-03-11 13:02:52 +080087-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000088-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000089-d : debug output from parser; also PYTHONDEBUG=x\n\
Christian Heimes790c8232008-01-07 21:14:23 +000090-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000092";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020093static const char usage_2[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000094-i : inspect interactively after running script; forces a prompt even\n\
95 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Christian Heimesad73a9c2013-08-10 16:36:18 +020096-I : isolate Python from the user's environment (implies -E and -s)\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000097-m mod : run library module as a script (terminates option list)\n\
Cheryl Sabella186b6062018-02-24 22:04:40 -050098-O : remove assert and __debug__-dependent statements; add .opt-1 before\n\
99 .pyc extension; also PYTHONOPTIMIZE=x\n\
100-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
101 .pyc extension\n\
Georg Brandl9d871192010-12-04 10:47:18 +0000102-q : don't print version and copyright messages on interactive startup\n\
Christian Heimes8dc226f2008-05-06 23:45:46 +0000103-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000104-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000105";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200106static const char usage_3[] = "\
Berker Peksag7f580972017-10-13 15:16:31 +0300107-u : force the stdout and stderr streams to be unbuffered;\n\
108 this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
110 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000111-V : print the Python version number and exit (also --version)\n\
INADA Naoki0e175a62016-11-21 20:57:14 +0900112 when given twice, print more information about the build\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000113-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenvey0805ca32010-04-07 04:04:10 +0000114 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000115-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000116-X opt : set implementation-specific option\n\
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800117--check-hash-based-pycs always|default|never:\n\
118 control how Python invalidates hash-based .pyc files\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000119";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200120static const char usage_4[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +0000121file : program read from script file\n\
122- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000123arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000124Other environment variables:\n\
125PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200126PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000127 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +0000128";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200129static const char usage_5[] =
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200130"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
Victor Stinner9802b392010-08-19 11:36:43 +0000131" The default module search path uses %s.\n"
132"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
133"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
Victor Stinner34be807c2016-03-14 12:04:26 +0100134"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
135static const char usage_6[] =
136"PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
137" to seed the hashes of str, bytes and datetime objects. It can also be\n"
138" set to an integer in the range [0,4294967295] to get hash values with a\n"
139" predictable seed.\n"
140"PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
141" on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000142" hooks.\n"
Stéphane Wirtel7d1017d2017-06-12 13:30:33 +0200143"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000144" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
Victor Stinner5e3806f2017-11-30 11:40:24 +0100145" locale coercion and locale compatibility warnings on stderr.\n"
Stéphane Wirtelb7fd7382018-07-29 12:27:16 +0200146"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
147" debugger. It can be set to the callable of your debugger of choice.\n"
Carl Meyerb193fa92018-06-15 22:40:56 -0600148"PYTHONDEVMODE: enable the development mode.\n"
149"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
Guido van Rossum667d7041995-08-04 04:20:48 +0000150
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800151static void
Victor Stinnera7368ac2017-11-15 18:11:45 -0800152pymain_usage(int error, const wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000153{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800154 FILE *f = error ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 fprintf(f, usage_line, program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800157 if (error)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 fprintf(f, "Try `python -h' for more information.\n");
159 else {
160 fputs(usage_1, f);
161 fputs(usage_2, f);
162 fputs(usage_3, f);
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200163 fprintf(f, usage_4, (wint_t)DELIM);
164 fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100165 fputs(usage_6, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000167}
168
Victor Stinnera7368ac2017-11-15 18:11:45 -0800169
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800170static void
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800171pymain_run_interactive_hook(void)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200172{
173 PyObject *sys, *hook, *result;
174 sys = PyImport_ImportModule("sys");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800175 if (sys == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200176 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800177 }
178
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200179 hook = PyObject_GetAttrString(sys, "__interactivehook__");
180 Py_DECREF(sys);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800181 if (hook == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200182 PyErr_Clear();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800183 return;
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200184 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800185
186 result = _PyObject_CallNoArg(hook);
187 Py_DECREF(hook);
188 if (result == NULL) {
189 goto error;
190 }
191 Py_DECREF(result);
192
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200193 return;
194
195error:
196 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
197 PyErr_Print();
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200198}
199
Thomas Woutersa9773292006-04-21 09:43:23 +0000200
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800201static int
Victor Stinnerc4bca952017-12-19 23:48:17 +0100202pymain_run_module(const wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 PyObject *module, *runpy, *runmodule, *runargs, *result;
205 runpy = PyImport_ImportModule("runpy");
206 if (runpy == NULL) {
207 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200208 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 return -1;
210 }
211 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
212 if (runmodule == NULL) {
213 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200214 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_DECREF(runpy);
216 return -1;
217 }
218 module = PyUnicode_FromWideChar(modname, wcslen(modname));
219 if (module == NULL) {
220 fprintf(stderr, "Could not convert module name to unicode\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 return -1;
225 }
226 runargs = Py_BuildValue("(Oi)", module, set_argv0);
227 if (runargs == NULL) {
228 fprintf(stderr,
229 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200230 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 Py_DECREF(runpy);
232 Py_DECREF(runmodule);
233 Py_DECREF(module);
234 return -1;
235 }
236 result = PyObject_Call(runmodule, runargs, NULL);
237 if (result == NULL) {
238 PyErr_Print();
239 }
240 Py_DECREF(runpy);
241 Py_DECREF(runmodule);
242 Py_DECREF(module);
243 Py_DECREF(runargs);
244 if (result == NULL) {
245 return -1;
246 }
247 Py_DECREF(result);
248 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000249}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000250
Nick Coghland2977a32017-03-12 20:38:32 +1000251static PyObject *
Victor Stinnerc4bca952017-12-19 23:48:17 +0100252pymain_get_importer(const wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000253{
Nick Coghland2977a32017-03-12 20:38:32 +1000254 PyObject *sys_path0 = NULL, *importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000255
Nick Coghland2977a32017-03-12 20:38:32 +1000256 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800257 if (sys_path0 == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000258 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800259 }
Victor Stinner4726e402010-10-06 23:24:57 +0000260
Nick Coghland2977a32017-03-12 20:38:32 +1000261 importer = PyImport_GetImporter(sys_path0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800262 if (importer == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000263 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800264 }
Victor Stinner4726e402010-10-06 23:24:57 +0000265
Brett Cannonaa936422012-04-27 15:30:58 -0400266 if (importer == Py_None) {
Nick Coghland2977a32017-03-12 20:38:32 +1000267 Py_DECREF(sys_path0);
Victor Stinner4726e402010-10-06 23:24:57 +0000268 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000269 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800271
Victor Stinner4726e402010-10-06 23:24:57 +0000272 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000273 return sys_path0;
Victor Stinner4726e402010-10-06 23:24:57 +0000274
Nick Coghland2977a32017-03-12 20:38:32 +1000275error:
276 Py_XDECREF(sys_path0);
277 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
278 PyErr_Print();
Nick Coghland2977a32017-03-12 20:38:32 +1000279 return NULL;
280}
281
282
283static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800284pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
Victor Stinnera62207c2010-08-07 10:57:17 +0000285{
286 PyObject *unicode, *bytes;
287 int ret;
288
289 unicode = PyUnicode_FromWideChar(command, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800290 if (unicode == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000291 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800292 }
293
Victor Stinnera62207c2010-08-07 10:57:17 +0000294 bytes = PyUnicode_AsUTF8String(unicode);
295 Py_DECREF(unicode);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800296 if (bytes == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000297 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800298 }
299
Victor Stinnera62207c2010-08-07 10:57:17 +0000300 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
301 Py_DECREF(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800302 return (ret != 0);
Victor Stinnera62207c2010-08-07 10:57:17 +0000303
304error:
Victor Stinner398356b2010-08-18 22:23:22 +0000305 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000306 PyErr_Print();
307 return 1;
308}
309
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800310
Guido van Rossum667d7041995-08-04 04:20:48 +0000311/* Main program */
312
Eric Snow6b4be192017-05-22 21:36:03 -0700313typedef struct {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100314 wchar_t **argv;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200315 int nwarnoption; /* Number of -W command line options */
316 wchar_t **warnoptions; /* Command line -W options */
317 int nenv_warnoption; /* Number of PYTHONWARNINGS environment variables */
318 wchar_t **env_warnoptions; /* PYTHONWARNINGS environment variables */
Eric Snow6b4be192017-05-22 21:36:03 -0700319 int print_help; /* -h, -? options */
320 int print_version; /* -V option */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200321} _PyCmdline;
Eric Snow6b4be192017-05-22 21:36:03 -0700322
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800323/* Structure used by Py_Main() to pass data to subfunctions */
324typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100325 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800326 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100327 int use_bytes_argv;
328 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100329 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100330
331 /* Exit status or "exit code": result of pymain_main() */
332 int status;
333 /* Error message if a function failed */
334 _PyInitError err;
335
Victor Stinner19760862017-12-20 01:41:59 +0100336 /* non-zero is stdin is a TTY or if -i option is used */
337 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100338 int skip_first_line; /* -x option */
339 wchar_t *filename; /* Trailing arg without -c or -m */
340 wchar_t *command; /* -c argument */
341 wchar_t *module; /* -m argument */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800342} _PyMain;
343
Victor Stinner1dc6e392018-07-25 02:49:17 +0200344#define _PyMain_INIT {.err = _Py_INIT_OK()}
Victor Stinnerd5dda982017-12-13 17:31:16 +0100345/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800346
347
Victor Stinner19760862017-12-20 01:41:59 +0100348/* Non-zero if filename, command (-c) or module (-m) is set
349 on the command line */
350#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100351 (pymain->command != NULL || pymain->filename != NULL \
352 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100353
354
Victor Stinnerca719ac2017-12-20 18:00:19 +0100355static wchar_t*
356pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800357{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100358 wchar_t *str2 = _PyMem_RawWcsdup(str);
359 if (str2 == NULL) {
360 pymain->err = _Py_INIT_NO_MEMORY();
361 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800362 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100363 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800364}
365
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100366
Victor Stinnerc4bca952017-12-19 23:48:17 +0100367static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200368pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,
369 _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100370{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100371 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100372
Victor Stinnerca719ac2017-12-20 18:00:19 +0100373 if (pymain->use_bytes_argv) {
374 /* +1 for a the NULL terminator */
375 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
376 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
377 if (argv == NULL) {
378 pymain->err = _Py_INIT_NO_MEMORY();
379 return -1;
380 }
381
382 for (int i = 0; i < pymain->argc; i++) {
383 size_t len;
384 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
385 if (arg == NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +0200386 _Py_wstrlist_clear(i, argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100387 pymain->err = DECODE_LOCALE_ERR("command line arguments",
388 (Py_ssize_t)len);
389 return -1;
390 }
391 argv[i] = arg;
392 }
393 argv[pymain->argc] = NULL;
394
395 cmdline->argv = argv;
396 }
397 else {
398 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100399 }
400
Victor Stinnerca719ac2017-12-20 18:00:19 +0100401 wchar_t *program;
402 if (pymain->argc >= 1 && cmdline->argv != NULL) {
403 program = cmdline->argv[0];
404 }
405 else {
406 program = L"";
407 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200408 config->program = pymain_wstrdup(pymain, program);
409 if (config->program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100410 return -1;
411 }
412
Victor Stinnerc4bca952017-12-19 23:48:17 +0100413 return 0;
414}
415
416
417static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200418pymain_clear_cmdline(_PyMain *pymain, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100419{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100420 PyMemAllocatorEx old_alloc;
421 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100422
Victor Stinner6c785c02018-08-01 17:56:14 +0200423 _Py_wstrlist_clear(cmdline->nwarnoption, cmdline->warnoptions);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100424 cmdline->nwarnoption = 0;
425 cmdline->warnoptions = NULL;
426
Victor Stinner6c785c02018-08-01 17:56:14 +0200427 _Py_wstrlist_clear(cmdline->nenv_warnoption, cmdline->env_warnoptions);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100428 cmdline->nenv_warnoption = 0;
429 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100430
431 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +0200432 _Py_wstrlist_clear(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100433 }
434 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100435
436 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
437}
438
439
440static void
441pymain_clear_pymain(_PyMain *pymain)
442{
443#define CLEAR(ATTR) \
444 do { \
445 PyMem_RawFree(ATTR); \
446 ATTR = NULL; \
447 } while (0)
448
449 CLEAR(pymain->filename);
450 CLEAR(pymain->command);
451 CLEAR(pymain->module);
452#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100453}
454
Victor Stinnerc4bca952017-12-19 23:48:17 +0100455static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200456pymain_clear_config(_PyCoreConfig *config)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100457{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100458 /* Clear core config with the memory allocator
459 used by pymain_read_conf() */
460 PyMemAllocatorEx old_alloc;
461 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
462
Victor Stinner1dc6e392018-07-25 02:49:17 +0200463 _PyCoreConfig_Clear(config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100464
465 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
466}
467
468
469static void
Victor Stinnerd1457752018-07-26 16:04:56 +0200470pymain_free(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100471{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100472 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100473
Victor Stinnerc4bca952017-12-19 23:48:17 +0100474 /* Free global variables which cannot be freed in Py_Finalize():
475 configuration options set before Py_Initialize() which should
476 remain valid after Py_Finalize(), since
477 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinnerb1147e42018-07-21 02:06:16 +0200478 _PyPathConfig_ClearGlobal();
Victor Stinner124b9eb2018-08-29 01:29:06 +0200479 _Py_ClearStandardStreamEncoding();
Victor Stinner94540602017-12-16 04:54:22 +0100480
Victor Stinnerc4bca952017-12-19 23:48:17 +0100481 /* Force the allocator used by pymain_read_conf() */
482 PyMemAllocatorEx old_alloc;
483 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100484
Victor Stinnerca719ac2017-12-20 18:00:19 +0100485 pymain_clear_pymain(pymain);
486
Victor Stinner6c785c02018-08-01 17:56:14 +0200487 _Py_wstrlist_clear(orig_argc, orig_argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100488 orig_argc = 0;
489 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100490
Victor Stinnerc4bca952017-12-19 23:48:17 +0100491 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200492
493#ifdef __INSURE__
494 /* Insure++ is a memory analysis tool that aids in discovering
495 * memory leaks and other memory problems. On Python exit, the
496 * interned string dictionaries are flagged as being in use at exit
497 * (which it is). Under normal circumstances, this is fine because
498 * the memory will be automatically reclaimed by the system. Under
499 * memory debugging, it's a huge source of useless noise, so we
500 * trade off slower shutdown for less distraction in the memory
501 * reports. -baw
502 */
503 _Py_ReleaseInternedUnicodeStrings();
504#endif /* __INSURE__ */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800505}
506
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100507
Eric Snow6b4be192017-05-22 21:36:03 -0700508static int
Victor Stinnerd3b19192018-07-25 10:21:03 +0200509pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
Guido van Rossum667d7041995-08-04 04:20:48 +0000510{
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200511 _Py_IDENTIFIER(path);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200512 PyObject *sys_path;
513 PyObject *sysdict = interp->sysdict;
514 if (sysdict != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200515 sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
516 if (sys_path == NULL && PyErr_Occurred()) {
517 goto error;
518 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200519 }
520 else {
521 sys_path = NULL;
522 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800523 if (sys_path == NULL) {
524 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
525 goto error;
526 }
527
Victor Stinnerd3b19192018-07-25 10:21:03 +0200528 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800529 goto error;
530 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200531 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800532
533error:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800534 PyErr_Print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200535 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800536}
537
538
Victor Stinnerb1147e42018-07-21 02:06:16 +0200539_PyInitError
540_Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800541{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200542 if (*len == INT_MAX) {
543 /* len+1 would overflow */
544 return _Py_INIT_NO_MEMORY();
545 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100546 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800547 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100548 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800549 }
550
Victor Stinnerca719ac2017-12-20 18:00:19 +0100551 size_t size = (*len + 1) * sizeof(list[0]);
552 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
553 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800554 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100555 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800556 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100557 list2[*len] = str2;
558 *list = list2;
559 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100560 return _Py_INIT_OK();
561}
562
563
564static int
565pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
566{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200567 _PyInitError err = _Py_wstrlist_append(len, list, str);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100568 if (_Py_INIT_FAILED(err)) {
569 pymain->err = err;
570 return -1;
571 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800572 return 0;
573}
574
575
576/* Parse the command line arguments
577 Return 0 on success.
578 Return 1 if parsing failed.
579 Set pymain->err and return -1 on other errors. */
580static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200581pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
582 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800583{
Antoine Pitrou86838b02012-02-21 19:03:47 +0100584 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800585 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800586 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100587 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800588 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800589 if (c == EOF) {
590 break;
591 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 /* -c is the last option; following arguments
595 that look like options are left for the
596 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800597 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
598 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
599 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100600 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800601 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800602 }
Victor Stinner58d16832018-05-31 15:09:28 +0200603 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 command[len - 2] = '\n';
605 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100606 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 break;
608 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 if (c == 'm') {
611 /* -m is the last option; following arguments
612 that look like options are left for the
613 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100614 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
615 if (pymain->module == NULL) {
616 return -1;
617 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 break;
619 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800622 case 0:
623 // Handle long option.
624 assert(longindex == 0); // Only one long option now.
625 if (!wcscmp(_PyOS_optarg, L"always")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200626 config->_check_hash_pycs_mode = "always";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800627 } else if (!wcscmp(_PyOS_optarg, L"never")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200628 config->_check_hash_pycs_mode = "never";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800629 } else if (!wcscmp(_PyOS_optarg, L"default")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200630 config->_check_hash_pycs_mode = "default";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800631 } else {
632 fprintf(stderr, "--check-hash-based-pycs must be one of "
633 "'default', 'always', or 'never'\n");
634 return 1;
635 }
636 break;
637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 case 'b':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200639 config->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 case 'd':
Victor Stinner98512272018-08-01 03:07:00 +0200643 config->parser_debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 case 'i':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200647 config->inspect++;
648 config->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000650
Christian Heimesad73a9c2013-08-10 16:36:18 +0200651 case 'I':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200652 config->isolated++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200653 break;
654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 case 'O':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200658 config->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 case 'B':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200662 config->write_bytecode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 case 's':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200666 config->user_site_directory = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 case 'S':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200670 config->site_import = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 case 'E':
Victor Stinnerd1457752018-07-26 16:04:56 +0200674 config->use_environment = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 case 't':
678 /* ignored for backwards compatibility */
679 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 case 'u':
Victor Stinner98512272018-08-01 03:07:00 +0200682 config->buffered_stdio = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 case 'v':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200686 config->verbose++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100690 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 case 'h':
694 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -0700695 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -0700699 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100703 if (pymain_wstrlist_append(pymain,
704 &cmdline->nwarnoption,
705 &cmdline->warnoptions,
706 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800707 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800708 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000710
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000711 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100712 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +0100713 &config->nxoption,
714 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +0100715 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800716 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800717 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000718 break;
719
Georg Brandl9d871192010-12-04 10:47:18 +0000720 case 'q':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200721 config->quiet++;
Georg Brandl9d871192010-12-04 10:47:18 +0000722 break;
723
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100724 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100725 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100726 break;
727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800731 /* unknown argument: parsing failed */
732 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800734 } while (1);
735
Victor Stinnerca719ac2017-12-20 18:00:19 +0100736 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800737 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +0100738 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800739 {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100740 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
741 if (pymain->filename == NULL) {
742 return -1;
743 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000745
Victor Stinnerd5dda982017-12-13 17:31:16 +0100746 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100747 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +0100748
Eric Snow6b4be192017-05-22 21:36:03 -0700749 return 0;
750}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000751
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800752
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800753static int
Victor Stinner9cfc0022017-12-20 19:36:46 +0100754add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +0100755{
756 PyObject *name, *value;
757
758 const wchar_t *name_end = wcschr(s, L'=');
759 if (!name_end) {
760 name = PyUnicode_FromWideChar(s, -1);
761 value = Py_True;
762 Py_INCREF(value);
763 }
764 else {
765 name = PyUnicode_FromWideChar(s, name_end - s);
766 value = PyUnicode_FromWideChar(name_end + 1, -1);
767 }
768 if (name == NULL || value == NULL) {
769 goto error;
770 }
771 if (PyDict_SetItem(opts, name, value) < 0) {
772 goto error;
773 }
774 Py_DECREF(name);
775 Py_DECREF(value);
776 return 0;
777
778error:
779 Py_XDECREF(name);
780 Py_XDECREF(value);
781 return -1;
782}
783
Victor Stinner9cfc0022017-12-20 19:36:46 +0100784
785static PyObject*
786config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800787{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100788 int nxoption = config->nxoption;
789 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +0100790 PyObject *dict = PyDict_New();
791 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100792 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +0100793 }
794
Victor Stinnerca719ac2017-12-20 18:00:19 +0100795 for (int i=0; i < nxoption; i++) {
796 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +0100797 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +0100798 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100799 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800800 }
801 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100802
Victor Stinner9cfc0022017-12-20 19:36:46 +0100803 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -0700804}
805
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800806
Victor Stinner9cfc0022017-12-20 19:36:46 +0100807static _PyInitError
808config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -0700809{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100810 for (int i = 0; i < len; i++) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200811 _PyInitError err = _Py_wstrlist_append(&config->nwarnoption,
812 &config->warnoptions,
813 options[i]);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100814 if (_Py_INIT_FAILED(err)) {
815 return err;
Eric Snow6b4be192017-05-22 21:36:03 -0700816 }
Eric Snow6b4be192017-05-22 21:36:03 -0700817 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100818 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800819}
Eric Snow6b4be192017-05-22 21:36:03 -0700820
Victor Stinner747f48e2017-12-12 22:59:48 +0100821
Victor Stinner9cfc0022017-12-20 19:36:46 +0100822static _PyInitError
Victor Stinner1dc6e392018-07-25 02:49:17 +0200823config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +0100824{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100825 _PyInitError err;
826
827 assert(config->nwarnoption == 0);
828
Victor Stinner747f48e2017-12-12 22:59:48 +0100829 /* The priority order for warnings configuration is (highest precedence
830 * first):
831 *
832 * - the BytesWarning filter, if needed ('-b', '-bb')
833 * - any '-W' command line options; then
834 * - the 'PYTHONWARNINGS' environment variable; then
835 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
836 * - any implicit filters added by _warnings.c/warnings.py
837 *
838 * All settings except the last are passed to the warnings module via
839 * the `sys.warnoptions` list. Since the warnings module works on the basis
840 * of "the most recently added filter will be checked first", we add
841 * the lowest precedence entries first so that later entries override them.
842 */
843
Victor Stinner9cfc0022017-12-20 19:36:46 +0100844 if (config->dev_mode) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200845 err = _Py_wstrlist_append(&config->nwarnoption,
846 &config->warnoptions,
847 L"default");
Victor Stinner9cfc0022017-12-20 19:36:46 +0100848 if (_Py_INIT_FAILED(err)) {
849 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100850 }
Victor Stinner747f48e2017-12-12 22:59:48 +0100851 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100852
Victor Stinner9cfc0022017-12-20 19:36:46 +0100853 err = config_add_warnings_optlist(config,
854 cmdline->nenv_warnoption,
855 cmdline->env_warnoptions);
856 if (_Py_INIT_FAILED(err)) {
857 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100858 }
859
Victor Stinner9cfc0022017-12-20 19:36:46 +0100860 err = config_add_warnings_optlist(config,
861 cmdline->nwarnoption,
862 cmdline->warnoptions);
863 if (_Py_INIT_FAILED(err)) {
864 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100865 }
866
867 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
868 * don't even try to emit a warning, so we skip setting the filter in that
869 * case.
870 */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200871 if (config->bytes_warning) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100872 wchar_t *filter;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200873 if (config->bytes_warning> 1) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100874 filter = L"error::BytesWarning";
875 }
876 else {
877 filter = L"default::BytesWarning";
878 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200879 err = _Py_wstrlist_append(&config->nwarnoption,
880 &config->warnoptions,
881 filter);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100882 if (_Py_INIT_FAILED(err)) {
883 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100884 }
885 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100886 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +0100887}
888
889
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800890/* Get warning options from PYTHONWARNINGS environment variable.
891 Return 0 on success.
892 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +0100893static _PyInitError
Victor Stinnerd1457752018-07-26 16:04:56 +0200894cmdline_init_env_warnoptions(_PyMain *pymain, const _PyCoreConfig *config,
895 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800896{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100897 wchar_t *env;
Victor Stinner6c785c02018-08-01 17:56:14 +0200898 int res = _PyCoreConfig_GetEnvDup(config, &env,
899 L"PYTHONWARNINGS", "PYTHONWARNINGS");
Victor Stinnerca719ac2017-12-20 18:00:19 +0100900 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100901 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100902 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800903
Victor Stinnerca719ac2017-12-20 18:00:19 +0100904 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100905 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +0100906 }
Philip Jenvey0805ca32010-04-07 04:04:10 +0000907
Victor Stinnerca719ac2017-12-20 18:00:19 +0100908
909 wchar_t *warning, *context = NULL;
910 for (warning = WCSTOK(env, L",", &context);
911 warning != NULL;
912 warning = WCSTOK(NULL, L",", &context))
913 {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200914 _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption,
915 &cmdline->env_warnoptions,
916 warning);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100917 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100918 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100919 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800920 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100922 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100923 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800924}
925
926
927static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200928pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800929{
930 pymain->stdin_is_interactive = (isatty(fileno(stdin))
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200931 || config->interactive);
Guido van Rossum775af911997-02-14 19:50:32 +0000932
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000933#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +0000934 /* don't translate newlines (\r\n <=> \n) */
935 _setmode(fileno(stdin), O_BINARY);
936 _setmode(fileno(stdout), O_BINARY);
937 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000938#endif
Victor Stinner89e34362011-01-07 18:47:22 +0000939
Victor Stinner98512272018-08-01 03:07:00 +0200940 if (!config->buffered_stdio) {
Guido van Rossum22ffac11998-03-06 15:30:39 +0000941#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
943 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
944 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000945#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 setbuf(stdin, (char *)NULL);
947 setbuf(stdout, (char *)NULL);
948 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000949#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200951 else if (config->interactive) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000952#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 /* Doesn't have to have line-buffered -- use unbuffered */
954 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
955 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000956#else /* !MS_WINDOWS */
957#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
959 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000960#endif /* HAVE_SETVBUF */
961#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 /* Leave stderr alone - it should be unbuffered anyway. */
963 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800964}
Guido van Rossum667d7041995-08-04 04:20:48 +0000965
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800966
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800967static void
Victor Stinnerfbca9082018-08-30 00:50:45 +0200968pymain_header(_PyMain *pymain, const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800969{
Victor Stinnerfbca9082018-08-30 00:50:45 +0200970 if (config->quiet) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800971 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000973
Victor Stinnerfbca9082018-08-30 00:50:45 +0200974 if (!config->verbose && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800975 return;
976 }
977
978 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
Victor Stinnerfbca9082018-08-30 00:50:45 +0200979 if (config->site_import) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800980 fprintf(stderr, "%s\n", COPYRIGHT);
981 }
982}
983
984
Victor Stinnerc4bca952017-12-19 23:48:17 +0100985static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200986pymain_init_core_argv(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100987{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100988 /* Copy argv to be able to modify it (to force -c/-m) */
989 int argc = pymain->argc - _PyOS_optind;
990 wchar_t **argv;
991
992 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100993 /* Ensure at least one (empty) argument is seen */
994 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +0100995 argc = 1;
Victor Stinner6c785c02018-08-01 17:56:14 +0200996 argv = _Py_wstrlist_copy(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +0100997 }
Victor Stinnerc4bca952017-12-19 23:48:17 +0100998 else {
Victor Stinner6c785c02018-08-01 17:56:14 +0200999 argv = _Py_wstrlist_copy(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001000 }
1001
1002 if (argv == NULL) {
1003 pymain->err = _Py_INIT_NO_MEMORY();
1004 return -1;
1005 }
1006
1007 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001008 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001009 /* Force sys.argv[0] = '-c' */
1010 arg0 = L"-c";
1011 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001012 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001013 /* Force sys.argv[0] = '-m'*/
1014 arg0 = L"-m";
1015 }
1016 if (arg0 != NULL) {
1017 arg0 = _PyMem_RawWcsdup(arg0);
1018 if (arg0 == NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +02001019 _Py_wstrlist_clear(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001020 pymain->err = _Py_INIT_NO_MEMORY();
1021 return -1;
1022 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001023
1024 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001025 PyMem_RawFree(argv[0]);
1026 argv[0] = arg0;
1027 }
1028
Victor Stinner1dc6e392018-07-25 02:49:17 +02001029 config->argc = argc;
1030 config->argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001031 return 0;
1032}
1033
1034
Victor Stinner2094c2b2018-09-03 17:06:39 +02001035PyObject*
1036_Py_wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001037{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001038 assert(list != NULL || len < 1);
1039
1040 PyObject *pylist = PyList_New(len);
1041 if (pylist == NULL) {
1042 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001043 }
1044
Victor Stinner8ded5b82018-01-24 17:03:28 +01001045 for (int i = 0; i < len; i++) {
1046 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001047 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001048 Py_DECREF(pylist);
1049 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001050 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001051 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001052 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001053 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001054}
1055
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001056
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001057static void
Victor Stinnerfbca9082018-08-30 00:50:45 +02001058pymain_import_readline(_PyMain *pymain, const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001059{
Victor Stinnerfbca9082018-08-30 00:50:45 +02001060 if (config->isolated) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001061 return;
1062 }
Victor Stinnerfbca9082018-08-30 00:50:45 +02001063 if (!config->inspect && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001064 return;
1065 }
1066 if (!isatty(fileno(stdin))) {
1067 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001068 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001069
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001070 PyObject *mod = PyImport_ImportModule("readline");
1071 if (mod == NULL) {
1072 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 }
1074 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001075 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001077}
1078
1079
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001080static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001081pymain_run_startup(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001082{
Victor Stinner6c785c02018-08-01 17:56:14 +02001083 const char *startup = _PyCoreConfig_GetEnv(config, "PYTHONSTARTUP");
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001084 if (startup == NULL) {
1085 return;
1086 }
1087
1088 FILE *fp = _Py_fopen(startup, "r");
1089 if (fp == NULL) {
1090 int save_errno = errno;
1091 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
1092 errno = save_errno;
1093
1094 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
1095 startup);
1096 PyErr_Print();
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001097 return;
1098 }
1099
1100 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
1101 PyErr_Clear();
1102 fclose(fp);
1103}
1104
1105
1106static void
Victor Stinner72ec3192018-08-02 19:34:20 +02001107pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001108{
Victor Stinner72ec3192018-08-02 19:34:20 +02001109 const wchar_t *filename = pymain->filename;
1110 FILE *fp = _Py_wfopen(filename, L"r");
1111 if (fp == NULL) {
1112 char *cfilename_buffer;
1113 const char *cfilename;
1114 int err = errno;
1115 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
1116 if (cfilename_buffer != NULL)
1117 cfilename = cfilename_buffer;
1118 else
1119 cfilename = "<unprintable file name>";
1120 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
1121 config->program, cfilename, err, strerror(err));
1122 PyMem_RawFree(cfilename_buffer);
1123 pymain->status = 2;
1124 return;
1125 }
1126
1127 if (pymain->skip_first_line) {
1128 int ch;
1129 /* Push back first newline so line numbers remain the same */
1130 while ((ch = getc(fp)) != EOF) {
1131 if (ch == '\n') {
1132 (void)ungetc(ch, fp);
1133 break;
1134 }
1135 }
1136 }
1137
1138 struct _Py_stat_struct sb;
1139 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
1140 fprintf(stderr,
1141 "%ls: '%ls' is a directory, cannot continue\n",
1142 config->program, filename);
1143 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001144 fclose(fp);
1145 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001146 }
1147
1148 /* call pending calls like signal handlers (SIGINT) */
1149 if (Py_MakePendingCalls() == -1) {
1150 PyErr_Print();
1151 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001152 fclose(fp);
1153 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001154 }
1155
1156 PyObject *unicode, *bytes = NULL;
1157 const char *filename_str;
1158
1159 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
1160 if (unicode != NULL) {
1161 bytes = PyUnicode_EncodeFSDefault(unicode);
1162 Py_DECREF(unicode);
1163 }
1164 if (bytes != NULL) {
1165 filename_str = PyBytes_AsString(bytes);
1166 }
1167 else {
1168 PyErr_Clear();
1169 filename_str = "<filename encoding error>";
1170 }
1171
Victor Stinnerd8078622018-08-03 23:54:06 +02001172 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
1173 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
Victor Stinner72ec3192018-08-02 19:34:20 +02001174 Py_XDECREF(bytes);
1175 pymain->status = (run != 0);
Victor Stinner72ec3192018-08-02 19:34:20 +02001176}
1177
1178
1179static void
1180pymain_run_stdin(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
1181{
1182 if (pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001183 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinnera4d20b22018-08-01 02:57:45 +02001184 config->inspect = 0;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001185 pymain_run_startup(pymain, config, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001186 pymain_run_interactive_hook();
1187 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001188
Victor Stinner72ec3192018-08-02 19:34:20 +02001189 /* call pending calls like signal handlers (SIGINT) */
1190 if (Py_MakePendingCalls() == -1) {
1191 PyErr_Print();
1192 pymain->status = 1;
1193 return;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001194 }
1195
Victor Stinner72ec3192018-08-02 19:34:20 +02001196 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
1197 pymain->status = (run != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001198}
1199
1200
1201static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001202pymain_repl(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001205 opportunity to set it from Python. */
Victor Stinner6c785c02018-08-01 17:56:14 +02001206 if (!Py_InspectFlag && _PyCoreConfig_GetEnv(config, "PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 Py_InspectFlag = 1;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001208 config->inspect = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001210
Victor Stinner19760862017-12-20 01:41:59 +01001211 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001212 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001214
1215 Py_InspectFlag = 0;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001216 config->inspect = 0;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001217 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001218
Victor Stinner19760862017-12-20 01:41:59 +01001219 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001220 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001221}
1222
1223
1224/* Parse the command line.
1225 Handle --version and --help options directly.
1226
1227 Return 1 if Python must exit.
1228 Return 0 on success.
1229 Set pymain->err and return -1 on failure. */
1230static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001231pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config,
1232 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001233{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001234 int res = pymain_parse_cmdline_impl(pymain, config, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001235 if (res < 0) {
1236 return -1;
1237 }
1238 if (res) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001239 pymain_usage(1, config->program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001240 pymain->status = 2;
1241 return 1;
1242 }
1243
Victor Stinnerca719ac2017-12-20 18:00:19 +01001244 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001245 /* Backup _PyOS_optind */
1246 _PyOS_optind--;
1247 }
1248
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001249 return 0;
1250}
1251
1252
Victor Stinnera7368ac2017-11-15 18:11:45 -08001253/* Parse command line options and environment variables.
1254 This code must not use Python runtime apart PyMem_Raw memory allocator.
1255
1256 Return 0 on success.
1257 Return 1 if Python is done and must exit.
1258 Set pymain->err and return -1 on error. */
1259static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001260pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
1261 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001262{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001263 _PyInitError err;
1264
Victor Stinner1dc6e392018-07-25 02:49:17 +02001265 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01001266 if (res != 0) {
1267 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001268 }
1269
Victor Stinner1dc6e392018-07-25 02:49:17 +02001270 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001271 return -1;
1272 }
1273
Victor Stinnerd1457752018-07-26 16:04:56 +02001274 err = _PyCoreConfig_Read(config);
1275 if (_Py_INIT_FAILED(err)) {
1276 pymain->err = err;
1277 return -1;
1278 }
1279
Victor Stinnerd1457752018-07-26 16:04:56 +02001280 if (config->use_environment) {
Victor Stinnerecf411c2018-07-26 02:37:22 +02001281 err = cmdline_init_env_warnoptions(pymain, config, cmdline);
1282 if (_Py_INIT_FAILED(err)) {
1283 pymain->err = err;
1284 return -1;
1285 }
1286 }
1287
Victor Stinnerd1457752018-07-26 16:04:56 +02001288 err = config_init_warnoptions(config, cmdline);
Victor Stinner31a83932017-12-04 13:39:15 +01001289 if (_Py_INIT_FAILED(err)) {
1290 pymain->err = err;
1291 return -1;
1292 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001293 return 0;
1294}
1295
1296
Victor Stinner177d9212018-08-29 11:25:15 +02001297/* Read the configuration and initialize the LC_CTYPE locale:
1298 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538). */
Victor Stinnera7368ac2017-11-15 18:11:45 -08001299static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001300pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
1301 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001302{
Victor Stinner89487f52018-08-23 12:23:46 +02001303 int init_utf8_mode = Py_UTF8Mode;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001304#ifdef MS_WINDOWS
1305 int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
1306#endif
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001307 _PyCoreConfig save_config = _PyCoreConfig_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001308 int res = -1;
Victor Stinner94540602017-12-16 04:54:22 +01001309 int locale_coerced = 0;
1310 int loops = 0;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001311
1312 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
1313 pymain->err = _Py_INIT_NO_MEMORY();
1314 goto done;
1315 }
Victor Stinner94540602017-12-16 04:54:22 +01001316
Victor Stinner73b00be2018-09-03 17:32:31 +02001317 /* Set LC_CTYPE to the user preferred locale */
1318 _Py_SetLocaleFromEnv(LC_CTYPE);
1319
Victor Stinner94540602017-12-16 04:54:22 +01001320 while (1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001321 int utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01001322 int encoding_changed = 0;
1323
1324 /* Watchdog to prevent an infinite loop */
1325 loops++;
1326 if (loops == 3) {
1327 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
1328 "reading the configuration");
1329 goto done;
1330 }
1331
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001332 /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
1333 on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
Victor Stinner89487f52018-08-23 12:23:46 +02001334 Py_UTF8Mode = config->utf8_mode;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001335#ifdef MS_WINDOWS
1336 Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
1337#endif
Victor Stinner89487f52018-08-23 12:23:46 +02001338
Victor Stinner1dc6e392018-07-25 02:49:17 +02001339 if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001340 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01001341 }
1342
Victor Stinner1dc6e392018-07-25 02:49:17 +02001343 int conf_res = pymain_read_conf_impl(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001344 if (conf_res != 0) {
1345 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01001346 goto done;
1347 }
1348
1349 /* The legacy C locale assumes ASCII as the default text encoding, which
1350 * causes problems not only for the CPython runtime, but also other
1351 * components like GNU readline.
1352 *
1353 * Accordingly, when the CLI detects it, it attempts to coerce it to a
1354 * more capable UTF-8 based alternative.
1355 *
1356 * See the documentation of the PYTHONCOERCECLOCALE setting for more
1357 * details.
1358 */
Victor Stinner06e76082018-09-19 14:56:36 -07001359 if (config->coerce_c_locale && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01001360 locale_coerced = 1;
Victor Stinner06e76082018-09-19 14:56:36 -07001361 _Py_CoerceLegacyLocale(config->coerce_c_locale_warn);
Victor Stinner94540602017-12-16 04:54:22 +01001362 encoding_changed = 1;
1363 }
1364
1365 if (utf8_mode == -1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001366 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01001367 /* UTF-8 Mode enabled */
1368 encoding_changed = 1;
1369 }
1370 }
1371 else {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001372 if (config->utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01001373 encoding_changed = 1;
1374 }
1375 }
1376
1377 if (!encoding_changed) {
1378 break;
1379 }
1380
Victor Stinnerd1457752018-07-26 16:04:56 +02001381 /* Reset the configuration before reading again the configuration,
1382 just keep UTF-8 Mode value. */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001383 int new_utf8_mode = config->utf8_mode;
Victor Stinner06e76082018-09-19 14:56:36 -07001384 int new_coerce_c_locale = config->coerce_c_locale;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001385 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
1386 pymain->err = _Py_INIT_NO_MEMORY();
1387 goto done;
1388 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001389 pymain_clear_cmdline(pymain, cmdline);
Lucas Cimon35ca1822019-01-22 17:15:01 +01001390 pymain_clear_pymain(pymain);
Victor Stinner6c5a4b32018-06-16 00:06:28 +02001391 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner1dc6e392018-07-25 02:49:17 +02001392 config->utf8_mode = new_utf8_mode;
Victor Stinner06e76082018-09-19 14:56:36 -07001393 config->coerce_c_locale = new_coerce_c_locale;
Victor Stinner94540602017-12-16 04:54:22 +01001394
1395 /* The encoding changed: read again the configuration
1396 with the new encoding */
1397 }
1398 res = 0;
1399
1400done:
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001401 _PyCoreConfig_Clear(&save_config);
Victor Stinner89487f52018-08-23 12:23:46 +02001402 Py_UTF8Mode = init_utf8_mode ;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001403#ifdef MS_WINDOWS
1404 Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;
1405#endif
Victor Stinnera7368ac2017-11-15 18:11:45 -08001406 return res;
1407}
1408
Victor Stinner91106cd2017-12-13 12:29:09 +01001409
Victor Stinnerda273412017-12-15 01:46:02 +01001410void
Victor Stinnerda273412017-12-15 01:46:02 +01001411_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
1412{
1413 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01001414 Py_CLEAR(config->executable);
1415 Py_CLEAR(config->prefix);
1416 Py_CLEAR(config->base_prefix);
1417 Py_CLEAR(config->exec_prefix);
1418 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001419 Py_CLEAR(config->warnoptions);
1420 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01001421 Py_CLEAR(config->module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06001422 Py_CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001423}
1424
1425
1426static PyObject*
1427config_copy_attr(PyObject *obj)
1428{
1429 if (PyUnicode_Check(obj)) {
1430 Py_INCREF(obj);
1431 return obj;
1432 }
1433 else if (PyList_Check(obj)) {
1434 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
1435 }
1436 else if (PyDict_Check(obj)) {
1437 /* The dict type is used for xoptions. Make the assumption that keys
1438 and values are immutables */
1439 return PyDict_Copy(obj);
1440 }
1441 else {
1442 PyErr_Format(PyExc_TypeError,
1443 "cannot copy config attribute of type %.200s",
1444 Py_TYPE(obj)->tp_name);
1445 return NULL;
1446 }
1447}
1448
1449
1450int
1451_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
1452 const _PyMainInterpreterConfig *config2)
1453{
1454 _PyMainInterpreterConfig_Clear(config);
1455
Victor Stinner00b137c2018-11-13 19:59:26 +01001456#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
Victor Stinner01de89c2018-11-14 17:39:45 +01001457#define COPY_OBJ_ATTR(ATTR) \
Victor Stinnerda273412017-12-15 01:46:02 +01001458 do { \
Victor Stinner01de89c2018-11-14 17:39:45 +01001459 if (config2->ATTR != NULL) { \
1460 config->ATTR = config_copy_attr(config2->ATTR); \
1461 if (config->ATTR == NULL) { \
Victor Stinnerda273412017-12-15 01:46:02 +01001462 return -1; \
1463 } \
1464 } \
1465 } while (0)
1466
Victor Stinner00b137c2018-11-13 19:59:26 +01001467 COPY_ATTR(install_signal_handlers);
1468 COPY_OBJ_ATTR(argv);
1469 COPY_OBJ_ATTR(executable);
1470 COPY_OBJ_ATTR(prefix);
1471 COPY_OBJ_ATTR(base_prefix);
1472 COPY_OBJ_ATTR(exec_prefix);
1473 COPY_OBJ_ATTR(base_exec_prefix);
1474 COPY_OBJ_ATTR(warnoptions);
1475 COPY_OBJ_ATTR(xoptions);
1476 COPY_OBJ_ATTR(module_search_path);
1477 COPY_OBJ_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001478#undef COPY_ATTR
Victor Stinner00b137c2018-11-13 19:59:26 +01001479#undef COPY_OBJ_ATTR
Victor Stinnerda273412017-12-15 01:46:02 +01001480 return 0;
1481}
1482
1483
Victor Stinner00b137c2018-11-13 19:59:26 +01001484PyObject*
1485_PyMainInterpreterConfig_AsDict(const _PyMainInterpreterConfig *config)
1486{
1487 PyObject *dict, *obj;
1488 int res;
1489
1490 dict = PyDict_New();
1491 if (dict == NULL) {
1492 return NULL;
1493 }
1494
Victor Stinner01de89c2018-11-14 17:39:45 +01001495#define SET_ITEM_INT(ATTR) \
1496 do { \
1497 obj = PyLong_FromLong(config->ATTR); \
1498 if (obj == NULL) { \
1499 goto fail; \
1500 } \
1501 res = PyDict_SetItemString(dict, #ATTR, obj); \
1502 Py_DECREF(obj); \
1503 if (res < 0) { \
1504 goto fail; \
1505 } \
1506 } while (0)
Victor Stinner00b137c2018-11-13 19:59:26 +01001507
Victor Stinner01de89c2018-11-14 17:39:45 +01001508#define SET_ITEM_OBJ(ATTR) \
1509 do { \
1510 obj = config->ATTR; \
1511 if (obj == NULL) { \
1512 obj = Py_None; \
1513 } \
1514 res = PyDict_SetItemString(dict, #ATTR, obj); \
1515 if (res < 0) { \
1516 goto fail; \
1517 } \
1518 } while (0)
Victor Stinner00b137c2018-11-13 19:59:26 +01001519
Victor Stinner01de89c2018-11-14 17:39:45 +01001520 SET_ITEM_INT(install_signal_handlers);
1521 SET_ITEM_OBJ(argv);
1522 SET_ITEM_OBJ(executable);
1523 SET_ITEM_OBJ(prefix);
1524 SET_ITEM_OBJ(base_prefix);
1525 SET_ITEM_OBJ(exec_prefix);
1526 SET_ITEM_OBJ(base_exec_prefix);
1527 SET_ITEM_OBJ(warnoptions);
1528 SET_ITEM_OBJ(xoptions);
1529 SET_ITEM_OBJ(module_search_path);
1530 SET_ITEM_OBJ(pycache_prefix);
Victor Stinner00b137c2018-11-13 19:59:26 +01001531
1532 return dict;
1533
1534fail:
1535 Py_DECREF(dict);
1536 return NULL;
1537
Victor Stinner01de89c2018-11-14 17:39:45 +01001538#undef SET_ITEM_OBJ
Victor Stinner00b137c2018-11-13 19:59:26 +01001539}
1540
1541
Victor Stinner41264f12017-12-15 02:05:29 +01001542_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01001543_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
1544 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001545{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001546 if (main_config->install_signal_handlers < 0) {
1547 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001548 }
1549
Victor Stinner9cfc0022017-12-20 19:36:46 +01001550 if (main_config->xoptions == NULL) {
1551 main_config->xoptions = config_create_xoptions_dict(config);
1552 if (main_config->xoptions == NULL) {
1553 return _Py_INIT_NO_MEMORY();
1554 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001555 }
1556
Victor Stinner8ded5b82018-01-24 17:03:28 +01001557#define COPY_WSTR(ATTR) \
1558 do { \
Victor Stinner1dc6e392018-07-25 02:49:17 +02001559 if (main_config->ATTR == NULL && config->ATTR != NULL) { \
Victor Stinner8ded5b82018-01-24 17:03:28 +01001560 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
1561 if (main_config->ATTR == NULL) { \
1562 return _Py_INIT_NO_MEMORY(); \
1563 } \
1564 } \
1565 } while (0)
1566#define COPY_WSTRLIST(ATTR, LEN, LIST) \
1567 do { \
1568 if (ATTR == NULL) { \
Victor Stinner2094c2b2018-09-03 17:06:39 +02001569 ATTR = _Py_wstrlist_as_pylist(LEN, LIST); \
Victor Stinner8ded5b82018-01-24 17:03:28 +01001570 if (ATTR == NULL) { \
1571 return _Py_INIT_NO_MEMORY(); \
1572 } \
1573 } \
1574 } while (0)
1575
1576 COPY_WSTRLIST(main_config->warnoptions,
1577 config->nwarnoption, config->warnoptions);
1578 if (config->argc >= 0) {
1579 COPY_WSTRLIST(main_config->argv,
1580 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001581 }
1582
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001583 if (config->_install_importlib) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001584 COPY_WSTR(executable);
1585 COPY_WSTR(prefix);
1586 COPY_WSTR(base_prefix);
1587 COPY_WSTR(exec_prefix);
1588 COPY_WSTR(base_exec_prefix);
1589
1590 COPY_WSTRLIST(main_config->module_search_path,
1591 config->nmodule_search_path, config->module_search_paths);
Carl Meyerb193fa92018-06-15 22:40:56 -06001592
1593 if (config->pycache_prefix != NULL) {
1594 COPY_WSTR(pycache_prefix);
1595 } else {
1596 main_config->pycache_prefix = NULL;
1597 }
1598
Victor Stinner9cfc0022017-12-20 19:36:46 +01001599 }
Victor Stinner41264f12017-12-15 02:05:29 +01001600
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001601 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01001602#undef COPY_WSTR
1603#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001604}
1605
1606
1607static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001608pymain_init_python_main(_PyMain *pymain, _PyCoreConfig *config,
1609 PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001610{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001611 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001612
Victor Stinner9cfc0022017-12-20 19:36:46 +01001613 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001614 err = _PyMainInterpreterConfig_Read(&main_config, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001615 if (!_Py_INIT_FAILED(err)) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001616 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001617 }
1618 _PyMainInterpreterConfig_Clear(&main_config);
1619
1620 if (_Py_INIT_FAILED(err)) {
1621 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001622 return -1;
1623 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001624 return 0;
1625}
Victor Stinnera7368ac2017-11-15 18:11:45 -08001626
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001627
1628static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02001629pymain_run_python(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001630{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001631 int res = 0;
1632 _PyCoreConfig *config = &interp->core_config;
1633
1634 PyObject *main_importer_path = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001635 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01001636 /* If filename is a package (ex: directory or ZIP file) which contains
1637 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +02001638 prepended to sys.path.
1639
1640 Otherwise, main_importer_path is set to NULL. */
1641 main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01001642 }
1643
Victor Stinnerd3b19192018-07-25 10:21:03 +02001644 if (main_importer_path != NULL) {
1645 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
1646 pymain->status = 1;
1647 goto done;
1648 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001649 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001650 else if (!config->isolated) {
1651 PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc,
1652 config->argv);
1653 if (path0 == NULL) {
1654 pymain->err = _Py_INIT_NO_MEMORY();
1655 res = -1;
1656 goto done;
1657 }
Victor Stinner19760862017-12-20 01:41:59 +01001658
Victor Stinnerd3b19192018-07-25 10:21:03 +02001659 if (pymain_sys_path_add_path0(interp, path0) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001660 Py_DECREF(path0);
Victor Stinnerd3b19192018-07-25 10:21:03 +02001661 pymain->status = 1;
1662 goto done;
Victor Stinner19760862017-12-20 01:41:59 +01001663 }
1664 Py_DECREF(path0);
1665 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001666
Victor Stinner19760862017-12-20 01:41:59 +01001667 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08001668
Victor Stinnerfbca9082018-08-30 00:50:45 +02001669 pymain_header(pymain, config);
1670 pymain_import_readline(pymain, config);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001671
Victor Stinnerca719ac2017-12-20 18:00:19 +01001672 if (pymain->command) {
1673 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001674 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001675 else if (pymain->module) {
1676 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001677 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001678 else if (main_importer_path != NULL) {
1679 int sts = pymain_run_module(L"__main__", 0);
1680 pymain->status = (sts != 0);
1681 }
Victor Stinner72ec3192018-08-02 19:34:20 +02001682 else if (pymain->filename != NULL) {
1683 pymain_run_file(pymain, config, &cf);
1684 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001685 else {
Victor Stinner72ec3192018-08-02 19:34:20 +02001686 pymain_run_stdin(pymain, config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001687 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001688
Victor Stinner1dc6e392018-07-25 02:49:17 +02001689 pymain_repl(pymain, config, &cf);
Victor Stinnerd3b19192018-07-25 10:21:03 +02001690
1691done:
1692 Py_XDECREF(main_importer_path);
1693 return res;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001694}
1695
Victor Stinnera7368ac2017-11-15 18:11:45 -08001696
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001697static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001698pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
1699 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001700{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001701 pymain->err = _PyRuntime_Initialize();
1702 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001703 return -1;
1704 }
1705
Victor Stinner1dc6e392018-07-25 02:49:17 +02001706 int res = pymain_read_conf(pymain, config, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001707 if (res < 0) {
1708 return -1;
1709 }
1710 if (res > 0) {
1711 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01001712 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001713 }
1714
Victor Stinner94540602017-12-16 04:54:22 +01001715 if (cmdline->print_help) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001716 pymain_usage(0, config->program);
Victor Stinner19760862017-12-20 01:41:59 +01001717 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01001718 }
1719
1720 if (cmdline->print_version) {
1721 printf("Python %s\n",
1722 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01001723 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01001724 }
1725
Victor Stinnerc4bca952017-12-19 23:48:17 +01001726 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinner6c785c02018-08-01 17:56:14 +02001727 orig_argv = _Py_wstrlist_copy(pymain->argc, cmdline->argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001728 if (orig_argv == NULL) {
1729 pymain->err = _Py_INIT_NO_MEMORY();
1730 return -1;
1731 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001732 orig_argc = pymain->argc;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001733 return 0;
1734}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00001735
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001736
Victor Stinnerca719ac2017-12-20 18:00:19 +01001737/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
1738 LC_CTYPE locale and Py_DecodeLocale().
1739
1740 Configuration:
1741
1742 * Command line arguments
1743 * Environment variables
1744 * Py_xxx global configuration variables
1745
Victor Stinner1dc6e392018-07-25 02:49:17 +02001746 _PyCmdline is a temporary structure used to prioritize these
Victor Stinnerca719ac2017-12-20 18:00:19 +01001747 variables. */
1748static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001749pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerca719ac2017-12-20 18:00:19 +01001750{
Victor Stinner31e99082017-12-20 23:41:38 +01001751 /* Force default allocator, since pymain_free() and pymain_clear_config()
1752 must use the same allocator than this function. */
1753 PyMemAllocatorEx old_alloc;
1754 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
1755#ifdef Py_DEBUG
1756 PyMemAllocatorEx default_alloc;
1757 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
1758#endif
1759
Victor Stinner1dc6e392018-07-25 02:49:17 +02001760 _PyCmdline cmdline;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001761 memset(&cmdline, 0, sizeof(cmdline));
1762
Victor Stinner1dc6e392018-07-25 02:49:17 +02001763 int res = pymain_cmdline_impl(pymain, config, &cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001764
Victor Stinnerca719ac2017-12-20 18:00:19 +01001765 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01001766
1767#ifdef Py_DEBUG
1768 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
1769 PyMemAllocatorEx cur_alloc;
1770 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
1771 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
1772#endif
1773 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001774 return res;
1775}
1776
1777
Victor Stinner94540602017-12-16 04:54:22 +01001778static int
Victor Stinner06e76082018-09-19 14:56:36 -07001779pymain_init(_PyMain *pymain, PyInterpreterState **interp_p)
Victor Stinner94540602017-12-16 04:54:22 +01001780{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001781 /* 754 requires that FP exceptions run in "no stop" mode by default,
1782 * and until C vendors implement C99's ways to control FP exceptions,
1783 * Python requires non-stop mode. Alas, some platforms enable FP
1784 * exceptions by default. Here we disable them.
1785 */
1786#ifdef __FreeBSD__
1787 fedisableexcept(FE_OVERFLOW);
1788#endif
Victor Stinner94540602017-12-16 04:54:22 +01001789
Victor Stinner1dc6e392018-07-25 02:49:17 +02001790 _PyCoreConfig local_config = _PyCoreConfig_INIT;
1791 _PyCoreConfig *config = &local_config;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001792
Victor Stinner1dc6e392018-07-25 02:49:17 +02001793 _PyCoreConfig_GetGlobalConfig(config);
1794
1795 int cmd_res = pymain_cmdline(pymain, config);
1796 if (cmd_res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01001797 _Py_FatalInitError(pymain->err);
1798 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001799 if (cmd_res == 1) {
1800 pymain_clear_config(config);
1801 return 1;
Victor Stinner19760862017-12-20 01:41:59 +01001802 }
1803
Victor Stinner1dc6e392018-07-25 02:49:17 +02001804 _PyCoreConfig_SetGlobalConfig(config);
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001805
Victor Stinner1dc6e392018-07-25 02:49:17 +02001806 pymain_init_stdio(pymain, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001807
Victor Stinner1dc6e392018-07-25 02:49:17 +02001808 PyInterpreterState *interp;
1809 pymain->err = _Py_InitializeCore(&interp, config);
1810 if (_Py_INIT_FAILED(pymain->err)) {
1811 _Py_FatalInitError(pymain->err);
1812 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001813 *interp_p = interp;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001814
1815 pymain_clear_config(config);
1816 config = &interp->core_config;
1817
1818 if (pymain_init_python_main(pymain, config, interp) < 0) {
1819 _Py_FatalInitError(pymain->err);
1820 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001821 return 0;
1822}
1823
1824
1825static int
Victor Stinner06e76082018-09-19 14:56:36 -07001826pymain_main(_PyMain *pymain)
Victor Stinner1dc6e392018-07-25 02:49:17 +02001827{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001828 PyInterpreterState *interp;
Victor Stinner06e76082018-09-19 14:56:36 -07001829 int res = pymain_init(pymain, &interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +02001830 if (res != 1) {
Victor Stinnerd3b19192018-07-25 10:21:03 +02001831 if (pymain_run_python(pymain, interp) < 0) {
1832 _Py_FatalInitError(pymain->err);
1833 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001834
1835 if (Py_FinalizeEx() < 0) {
1836 /* Value unlikely to be confused with a non-error exit status or
1837 other special meaning */
1838 pymain->status = 120;
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001839 }
Victor Stinner19760862017-12-20 01:41:59 +01001840 }
1841
Victor Stinner94540602017-12-16 04:54:22 +01001842 pymain_free(pymain);
1843
Gregory P. Smith38f11cc2019-02-16 12:57:40 -08001844 if (_Py_UnhandledKeyboardInterrupt) {
1845 /* https://bugs.python.org/issue1054041 - We need to exit via the
1846 * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
1847 * If we don't, a calling process such as a shell may not know
1848 * about the user's ^C. https://www.cons.org/cracauer/sigint.html */
1849#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
1850 if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
1851 perror("signal"); /* Impossible in normal environments. */
1852 } else {
1853 kill(getpid(), SIGINT);
1854 }
1855 /* If setting SIG_DFL failed, or kill failed to terminate us,
1856 * there isn't much else we can do aside from an error code. */
1857#endif /* HAVE_GETPID && !MS_WINDOWS */
1858#ifdef MS_WINDOWS
1859 /* cmd.exe detects this, prints ^C, and offers to terminate. */
1860 /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
1861 pymain->status = STATUS_CONTROL_C_EXIT;
1862#else
1863 pymain->status = SIGINT + 128;
1864#endif /* !MS_WINDOWS */
1865 }
1866
Victor Stinner94540602017-12-16 04:54:22 +01001867 return pymain->status;
1868}
1869
1870
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001871int
1872Py_Main(int argc, wchar_t **argv)
1873{
1874 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001875 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001876 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001877 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001878
Victor Stinner06e76082018-09-19 14:56:36 -07001879 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00001880}
1881
Victor Stinner94540602017-12-16 04:54:22 +01001882
1883int
1884_Py_UnixMain(int argc, char **argv)
1885{
1886 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001887 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001888 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01001889 pymain.bytes_argv = argv;
1890
Victor Stinner06e76082018-09-19 14:56:36 -07001891 return pymain_main(&pymain);
Victor Stinner94540602017-12-16 04:54:22 +01001892}
1893
1894
Skip Montanaro786ea6b2004-03-01 15:44:05 +00001895/* this is gonna seem *real weird*, but if you put some other code between
1896 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
1897 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00001898
Guido van Rossum667d7041995-08-04 04:20:48 +00001899/* Make the *original* argc/argv available to other modules.
1900 This is rare, but it is needed by the secureware extension. */
1901
1902void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001903Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00001904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 *argc = orig_argc;
1906 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00001907}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001908
1909#ifdef __cplusplus
1910}
1911#endif