blob: a0629911adb988f17dd6359fb289bc436e4237ba [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 Stinner34be8072016-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{
Victor Stinnerd3b19192018-07-25 10:21:03 +0200511 PyObject *sys_path;
512 PyObject *sysdict = interp->sysdict;
513 if (sysdict != NULL) {
514 sys_path = PyDict_GetItemString(sysdict, "path");
515 }
516 else {
517 sys_path = NULL;
518 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800519 if (sys_path == NULL) {
520 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
521 goto error;
522 }
523
Victor Stinnerd3b19192018-07-25 10:21:03 +0200524 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800525 goto error;
526 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200527 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800528
529error:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800530 PyErr_Print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200531 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800532}
533
534
Victor Stinnerb1147e42018-07-21 02:06:16 +0200535_PyInitError
536_Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800537{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200538 if (*len == INT_MAX) {
539 /* len+1 would overflow */
540 return _Py_INIT_NO_MEMORY();
541 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100542 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800543 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100544 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800545 }
546
Victor Stinnerca719ac2017-12-20 18:00:19 +0100547 size_t size = (*len + 1) * sizeof(list[0]);
548 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
549 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800550 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100551 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800552 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100553 list2[*len] = str2;
554 *list = list2;
555 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100556 return _Py_INIT_OK();
557}
558
559
560static int
561pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
562{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200563 _PyInitError err = _Py_wstrlist_append(len, list, str);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100564 if (_Py_INIT_FAILED(err)) {
565 pymain->err = err;
566 return -1;
567 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800568 return 0;
569}
570
571
572/* Parse the command line arguments
573 Return 0 on success.
574 Return 1 if parsing failed.
575 Set pymain->err and return -1 on other errors. */
576static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200577pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
578 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800579{
Antoine Pitrou86838b02012-02-21 19:03:47 +0100580 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800581 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800582 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100583 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800584 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800585 if (c == EOF) {
586 break;
587 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 /* -c is the last option; following arguments
591 that look like options are left for the
592 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800593 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
594 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
595 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100596 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800597 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800598 }
Victor Stinner58d16832018-05-31 15:09:28 +0200599 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 command[len - 2] = '\n';
601 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100602 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 break;
604 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (c == 'm') {
607 /* -m is the last option; following arguments
608 that look like options are left for the
609 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100610 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
611 if (pymain->module == NULL) {
612 return -1;
613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 break;
615 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800618 case 0:
619 // Handle long option.
620 assert(longindex == 0); // Only one long option now.
621 if (!wcscmp(_PyOS_optarg, L"always")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200622 config->_check_hash_pycs_mode = "always";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800623 } else if (!wcscmp(_PyOS_optarg, L"never")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200624 config->_check_hash_pycs_mode = "never";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800625 } else if (!wcscmp(_PyOS_optarg, L"default")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200626 config->_check_hash_pycs_mode = "default";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800627 } else {
628 fprintf(stderr, "--check-hash-based-pycs must be one of "
629 "'default', 'always', or 'never'\n");
630 return 1;
631 }
632 break;
633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 case 'b':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200635 config->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 case 'd':
Victor Stinner98512272018-08-01 03:07:00 +0200639 config->parser_debug++;
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 'i':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200643 config->inspect++;
644 config->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000646
Christian Heimesad73a9c2013-08-10 16:36:18 +0200647 case 'I':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200648 config->isolated++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200649 break;
650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 case 'O':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200654 config->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 case 'B':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200658 config->write_bytecode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 case 's':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200662 config->user_site_directory = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 case 'S':
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200666 config->site_import = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 case 'E':
Victor Stinnerd1457752018-07-26 16:04:56 +0200670 config->use_environment = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 case 't':
674 /* ignored for backwards compatibility */
675 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 case 'u':
Victor Stinner98512272018-08-01 03:07:00 +0200678 config->buffered_stdio = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 case 'v':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200682 config->verbose++;
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 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100686 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 case 'h':
690 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -0700691 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -0700695 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100699 if (pymain_wstrlist_append(pymain,
700 &cmdline->nwarnoption,
701 &cmdline->warnoptions,
702 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800703 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800704 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000706
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000707 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100708 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +0100709 &config->nxoption,
710 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +0100711 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800712 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800713 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000714 break;
715
Georg Brandl9d871192010-12-04 10:47:18 +0000716 case 'q':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200717 config->quiet++;
Georg Brandl9d871192010-12-04 10:47:18 +0000718 break;
719
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100720 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100721 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100722 break;
723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800727 /* unknown argument: parsing failed */
728 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800730 } while (1);
731
Victor Stinnerca719ac2017-12-20 18:00:19 +0100732 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800733 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +0100734 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800735 {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100736 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
737 if (pymain->filename == NULL) {
738 return -1;
739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000741
Victor Stinnerd5dda982017-12-13 17:31:16 +0100742 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100743 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +0100744
Eric Snow6b4be192017-05-22 21:36:03 -0700745 return 0;
746}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000747
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800748
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800749static int
Victor Stinner9cfc0022017-12-20 19:36:46 +0100750add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +0100751{
752 PyObject *name, *value;
753
754 const wchar_t *name_end = wcschr(s, L'=');
755 if (!name_end) {
756 name = PyUnicode_FromWideChar(s, -1);
757 value = Py_True;
758 Py_INCREF(value);
759 }
760 else {
761 name = PyUnicode_FromWideChar(s, name_end - s);
762 value = PyUnicode_FromWideChar(name_end + 1, -1);
763 }
764 if (name == NULL || value == NULL) {
765 goto error;
766 }
767 if (PyDict_SetItem(opts, name, value) < 0) {
768 goto error;
769 }
770 Py_DECREF(name);
771 Py_DECREF(value);
772 return 0;
773
774error:
775 Py_XDECREF(name);
776 Py_XDECREF(value);
777 return -1;
778}
779
Victor Stinner9cfc0022017-12-20 19:36:46 +0100780
781static PyObject*
782config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800783{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100784 int nxoption = config->nxoption;
785 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +0100786 PyObject *dict = PyDict_New();
787 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100788 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +0100789 }
790
Victor Stinnerca719ac2017-12-20 18:00:19 +0100791 for (int i=0; i < nxoption; i++) {
792 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +0100793 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +0100794 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100795 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800796 }
797 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100798
Victor Stinner9cfc0022017-12-20 19:36:46 +0100799 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -0700800}
801
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800802
Victor Stinner9cfc0022017-12-20 19:36:46 +0100803static _PyInitError
804config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -0700805{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100806 for (int i = 0; i < len; i++) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200807 _PyInitError err = _Py_wstrlist_append(&config->nwarnoption,
808 &config->warnoptions,
809 options[i]);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100810 if (_Py_INIT_FAILED(err)) {
811 return err;
Eric Snow6b4be192017-05-22 21:36:03 -0700812 }
Eric Snow6b4be192017-05-22 21:36:03 -0700813 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100814 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800815}
Eric Snow6b4be192017-05-22 21:36:03 -0700816
Victor Stinner747f48e2017-12-12 22:59:48 +0100817
Victor Stinner9cfc0022017-12-20 19:36:46 +0100818static _PyInitError
Victor Stinner1dc6e392018-07-25 02:49:17 +0200819config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +0100820{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100821 _PyInitError err;
822
823 assert(config->nwarnoption == 0);
824
Victor Stinner747f48e2017-12-12 22:59:48 +0100825 /* The priority order for warnings configuration is (highest precedence
826 * first):
827 *
828 * - the BytesWarning filter, if needed ('-b', '-bb')
829 * - any '-W' command line options; then
830 * - the 'PYTHONWARNINGS' environment variable; then
831 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
832 * - any implicit filters added by _warnings.c/warnings.py
833 *
834 * All settings except the last are passed to the warnings module via
835 * the `sys.warnoptions` list. Since the warnings module works on the basis
836 * of "the most recently added filter will be checked first", we add
837 * the lowest precedence entries first so that later entries override them.
838 */
839
Victor Stinner9cfc0022017-12-20 19:36:46 +0100840 if (config->dev_mode) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200841 err = _Py_wstrlist_append(&config->nwarnoption,
842 &config->warnoptions,
843 L"default");
Victor Stinner9cfc0022017-12-20 19:36:46 +0100844 if (_Py_INIT_FAILED(err)) {
845 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100846 }
Victor Stinner747f48e2017-12-12 22:59:48 +0100847 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100848
Victor Stinner9cfc0022017-12-20 19:36:46 +0100849 err = config_add_warnings_optlist(config,
850 cmdline->nenv_warnoption,
851 cmdline->env_warnoptions);
852 if (_Py_INIT_FAILED(err)) {
853 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100854 }
855
Victor Stinner9cfc0022017-12-20 19:36:46 +0100856 err = config_add_warnings_optlist(config,
857 cmdline->nwarnoption,
858 cmdline->warnoptions);
859 if (_Py_INIT_FAILED(err)) {
860 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100861 }
862
863 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
864 * don't even try to emit a warning, so we skip setting the filter in that
865 * case.
866 */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200867 if (config->bytes_warning) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100868 wchar_t *filter;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200869 if (config->bytes_warning> 1) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100870 filter = L"error::BytesWarning";
871 }
872 else {
873 filter = L"default::BytesWarning";
874 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200875 err = _Py_wstrlist_append(&config->nwarnoption,
876 &config->warnoptions,
877 filter);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100878 if (_Py_INIT_FAILED(err)) {
879 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100880 }
881 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100882 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +0100883}
884
885
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800886/* Get warning options from PYTHONWARNINGS environment variable.
887 Return 0 on success.
888 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +0100889static _PyInitError
Victor Stinnerd1457752018-07-26 16:04:56 +0200890cmdline_init_env_warnoptions(_PyMain *pymain, const _PyCoreConfig *config,
891 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800892{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100893 wchar_t *env;
Victor Stinner6c785c02018-08-01 17:56:14 +0200894 int res = _PyCoreConfig_GetEnvDup(config, &env,
895 L"PYTHONWARNINGS", "PYTHONWARNINGS");
Victor Stinnerca719ac2017-12-20 18:00:19 +0100896 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100897 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +0100898 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800899
Victor Stinnerca719ac2017-12-20 18:00:19 +0100900 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100901 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +0100902 }
Philip Jenvey0805ca32010-04-07 04:04:10 +0000903
Victor Stinnerca719ac2017-12-20 18:00:19 +0100904
905 wchar_t *warning, *context = NULL;
906 for (warning = WCSTOK(env, L",", &context);
907 warning != NULL;
908 warning = WCSTOK(NULL, L",", &context))
909 {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200910 _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption,
911 &cmdline->env_warnoptions,
912 warning);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100913 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100914 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100915 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800916 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100918 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100919 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800920}
921
922
923static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200924pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800925{
926 pymain->stdin_is_interactive = (isatty(fileno(stdin))
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200927 || config->interactive);
Guido van Rossum775af911997-02-14 19:50:32 +0000928
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000929#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +0000930 /* don't translate newlines (\r\n <=> \n) */
931 _setmode(fileno(stdin), O_BINARY);
932 _setmode(fileno(stdout), O_BINARY);
933 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000934#endif
Victor Stinner89e34362011-01-07 18:47:22 +0000935
Victor Stinner98512272018-08-01 03:07:00 +0200936 if (!config->buffered_stdio) {
Guido van Rossum22ffac11998-03-06 15:30:39 +0000937#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
939 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
940 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000941#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 setbuf(stdin, (char *)NULL);
943 setbuf(stdout, (char *)NULL);
944 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000945#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200947 else if (config->interactive) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000948#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 /* Doesn't have to have line-buffered -- use unbuffered */
950 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
951 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000952#else /* !MS_WINDOWS */
953#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
955 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000956#endif /* HAVE_SETVBUF */
957#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 /* Leave stderr alone - it should be unbuffered anyway. */
959 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800960}
Guido van Rossum667d7041995-08-04 04:20:48 +0000961
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800962
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800963static void
Victor Stinnerfbca9082018-08-30 00:50:45 +0200964pymain_header(_PyMain *pymain, const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800965{
Victor Stinnerfbca9082018-08-30 00:50:45 +0200966 if (config->quiet) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800967 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000969
Victor Stinnerfbca9082018-08-30 00:50:45 +0200970 if (!config->verbose && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800971 return;
972 }
973
974 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
Victor Stinnerfbca9082018-08-30 00:50:45 +0200975 if (config->site_import) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800976 fprintf(stderr, "%s\n", COPYRIGHT);
977 }
978}
979
980
Victor Stinnerc4bca952017-12-19 23:48:17 +0100981static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200982pymain_init_core_argv(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100983{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100984 /* Copy argv to be able to modify it (to force -c/-m) */
985 int argc = pymain->argc - _PyOS_optind;
986 wchar_t **argv;
987
988 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100989 /* Ensure at least one (empty) argument is seen */
990 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +0100991 argc = 1;
Victor Stinner6c785c02018-08-01 17:56:14 +0200992 argv = _Py_wstrlist_copy(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +0100993 }
Victor Stinnerc4bca952017-12-19 23:48:17 +0100994 else {
Victor Stinner6c785c02018-08-01 17:56:14 +0200995 argv = _Py_wstrlist_copy(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100996 }
997
998 if (argv == NULL) {
999 pymain->err = _Py_INIT_NO_MEMORY();
1000 return -1;
1001 }
1002
1003 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001004 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001005 /* Force sys.argv[0] = '-c' */
1006 arg0 = L"-c";
1007 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001008 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001009 /* Force sys.argv[0] = '-m'*/
1010 arg0 = L"-m";
1011 }
1012 if (arg0 != NULL) {
1013 arg0 = _PyMem_RawWcsdup(arg0);
1014 if (arg0 == NULL) {
Victor Stinner6c785c02018-08-01 17:56:14 +02001015 _Py_wstrlist_clear(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001016 pymain->err = _Py_INIT_NO_MEMORY();
1017 return -1;
1018 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001019
1020 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001021 PyMem_RawFree(argv[0]);
1022 argv[0] = arg0;
1023 }
1024
Victor Stinner1dc6e392018-07-25 02:49:17 +02001025 config->argc = argc;
1026 config->argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001027 return 0;
1028}
1029
1030
Victor Stinner2094c2b2018-09-03 17:06:39 +02001031PyObject*
1032_Py_wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001033{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001034 assert(list != NULL || len < 1);
1035
1036 PyObject *pylist = PyList_New(len);
1037 if (pylist == NULL) {
1038 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001039 }
1040
Victor Stinner8ded5b82018-01-24 17:03:28 +01001041 for (int i = 0; i < len; i++) {
1042 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001043 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001044 Py_DECREF(pylist);
1045 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001046 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001047 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001048 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001049 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001050}
1051
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001052
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001053static void
Victor Stinnerfbca9082018-08-30 00:50:45 +02001054pymain_import_readline(_PyMain *pymain, const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001055{
Victor Stinnerfbca9082018-08-30 00:50:45 +02001056 if (config->isolated) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001057 return;
1058 }
Victor Stinnerfbca9082018-08-30 00:50:45 +02001059 if (!config->inspect && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001060 return;
1061 }
1062 if (!isatty(fileno(stdin))) {
1063 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001064 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001065
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001066 PyObject *mod = PyImport_ImportModule("readline");
1067 if (mod == NULL) {
1068 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 }
1070 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001071 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001073}
1074
1075
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001076static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001077pymain_run_startup(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001078{
Victor Stinner6c785c02018-08-01 17:56:14 +02001079 const char *startup = _PyCoreConfig_GetEnv(config, "PYTHONSTARTUP");
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001080 if (startup == NULL) {
1081 return;
1082 }
1083
1084 FILE *fp = _Py_fopen(startup, "r");
1085 if (fp == NULL) {
1086 int save_errno = errno;
1087 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
1088 errno = save_errno;
1089
1090 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
1091 startup);
1092 PyErr_Print();
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001093 return;
1094 }
1095
1096 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
1097 PyErr_Clear();
1098 fclose(fp);
1099}
1100
1101
1102static void
Victor Stinner72ec3192018-08-02 19:34:20 +02001103pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001104{
Victor Stinner72ec3192018-08-02 19:34:20 +02001105 const wchar_t *filename = pymain->filename;
1106 FILE *fp = _Py_wfopen(filename, L"r");
1107 if (fp == NULL) {
1108 char *cfilename_buffer;
1109 const char *cfilename;
1110 int err = errno;
1111 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
1112 if (cfilename_buffer != NULL)
1113 cfilename = cfilename_buffer;
1114 else
1115 cfilename = "<unprintable file name>";
1116 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
1117 config->program, cfilename, err, strerror(err));
1118 PyMem_RawFree(cfilename_buffer);
1119 pymain->status = 2;
1120 return;
1121 }
1122
1123 if (pymain->skip_first_line) {
1124 int ch;
1125 /* Push back first newline so line numbers remain the same */
1126 while ((ch = getc(fp)) != EOF) {
1127 if (ch == '\n') {
1128 (void)ungetc(ch, fp);
1129 break;
1130 }
1131 }
1132 }
1133
1134 struct _Py_stat_struct sb;
1135 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
1136 fprintf(stderr,
1137 "%ls: '%ls' is a directory, cannot continue\n",
1138 config->program, filename);
1139 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001140 fclose(fp);
1141 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001142 }
1143
1144 /* call pending calls like signal handlers (SIGINT) */
1145 if (Py_MakePendingCalls() == -1) {
1146 PyErr_Print();
1147 pymain->status = 1;
Victor Stinnerd8078622018-08-03 23:54:06 +02001148 fclose(fp);
1149 return;
Victor Stinner72ec3192018-08-02 19:34:20 +02001150 }
1151
1152 PyObject *unicode, *bytes = NULL;
1153 const char *filename_str;
1154
1155 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
1156 if (unicode != NULL) {
1157 bytes = PyUnicode_EncodeFSDefault(unicode);
1158 Py_DECREF(unicode);
1159 }
1160 if (bytes != NULL) {
1161 filename_str = PyBytes_AsString(bytes);
1162 }
1163 else {
1164 PyErr_Clear();
1165 filename_str = "<filename encoding error>";
1166 }
1167
Victor Stinnerd8078622018-08-03 23:54:06 +02001168 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
1169 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
Victor Stinner72ec3192018-08-02 19:34:20 +02001170 Py_XDECREF(bytes);
1171 pymain->status = (run != 0);
Victor Stinner72ec3192018-08-02 19:34:20 +02001172}
1173
1174
1175static void
1176pymain_run_stdin(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
1177{
1178 if (pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001179 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinnera4d20b22018-08-01 02:57:45 +02001180 config->inspect = 0;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001181 pymain_run_startup(pymain, config, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001182 pymain_run_interactive_hook();
1183 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001184
Victor Stinner72ec3192018-08-02 19:34:20 +02001185 /* call pending calls like signal handlers (SIGINT) */
1186 if (Py_MakePendingCalls() == -1) {
1187 PyErr_Print();
1188 pymain->status = 1;
1189 return;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001190 }
1191
Victor Stinner72ec3192018-08-02 19:34:20 +02001192 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
1193 pymain->status = (run != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001194}
1195
1196
1197static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001198pymain_repl(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001201 opportunity to set it from Python. */
Victor Stinner6c785c02018-08-01 17:56:14 +02001202 if (!Py_InspectFlag && _PyCoreConfig_GetEnv(config, "PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 Py_InspectFlag = 1;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001204 config->inspect = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001206
Victor Stinner19760862017-12-20 01:41:59 +01001207 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001208 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001210
1211 Py_InspectFlag = 0;
Victor Stinnera4d20b22018-08-01 02:57:45 +02001212 config->inspect = 0;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001213 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001214
Victor Stinner19760862017-12-20 01:41:59 +01001215 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001216 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001217}
1218
1219
1220/* Parse the command line.
1221 Handle --version and --help options directly.
1222
1223 Return 1 if Python must exit.
1224 Return 0 on success.
1225 Set pymain->err and return -1 on failure. */
1226static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001227pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config,
1228 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001229{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001230 int res = pymain_parse_cmdline_impl(pymain, config, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001231 if (res < 0) {
1232 return -1;
1233 }
1234 if (res) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001235 pymain_usage(1, config->program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001236 pymain->status = 2;
1237 return 1;
1238 }
1239
Victor Stinnerca719ac2017-12-20 18:00:19 +01001240 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001241 /* Backup _PyOS_optind */
1242 _PyOS_optind--;
1243 }
1244
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001245 return 0;
1246}
1247
1248
Victor Stinnera7368ac2017-11-15 18:11:45 -08001249/* Parse command line options and environment variables.
1250 This code must not use Python runtime apart PyMem_Raw memory allocator.
1251
1252 Return 0 on success.
1253 Return 1 if Python is done and must exit.
1254 Set pymain->err and return -1 on error. */
1255static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001256pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
1257 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001258{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001259 _PyInitError err;
1260
Victor Stinner1dc6e392018-07-25 02:49:17 +02001261 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01001262 if (res != 0) {
1263 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001264 }
1265
Victor Stinner1dc6e392018-07-25 02:49:17 +02001266 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001267 return -1;
1268 }
1269
Victor Stinnerd1457752018-07-26 16:04:56 +02001270 err = _PyCoreConfig_Read(config);
1271 if (_Py_INIT_FAILED(err)) {
1272 pymain->err = err;
1273 return -1;
1274 }
1275
Victor Stinnerd1457752018-07-26 16:04:56 +02001276 if (config->use_environment) {
Victor Stinnerecf411c2018-07-26 02:37:22 +02001277 err = cmdline_init_env_warnoptions(pymain, config, cmdline);
1278 if (_Py_INIT_FAILED(err)) {
1279 pymain->err = err;
1280 return -1;
1281 }
1282 }
1283
Victor Stinnerd1457752018-07-26 16:04:56 +02001284 err = config_init_warnoptions(config, cmdline);
Victor Stinner31a83932017-12-04 13:39:15 +01001285 if (_Py_INIT_FAILED(err)) {
1286 pymain->err = err;
1287 return -1;
1288 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001289 return 0;
1290}
1291
1292
Victor Stinner177d9212018-08-29 11:25:15 +02001293/* Read the configuration and initialize the LC_CTYPE locale:
1294 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538). */
Victor Stinnera7368ac2017-11-15 18:11:45 -08001295static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001296pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
1297 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001298{
Victor Stinner89487f52018-08-23 12:23:46 +02001299 int init_utf8_mode = Py_UTF8Mode;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001300#ifdef MS_WINDOWS
1301 int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
1302#endif
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001303 _PyCoreConfig save_config = _PyCoreConfig_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001304 int res = -1;
Victor Stinner94540602017-12-16 04:54:22 +01001305 int locale_coerced = 0;
1306 int loops = 0;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001307
1308 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
1309 pymain->err = _Py_INIT_NO_MEMORY();
1310 goto done;
1311 }
Victor Stinner94540602017-12-16 04:54:22 +01001312
Victor Stinner73b00be2018-09-03 17:32:31 +02001313 /* Set LC_CTYPE to the user preferred locale */
1314 _Py_SetLocaleFromEnv(LC_CTYPE);
1315
Victor Stinner94540602017-12-16 04:54:22 +01001316 while (1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001317 int utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01001318 int encoding_changed = 0;
1319
1320 /* Watchdog to prevent an infinite loop */
1321 loops++;
1322 if (loops == 3) {
1323 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
1324 "reading the configuration");
1325 goto done;
1326 }
1327
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001328 /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
1329 on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
Victor Stinner89487f52018-08-23 12:23:46 +02001330 Py_UTF8Mode = config->utf8_mode;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001331#ifdef MS_WINDOWS
1332 Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
1333#endif
Victor Stinner89487f52018-08-23 12:23:46 +02001334
Victor Stinner1dc6e392018-07-25 02:49:17 +02001335 if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001336 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01001337 }
1338
Victor Stinner1dc6e392018-07-25 02:49:17 +02001339 int conf_res = pymain_read_conf_impl(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001340 if (conf_res != 0) {
1341 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01001342 goto done;
1343 }
1344
1345 /* The legacy C locale assumes ASCII as the default text encoding, which
1346 * causes problems not only for the CPython runtime, but also other
1347 * components like GNU readline.
1348 *
1349 * Accordingly, when the CLI detects it, it attempts to coerce it to a
1350 * more capable UTF-8 based alternative.
1351 *
1352 * See the documentation of the PYTHONCOERCECLOCALE setting for more
1353 * details.
1354 */
Victor Stinner06e76082018-09-19 14:56:36 -07001355 if (config->coerce_c_locale && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01001356 locale_coerced = 1;
Victor Stinner06e76082018-09-19 14:56:36 -07001357 _Py_CoerceLegacyLocale(config->coerce_c_locale_warn);
Victor Stinner94540602017-12-16 04:54:22 +01001358 encoding_changed = 1;
1359 }
1360
1361 if (utf8_mode == -1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001362 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01001363 /* UTF-8 Mode enabled */
1364 encoding_changed = 1;
1365 }
1366 }
1367 else {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001368 if (config->utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01001369 encoding_changed = 1;
1370 }
1371 }
1372
1373 if (!encoding_changed) {
1374 break;
1375 }
1376
Victor Stinnerd1457752018-07-26 16:04:56 +02001377 /* Reset the configuration before reading again the configuration,
1378 just keep UTF-8 Mode value. */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001379 int new_utf8_mode = config->utf8_mode;
Victor Stinner06e76082018-09-19 14:56:36 -07001380 int new_coerce_c_locale = config->coerce_c_locale;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001381 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
1382 pymain->err = _Py_INIT_NO_MEMORY();
1383 goto done;
1384 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001385 pymain_clear_cmdline(pymain, cmdline);
Lucas Cimon35ca1822019-01-22 17:15:01 +01001386 pymain_clear_pymain(pymain);
Victor Stinner6c5a4b32018-06-16 00:06:28 +02001387 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner1dc6e392018-07-25 02:49:17 +02001388 config->utf8_mode = new_utf8_mode;
Victor Stinner06e76082018-09-19 14:56:36 -07001389 config->coerce_c_locale = new_coerce_c_locale;
Victor Stinner94540602017-12-16 04:54:22 +01001390
1391 /* The encoding changed: read again the configuration
1392 with the new encoding */
1393 }
1394 res = 0;
1395
1396done:
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001397 _PyCoreConfig_Clear(&save_config);
Victor Stinner89487f52018-08-23 12:23:46 +02001398 Py_UTF8Mode = init_utf8_mode ;
Victor Stinnerc5989cd2018-08-29 19:32:47 +02001399#ifdef MS_WINDOWS
1400 Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;
1401#endif
Victor Stinnera7368ac2017-11-15 18:11:45 -08001402 return res;
1403}
1404
Victor Stinner91106cd2017-12-13 12:29:09 +01001405
Victor Stinnerda273412017-12-15 01:46:02 +01001406void
Victor Stinnerda273412017-12-15 01:46:02 +01001407_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
1408{
1409 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01001410 Py_CLEAR(config->executable);
1411 Py_CLEAR(config->prefix);
1412 Py_CLEAR(config->base_prefix);
1413 Py_CLEAR(config->exec_prefix);
1414 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001415 Py_CLEAR(config->warnoptions);
1416 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01001417 Py_CLEAR(config->module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06001418 Py_CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001419}
1420
1421
1422static PyObject*
1423config_copy_attr(PyObject *obj)
1424{
1425 if (PyUnicode_Check(obj)) {
1426 Py_INCREF(obj);
1427 return obj;
1428 }
1429 else if (PyList_Check(obj)) {
1430 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
1431 }
1432 else if (PyDict_Check(obj)) {
1433 /* The dict type is used for xoptions. Make the assumption that keys
1434 and values are immutables */
1435 return PyDict_Copy(obj);
1436 }
1437 else {
1438 PyErr_Format(PyExc_TypeError,
1439 "cannot copy config attribute of type %.200s",
1440 Py_TYPE(obj)->tp_name);
1441 return NULL;
1442 }
1443}
1444
1445
1446int
1447_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
1448 const _PyMainInterpreterConfig *config2)
1449{
1450 _PyMainInterpreterConfig_Clear(config);
1451
Victor Stinner00b137c2018-11-13 19:59:26 +01001452#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
Victor Stinner01de89c2018-11-14 17:39:45 +01001453#define COPY_OBJ_ATTR(ATTR) \
Victor Stinnerda273412017-12-15 01:46:02 +01001454 do { \
Victor Stinner01de89c2018-11-14 17:39:45 +01001455 if (config2->ATTR != NULL) { \
1456 config->ATTR = config_copy_attr(config2->ATTR); \
1457 if (config->ATTR == NULL) { \
Victor Stinnerda273412017-12-15 01:46:02 +01001458 return -1; \
1459 } \
1460 } \
1461 } while (0)
1462
Victor Stinner00b137c2018-11-13 19:59:26 +01001463 COPY_ATTR(install_signal_handlers);
1464 COPY_OBJ_ATTR(argv);
1465 COPY_OBJ_ATTR(executable);
1466 COPY_OBJ_ATTR(prefix);
1467 COPY_OBJ_ATTR(base_prefix);
1468 COPY_OBJ_ATTR(exec_prefix);
1469 COPY_OBJ_ATTR(base_exec_prefix);
1470 COPY_OBJ_ATTR(warnoptions);
1471 COPY_OBJ_ATTR(xoptions);
1472 COPY_OBJ_ATTR(module_search_path);
1473 COPY_OBJ_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01001474#undef COPY_ATTR
Victor Stinner00b137c2018-11-13 19:59:26 +01001475#undef COPY_OBJ_ATTR
Victor Stinnerda273412017-12-15 01:46:02 +01001476 return 0;
1477}
1478
1479
Victor Stinner00b137c2018-11-13 19:59:26 +01001480PyObject*
1481_PyMainInterpreterConfig_AsDict(const _PyMainInterpreterConfig *config)
1482{
1483 PyObject *dict, *obj;
1484 int res;
1485
1486 dict = PyDict_New();
1487 if (dict == NULL) {
1488 return NULL;
1489 }
1490
Victor Stinner01de89c2018-11-14 17:39:45 +01001491#define SET_ITEM_INT(ATTR) \
1492 do { \
1493 obj = PyLong_FromLong(config->ATTR); \
1494 if (obj == NULL) { \
1495 goto fail; \
1496 } \
1497 res = PyDict_SetItemString(dict, #ATTR, obj); \
1498 Py_DECREF(obj); \
1499 if (res < 0) { \
1500 goto fail; \
1501 } \
1502 } while (0)
Victor Stinner00b137c2018-11-13 19:59:26 +01001503
Victor Stinner01de89c2018-11-14 17:39:45 +01001504#define SET_ITEM_OBJ(ATTR) \
1505 do { \
1506 obj = config->ATTR; \
1507 if (obj == NULL) { \
1508 obj = Py_None; \
1509 } \
1510 res = PyDict_SetItemString(dict, #ATTR, obj); \
1511 if (res < 0) { \
1512 goto fail; \
1513 } \
1514 } while (0)
Victor Stinner00b137c2018-11-13 19:59:26 +01001515
Victor Stinner01de89c2018-11-14 17:39:45 +01001516 SET_ITEM_INT(install_signal_handlers);
1517 SET_ITEM_OBJ(argv);
1518 SET_ITEM_OBJ(executable);
1519 SET_ITEM_OBJ(prefix);
1520 SET_ITEM_OBJ(base_prefix);
1521 SET_ITEM_OBJ(exec_prefix);
1522 SET_ITEM_OBJ(base_exec_prefix);
1523 SET_ITEM_OBJ(warnoptions);
1524 SET_ITEM_OBJ(xoptions);
1525 SET_ITEM_OBJ(module_search_path);
1526 SET_ITEM_OBJ(pycache_prefix);
Victor Stinner00b137c2018-11-13 19:59:26 +01001527
1528 return dict;
1529
1530fail:
1531 Py_DECREF(dict);
1532 return NULL;
1533
Victor Stinner01de89c2018-11-14 17:39:45 +01001534#undef SET_ITEM_OBJ
Victor Stinner00b137c2018-11-13 19:59:26 +01001535}
1536
1537
Victor Stinner41264f12017-12-15 02:05:29 +01001538_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01001539_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
1540 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001541{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001542 if (main_config->install_signal_handlers < 0) {
1543 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001544 }
1545
Victor Stinner9cfc0022017-12-20 19:36:46 +01001546 if (main_config->xoptions == NULL) {
1547 main_config->xoptions = config_create_xoptions_dict(config);
1548 if (main_config->xoptions == NULL) {
1549 return _Py_INIT_NO_MEMORY();
1550 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001551 }
1552
Victor Stinner8ded5b82018-01-24 17:03:28 +01001553#define COPY_WSTR(ATTR) \
1554 do { \
Victor Stinner1dc6e392018-07-25 02:49:17 +02001555 if (main_config->ATTR == NULL && config->ATTR != NULL) { \
Victor Stinner8ded5b82018-01-24 17:03:28 +01001556 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
1557 if (main_config->ATTR == NULL) { \
1558 return _Py_INIT_NO_MEMORY(); \
1559 } \
1560 } \
1561 } while (0)
1562#define COPY_WSTRLIST(ATTR, LEN, LIST) \
1563 do { \
1564 if (ATTR == NULL) { \
Victor Stinner2094c2b2018-09-03 17:06:39 +02001565 ATTR = _Py_wstrlist_as_pylist(LEN, LIST); \
Victor Stinner8ded5b82018-01-24 17:03:28 +01001566 if (ATTR == NULL) { \
1567 return _Py_INIT_NO_MEMORY(); \
1568 } \
1569 } \
1570 } while (0)
1571
1572 COPY_WSTRLIST(main_config->warnoptions,
1573 config->nwarnoption, config->warnoptions);
1574 if (config->argc >= 0) {
1575 COPY_WSTRLIST(main_config->argv,
1576 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001577 }
1578
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001579 if (config->_install_importlib) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001580 COPY_WSTR(executable);
1581 COPY_WSTR(prefix);
1582 COPY_WSTR(base_prefix);
1583 COPY_WSTR(exec_prefix);
1584 COPY_WSTR(base_exec_prefix);
1585
1586 COPY_WSTRLIST(main_config->module_search_path,
1587 config->nmodule_search_path, config->module_search_paths);
Carl Meyerb193fa92018-06-15 22:40:56 -06001588
1589 if (config->pycache_prefix != NULL) {
1590 COPY_WSTR(pycache_prefix);
1591 } else {
1592 main_config->pycache_prefix = NULL;
1593 }
1594
Victor Stinner9cfc0022017-12-20 19:36:46 +01001595 }
Victor Stinner41264f12017-12-15 02:05:29 +01001596
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001597 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01001598#undef COPY_WSTR
1599#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001600}
1601
1602
1603static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001604pymain_init_python_main(_PyMain *pymain, _PyCoreConfig *config,
1605 PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001606{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001607 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001608
Victor Stinner9cfc0022017-12-20 19:36:46 +01001609 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001610 err = _PyMainInterpreterConfig_Read(&main_config, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001611 if (!_Py_INIT_FAILED(err)) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001612 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001613 }
1614 _PyMainInterpreterConfig_Clear(&main_config);
1615
1616 if (_Py_INIT_FAILED(err)) {
1617 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001618 return -1;
1619 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001620 return 0;
1621}
Victor Stinnera7368ac2017-11-15 18:11:45 -08001622
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001623
1624static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02001625pymain_run_python(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001626{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001627 int res = 0;
1628 _PyCoreConfig *config = &interp->core_config;
1629
1630 PyObject *main_importer_path = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001631 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01001632 /* If filename is a package (ex: directory or ZIP file) which contains
1633 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +02001634 prepended to sys.path.
1635
1636 Otherwise, main_importer_path is set to NULL. */
1637 main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01001638 }
1639
Victor Stinnerd3b19192018-07-25 10:21:03 +02001640 if (main_importer_path != NULL) {
1641 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
1642 pymain->status = 1;
1643 goto done;
1644 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001645 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001646 else if (!config->isolated) {
1647 PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc,
1648 config->argv);
1649 if (path0 == NULL) {
1650 pymain->err = _Py_INIT_NO_MEMORY();
1651 res = -1;
1652 goto done;
1653 }
Victor Stinner19760862017-12-20 01:41:59 +01001654
Victor Stinnerd3b19192018-07-25 10:21:03 +02001655 if (pymain_sys_path_add_path0(interp, path0) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001656 Py_DECREF(path0);
Victor Stinnerd3b19192018-07-25 10:21:03 +02001657 pymain->status = 1;
1658 goto done;
Victor Stinner19760862017-12-20 01:41:59 +01001659 }
1660 Py_DECREF(path0);
1661 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001662
Victor Stinner19760862017-12-20 01:41:59 +01001663 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08001664
Victor Stinnerfbca9082018-08-30 00:50:45 +02001665 pymain_header(pymain, config);
1666 pymain_import_readline(pymain, config);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001667
Victor Stinnerca719ac2017-12-20 18:00:19 +01001668 if (pymain->command) {
1669 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001670 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001671 else if (pymain->module) {
1672 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001673 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001674 else if (main_importer_path != NULL) {
1675 int sts = pymain_run_module(L"__main__", 0);
1676 pymain->status = (sts != 0);
1677 }
Victor Stinner72ec3192018-08-02 19:34:20 +02001678 else if (pymain->filename != NULL) {
1679 pymain_run_file(pymain, config, &cf);
1680 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001681 else {
Victor Stinner72ec3192018-08-02 19:34:20 +02001682 pymain_run_stdin(pymain, config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001683 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001684
Victor Stinner1dc6e392018-07-25 02:49:17 +02001685 pymain_repl(pymain, config, &cf);
Victor Stinnerd3b19192018-07-25 10:21:03 +02001686
1687done:
1688 Py_XDECREF(main_importer_path);
1689 return res;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001690}
1691
Victor Stinnera7368ac2017-11-15 18:11:45 -08001692
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001693static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001694pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
1695 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001696{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001697 pymain->err = _PyRuntime_Initialize();
1698 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001699 return -1;
1700 }
1701
Victor Stinner1dc6e392018-07-25 02:49:17 +02001702 int res = pymain_read_conf(pymain, config, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001703 if (res < 0) {
1704 return -1;
1705 }
1706 if (res > 0) {
1707 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01001708 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001709 }
1710
Victor Stinner94540602017-12-16 04:54:22 +01001711 if (cmdline->print_help) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001712 pymain_usage(0, config->program);
Victor Stinner19760862017-12-20 01:41:59 +01001713 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01001714 }
1715
1716 if (cmdline->print_version) {
1717 printf("Python %s\n",
1718 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01001719 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01001720 }
1721
Victor Stinnerc4bca952017-12-19 23:48:17 +01001722 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinner6c785c02018-08-01 17:56:14 +02001723 orig_argv = _Py_wstrlist_copy(pymain->argc, cmdline->argv);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001724 if (orig_argv == NULL) {
1725 pymain->err = _Py_INIT_NO_MEMORY();
1726 return -1;
1727 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001728 orig_argc = pymain->argc;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001729 return 0;
1730}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00001731
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001732
Victor Stinnerca719ac2017-12-20 18:00:19 +01001733/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
1734 LC_CTYPE locale and Py_DecodeLocale().
1735
1736 Configuration:
1737
1738 * Command line arguments
1739 * Environment variables
1740 * Py_xxx global configuration variables
1741
Victor Stinner1dc6e392018-07-25 02:49:17 +02001742 _PyCmdline is a temporary structure used to prioritize these
Victor Stinnerca719ac2017-12-20 18:00:19 +01001743 variables. */
1744static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001745pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerca719ac2017-12-20 18:00:19 +01001746{
Victor Stinner31e99082017-12-20 23:41:38 +01001747 /* Force default allocator, since pymain_free() and pymain_clear_config()
1748 must use the same allocator than this function. */
1749 PyMemAllocatorEx old_alloc;
1750 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
1751#ifdef Py_DEBUG
1752 PyMemAllocatorEx default_alloc;
1753 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
1754#endif
1755
Victor Stinner1dc6e392018-07-25 02:49:17 +02001756 _PyCmdline cmdline;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001757 memset(&cmdline, 0, sizeof(cmdline));
1758
Victor Stinner1dc6e392018-07-25 02:49:17 +02001759 int res = pymain_cmdline_impl(pymain, config, &cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001760
Victor Stinnerca719ac2017-12-20 18:00:19 +01001761 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01001762
1763#ifdef Py_DEBUG
1764 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
1765 PyMemAllocatorEx cur_alloc;
1766 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
1767 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
1768#endif
1769 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001770 return res;
1771}
1772
1773
Victor Stinner94540602017-12-16 04:54:22 +01001774static int
Victor Stinner06e76082018-09-19 14:56:36 -07001775pymain_init(_PyMain *pymain, PyInterpreterState **interp_p)
Victor Stinner94540602017-12-16 04:54:22 +01001776{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001777 /* 754 requires that FP exceptions run in "no stop" mode by default,
1778 * and until C vendors implement C99's ways to control FP exceptions,
1779 * Python requires non-stop mode. Alas, some platforms enable FP
1780 * exceptions by default. Here we disable them.
1781 */
1782#ifdef __FreeBSD__
1783 fedisableexcept(FE_OVERFLOW);
1784#endif
Victor Stinner94540602017-12-16 04:54:22 +01001785
Victor Stinner1dc6e392018-07-25 02:49:17 +02001786 _PyCoreConfig local_config = _PyCoreConfig_INIT;
1787 _PyCoreConfig *config = &local_config;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001788
Victor Stinner1dc6e392018-07-25 02:49:17 +02001789 _PyCoreConfig_GetGlobalConfig(config);
1790
1791 int cmd_res = pymain_cmdline(pymain, config);
1792 if (cmd_res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01001793 _Py_FatalInitError(pymain->err);
1794 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001795 if (cmd_res == 1) {
1796 pymain_clear_config(config);
1797 return 1;
Victor Stinner19760862017-12-20 01:41:59 +01001798 }
1799
Victor Stinner1dc6e392018-07-25 02:49:17 +02001800 _PyCoreConfig_SetGlobalConfig(config);
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001801
Victor Stinner1dc6e392018-07-25 02:49:17 +02001802 pymain_init_stdio(pymain, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001803
Victor Stinner1dc6e392018-07-25 02:49:17 +02001804 PyInterpreterState *interp;
1805 pymain->err = _Py_InitializeCore(&interp, config);
1806 if (_Py_INIT_FAILED(pymain->err)) {
1807 _Py_FatalInitError(pymain->err);
1808 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02001809 *interp_p = interp;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001810
1811 pymain_clear_config(config);
1812 config = &interp->core_config;
1813
1814 if (pymain_init_python_main(pymain, config, interp) < 0) {
1815 _Py_FatalInitError(pymain->err);
1816 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001817 return 0;
1818}
1819
1820
1821static int
Victor Stinner06e76082018-09-19 14:56:36 -07001822pymain_main(_PyMain *pymain)
Victor Stinner1dc6e392018-07-25 02:49:17 +02001823{
Victor Stinnerd3b19192018-07-25 10:21:03 +02001824 PyInterpreterState *interp;
Victor Stinner06e76082018-09-19 14:56:36 -07001825 int res = pymain_init(pymain, &interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +02001826 if (res != 1) {
Victor Stinnerd3b19192018-07-25 10:21:03 +02001827 if (pymain_run_python(pymain, interp) < 0) {
1828 _Py_FatalInitError(pymain->err);
1829 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02001830
1831 if (Py_FinalizeEx() < 0) {
1832 /* Value unlikely to be confused with a non-error exit status or
1833 other special meaning */
1834 pymain->status = 120;
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001835 }
Victor Stinner19760862017-12-20 01:41:59 +01001836 }
1837
Victor Stinner94540602017-12-16 04:54:22 +01001838 pymain_free(pymain);
1839
Gregory P. Smith38f11cc2019-02-16 12:57:40 -08001840 if (_Py_UnhandledKeyboardInterrupt) {
1841 /* https://bugs.python.org/issue1054041 - We need to exit via the
1842 * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
1843 * If we don't, a calling process such as a shell may not know
1844 * about the user's ^C. https://www.cons.org/cracauer/sigint.html */
1845#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
1846 if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
1847 perror("signal"); /* Impossible in normal environments. */
1848 } else {
1849 kill(getpid(), SIGINT);
1850 }
1851 /* If setting SIG_DFL failed, or kill failed to terminate us,
1852 * there isn't much else we can do aside from an error code. */
1853#endif /* HAVE_GETPID && !MS_WINDOWS */
1854#ifdef MS_WINDOWS
1855 /* cmd.exe detects this, prints ^C, and offers to terminate. */
1856 /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
1857 pymain->status = STATUS_CONTROL_C_EXIT;
1858#else
1859 pymain->status = SIGINT + 128;
1860#endif /* !MS_WINDOWS */
1861 }
1862
Victor Stinner94540602017-12-16 04:54:22 +01001863 return pymain->status;
1864}
1865
1866
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001867int
1868Py_Main(int argc, wchar_t **argv)
1869{
1870 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001871 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001872 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001873 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001874
Victor Stinner06e76082018-09-19 14:56:36 -07001875 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00001876}
1877
Victor Stinner94540602017-12-16 04:54:22 +01001878
1879int
1880_Py_UnixMain(int argc, char **argv)
1881{
1882 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01001883 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001884 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01001885 pymain.bytes_argv = argv;
1886
Victor Stinner06e76082018-09-19 14:56:36 -07001887 return pymain_main(&pymain);
Victor Stinner94540602017-12-16 04:54:22 +01001888}
1889
1890
Skip Montanaro786ea6b2004-03-01 15:44:05 +00001891/* this is gonna seem *real weird*, but if you put some other code between
1892 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
1893 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00001894
Guido van Rossum667d7041995-08-04 04:20:48 +00001895/* Make the *original* argc/argv available to other modules.
1896 This is rare, but it is needed by the secureware extension. */
1897
1898void
Martin v. Löwis790465f2008-04-05 20:41:37 +00001899Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00001900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 *argc = orig_argc;
1902 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00001903}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001904
1905#ifdef __cplusplus
1906}
1907#endif