blob: e116dd076538ba43a99c0c9486c4de455349321f [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Guido van Rossuma075ce11997-12-05 21:56:45 +00004#include "osdefs.h"
Benjamin Peterson42aa93b2017-12-09 10:26:52 -08005#include "internal/import.h"
Benjamin Petersone425bd72017-12-14 23:48:12 -08006#include "internal/pygetopt.h"
Victor Stinnerf7e5b562017-11-15 15:48:08 -08007#include "internal/pystate.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00008
Antoine Pitrou5651eaa2008-09-06 20:46:58 +00009#include <locale.h>
10
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000011#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010012# include <windows.h>
13# ifdef HAVE_IO_H
14# include <io.h>
15# endif
16# ifdef HAVE_FCNTL_H
17# include <fcntl.h>
18# endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000019#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000020
Martin v. Löwis945362c2007-08-30 14:57:25 +000021#ifdef _MSC_VER
Victor Stinner6efcb6d2017-12-18 23:42:55 +010022# include <crtdbg.h>
23#endif
24
25#ifdef __FreeBSD__
26# include <fenv.h>
Martin v. Löwis945362c2007-08-30 14:57:25 +000027#endif
28
Jesus Ceaab70e2a2012-10-05 01:48:08 +020029#if defined(MS_WINDOWS)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010030# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
Guido van Rossuma075ce11997-12-05 21:56:45 +000031#else
Victor Stinner6efcb6d2017-12-18 23:42:55 +010032# define PYTHONHOMEHELP "<prefix>/lib/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000033#endif
34
Guido van Rossuma22865e2000-09-05 04:41:18 +000035#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000036 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
37 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000038
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039#ifdef __cplusplus
40extern "C" {
41#endif
42
Victor Stinner46972b72017-11-24 22:55:40 +010043#define DECODE_LOCALE_ERR(NAME, LEN) \
44 (((LEN) == -2) \
Victor Stinner94540602017-12-16 04:54:22 +010045 ? _Py_INIT_USER_ERR("cannot decode " NAME) \
Victor Stinner46972b72017-11-24 22:55:40 +010046 : _Py_INIT_NO_MEMORY())
47
48
Victor Stinner0327bde2017-11-23 17:03:20 +010049#define SET_DECODE_ERROR(NAME, LEN) \
50 do { \
51 if ((LEN) == (size_t)-2) { \
Victor Stinner94540602017-12-16 04:54:22 +010052 pymain->err = _Py_INIT_USER_ERR("cannot decode " NAME); \
Victor Stinner0327bde2017-11-23 17:03:20 +010053 } \
54 else { \
55 pymain->err = _Py_INIT_NO_MEMORY(); \
56 } \
57 } while (0)
58
Victor Stinnerca719ac2017-12-20 18:00:19 +010059#ifdef MS_WINDOWS
60#define WCSTOK wcstok_s
61#else
62#define WCSTOK wcstok
63#endif
64
Guido van Rossumac56b031996-07-21 02:33:38 +000065/* For Py_GetArgcArgv(); set by main() */
Victor Stinner94540602017-12-16 04:54:22 +010066static wchar_t **orig_argv = NULL;
67static int orig_argc = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +000068
Guido van Rossumbceccf52001-04-10 22:07:43 +000069/* command line options */
Christian Heimesad73a9c2013-08-10 16:36:18 +020070#define BASE_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000071
Guido van Rossumbceccf52001-04-10 22:07:43 +000072#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000073
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080074static const _PyOS_LongOption longoptions[] = {
75 {L"check-hash-based-pycs", 1, 0},
76 {NULL, 0, 0},
77};
78
Guido van Rossum667d7041995-08-04 04:20:48 +000079/* Short usage message (with %s for argv0) */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020080static const char usage_line[] =
Martin v. Löwis790465f2008-04-05 20:41:37 +000081"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000082
83/* Long usage message, split into parts < 512 bytes */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020084static const char usage_1[] = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000085Options and arguments (and corresponding environment variables):\n\
Christian Heimes2ab34442008-09-03 20:31:07 +000086-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
87 and comparing bytes/bytearray with str. (-bb: issue errors)\n\
Xiang Zhang0710d752017-03-11 13:02:52 +080088-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000089-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000090-d : debug output from parser; also PYTHONDEBUG=x\n\
Christian Heimes790c8232008-01-07 21:14:23 +000091-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000093";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020094static const char usage_2[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000095-i : inspect interactively after running script; forces a prompt even\n\
96 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Christian Heimesad73a9c2013-08-10 16:36:18 +020097-I : isolate Python from the user's environment (implies -E and -s)\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000098-m mod : run library module as a script (terminates option list)\n\
Cheryl Sabella186b6062018-02-24 22:04:40 -050099-O : remove assert and __debug__-dependent statements; add .opt-1 before\n\
100 .pyc extension; also PYTHONOPTIMIZE=x\n\
101-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
102 .pyc extension\n\
Georg Brandl9d871192010-12-04 10:47:18 +0000103-q : don't print version and copyright messages on interactive startup\n\
Christian Heimes8dc226f2008-05-06 23:45:46 +0000104-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000105-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000106";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200107static const char usage_3[] = "\
Berker Peksag7f580972017-10-13 15:16:31 +0300108-u : force the stdout and stderr streams to be unbuffered;\n\
109 this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
111 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000112-V : print the Python version number and exit (also --version)\n\
INADA Naoki0e175a62016-11-21 20:57:14 +0900113 when given twice, print more information about the build\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenvey0805ca32010-04-07 04:04:10 +0000115 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000116-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000117-X opt : set implementation-specific option\n\
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800118--check-hash-based-pycs always|default|never:\n\
119 control how Python invalidates hash-based .pyc files\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000120";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200121static const char usage_4[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +0000122file : program read from script file\n\
123- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000125Other environment variables:\n\
126PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200127PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000128 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +0000129";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200130static const char usage_5[] =
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200131"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
Victor Stinner9802b392010-08-19 11:36:43 +0000132" The default module search path uses %s.\n"
133"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
134"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
Victor Stinner34be807c2016-03-14 12:04:26 +0100135"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
136static const char usage_6[] =
137"PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
138" to seed the hashes of str, bytes and datetime objects. It can also be\n"
139" set to an integer in the range [0,4294967295] to get hash values with a\n"
140" predictable seed.\n"
141"PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
142" on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000143" hooks.\n"
Stéphane Wirtel7d1017d2017-06-12 13:30:33 +0200144"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000145" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
Victor Stinner5e3806f2017-11-30 11:40:24 +0100146" locale coercion and locale compatibility warnings on stderr.\n"
Carl Meyerb193fa92018-06-15 22:40:56 -0600147"PYTHONDEVMODE: enable the development mode.\n"
148"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
Guido van Rossum667d7041995-08-04 04:20:48 +0000149
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800150static void
Victor Stinnera7368ac2017-11-15 18:11:45 -0800151pymain_usage(int error, const wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000152{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800153 FILE *f = error ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 fprintf(f, usage_line, program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800156 if (error)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 fprintf(f, "Try `python -h' for more information.\n");
158 else {
159 fputs(usage_1, f);
160 fputs(usage_2, f);
161 fputs(usage_3, f);
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200162 fprintf(f, usage_4, (wint_t)DELIM);
163 fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100164 fputs(usage_6, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000166}
167
Victor Stinnera7368ac2017-11-15 18:11:45 -0800168
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200169static const char*
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200170config_get_env_var(const _PyCoreConfig *config, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -0800171{
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200172 assert(config->ignore_environment >= 0);
173
174 if (config->ignore_environment) {
175 return NULL;
176 }
177
178 const char *var = getenv(name);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800179 if (var && var[0] != '\0') {
180 return var;
181 }
182 else {
183 return NULL;
184 }
185}
186
187
Victor Stinnerca719ac2017-12-20 18:00:19 +0100188static int
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200189config_get_env_var_dup(const _PyCoreConfig *config, wchar_t **dest, wchar_t *wname, char *name)
Victor Stinnerca719ac2017-12-20 18:00:19 +0100190{
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200191 assert(config->ignore_environment >= 0);
192
193 if (config->ignore_environment) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100194 *dest = NULL;
195 return 0;
196 }
197
198#ifdef MS_WINDOWS
199 const wchar_t *var = _wgetenv(wname);
200 if (!var || var[0] == '\0') {
201 *dest = NULL;
202 return 0;
203 }
204
205 wchar_t *copy = _PyMem_RawWcsdup(var);
206 if (copy == NULL) {
207 return -1;
208 }
209
210 *dest = copy;
211#else
212 const char *var = getenv(name);
213 if (!var || var[0] == '\0') {
214 *dest = NULL;
215 return 0;
216 }
217
218 size_t len;
219 wchar_t *wvar = Py_DecodeLocale(var, &len);
220 if (!wvar) {
221 if (len == (size_t)-2) {
222 return -2;
223 }
224 else {
225 return -1;
226 }
227 }
228 *dest = wvar;
229#endif
230 return 0;
231}
232
233
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800234static void
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800235pymain_run_interactive_hook(void)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200236{
237 PyObject *sys, *hook, *result;
238 sys = PyImport_ImportModule("sys");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800239 if (sys == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200240 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800241 }
242
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200243 hook = PyObject_GetAttrString(sys, "__interactivehook__");
244 Py_DECREF(sys);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800245 if (hook == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200246 PyErr_Clear();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800247 return;
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200248 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800249
250 result = _PyObject_CallNoArg(hook);
251 Py_DECREF(hook);
252 if (result == NULL) {
253 goto error;
254 }
255 Py_DECREF(result);
256
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200257 return;
258
259error:
260 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
261 PyErr_Print();
262 PyErr_Clear();
263}
264
Thomas Woutersa9773292006-04-21 09:43:23 +0000265
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800266static int
Victor Stinnerc4bca952017-12-19 23:48:17 +0100267pymain_run_module(const wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 PyObject *module, *runpy, *runmodule, *runargs, *result;
270 runpy = PyImport_ImportModule("runpy");
271 if (runpy == NULL) {
272 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200273 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 return -1;
275 }
276 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
277 if (runmodule == NULL) {
278 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200279 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_DECREF(runpy);
281 return -1;
282 }
283 module = PyUnicode_FromWideChar(modname, wcslen(modname));
284 if (module == NULL) {
285 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200286 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 Py_DECREF(runpy);
288 Py_DECREF(runmodule);
289 return -1;
290 }
291 runargs = Py_BuildValue("(Oi)", module, set_argv0);
292 if (runargs == NULL) {
293 fprintf(stderr,
294 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200295 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 Py_DECREF(runpy);
297 Py_DECREF(runmodule);
298 Py_DECREF(module);
299 return -1;
300 }
301 result = PyObject_Call(runmodule, runargs, NULL);
302 if (result == NULL) {
303 PyErr_Print();
304 }
305 Py_DECREF(runpy);
306 Py_DECREF(runmodule);
307 Py_DECREF(module);
308 Py_DECREF(runargs);
309 if (result == NULL) {
310 return -1;
311 }
312 Py_DECREF(result);
313 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000314}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000315
Nick Coghland2977a32017-03-12 20:38:32 +1000316static PyObject *
Victor Stinnerc4bca952017-12-19 23:48:17 +0100317pymain_get_importer(const wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000318{
Nick Coghland2977a32017-03-12 20:38:32 +1000319 PyObject *sys_path0 = NULL, *importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000320
Nick Coghland2977a32017-03-12 20:38:32 +1000321 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800322 if (sys_path0 == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000323 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800324 }
Victor Stinner4726e402010-10-06 23:24:57 +0000325
Nick Coghland2977a32017-03-12 20:38:32 +1000326 importer = PyImport_GetImporter(sys_path0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800327 if (importer == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000328 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800329 }
Victor Stinner4726e402010-10-06 23:24:57 +0000330
Brett Cannonaa936422012-04-27 15:30:58 -0400331 if (importer == Py_None) {
Nick Coghland2977a32017-03-12 20:38:32 +1000332 Py_DECREF(sys_path0);
Victor Stinner4726e402010-10-06 23:24:57 +0000333 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000334 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800336
Victor Stinner4726e402010-10-06 23:24:57 +0000337 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000338 return sys_path0;
Victor Stinner4726e402010-10-06 23:24:57 +0000339
Nick Coghland2977a32017-03-12 20:38:32 +1000340error:
341 Py_XDECREF(sys_path0);
342 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
343 PyErr_Print();
344 PyErr_Clear();
345 return NULL;
346}
347
348
349static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800350pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
Victor Stinnera62207c2010-08-07 10:57:17 +0000351{
352 PyObject *unicode, *bytes;
353 int ret;
354
355 unicode = PyUnicode_FromWideChar(command, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800356 if (unicode == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000357 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800358 }
359
Victor Stinnera62207c2010-08-07 10:57:17 +0000360 bytes = PyUnicode_AsUTF8String(unicode);
361 Py_DECREF(unicode);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800362 if (bytes == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000363 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800364 }
365
Victor Stinnera62207c2010-08-07 10:57:17 +0000366 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
367 Py_DECREF(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800368 return (ret != 0);
Victor Stinnera62207c2010-08-07 10:57:17 +0000369
370error:
Victor Stinner398356b2010-08-18 22:23:22 +0000371 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000372 PyErr_Print();
373 return 1;
374}
375
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800376
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000377static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800378pymain_run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf)
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000379{
380 PyObject *unicode, *bytes = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200381 const char *filename_str;
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000382 int run;
383
384 /* call pending calls like signal handlers (SIGINT) */
385 if (Py_MakePendingCalls() == -1) {
386 PyErr_Print();
387 return 1;
388 }
389
390 if (filename) {
391 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
392 if (unicode != NULL) {
Victor Stinnere0f32682010-10-17 19:34:51 +0000393 bytes = PyUnicode_EncodeFSDefault(unicode);
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000394 Py_DECREF(unicode);
395 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800396 if (bytes != NULL) {
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000397 filename_str = PyBytes_AsString(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800398 }
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000399 else {
400 PyErr_Clear();
Victor Stinnere0f32682010-10-17 19:34:51 +0000401 filename_str = "<encoding error>";
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000402 }
403 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800404 else {
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000405 filename_str = "<stdin>";
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800406 }
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000407
408 run = PyRun_AnyFileExFlags(fp, filename_str, filename != NULL, p_cf);
409 Py_XDECREF(bytes);
410 return run != 0;
411}
412
Christian Heimes9cd17752007-11-18 19:35:23 +0000413
Guido van Rossum667d7041995-08-04 04:20:48 +0000414/* Main program */
415
Eric Snow6b4be192017-05-22 21:36:03 -0700416typedef struct {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100417 wchar_t **argv;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200418 int nwarnoption; /* Number of -W command line options */
419 wchar_t **warnoptions; /* Command line -W options */
420 int nenv_warnoption; /* Number of PYTHONWARNINGS environment variables */
421 wchar_t **env_warnoptions; /* PYTHONWARNINGS environment variables */
Eric Snow6b4be192017-05-22 21:36:03 -0700422 int print_help; /* -h, -? options */
423 int print_version; /* -V option */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200424} _PyCmdline;
Eric Snow6b4be192017-05-22 21:36:03 -0700425
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800426/* Structure used by Py_Main() to pass data to subfunctions */
427typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100428 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800429 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100430 int use_bytes_argv;
431 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100432 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100433
434 /* Exit status or "exit code": result of pymain_main() */
435 int status;
436 /* Error message if a function failed */
437 _PyInitError err;
438
Victor Stinner19760862017-12-20 01:41:59 +0100439 /* non-zero is stdin is a TTY or if -i option is used */
440 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100441 int skip_first_line; /* -x option */
442 wchar_t *filename; /* Trailing arg without -c or -m */
443 wchar_t *command; /* -c argument */
444 wchar_t *module; /* -m argument */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800445} _PyMain;
446
Victor Stinner1dc6e392018-07-25 02:49:17 +0200447#define _PyMain_INIT {.err = _Py_INIT_OK()}
Victor Stinnerd5dda982017-12-13 17:31:16 +0100448/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800449
450
Victor Stinner19760862017-12-20 01:41:59 +0100451/* Non-zero if filename, command (-c) or module (-m) is set
452 on the command line */
453#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100454 (pymain->command != NULL || pymain->filename != NULL \
455 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100456
457
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200458static void
459clear_wstrlist(int len, wchar_t **list)
460{
461 for (int i=0; i < len; i++) {
462 PyMem_RawFree(list[i]);
463 }
464 PyMem_RawFree(list);
465}
466
467
468static wchar_t**
469copy_wstrlist(int len, wchar_t **list)
470{
471 assert((len > 0 && list != NULL) || len == 0);
472 size_t size = len * sizeof(list[0]);
473 wchar_t **list_copy = PyMem_RawMalloc(size);
474 for (int i=0; i < len; i++) {
475 wchar_t* arg = _PyMem_RawWcsdup(list[i]);
476 if (arg == NULL) {
477 clear_wstrlist(i, list);
478 return NULL;
479 }
480 list_copy[i] = arg;
481 }
482 return list_copy;
483}
484
485
Victor Stinnerca719ac2017-12-20 18:00:19 +0100486static wchar_t*
487pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800488{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100489 wchar_t *str2 = _PyMem_RawWcsdup(str);
490 if (str2 == NULL) {
491 pymain->err = _Py_INIT_NO_MEMORY();
492 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800493 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100494 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800495}
496
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100497
Victor Stinnerc4bca952017-12-19 23:48:17 +0100498static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200499pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,
500 _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100501{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100502 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100503
Victor Stinnerca719ac2017-12-20 18:00:19 +0100504 if (pymain->use_bytes_argv) {
505 /* +1 for a the NULL terminator */
506 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
507 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
508 if (argv == NULL) {
509 pymain->err = _Py_INIT_NO_MEMORY();
510 return -1;
511 }
512
513 for (int i = 0; i < pymain->argc; i++) {
514 size_t len;
515 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
516 if (arg == NULL) {
517 clear_wstrlist(i, argv);
518 pymain->err = DECODE_LOCALE_ERR("command line arguments",
519 (Py_ssize_t)len);
520 return -1;
521 }
522 argv[i] = arg;
523 }
524 argv[pymain->argc] = NULL;
525
526 cmdline->argv = argv;
527 }
528 else {
529 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100530 }
531
Victor Stinnerca719ac2017-12-20 18:00:19 +0100532 wchar_t *program;
533 if (pymain->argc >= 1 && cmdline->argv != NULL) {
534 program = cmdline->argv[0];
535 }
536 else {
537 program = L"";
538 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200539 config->program = pymain_wstrdup(pymain, program);
540 if (config->program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100541 return -1;
542 }
543
Victor Stinnerc4bca952017-12-19 23:48:17 +0100544 return 0;
545}
546
547
548static void
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200549_PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config)
550{
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200551#define COPY_FLAG(ATTR, VALUE) \
552 if (config->ATTR == -1) { \
553 config->ATTR = VALUE; \
554 }
555#define COPY_NOT_FLAG(ATTR, VALUE) \
556 if (config->ATTR == -1) { \
557 config->ATTR = !(VALUE); \
558 }
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200559
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200560 COPY_FLAG(ignore_environment, Py_IgnoreEnvironmentFlag);
561 COPY_FLAG(utf8_mode, Py_UTF8Mode);
562 COPY_FLAG(isolated, Py_IsolatedFlag);
563 COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
564 COPY_FLAG(inspect, Py_InspectFlag);
565 COPY_FLAG(interactive, Py_InteractiveFlag);
566 COPY_FLAG(optimization_level, Py_OptimizeFlag);
567 COPY_FLAG(debug, Py_DebugFlag);
568 COPY_FLAG(verbose, Py_VerboseFlag);
569 COPY_FLAG(quiet, Py_QuietFlag);
570 COPY_FLAG(unbuffered_stdio, Py_UnbufferedStdioFlag);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200571#ifdef MS_WINDOWS
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200572 COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
573 COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200574#endif
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200575
576 COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
577 COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
578 COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
579
580 if (config->_check_hash_pycs_mode == NULL) {
581 config->_check_hash_pycs_mode = _Py_CheckHashBasedPycsMode;
582 }
583
584#undef COPY_FLAG
585#undef COPY_NOT_FLAG
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200586}
587
588
589/* Set Py_xxx global configuration variables from 'config' configuration. */
590void
591_PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config)
592{
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200593#define COPY_FLAG(ATTR, VAR) \
594 if (config->ATTR != -1) { \
595 VAR = config->ATTR; \
596 }
597#define COPY_NOT_FLAG(ATTR, VAR) \
598 if (config->ATTR != -1) { \
599 VAR = !config->ATTR; \
600 }
601
602 COPY_FLAG(ignore_environment, Py_IgnoreEnvironmentFlag);
603 COPY_FLAG(utf8_mode, Py_UTF8Mode);
604 COPY_FLAG(isolated, Py_IsolatedFlag);
605 COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
606 COPY_FLAG(inspect, Py_InspectFlag);
607 COPY_FLAG(interactive, Py_InteractiveFlag);
608 COPY_FLAG(optimization_level, Py_OptimizeFlag);
609 COPY_FLAG(debug, Py_DebugFlag);
610 COPY_FLAG(verbose, Py_VerboseFlag);
611 COPY_FLAG(quiet, Py_QuietFlag);
612 COPY_FLAG(unbuffered_stdio, Py_UnbufferedStdioFlag);
613#ifdef MS_WINDOWS
614 COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
615 COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
616#endif
617
618 COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
619 COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
620 COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
621
622 if (config->_check_hash_pycs_mode != NULL) {
623 _Py_CheckHashBasedPycsMode = config->_check_hash_pycs_mode;
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200624 }
625
626 /* Random or non-zero hash seed */
627 Py_HashRandomizationFlag = (config->use_hash_seed == 0 ||
628 config->hash_seed != 0);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200629
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200630#undef COPY_FLAG
631#undef COPY_NOT_FLAG
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200632}
633
634
635/* Free memory allocated in config, but don't clear all attributes */
636void
637_PyCoreConfig_Clear(_PyCoreConfig *config)
638{
639#define CLEAR(ATTR) \
640 do { \
641 PyMem_RawFree(ATTR); \
642 ATTR = NULL; \
643 } while (0)
644#define CLEAR_WSTRLIST(LEN, LIST) \
645 do { \
646 clear_wstrlist(LEN, LIST); \
647 LEN = 0; \
648 LIST = NULL; \
649 } while (0)
650
651 CLEAR(config->pycache_prefix);
652 CLEAR(config->module_search_path_env);
653 CLEAR(config->home);
654 CLEAR(config->program_name);
655 CLEAR(config->program);
656
657 CLEAR_WSTRLIST(config->argc, config->argv);
658 config->argc = -1;
659
660 CLEAR_WSTRLIST(config->nwarnoption, config->warnoptions);
661 CLEAR_WSTRLIST(config->nxoption, config->xoptions);
662 CLEAR_WSTRLIST(config->nmodule_search_path, config->module_search_paths);
663 config->nmodule_search_path = -1;
664
665 CLEAR(config->executable);
666 CLEAR(config->prefix);
667 CLEAR(config->base_prefix);
668 CLEAR(config->exec_prefix);
669#ifdef MS_WINDOWS
670 CLEAR(config->dll_path);
671#endif
672 CLEAR(config->base_exec_prefix);
673#undef CLEAR
674#undef CLEAR_WSTRLIST
675}
676
677
678int
679_PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
680{
681 _PyCoreConfig_Clear(config);
682
683#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
684#define COPY_STR_ATTR(ATTR) \
685 do { \
686 if (config2->ATTR != NULL) { \
687 config->ATTR = _PyMem_RawWcsdup(config2->ATTR); \
688 if (config->ATTR == NULL) { \
689 return -1; \
690 } \
691 } \
692 } while (0)
693#define COPY_WSTRLIST(LEN, LIST) \
694 do { \
695 if (config2->LIST != NULL) { \
696 config->LIST = copy_wstrlist(config2->LEN, config2->LIST); \
697 if (config->LIST == NULL) { \
698 return -1; \
699 } \
700 } \
701 config->LEN = config2->LEN; \
702 } while (0)
703
704 COPY_ATTR(ignore_environment);
705 COPY_ATTR(use_hash_seed);
706 COPY_ATTR(hash_seed);
707 COPY_ATTR(_install_importlib);
708 COPY_ATTR(allocator);
709 COPY_ATTR(dev_mode);
710 COPY_ATTR(faulthandler);
711 COPY_ATTR(tracemalloc);
712 COPY_ATTR(import_time);
713 COPY_ATTR(show_ref_count);
714 COPY_ATTR(show_alloc_count);
715 COPY_ATTR(dump_refs);
716 COPY_ATTR(malloc_stats);
717 COPY_ATTR(utf8_mode);
718
719 COPY_STR_ATTR(pycache_prefix);
720 COPY_STR_ATTR(module_search_path_env);
721 COPY_STR_ATTR(home);
722 COPY_STR_ATTR(program_name);
723 COPY_STR_ATTR(program);
724
725 COPY_WSTRLIST(argc, argv);
726 COPY_WSTRLIST(nwarnoption, warnoptions);
727 COPY_WSTRLIST(nxoption, xoptions);
728 COPY_WSTRLIST(nmodule_search_path, module_search_paths);
729
730 COPY_STR_ATTR(executable);
731 COPY_STR_ATTR(prefix);
732 COPY_STR_ATTR(base_prefix);
733 COPY_STR_ATTR(exec_prefix);
734#ifdef MS_WINDOWS
735 COPY_STR_ATTR(dll_path);
736#endif
737 COPY_STR_ATTR(base_exec_prefix);
738
739 COPY_ATTR(isolated);
740 COPY_ATTR(site_import);
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200741 COPY_ATTR(bytes_warning);
742 COPY_ATTR(inspect);
743 COPY_ATTR(interactive);
744 COPY_ATTR(optimization_level);
745 COPY_ATTR(debug);
746 COPY_ATTR(write_bytecode);
747 COPY_ATTR(verbose);
748 COPY_ATTR(quiet);
749 COPY_ATTR(user_site_directory);
750 COPY_ATTR(unbuffered_stdio);
751#ifdef MS_WINDOWS
752 COPY_ATTR(legacy_windows_fs_encoding);
753 COPY_ATTR(legacy_windows_stdio);
754#endif
755 COPY_ATTR(_check_hash_pycs_mode);
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200756
757#undef COPY_ATTR
758#undef COPY_STR_ATTR
759#undef COPY_WSTRLIST
760 return 0;
761}
762
763
764static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200765pymain_clear_cmdline(_PyMain *pymain, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100766{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100767 PyMemAllocatorEx old_alloc;
768 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100769
Victor Stinnerca719ac2017-12-20 18:00:19 +0100770 clear_wstrlist(cmdline->nwarnoption, cmdline->warnoptions);
771 cmdline->nwarnoption = 0;
772 cmdline->warnoptions = NULL;
773
774 clear_wstrlist(cmdline->nenv_warnoption, cmdline->env_warnoptions);
775 cmdline->nenv_warnoption = 0;
776 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100777
778 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100779 clear_wstrlist(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100780 }
781 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100782
783 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
784}
785
786
787static void
788pymain_clear_pymain(_PyMain *pymain)
789{
790#define CLEAR(ATTR) \
791 do { \
792 PyMem_RawFree(ATTR); \
793 ATTR = NULL; \
794 } while (0)
795
796 CLEAR(pymain->filename);
797 CLEAR(pymain->command);
798 CLEAR(pymain->module);
799#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100800}
801
Victor Stinnerc4bca952017-12-19 23:48:17 +0100802static void
Victor Stinner1dc6e392018-07-25 02:49:17 +0200803pymain_clear_config(_PyCoreConfig *config)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100804{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100805 /* Clear core config with the memory allocator
806 used by pymain_read_conf() */
807 PyMemAllocatorEx old_alloc;
808 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
809
Victor Stinner1dc6e392018-07-25 02:49:17 +0200810 _PyCoreConfig_Clear(config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100811
812 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
813}
814
815
816static void
Victor Stinnerc4bca952017-12-19 23:48:17 +0100817pymain_free_raw(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100818{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100819 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100820
Victor Stinnerc4bca952017-12-19 23:48:17 +0100821 /* Free global variables which cannot be freed in Py_Finalize():
822 configuration options set before Py_Initialize() which should
823 remain valid after Py_Finalize(), since
824 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinnerb1147e42018-07-21 02:06:16 +0200825 _PyPathConfig_ClearGlobal();
Victor Stinner94540602017-12-16 04:54:22 +0100826
Victor Stinnerc4bca952017-12-19 23:48:17 +0100827 /* Force the allocator used by pymain_read_conf() */
828 PyMemAllocatorEx old_alloc;
829 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100830
Victor Stinnerca719ac2017-12-20 18:00:19 +0100831 pymain_clear_pymain(pymain);
832
833 clear_wstrlist(orig_argc, orig_argv);
834 orig_argc = 0;
835 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100836
Victor Stinnerc4bca952017-12-19 23:48:17 +0100837 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100838}
839
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100840
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800841static void
842pymain_free(_PyMain *pymain)
843{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100844 pymain_free_raw(pymain);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200845
846#ifdef __INSURE__
847 /* Insure++ is a memory analysis tool that aids in discovering
848 * memory leaks and other memory problems. On Python exit, the
849 * interned string dictionaries are flagged as being in use at exit
850 * (which it is). Under normal circumstances, this is fine because
851 * the memory will be automatically reclaimed by the system. Under
852 * memory debugging, it's a huge source of useless noise, so we
853 * trade off slower shutdown for less distraction in the memory
854 * reports. -baw
855 */
856 _Py_ReleaseInternedUnicodeStrings();
857#endif /* __INSURE__ */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800858}
859
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100860
Eric Snow6b4be192017-05-22 21:36:03 -0700861static int
Victor Stinnerd3b19192018-07-25 10:21:03 +0200862pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
Guido van Rossum667d7041995-08-04 04:20:48 +0000863{
Victor Stinnerd3b19192018-07-25 10:21:03 +0200864 PyObject *sys_path;
865 PyObject *sysdict = interp->sysdict;
866 if (sysdict != NULL) {
867 sys_path = PyDict_GetItemString(sysdict, "path");
868 }
869 else {
870 sys_path = NULL;
871 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800872 if (sys_path == NULL) {
873 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
874 goto error;
875 }
876
Victor Stinnerd3b19192018-07-25 10:21:03 +0200877 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800878 goto error;
879 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200880 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800881
882error:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800883 PyErr_Print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200884 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800885}
886
887
Victor Stinnerb1147e42018-07-21 02:06:16 +0200888_PyInitError
889_Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800890{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200891 if (*len == INT_MAX) {
892 /* len+1 would overflow */
893 return _Py_INIT_NO_MEMORY();
894 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100895 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800896 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100897 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800898 }
899
Victor Stinnerca719ac2017-12-20 18:00:19 +0100900 size_t size = (*len + 1) * sizeof(list[0]);
901 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
902 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800903 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100904 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800905 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100906 list2[*len] = str2;
907 *list = list2;
908 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100909 return _Py_INIT_OK();
910}
911
912
913static int
914pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
915{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200916 _PyInitError err = _Py_wstrlist_append(len, list, str);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100917 if (_Py_INIT_FAILED(err)) {
918 pymain->err = err;
919 return -1;
920 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800921 return 0;
922}
923
924
925/* Parse the command line arguments
926 Return 0 on success.
927 Return 1 if parsing failed.
928 Set pymain->err and return -1 on other errors. */
929static int
Victor Stinner1dc6e392018-07-25 02:49:17 +0200930pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
931 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800932{
Antoine Pitrou86838b02012-02-21 19:03:47 +0100933 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800934 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800935 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100936 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800937 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800938 if (c == EOF) {
939 break;
940 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 /* -c is the last option; following arguments
944 that look like options are left for the
945 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800946 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
947 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
948 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100949 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800950 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800951 }
Victor Stinner58d16832018-05-31 15:09:28 +0200952 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 command[len - 2] = '\n';
954 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100955 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 break;
957 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 if (c == 'm') {
960 /* -m is the last option; following arguments
961 that look like options are left for the
962 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100963 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
964 if (pymain->module == NULL) {
965 return -1;
966 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 break;
968 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800971 case 0:
972 // Handle long option.
973 assert(longindex == 0); // Only one long option now.
974 if (!wcscmp(_PyOS_optarg, L"always")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200975 config->_check_hash_pycs_mode = "always";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800976 } else if (!wcscmp(_PyOS_optarg, L"never")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200977 config->_check_hash_pycs_mode = "never";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800978 } else if (!wcscmp(_PyOS_optarg, L"default")) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200979 config->_check_hash_pycs_mode = "default";
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800980 } else {
981 fprintf(stderr, "--check-hash-based-pycs must be one of "
982 "'default', 'always', or 'never'\n");
983 return 1;
984 }
985 break;
986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 case 'b':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200988 config->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 case 'd':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200992 config->debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 case 'i':
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200996 config->inspect++;
997 config->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000999
Christian Heimesad73a9c2013-08-10 16:36:18 +02001000 case 'I':
Victor Stinner9cfc0022017-12-20 19:36:46 +01001001 config->ignore_environment++;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001002 config->isolated++;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001003 config->user_site_directory = 0;
Christian Heimesad73a9c2013-08-10 16:36:18 +02001004 break;
1005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 case 'O':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001009 config->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 break;
Guido van Rossum7614da61997-03-03 19:14:45 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 case 'B':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001013 config->write_bytecode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 break;
Christian Heimes790c8232008-01-07 21:14:23 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 case 's':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001017 config->user_site_directory = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 case 'S':
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001021 config->site_import = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 case 'E':
Victor Stinner9cfc0022017-12-20 19:36:46 +01001025 config->ignore_environment++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 case 't':
1029 /* ignored for backwards compatibility */
1030 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 case 'u':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001033 config->unbuffered_stdio = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 break;
Guido van Rossum667d7041995-08-04 04:20:48 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 case 'v':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001037 config->verbose++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 break;
Guido van Rossum667d7041995-08-04 04:20:48 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +01001041 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 case 'h':
1045 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -07001046 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -07001050 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +01001054 if (pymain_wstrlist_append(pymain,
1055 &cmdline->nwarnoption,
1056 &cmdline->warnoptions,
1057 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001058 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001059 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +00001061
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001062 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +01001063 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +01001064 &config->nxoption,
1065 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +01001066 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001067 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001068 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001069 break;
1070
Georg Brandl9d871192010-12-04 10:47:18 +00001071 case 'q':
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001072 config->quiet++;
Georg Brandl9d871192010-12-04 10:47:18 +00001073 break;
1074
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001075 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +01001076 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001077 break;
1078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001082 /* unknown argument: parsing failed */
1083 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001085 } while (1);
1086
Victor Stinnerca719ac2017-12-20 18:00:19 +01001087 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001088 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +01001089 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001090 {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001091 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
1092 if (pymain->filename == NULL) {
1093 return -1;
1094 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001096
Victor Stinnerd5dda982017-12-13 17:31:16 +01001097 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +01001098 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +01001099
Eric Snow6b4be192017-05-22 21:36:03 -07001100 return 0;
1101}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +00001102
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001103
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001104static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01001105add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +01001106{
1107 PyObject *name, *value;
1108
1109 const wchar_t *name_end = wcschr(s, L'=');
1110 if (!name_end) {
1111 name = PyUnicode_FromWideChar(s, -1);
1112 value = Py_True;
1113 Py_INCREF(value);
1114 }
1115 else {
1116 name = PyUnicode_FromWideChar(s, name_end - s);
1117 value = PyUnicode_FromWideChar(name_end + 1, -1);
1118 }
1119 if (name == NULL || value == NULL) {
1120 goto error;
1121 }
1122 if (PyDict_SetItem(opts, name, value) < 0) {
1123 goto error;
1124 }
1125 Py_DECREF(name);
1126 Py_DECREF(value);
1127 return 0;
1128
1129error:
1130 Py_XDECREF(name);
1131 Py_XDECREF(value);
1132 return -1;
1133}
1134
Victor Stinner9cfc0022017-12-20 19:36:46 +01001135
1136static PyObject*
1137config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001138{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001139 int nxoption = config->nxoption;
1140 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +01001141 PyObject *dict = PyDict_New();
1142 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001143 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +01001144 }
1145
Victor Stinnerca719ac2017-12-20 18:00:19 +01001146 for (int i=0; i < nxoption; i++) {
1147 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +01001148 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +01001149 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001150 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001151 }
1152 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001153
Victor Stinner9cfc0022017-12-20 19:36:46 +01001154 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -07001155}
1156
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001157
Victor Stinner9cfc0022017-12-20 19:36:46 +01001158static _PyInitError
1159config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -07001160{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001161 for (int i = 0; i < len; i++) {
Victor Stinnerb1147e42018-07-21 02:06:16 +02001162 _PyInitError err = _Py_wstrlist_append(&config->nwarnoption,
1163 &config->warnoptions,
1164 options[i]);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001165 if (_Py_INIT_FAILED(err)) {
1166 return err;
Eric Snow6b4be192017-05-22 21:36:03 -07001167 }
Eric Snow6b4be192017-05-22 21:36:03 -07001168 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001169 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001170}
Eric Snow6b4be192017-05-22 21:36:03 -07001171
Victor Stinner747f48e2017-12-12 22:59:48 +01001172
Victor Stinner9cfc0022017-12-20 19:36:46 +01001173static _PyInitError
Victor Stinner1dc6e392018-07-25 02:49:17 +02001174config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +01001175{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001176 _PyInitError err;
1177
1178 assert(config->nwarnoption == 0);
1179
Victor Stinner747f48e2017-12-12 22:59:48 +01001180 /* The priority order for warnings configuration is (highest precedence
1181 * first):
1182 *
1183 * - the BytesWarning filter, if needed ('-b', '-bb')
1184 * - any '-W' command line options; then
1185 * - the 'PYTHONWARNINGS' environment variable; then
1186 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
1187 * - any implicit filters added by _warnings.c/warnings.py
1188 *
1189 * All settings except the last are passed to the warnings module via
1190 * the `sys.warnoptions` list. Since the warnings module works on the basis
1191 * of "the most recently added filter will be checked first", we add
1192 * the lowest precedence entries first so that later entries override them.
1193 */
1194
Victor Stinner9cfc0022017-12-20 19:36:46 +01001195 if (config->dev_mode) {
Victor Stinnerb1147e42018-07-21 02:06:16 +02001196 err = _Py_wstrlist_append(&config->nwarnoption,
1197 &config->warnoptions,
1198 L"default");
Victor Stinner9cfc0022017-12-20 19:36:46 +01001199 if (_Py_INIT_FAILED(err)) {
1200 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001201 }
Victor Stinner747f48e2017-12-12 22:59:48 +01001202 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001203
Victor Stinner9cfc0022017-12-20 19:36:46 +01001204 err = config_add_warnings_optlist(config,
1205 cmdline->nenv_warnoption,
1206 cmdline->env_warnoptions);
1207 if (_Py_INIT_FAILED(err)) {
1208 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001209 }
1210
Victor Stinner9cfc0022017-12-20 19:36:46 +01001211 err = config_add_warnings_optlist(config,
1212 cmdline->nwarnoption,
1213 cmdline->warnoptions);
1214 if (_Py_INIT_FAILED(err)) {
1215 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001216 }
1217
1218 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
1219 * don't even try to emit a warning, so we skip setting the filter in that
1220 * case.
1221 */
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001222 if (config->bytes_warning) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001223 wchar_t *filter;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001224 if (config->bytes_warning> 1) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001225 filter = L"error::BytesWarning";
1226 }
1227 else {
1228 filter = L"default::BytesWarning";
1229 }
Victor Stinnerb1147e42018-07-21 02:06:16 +02001230 err = _Py_wstrlist_append(&config->nwarnoption,
1231 &config->warnoptions,
1232 filter);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001233 if (_Py_INIT_FAILED(err)) {
1234 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001235 }
1236 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001237 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001238}
1239
1240
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001241/* Get warning options from PYTHONWARNINGS environment variable.
1242 Return 0 on success.
1243 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001244static _PyInitError
Victor Stinner1dc6e392018-07-25 02:49:17 +02001245cmdline_init_env_warnoptions(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001246{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001247 if (config->ignore_environment) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001248 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +00001250
Victor Stinnerca719ac2017-12-20 18:00:19 +01001251 wchar_t *env;
Victor Stinner1dc6e392018-07-25 02:49:17 +02001252 int res = config_get_env_var_dup(config, &env,
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001253 L"PYTHONWARNINGS", "PYTHONWARNINGS");
Victor Stinnerca719ac2017-12-20 18:00:19 +01001254 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001255 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001256 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001257
Victor Stinnerca719ac2017-12-20 18:00:19 +01001258 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001259 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001260 }
Philip Jenvey0805ca32010-04-07 04:04:10 +00001261
Victor Stinnerca719ac2017-12-20 18:00:19 +01001262
1263 wchar_t *warning, *context = NULL;
1264 for (warning = WCSTOK(env, L",", &context);
1265 warning != NULL;
1266 warning = WCSTOK(NULL, L",", &context))
1267 {
Victor Stinnerb1147e42018-07-21 02:06:16 +02001268 _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption,
1269 &cmdline->env_warnoptions,
1270 warning);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001271 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001272 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001273 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001274 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001276 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001277 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001278}
1279
1280
1281static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001282pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001283{
1284 pymain->stdin_is_interactive = (isatty(fileno(stdin))
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001285 || config->interactive);
Guido van Rossum775af911997-02-14 19:50:32 +00001286
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +00001287#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +00001288 /* don't translate newlines (\r\n <=> \n) */
1289 _setmode(fileno(stdin), O_BINARY);
1290 _setmode(fileno(stdout), O_BINARY);
1291 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +00001292#endif
Victor Stinner89e34362011-01-07 18:47:22 +00001293
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001294 if (config->unbuffered_stdio) {
Guido van Rossum22ffac11998-03-06 15:30:39 +00001295#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
1297 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
1298 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001299#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 setbuf(stdin, (char *)NULL);
1301 setbuf(stdout, (char *)NULL);
1302 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001303#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001305 else if (config->interactive) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +00001306#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 /* Doesn't have to have line-buffered -- use unbuffered */
1308 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
1309 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001310#else /* !MS_WINDOWS */
1311#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
1313 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001314#endif /* HAVE_SETVBUF */
1315#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 /* Leave stderr alone - it should be unbuffered anyway. */
1317 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001318}
Guido van Rossum667d7041995-08-04 04:20:48 +00001319
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001320
1321/* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
Victor Stinner31a83932017-12-04 13:39:15 +01001322 environment variables on macOS if available. */
1323static _PyInitError
Victor Stinnerca719ac2017-12-20 18:00:19 +01001324config_init_program_name(_PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001325{
Victor Stinner31a83932017-12-04 13:39:15 +01001326 /* If Py_SetProgramName() was called, use its value */
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001327 const wchar_t *program_name = _Py_path_config.program_name;
Victor Stinner31a83932017-12-04 13:39:15 +01001328 if (program_name != NULL) {
1329 config->program_name = _PyMem_RawWcsdup(program_name);
1330 if (config->program_name == NULL) {
1331 return _Py_INIT_NO_MEMORY();
1332 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001333 return _Py_INIT_OK();
Victor Stinner31a83932017-12-04 13:39:15 +01001334 }
1335
Just van Rossum2ac79ef2003-03-05 15:46:54 +00001336#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 /* On MacOS X, when the Python interpreter is embedded in an
1338 application bundle, it gets executed by a bootstrapping script
1339 that does os.execve() with an argv[0] that's different from the
1340 actual Python executable. This is needed to keep the Finder happy,
1341 or rather, to work around Apple's overly strict requirements of
1342 the process name. However, we still need a usable sys.executable,
1343 so the actual executable path is passed in an environment variable.
1344 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
1345 script. */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001346 const char *p = config_get_env_var(config, "PYTHONEXECUTABLE");
Victor Stinner6bf992a2017-12-06 17:26:10 +01001347 if (p != NULL) {
Victor Stinner31a83932017-12-04 13:39:15 +01001348 size_t len;
1349 wchar_t* program_name = Py_DecodeLocale(p, &len);
1350 if (program_name == NULL) {
1351 return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment "
1352 "variable", (Py_ssize_t)len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 }
Victor Stinner31a83932017-12-04 13:39:15 +01001354 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001355 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001356 }
Vinay Sajip90db6612012-07-17 17:33:46 +01001357#ifdef WITH_NEXT_FRAMEWORK
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001358 else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001359 const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
Vinay Sajip90db6612012-07-17 17:33:46 +01001360 if (pyvenv_launcher && *pyvenv_launcher) {
1361 /* Used by Mac/Tools/pythonw.c to forward
1362 * the argv0 of the stub executable
1363 */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001364 size_t len;
Victor Stinner31a83932017-12-04 13:39:15 +01001365 wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len);
1366 if (program_name == NULL) {
1367 return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment "
1368 "variable", (Py_ssize_t)len);
Vinay Sajip90db6612012-07-17 17:33:46 +01001369 }
Victor Stinner31a83932017-12-04 13:39:15 +01001370 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001371 return _Py_INIT_OK();
Vinay Sajip90db6612012-07-17 17:33:46 +01001372 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001374#endif /* WITH_NEXT_FRAMEWORK */
1375#endif /* __APPLE__ */
Victor Stinneraf5a8952017-12-02 10:11:32 +01001376
Victor Stinnerca719ac2017-12-20 18:00:19 +01001377 /* Use argv[0] by default, if available */
1378 if (config->program != NULL) {
1379 config->program_name = _PyMem_RawWcsdup(config->program);
1380 if (config->program_name == NULL) {
1381 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001382 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001383 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001384 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001385
1386 /* Last fall back: hardcoded string */
1387#ifdef MS_WINDOWS
1388 const wchar_t *default_program_name = L"python";
1389#else
1390 const wchar_t *default_program_name = L"python3";
1391#endif
1392 config->program_name = _PyMem_RawWcsdup(default_program_name);
1393 if (config->program_name == NULL) {
1394 return _Py_INIT_NO_MEMORY();
1395 }
1396 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001397}
1398
1399
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001400static void
1401pymain_header(_PyMain *pymain)
1402{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001403 if (Py_QuietFlag) {
1404 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 }
Guido van Rossum393661d2001-08-31 17:40:15 +00001406
Victor Stinner19760862017-12-20 01:41:59 +01001407 if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001408 return;
1409 }
1410
1411 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
1412 if (!Py_NoSiteFlag) {
1413 fprintf(stderr, "%s\n", COPYRIGHT);
1414 }
1415}
1416
1417
Victor Stinnerc4bca952017-12-19 23:48:17 +01001418static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001419pymain_init_core_argv(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001420{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001421 /* Copy argv to be able to modify it (to force -c/-m) */
1422 int argc = pymain->argc - _PyOS_optind;
1423 wchar_t **argv;
1424
1425 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +01001426 /* Ensure at least one (empty) argument is seen */
1427 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +01001428 argc = 1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001429 argv = copy_wstrlist(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +01001430 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001431 else {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001432 argv = copy_wstrlist(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001433 }
1434
1435 if (argv == NULL) {
1436 pymain->err = _Py_INIT_NO_MEMORY();
1437 return -1;
1438 }
1439
1440 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001441 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001442 /* Force sys.argv[0] = '-c' */
1443 arg0 = L"-c";
1444 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001445 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001446 /* Force sys.argv[0] = '-m'*/
1447 arg0 = L"-m";
1448 }
1449 if (arg0 != NULL) {
1450 arg0 = _PyMem_RawWcsdup(arg0);
1451 if (arg0 == NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001452 clear_wstrlist(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001453 pymain->err = _Py_INIT_NO_MEMORY();
1454 return -1;
1455 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001456
1457 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001458 PyMem_RawFree(argv[0]);
1459 argv[0] = arg0;
1460 }
1461
Victor Stinner1dc6e392018-07-25 02:49:17 +02001462 config->argc = argc;
1463 config->argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001464 return 0;
1465}
1466
1467
Victor Stinner8ded5b82018-01-24 17:03:28 +01001468static PyObject*
1469wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001470{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001471 assert(list != NULL || len < 1);
1472
1473 PyObject *pylist = PyList_New(len);
1474 if (pylist == NULL) {
1475 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001476 }
1477
Victor Stinner8ded5b82018-01-24 17:03:28 +01001478 for (int i = 0; i < len; i++) {
1479 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001480 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001481 Py_DECREF(pylist);
1482 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001483 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001484 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001485 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001486 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001487}
1488
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001489
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001490static void
1491pymain_import_readline(_PyMain *pymain)
1492{
1493 if (Py_IsolatedFlag) {
1494 return;
1495 }
Victor Stinner19760862017-12-20 01:41:59 +01001496 if (!Py_InspectFlag && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001497 return;
1498 }
1499 if (!isatty(fileno(stdin))) {
1500 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001501 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001502
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001503 PyObject *mod = PyImport_ImportModule("readline");
1504 if (mod == NULL) {
1505 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 }
1507 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001508 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001510}
1511
1512
1513static FILE*
Victor Stinner1dc6e392018-07-25 02:49:17 +02001514pymain_open_filename(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001515{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001516 FILE* fp;
1517
Victor Stinnerca719ac2017-12-20 18:00:19 +01001518 fp = _Py_wfopen(pymain->filename, L"r");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001519 if (fp == NULL) {
1520 char *cfilename_buffer;
1521 const char *cfilename;
1522 int err = errno;
Victor Stinner9dd76202017-12-21 16:20:32 +01001523 cfilename_buffer = _Py_EncodeLocaleRaw(pymain->filename, NULL);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001524 if (cfilename_buffer != NULL)
1525 cfilename = cfilename_buffer;
1526 else
1527 cfilename = "<unprintable file name>";
1528 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinner1dc6e392018-07-25 02:49:17 +02001529 config->program, cfilename, err, strerror(err));
Victor Stinner9dd76202017-12-21 16:20:32 +01001530 PyMem_RawFree(cfilename_buffer);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001531 pymain->status = 2;
1532 return NULL;
1533 }
1534
Victor Stinnerca719ac2017-12-20 18:00:19 +01001535 if (pymain->skip_first_line) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001536 int ch;
1537 /* Push back first newline so line numbers
1538 remain the same */
1539 while ((ch = getc(fp)) != EOF) {
1540 if (ch == '\n') {
1541 (void)ungetc(ch, fp);
1542 break;
1543 }
1544 }
1545 }
1546
1547 struct _Py_stat_struct sb;
1548 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 &&
1549 S_ISDIR(sb.st_mode)) {
1550 fprintf(stderr,
1551 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinner1dc6e392018-07-25 02:49:17 +02001552 config->program, pymain->filename);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001553 fclose(fp);
1554 pymain->status = 1;
1555 return NULL;
1556 }
1557
1558 return fp;
1559}
1560
1561
1562static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001563pymain_run_startup(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001564{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001565 const char *startup = config_get_env_var(config, "PYTHONSTARTUP");
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001566 if (startup == NULL) {
1567 return;
1568 }
1569
1570 FILE *fp = _Py_fopen(startup, "r");
1571 if (fp == NULL) {
1572 int save_errno = errno;
1573 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
1574 errno = save_errno;
1575
1576 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
1577 startup);
1578 PyErr_Print();
1579 PyErr_Clear();
1580 return;
1581 }
1582
1583 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
1584 PyErr_Clear();
1585 fclose(fp);
1586}
1587
1588
1589static void
Victor Stinnerd3b19192018-07-25 10:21:03 +02001590pymain_run_filename(_PyMain *pymain, _PyCoreConfig *config,
1591 PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001592{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001593 if (pymain->filename == NULL && pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001594 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner1dc6e392018-07-25 02:49:17 +02001595 pymain_run_startup(pymain, config, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001596 pymain_run_interactive_hook();
1597 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001598
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001599 FILE *fp;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001600 if (pymain->filename != NULL) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001601 fp = pymain_open_filename(pymain, config);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001602 if (fp == NULL) {
1603 return;
1604 }
1605 }
1606 else {
1607 fp = stdin;
1608 }
1609
Victor Stinnerca719ac2017-12-20 18:00:19 +01001610 pymain->status = pymain_run_file(fp, pymain->filename, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001611}
1612
1613
1614static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001615pymain_repl(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001618 opportunity to set it from Python. */
Victor Stinner1dc6e392018-07-25 02:49:17 +02001619 if (!Py_InspectFlag && config_get_env_var(config, "PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 Py_InspectFlag = 1;
1621 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001622
Victor Stinner19760862017-12-20 01:41:59 +01001623 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001624 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001626
1627 Py_InspectFlag = 0;
1628 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001629
Victor Stinner19760862017-12-20 01:41:59 +01001630 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001631 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001632}
1633
1634
1635/* Parse the command line.
1636 Handle --version and --help options directly.
1637
1638 Return 1 if Python must exit.
1639 Return 0 on success.
1640 Set pymain->err and return -1 on failure. */
1641static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02001642pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config,
1643 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001644{
Victor Stinner1dc6e392018-07-25 02:49:17 +02001645 int res = pymain_parse_cmdline_impl(pymain, config, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001646 if (res < 0) {
1647 return -1;
1648 }
1649 if (res) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001650 pymain_usage(1, config->program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001651 pymain->status = 2;
1652 return 1;
1653 }
1654
Victor Stinnerca719ac2017-12-20 18:00:19 +01001655 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001656 /* Backup _PyOS_optind */
1657 _PyOS_optind--;
1658 }
1659
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001660 return 0;
1661}
1662
1663
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001664static const wchar_t*
Victor Stinner9cfc0022017-12-20 19:36:46 +01001665config_get_xoption(_PyCoreConfig *config, wchar_t *name)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001666{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001667 int nxoption = config->nxoption;
1668 wchar_t **xoptions = config->xoptions;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001669 for (int i=0; i < nxoption; i++) {
1670 wchar_t *option = xoptions[i];
Victor Stinnera7368ac2017-11-15 18:11:45 -08001671 size_t len;
1672 wchar_t *sep = wcschr(option, L'=');
1673 if (sep != NULL) {
1674 len = (sep - option);
1675 }
1676 else {
1677 len = wcslen(option);
1678 }
1679 if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
1680 return option;
1681 }
1682 }
1683 return NULL;
1684}
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001685
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001686
Victor Stinnera7368ac2017-11-15 18:11:45 -08001687static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001688pymain_str_to_int(const char *str, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001689{
1690 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001691 const char *endptr = str;
1692 long value = strtol(str, (char **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001693 if (*endptr != '\0' || errno == ERANGE) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001694 return -1;
1695 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001696 if (value < INT_MIN || value > INT_MAX) {
1697 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001698 }
1699
Victor Stinnera7368ac2017-11-15 18:11:45 -08001700 *result = (int)value;
1701 return 0;
1702}
1703
1704
1705static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001706pymain_wstr_to_int(const wchar_t *wstr, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001707{
1708 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001709 const wchar_t *endptr = wstr;
1710 long value = wcstol(wstr, (wchar_t **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001711 if (*endptr != '\0' || errno == ERANGE) {
1712 return -1;
1713 }
1714 if (value < INT_MIN || value > INT_MAX) {
1715 return -1;
1716 }
1717
1718 *result = (int)value;
1719 return 0;
1720}
1721
1722
Victor Stinner9cfc0022017-12-20 19:36:46 +01001723static _PyInitError
1724pymain_init_tracemalloc(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001725{
1726 int nframe;
1727 int valid;
1728
Victor Stinner60b04c92018-07-25 19:23:53 +02001729 if (config->tracemalloc >= 0) {
1730 return _Py_INIT_OK();
1731 }
1732
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001733 const char *env = config_get_env_var(config, "PYTHONTRACEMALLOC");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001734 if (env) {
1735 if (!pymain_str_to_int(env, &nframe)) {
Victor Stinner60b04c92018-07-25 19:23:53 +02001736 valid = (nframe >= 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001737 }
1738 else {
1739 valid = 0;
1740 }
1741 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001742 return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number "
1743 "of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001744 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001745 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001746 }
1747
Victor Stinner9cfc0022017-12-20 19:36:46 +01001748 const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001749 if (xoption) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001750 const wchar_t *sep = wcschr(xoption, L'=');
Victor Stinnera7368ac2017-11-15 18:11:45 -08001751 if (sep) {
1752 if (!pymain_wstr_to_int(sep + 1, &nframe)) {
Victor Stinner60b04c92018-07-25 19:23:53 +02001753 valid = (nframe >= 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001754 }
1755 else {
1756 valid = 0;
1757 }
1758 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001759 return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: "
1760 "invalid number of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001761 }
1762 }
1763 else {
1764 /* -X tracemalloc behaves as -X tracemalloc=1 */
1765 nframe = 1;
1766 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001767 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001768 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001769 return _Py_INIT_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001770}
1771
1772
Carl Meyerb193fa92018-06-15 22:40:56 -06001773static _PyInitError
1774pymain_init_pycache_prefix(_PyCoreConfig *config)
1775{
1776 wchar_t *env;
1777
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001778 int res = config_get_env_var_dup(config, &env,
1779 L"PYTHONPYCACHEPREFIX", "PYTHONPYCACHEPREFIX");
Carl Meyerb193fa92018-06-15 22:40:56 -06001780 if (res < 0) {
1781 return DECODE_LOCALE_ERR("PYTHONPYCACHEPREFIX", res);
1782 } else if (env) {
1783 config->pycache_prefix = env;
1784 }
1785
1786 const wchar_t *xoption = config_get_xoption(config, L"pycache_prefix");
1787 if (xoption) {
1788 const wchar_t *sep = wcschr(xoption, L'=');
1789 if (sep && wcslen(sep) > 1) {
1790 config->pycache_prefix = _PyMem_RawWcsdup(sep + 1);
1791 if (config->pycache_prefix == NULL) {
1792 return _Py_INIT_NO_MEMORY();
1793 }
1794 } else {
1795 // -X pycache_prefix= can cancel the env var
1796 config->pycache_prefix = NULL;
1797 }
1798 }
1799
1800 return _Py_INIT_OK();
1801}
1802
1803
Victor Stinnera7368ac2017-11-15 18:11:45 -08001804static void
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001805get_env_flag(_PyCoreConfig *config, int *flag, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001806{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001807 const char *var = config_get_env_var(config, name);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001808 if (!var) {
1809 return;
1810 }
1811 int value;
1812 if (pymain_str_to_int(var, &value) < 0 || value < 0) {
1813 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
1814 value = 1;
1815 }
1816 if (*flag < value) {
1817 *flag = value;
1818 }
1819}
1820
1821
1822static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001823cmdline_get_env_flags(_PyMain *pymain, _PyCoreConfig *config,
1824 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001825{
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001826 get_env_flag(config, &config->debug, "PYTHONDEBUG");
1827 get_env_flag(config, &config->verbose, "PYTHONVERBOSE");
1828 get_env_flag(config, &config->optimization_level, "PYTHONOPTIMIZE");
1829 get_env_flag(config, &config->inspect, "PYTHONINSPECT");
1830
1831 int dont_write_bytecode = 0;
1832 get_env_flag(config, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
1833 if (dont_write_bytecode) {
1834 config->write_bytecode = 0;
1835 }
1836
1837 int no_user_site_directory = 0;
1838 get_env_flag(config, &no_user_site_directory, "PYTHONNOUSERSITE");
1839 if (no_user_site_directory) {
1840 config->user_site_directory = 0;
1841 }
1842
1843 get_env_flag(config, &config->unbuffered_stdio, "PYTHONUNBUFFERED");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001844#ifdef MS_WINDOWS
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001845 get_env_flag(config, &config->legacy_windows_fs_encoding,
Victor Stinner9cfc0022017-12-20 19:36:46 +01001846 "PYTHONLEGACYWINDOWSFSENCODING");
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001847 get_env_flag(config, &config->legacy_windows_stdio,
Victor Stinner9cfc0022017-12-20 19:36:46 +01001848 "PYTHONLEGACYWINDOWSSTDIO");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001849#endif
1850}
1851
1852
Victor Stinner46972b72017-11-24 22:55:40 +01001853static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001854config_init_home(_PyCoreConfig *config)
Victor Stinner1f151112017-11-23 10:43:14 +01001855{
1856 wchar_t *home;
1857
Victor Stinner31a83932017-12-04 13:39:15 +01001858 /* If Py_SetPythonHome() was called, use its value */
1859 home = _Py_path_config.home;
Victor Stinner1f151112017-11-23 10:43:14 +01001860 if (home) {
Victor Stinner46972b72017-11-24 22:55:40 +01001861 config->home = _PyMem_RawWcsdup(home);
1862 if (config->home == NULL) {
1863 return _Py_INIT_NO_MEMORY();
Victor Stinner1f151112017-11-23 10:43:14 +01001864 }
Victor Stinner46972b72017-11-24 22:55:40 +01001865 return _Py_INIT_OK();
Victor Stinner1f151112017-11-23 10:43:14 +01001866 }
1867
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001868 int res = config_get_env_var_dup(config, &home, L"PYTHONHOME", "PYTHONHOME");
Victor Stinner1f151112017-11-23 10:43:14 +01001869 if (res < 0) {
Victor Stinner46972b72017-11-24 22:55:40 +01001870 return DECODE_LOCALE_ERR("PYTHONHOME", res);
Victor Stinner1f151112017-11-23 10:43:14 +01001871 }
Victor Stinner46972b72017-11-24 22:55:40 +01001872 config->home = home;
1873 return _Py_INIT_OK();
Victor Stinnerd4341102017-11-23 00:12:09 +01001874}
1875
1876
Victor Stinner358e5e12017-12-15 00:51:22 +01001877static _PyInitError
1878config_init_hash_seed(_PyCoreConfig *config)
1879{
1880 if (config->use_hash_seed < 0) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001881 const char *seed_text = config_get_env_var(config, "PYTHONHASHSEED");
Victor Stinner358e5e12017-12-15 00:51:22 +01001882 int use_hash_seed;
1883 unsigned long hash_seed;
1884 if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
1885 return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
1886 "or an integer in range [0; 4294967295]");
1887 }
1888 config->use_hash_seed = use_hash_seed;
1889 config->hash_seed = hash_seed;
1890 }
1891 return _Py_INIT_OK();
1892}
1893
1894
Victor Stinner9cfc0022017-12-20 19:36:46 +01001895static _PyInitError
1896config_init_utf8_mode(_PyCoreConfig *config)
Victor Stinner91106cd2017-12-13 12:29:09 +01001897{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001898 /* The option was already set in config.Py_UTF8Mode,
1899 by Py_LegacyWindowsFSEncodingFlag or PYTHONLEGACYWINDOWSFSENCODING. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001900 if (config->utf8_mode >= 0) {
1901 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001902 }
Victor Stinner91106cd2017-12-13 12:29:09 +01001903
Victor Stinner9cfc0022017-12-20 19:36:46 +01001904 const wchar_t *xopt = config_get_xoption(config, L"utf8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001905 if (xopt) {
1906 wchar_t *sep = wcschr(xopt, L'=');
1907 if (sep) {
1908 xopt = sep + 1;
1909 if (wcscmp(xopt, L"1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001910 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001911 }
1912 else if (wcscmp(xopt, L"0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001913 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001914 }
1915 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001916 return _Py_INIT_USER_ERR("invalid -X utf8 option value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001917 }
1918 }
1919 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001920 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001921 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001922 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001923 }
1924
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001925 const char *opt = config_get_env_var(config, "PYTHONUTF8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001926 if (opt) {
1927 if (strcmp(opt, "1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001928 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001929 }
1930 else if (strcmp(opt, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001931 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001932 }
1933 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001934 return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
1935 "variable value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001936 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001937 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001938 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001939
1940 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001941}
Victor Stinner46972b72017-11-24 22:55:40 +01001942
1943
Victor Stinner9cfc0022017-12-20 19:36:46 +01001944static _PyInitError
1945config_read_env_vars(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001946{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001947 config->allocator = config_get_env_var(config, "PYTHONMALLOC");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001948
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001949 if (config_get_env_var(config, "PYTHONDUMPREFS")) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001950 config->dump_refs = 1;
1951 }
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001952 if (config_get_env_var(config, "PYTHONMALLOCSTATS")) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001953 config->malloc_stats = 1;
Victor Stinner31a83932017-12-04 13:39:15 +01001954 }
1955
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001956 const char *env = config_get_env_var(config, "PYTHONCOERCECLOCALE");
Victor Stinner94540602017-12-16 04:54:22 +01001957 if (env) {
1958 if (strcmp(env, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001959 config->coerce_c_locale = 0;
Victor Stinner94540602017-12-16 04:54:22 +01001960 }
1961 else if (strcmp(env, "warn") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001962 config->coerce_c_locale_warn = 1;
Victor Stinner94540602017-12-16 04:54:22 +01001963 }
1964 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001965 config->coerce_c_locale = 1;
Victor Stinner94540602017-12-16 04:54:22 +01001966 }
1967 }
1968
Victor Stinner9cfc0022017-12-20 19:36:46 +01001969 wchar_t *path;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001970 int res = config_get_env_var_dup(config, &path, L"PYTHONPATH", "PYTHONPATH");
Victor Stinner9cfc0022017-12-20 19:36:46 +01001971 if (res < 0) {
Carl Meyer48575432018-05-19 16:48:22 -06001972 return DECODE_LOCALE_ERR("PYTHONPATH", res);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001973 }
1974 config->module_search_path_env = path;
1975
1976 _PyInitError err = config_init_hash_seed(config);
1977 if (_Py_INIT_FAILED(err)) {
1978 return err;
1979 }
1980
1981 return _Py_INIT_OK();
1982}
1983
1984
1985static _PyInitError
1986config_read_complex_options(_PyCoreConfig *config)
1987{
1988 /* More complex options configured by env var and -X option */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001989 if (config_get_env_var(config, "PYTHONFAULTHANDLER")
Victor Stinner9cfc0022017-12-20 19:36:46 +01001990 || config_get_xoption(config, L"faulthandler")) {
1991 config->faulthandler = 1;
1992 }
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001993 if (config_get_env_var(config, "PYTHONPROFILEIMPORTTIME")
Victor Stinner9cfc0022017-12-20 19:36:46 +01001994 || config_get_xoption(config, L"importtime")) {
1995 config->import_time = 1;
1996 }
1997 if (config_get_xoption(config, L"dev" ) ||
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001998 config_get_env_var(config, "PYTHONDEVMODE"))
Victor Stinner9cfc0022017-12-20 19:36:46 +01001999 {
2000 config->dev_mode = 1;
2001 config->faulthandler = 1;
2002 config->allocator = "debug";
2003 }
2004
2005 _PyInitError err = pymain_init_tracemalloc(config);
2006 if (_Py_INIT_FAILED(err)) {
2007 return err;
2008 }
Carl Meyerb193fa92018-06-15 22:40:56 -06002009
2010 err = pymain_init_pycache_prefix(config);
2011 if (_Py_INIT_FAILED(err)) {
2012 return err;
2013 }
2014
Victor Stinner9cfc0022017-12-20 19:36:46 +01002015 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002016}
2017
2018
Victor Stinnera7368ac2017-11-15 18:11:45 -08002019/* Parse command line options and environment variables.
2020 This code must not use Python runtime apart PyMem_Raw memory allocator.
2021
2022 Return 0 on success.
2023 Return 1 if Python is done and must exit.
2024 Set pymain->err and return -1 on error. */
2025static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002026pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
2027 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002028{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002029 _PyInitError err;
2030
Victor Stinner1dc6e392018-07-25 02:49:17 +02002031 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01002032 if (res != 0) {
2033 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002034 }
2035
Victor Stinner9cfc0022017-12-20 19:36:46 +01002036 /* Get environment variables */
Victor Stinner1dc6e392018-07-25 02:49:17 +02002037 cmdline_get_env_flags(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002038
Victor Stinner1dc6e392018-07-25 02:49:17 +02002039 err = cmdline_init_env_warnoptions(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002040 if (_Py_INIT_FAILED(err)) {
2041 pymain->err = err;
2042 return -1;
2043 }
2044
2045#ifdef MS_WINDOWS
Victor Stinner53b7d4e2018-07-25 01:37:05 +02002046 if (config->legacy_windows_fs_encoding) {
2047 config->utf8_mode = 0;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002048 }
2049#endif
2050
Victor Stinner1dc6e392018-07-25 02:49:17 +02002051 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01002052 return -1;
2053 }
2054
Victor Stinner53b7d4e2018-07-25 01:37:05 +02002055 err = _PyCoreConfig_Read(config);
Victor Stinner31a83932017-12-04 13:39:15 +01002056 if (_Py_INIT_FAILED(err)) {
2057 pymain->err = err;
2058 return -1;
2059 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002060 return 0;
2061}
2062
2063
Victor Stinner19760862017-12-20 01:41:59 +01002064/* Read the configuration, but initialize also the LC_CTYPE locale:
2065 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
Victor Stinnera7368ac2017-11-15 18:11:45 -08002066static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002067pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
2068 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002069{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002070 _PyCoreConfig save_config = _PyCoreConfig_INIT;
2071 char *oldloc = NULL;
Victor Stinner94540602017-12-16 04:54:22 +01002072 int res = -1;
2073
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002074 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
Victor Stinner94540602017-12-16 04:54:22 +01002075 if (oldloc == NULL) {
2076 pymain->err = _Py_INIT_NO_MEMORY();
2077 goto done;
2078 }
2079
2080 /* Reconfigure the locale to the default for this process */
2081 _Py_SetLocaleFromEnv(LC_ALL);
2082
2083 int locale_coerced = 0;
2084 int loops = 0;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002085
2086 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
2087 pymain->err = _Py_INIT_NO_MEMORY();
2088 goto done;
2089 }
Victor Stinner94540602017-12-16 04:54:22 +01002090
2091 while (1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002092 int utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002093 int encoding_changed = 0;
2094
2095 /* Watchdog to prevent an infinite loop */
2096 loops++;
2097 if (loops == 3) {
2098 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
2099 "reading the configuration");
2100 goto done;
2101 }
2102
Victor Stinner1dc6e392018-07-25 02:49:17 +02002103 if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01002104 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01002105 }
2106
Victor Stinner1dc6e392018-07-25 02:49:17 +02002107 int conf_res = pymain_read_conf_impl(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002108 if (conf_res != 0) {
2109 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01002110 goto done;
2111 }
2112
2113 /* The legacy C locale assumes ASCII as the default text encoding, which
2114 * causes problems not only for the CPython runtime, but also other
2115 * components like GNU readline.
2116 *
2117 * Accordingly, when the CLI detects it, it attempts to coerce it to a
2118 * more capable UTF-8 based alternative.
2119 *
2120 * See the documentation of the PYTHONCOERCECLOCALE setting for more
2121 * details.
2122 */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002123 if (config->coerce_c_locale == 1 && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01002124 locale_coerced = 1;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002125 _Py_CoerceLegacyLocale(config);
Victor Stinner94540602017-12-16 04:54:22 +01002126 encoding_changed = 1;
2127 }
2128
2129 if (utf8_mode == -1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002130 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01002131 /* UTF-8 Mode enabled */
2132 encoding_changed = 1;
2133 }
2134 }
2135 else {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002136 if (config->utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01002137 encoding_changed = 1;
2138 }
2139 }
2140
2141 if (!encoding_changed) {
2142 break;
2143 }
2144
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002145 /* Reset the configuration before reading the configuration,
2146 except UTF-8 Mode. */
2147 int new_utf8_mode = config->utf8_mode;
2148 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
2149 pymain->err = _Py_INIT_NO_MEMORY();
2150 goto done;
2151 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002152 pymain_clear_cmdline(pymain, cmdline);
Victor Stinner6c5a4b32018-06-16 00:06:28 +02002153 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner1dc6e392018-07-25 02:49:17 +02002154 config->utf8_mode = new_utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002155
2156 /* The encoding changed: read again the configuration
2157 with the new encoding */
2158 }
2159 res = 0;
2160
2161done:
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002162 _PyCoreConfig_Clear(&save_config);
Victor Stinner94540602017-12-16 04:54:22 +01002163 if (oldloc != NULL) {
2164 setlocale(LC_ALL, oldloc);
2165 PyMem_RawFree(oldloc);
2166 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002167
Victor Stinnera7368ac2017-11-15 18:11:45 -08002168 return res;
2169}
2170
Victor Stinner91106cd2017-12-13 12:29:09 +01002171
Victor Stinner9cfc0022017-12-20 19:36:46 +01002172static void
2173config_init_locale(_PyCoreConfig *config)
2174{
2175 if (config->utf8_mode >= 0 && config->coerce_c_locale >= 0) {
2176 return;
2177 }
2178
2179 if (_Py_LegacyLocaleDetected()) {
2180 /* POSIX locale: enable C locale coercion and UTF-8 Mode */
2181 if (config->utf8_mode < 0) {
2182 config->utf8_mode = 1;
2183 }
2184 if (config->coerce_c_locale < 0) {
2185 config->coerce_c_locale = 1;
2186 }
2187 return;
2188 }
2189
2190 /* By default, C locale coercion and UTF-8 Mode are disabled */
2191 if (config->coerce_c_locale < 0) {
2192 config->coerce_c_locale = 0;
2193 }
2194 if (config->utf8_mode < 0) {
2195 config->utf8_mode = 0;
2196 }
2197}
2198
2199
Victor Stinnerda273412017-12-15 01:46:02 +01002200/* Read configuration settings from standard locations
2201 *
2202 * This function doesn't make any changes to the interpreter state - it
2203 * merely populates any missing configuration settings. This allows an
2204 * embedding application to completely override a config option by
2205 * setting it before calling this function, or else modify the default
2206 * setting before passing the fully populated config to Py_EndInitialization.
2207 *
2208 * More advanced selective initialization tricks are possible by calling
2209 * this function multiple times with various preconfigured settings.
2210 */
2211
2212_PyInitError
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002213_PyCoreConfig_Read(_PyCoreConfig *config)
Victor Stinnerda273412017-12-15 01:46:02 +01002214{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002215 _PyInitError err;
2216
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002217 _PyCoreConfig_GetGlobalConfig(config);
2218
Victor Stinner9cfc0022017-12-20 19:36:46 +01002219 err = config_read_env_vars(config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002220 if (_Py_INIT_FAILED(err)) {
2221 return err;
2222 }
2223
Victor Stinner9cfc0022017-12-20 19:36:46 +01002224 /* -X options */
2225 if (config_get_xoption(config, L"showrefcount")) {
2226 config->show_ref_count = 1;
2227 }
2228 if (config_get_xoption(config, L"showalloccount")) {
2229 config->show_alloc_count = 1;
2230 }
2231
2232 err = config_read_complex_options(config);
2233 if (_Py_INIT_FAILED(err)) {
2234 return err;
2235 }
2236
2237 err = config_init_utf8_mode(config);
2238 if (_Py_INIT_FAILED(err)) {
2239 return err;
2240 }
2241
2242 err = config_init_home(config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002243 if (_Py_INIT_FAILED(err)) {
2244 return err;
2245 }
2246
Victor Stinner1dc6e392018-07-25 02:49:17 +02002247 if (config->program_name == NULL) {
2248 err = config_init_program_name(config);
2249 if (_Py_INIT_FAILED(err)) {
2250 return err;
2251 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002252 }
2253
Victor Stinner9cfc0022017-12-20 19:36:46 +01002254 config_init_locale(config);
Victor Stinnerda273412017-12-15 01:46:02 +01002255
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002256 if (config->_install_importlib) {
2257 err = _PyCoreConfig_InitPathConfig(config);
Victor Stinner8ded5b82018-01-24 17:03:28 +01002258 if (_Py_INIT_FAILED(err)) {
2259 return err;
2260 }
2261 }
Victor Stinner60b04c92018-07-25 19:23:53 +02002262
2263 /* default values */
2264 if (config->tracemalloc < 0) {
2265 config->tracemalloc = 0;
2266 }
2267 if (config->install_signal_handlers < 0) {
2268 /* Signal handlers are installed by default */
2269 config->install_signal_handlers = 1;
2270 }
2271
Victor Stinnerda273412017-12-15 01:46:02 +01002272 return _Py_INIT_OK();
2273}
2274
2275
2276void
Victor Stinnerda273412017-12-15 01:46:02 +01002277_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
2278{
2279 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002280 Py_CLEAR(config->executable);
2281 Py_CLEAR(config->prefix);
2282 Py_CLEAR(config->base_prefix);
2283 Py_CLEAR(config->exec_prefix);
2284 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002285 Py_CLEAR(config->warnoptions);
2286 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002287 Py_CLEAR(config->module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06002288 Py_CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002289}
2290
2291
2292static PyObject*
2293config_copy_attr(PyObject *obj)
2294{
2295 if (PyUnicode_Check(obj)) {
2296 Py_INCREF(obj);
2297 return obj;
2298 }
2299 else if (PyList_Check(obj)) {
2300 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
2301 }
2302 else if (PyDict_Check(obj)) {
2303 /* The dict type is used for xoptions. Make the assumption that keys
2304 and values are immutables */
2305 return PyDict_Copy(obj);
2306 }
2307 else {
2308 PyErr_Format(PyExc_TypeError,
2309 "cannot copy config attribute of type %.200s",
2310 Py_TYPE(obj)->tp_name);
2311 return NULL;
2312 }
2313}
2314
2315
2316int
2317_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
2318 const _PyMainInterpreterConfig *config2)
2319{
2320 _PyMainInterpreterConfig_Clear(config);
2321
2322#define COPY_ATTR(ATTR) \
2323 do { \
2324 if (config2->ATTR != NULL) { \
2325 config->ATTR = config_copy_attr(config2->ATTR); \
2326 if (config->ATTR == NULL) { \
2327 return -1; \
2328 } \
2329 } \
2330 } while (0)
2331
2332 COPY_ATTR(argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002333 COPY_ATTR(executable);
2334 COPY_ATTR(prefix);
2335 COPY_ATTR(base_prefix);
2336 COPY_ATTR(exec_prefix);
2337 COPY_ATTR(base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002338 COPY_ATTR(warnoptions);
2339 COPY_ATTR(xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002340 COPY_ATTR(module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06002341 COPY_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002342#undef COPY_ATTR
2343 return 0;
2344}
2345
2346
2347
2348
Victor Stinner41264f12017-12-15 02:05:29 +01002349_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01002350_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
2351 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002352{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002353 if (main_config->install_signal_handlers < 0) {
2354 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002355 }
2356
Victor Stinner9cfc0022017-12-20 19:36:46 +01002357 if (main_config->xoptions == NULL) {
2358 main_config->xoptions = config_create_xoptions_dict(config);
2359 if (main_config->xoptions == NULL) {
2360 return _Py_INIT_NO_MEMORY();
2361 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002362 }
2363
Victor Stinner8ded5b82018-01-24 17:03:28 +01002364#define COPY_WSTR(ATTR) \
2365 do { \
Victor Stinner1dc6e392018-07-25 02:49:17 +02002366 if (main_config->ATTR == NULL && config->ATTR != NULL) { \
Victor Stinner8ded5b82018-01-24 17:03:28 +01002367 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
2368 if (main_config->ATTR == NULL) { \
2369 return _Py_INIT_NO_MEMORY(); \
2370 } \
2371 } \
2372 } while (0)
2373#define COPY_WSTRLIST(ATTR, LEN, LIST) \
2374 do { \
2375 if (ATTR == NULL) { \
2376 ATTR = wstrlist_as_pylist(LEN, LIST); \
2377 if (ATTR == NULL) { \
2378 return _Py_INIT_NO_MEMORY(); \
2379 } \
2380 } \
2381 } while (0)
2382
2383 COPY_WSTRLIST(main_config->warnoptions,
2384 config->nwarnoption, config->warnoptions);
2385 if (config->argc >= 0) {
2386 COPY_WSTRLIST(main_config->argv,
2387 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002388 }
2389
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002390 if (config->_install_importlib) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01002391 COPY_WSTR(executable);
2392 COPY_WSTR(prefix);
2393 COPY_WSTR(base_prefix);
2394 COPY_WSTR(exec_prefix);
2395 COPY_WSTR(base_exec_prefix);
2396
2397 COPY_WSTRLIST(main_config->module_search_path,
2398 config->nmodule_search_path, config->module_search_paths);
Carl Meyerb193fa92018-06-15 22:40:56 -06002399
2400 if (config->pycache_prefix != NULL) {
2401 COPY_WSTR(pycache_prefix);
2402 } else {
2403 main_config->pycache_prefix = NULL;
2404 }
2405
Victor Stinner9cfc0022017-12-20 19:36:46 +01002406 }
Victor Stinner41264f12017-12-15 02:05:29 +01002407
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002408 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01002409#undef COPY_WSTR
2410#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002411}
2412
2413
2414static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002415pymain_init_python_main(_PyMain *pymain, _PyCoreConfig *config,
2416 PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002417{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002418 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002419
Victor Stinner9cfc0022017-12-20 19:36:46 +01002420 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner1dc6e392018-07-25 02:49:17 +02002421 err = _PyMainInterpreterConfig_Read(&main_config, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002422 if (!_Py_INIT_FAILED(err)) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02002423 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002424 }
2425 _PyMainInterpreterConfig_Clear(&main_config);
2426
2427 if (_Py_INIT_FAILED(err)) {
2428 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002429 return -1;
2430 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002431 return 0;
2432}
Victor Stinnera7368ac2017-11-15 18:11:45 -08002433
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002434
2435static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02002436pymain_run_python(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002437{
Victor Stinnerd3b19192018-07-25 10:21:03 +02002438 int res = 0;
2439 _PyCoreConfig *config = &interp->core_config;
2440
2441 PyObject *main_importer_path = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002442 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002443 /* If filename is a package (ex: directory or ZIP file) which contains
2444 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +02002445 prepended to sys.path.
2446
2447 Otherwise, main_importer_path is set to NULL. */
2448 main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01002449 }
2450
Victor Stinnerd3b19192018-07-25 10:21:03 +02002451 if (main_importer_path != NULL) {
2452 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
2453 pymain->status = 1;
2454 goto done;
2455 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01002456 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02002457 else if (!config->isolated) {
2458 PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc,
2459 config->argv);
2460 if (path0 == NULL) {
2461 pymain->err = _Py_INIT_NO_MEMORY();
2462 res = -1;
2463 goto done;
2464 }
Victor Stinner19760862017-12-20 01:41:59 +01002465
Victor Stinnerd3b19192018-07-25 10:21:03 +02002466 if (pymain_sys_path_add_path0(interp, path0) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01002467 Py_DECREF(path0);
Victor Stinnerd3b19192018-07-25 10:21:03 +02002468 pymain->status = 1;
2469 goto done;
Victor Stinner19760862017-12-20 01:41:59 +01002470 }
2471 Py_DECREF(path0);
2472 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002473
Victor Stinner19760862017-12-20 01:41:59 +01002474 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08002475
2476 pymain_header(pymain);
2477 pymain_import_readline(pymain);
2478
Victor Stinnerca719ac2017-12-20 18:00:19 +01002479 if (pymain->command) {
2480 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002481 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002482 else if (pymain->module) {
2483 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002484 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02002485 else if (main_importer_path != NULL) {
2486 int sts = pymain_run_module(L"__main__", 0);
2487 pymain->status = (sts != 0);
2488 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002489 else {
Victor Stinner1dc6e392018-07-25 02:49:17 +02002490 pymain_run_filename(pymain, config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002491 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002492
Victor Stinner1dc6e392018-07-25 02:49:17 +02002493 pymain_repl(pymain, config, &cf);
Victor Stinnerd3b19192018-07-25 10:21:03 +02002494
2495done:
2496 Py_XDECREF(main_importer_path);
2497 return res;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002498}
2499
Victor Stinnera7368ac2017-11-15 18:11:45 -08002500
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002501static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002502pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
2503 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002504{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002505 pymain->err = _PyRuntime_Initialize();
2506 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002507 return -1;
2508 }
2509
Victor Stinner1dc6e392018-07-25 02:49:17 +02002510 int res = pymain_read_conf(pymain, config, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002511 if (res < 0) {
2512 return -1;
2513 }
2514 if (res > 0) {
2515 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01002516 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002517 }
2518
Victor Stinner94540602017-12-16 04:54:22 +01002519 if (cmdline->print_help) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02002520 pymain_usage(0, config->program);
Victor Stinner19760862017-12-20 01:41:59 +01002521 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002522 }
2523
2524 if (cmdline->print_version) {
2525 printf("Python %s\n",
2526 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01002527 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002528 }
2529
Victor Stinnerc4bca952017-12-19 23:48:17 +01002530 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002531 orig_argv = copy_wstrlist(pymain->argc, cmdline->argv);
2532 if (orig_argv == NULL) {
2533 pymain->err = _Py_INIT_NO_MEMORY();
2534 return -1;
2535 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01002536 orig_argc = pymain->argc;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002537
Victor Stinner1dc6e392018-07-25 02:49:17 +02002538 _PyInitError err = config_init_warnoptions(config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002539 if (_Py_INIT_FAILED(err)) {
2540 pymain->err = err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002541 return -1;
2542 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002543 return 0;
2544}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00002545
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002546
Victor Stinnerca719ac2017-12-20 18:00:19 +01002547/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
2548 LC_CTYPE locale and Py_DecodeLocale().
2549
2550 Configuration:
2551
2552 * Command line arguments
2553 * Environment variables
2554 * Py_xxx global configuration variables
2555
Victor Stinner1dc6e392018-07-25 02:49:17 +02002556 _PyCmdline is a temporary structure used to prioritize these
Victor Stinnerca719ac2017-12-20 18:00:19 +01002557 variables. */
2558static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002559pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerca719ac2017-12-20 18:00:19 +01002560{
Victor Stinner31e99082017-12-20 23:41:38 +01002561 /* Force default allocator, since pymain_free() and pymain_clear_config()
2562 must use the same allocator than this function. */
2563 PyMemAllocatorEx old_alloc;
2564 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2565#ifdef Py_DEBUG
2566 PyMemAllocatorEx default_alloc;
2567 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
2568#endif
2569
Victor Stinner1dc6e392018-07-25 02:49:17 +02002570 _PyCmdline cmdline;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002571 memset(&cmdline, 0, sizeof(cmdline));
2572
Victor Stinner1dc6e392018-07-25 02:49:17 +02002573 int res = pymain_cmdline_impl(pymain, config, &cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002574
Victor Stinnerca719ac2017-12-20 18:00:19 +01002575 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01002576
2577#ifdef Py_DEBUG
2578 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
2579 PyMemAllocatorEx cur_alloc;
2580 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
2581 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
2582#endif
2583 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002584 return res;
2585}
2586
2587
Victor Stinner94540602017-12-16 04:54:22 +01002588static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02002589pymain_init(_PyMain *pymain, PyInterpreterState **interp_p)
Victor Stinner94540602017-12-16 04:54:22 +01002590{
Victor Stinner1dc6e392018-07-25 02:49:17 +02002591 /* 754 requires that FP exceptions run in "no stop" mode by default,
2592 * and until C vendors implement C99's ways to control FP exceptions,
2593 * Python requires non-stop mode. Alas, some platforms enable FP
2594 * exceptions by default. Here we disable them.
2595 */
2596#ifdef __FreeBSD__
2597 fedisableexcept(FE_OVERFLOW);
2598#endif
Victor Stinner94540602017-12-16 04:54:22 +01002599
Victor Stinner1dc6e392018-07-25 02:49:17 +02002600 _PyCoreConfig local_config = _PyCoreConfig_INIT;
2601 _PyCoreConfig *config = &local_config;
2602 config->install_signal_handlers = 1;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02002603
Victor Stinner1dc6e392018-07-25 02:49:17 +02002604 _PyCoreConfig_GetGlobalConfig(config);
2605
2606 int cmd_res = pymain_cmdline(pymain, config);
2607 if (cmd_res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01002608 _Py_FatalInitError(pymain->err);
2609 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02002610 if (cmd_res == 1) {
2611 pymain_clear_config(config);
2612 return 1;
Victor Stinner19760862017-12-20 01:41:59 +01002613 }
2614
Victor Stinner1dc6e392018-07-25 02:49:17 +02002615 _PyCoreConfig_SetGlobalConfig(config);
Victor Stinner53b7d4e2018-07-25 01:37:05 +02002616
Victor Stinner1dc6e392018-07-25 02:49:17 +02002617 pymain_init_stdio(pymain, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002618
Victor Stinner1dc6e392018-07-25 02:49:17 +02002619 PyInterpreterState *interp;
2620 pymain->err = _Py_InitializeCore(&interp, config);
2621 if (_Py_INIT_FAILED(pymain->err)) {
2622 _Py_FatalInitError(pymain->err);
2623 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02002624 *interp_p = interp;
Victor Stinner1dc6e392018-07-25 02:49:17 +02002625
2626 pymain_clear_config(config);
2627 config = &interp->core_config;
2628
2629 if (pymain_init_python_main(pymain, config, interp) < 0) {
2630 _Py_FatalInitError(pymain->err);
2631 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02002632 return 0;
2633}
2634
2635
2636static int
2637pymain_main(_PyMain *pymain)
2638{
Victor Stinnerd3b19192018-07-25 10:21:03 +02002639 PyInterpreterState *interp;
2640 int res = pymain_init(pymain, &interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +02002641 if (res != 1) {
Victor Stinnerd3b19192018-07-25 10:21:03 +02002642 if (pymain_run_python(pymain, interp) < 0) {
2643 _Py_FatalInitError(pymain->err);
2644 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02002645
2646 if (Py_FinalizeEx() < 0) {
2647 /* Value unlikely to be confused with a non-error exit status or
2648 other special meaning */
2649 pymain->status = 120;
Victor Stinnerfb47bca2018-07-20 17:34:23 +02002650 }
Victor Stinner19760862017-12-20 01:41:59 +01002651 }
2652
Victor Stinner94540602017-12-16 04:54:22 +01002653 pymain_free(pymain);
2654
Victor Stinner94540602017-12-16 04:54:22 +01002655 return pymain->status;
2656}
2657
2658
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002659int
2660Py_Main(int argc, wchar_t **argv)
2661{
2662 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002663 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002664 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002665 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002666
Victor Stinner94540602017-12-16 04:54:22 +01002667 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00002668}
2669
Victor Stinner94540602017-12-16 04:54:22 +01002670
2671int
2672_Py_UnixMain(int argc, char **argv)
2673{
2674 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01002675 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002676 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01002677 pymain.bytes_argv = argv;
2678
2679 return pymain_main(&pymain);
2680}
2681
2682
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002683/* this is gonna seem *real weird*, but if you put some other code between
2684 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
2685 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00002686
Guido van Rossum667d7041995-08-04 04:20:48 +00002687/* Make the *original* argc/argv available to other modules.
2688 This is rare, but it is needed by the secureware extension. */
2689
2690void
Martin v. Löwis790465f2008-04-05 20:41:37 +00002691Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00002692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 *argc = orig_argc;
2694 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00002695}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002696
2697#ifdef __cplusplus
2698}
2699#endif