blob: a16c3401914608e3ad02adc291f6513fc55d0098 [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 Stinnerd19d8d52018-07-24 13:55:48 +02001729 const char *env = config_get_env_var(config, "PYTHONTRACEMALLOC");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001730 if (env) {
1731 if (!pymain_str_to_int(env, &nframe)) {
1732 valid = (nframe >= 1);
1733 }
1734 else {
1735 valid = 0;
1736 }
1737 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001738 return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number "
1739 "of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001740 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001741 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001742 }
1743
Victor Stinner9cfc0022017-12-20 19:36:46 +01001744 const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001745 if (xoption) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001746 const wchar_t *sep = wcschr(xoption, L'=');
Victor Stinnera7368ac2017-11-15 18:11:45 -08001747 if (sep) {
1748 if (!pymain_wstr_to_int(sep + 1, &nframe)) {
1749 valid = (nframe >= 1);
1750 }
1751 else {
1752 valid = 0;
1753 }
1754 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001755 return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: "
1756 "invalid number of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001757 }
1758 }
1759 else {
1760 /* -X tracemalloc behaves as -X tracemalloc=1 */
1761 nframe = 1;
1762 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001763 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001764 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001765 return _Py_INIT_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001766}
1767
1768
Carl Meyerb193fa92018-06-15 22:40:56 -06001769static _PyInitError
1770pymain_init_pycache_prefix(_PyCoreConfig *config)
1771{
1772 wchar_t *env;
1773
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001774 int res = config_get_env_var_dup(config, &env,
1775 L"PYTHONPYCACHEPREFIX", "PYTHONPYCACHEPREFIX");
Carl Meyerb193fa92018-06-15 22:40:56 -06001776 if (res < 0) {
1777 return DECODE_LOCALE_ERR("PYTHONPYCACHEPREFIX", res);
1778 } else if (env) {
1779 config->pycache_prefix = env;
1780 }
1781
1782 const wchar_t *xoption = config_get_xoption(config, L"pycache_prefix");
1783 if (xoption) {
1784 const wchar_t *sep = wcschr(xoption, L'=');
1785 if (sep && wcslen(sep) > 1) {
1786 config->pycache_prefix = _PyMem_RawWcsdup(sep + 1);
1787 if (config->pycache_prefix == NULL) {
1788 return _Py_INIT_NO_MEMORY();
1789 }
1790 } else {
1791 // -X pycache_prefix= can cancel the env var
1792 config->pycache_prefix = NULL;
1793 }
1794 }
1795
1796 return _Py_INIT_OK();
1797}
1798
1799
Victor Stinnera7368ac2017-11-15 18:11:45 -08001800static void
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001801get_env_flag(_PyCoreConfig *config, int *flag, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001802{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001803 const char *var = config_get_env_var(config, name);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001804 if (!var) {
1805 return;
1806 }
1807 int value;
1808 if (pymain_str_to_int(var, &value) < 0 || value < 0) {
1809 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
1810 value = 1;
1811 }
1812 if (*flag < value) {
1813 *flag = value;
1814 }
1815}
1816
1817
1818static void
Victor Stinner1dc6e392018-07-25 02:49:17 +02001819cmdline_get_env_flags(_PyMain *pymain, _PyCoreConfig *config,
1820 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001821{
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001822 get_env_flag(config, &config->debug, "PYTHONDEBUG");
1823 get_env_flag(config, &config->verbose, "PYTHONVERBOSE");
1824 get_env_flag(config, &config->optimization_level, "PYTHONOPTIMIZE");
1825 get_env_flag(config, &config->inspect, "PYTHONINSPECT");
1826
1827 int dont_write_bytecode = 0;
1828 get_env_flag(config, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
1829 if (dont_write_bytecode) {
1830 config->write_bytecode = 0;
1831 }
1832
1833 int no_user_site_directory = 0;
1834 get_env_flag(config, &no_user_site_directory, "PYTHONNOUSERSITE");
1835 if (no_user_site_directory) {
1836 config->user_site_directory = 0;
1837 }
1838
1839 get_env_flag(config, &config->unbuffered_stdio, "PYTHONUNBUFFERED");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001840#ifdef MS_WINDOWS
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001841 get_env_flag(config, &config->legacy_windows_fs_encoding,
Victor Stinner9cfc0022017-12-20 19:36:46 +01001842 "PYTHONLEGACYWINDOWSFSENCODING");
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001843 get_env_flag(config, &config->legacy_windows_stdio,
Victor Stinner9cfc0022017-12-20 19:36:46 +01001844 "PYTHONLEGACYWINDOWSSTDIO");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001845#endif
1846}
1847
1848
Victor Stinner46972b72017-11-24 22:55:40 +01001849static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001850config_init_home(_PyCoreConfig *config)
Victor Stinner1f151112017-11-23 10:43:14 +01001851{
1852 wchar_t *home;
1853
Victor Stinner31a83932017-12-04 13:39:15 +01001854 /* If Py_SetPythonHome() was called, use its value */
1855 home = _Py_path_config.home;
Victor Stinner1f151112017-11-23 10:43:14 +01001856 if (home) {
Victor Stinner46972b72017-11-24 22:55:40 +01001857 config->home = _PyMem_RawWcsdup(home);
1858 if (config->home == NULL) {
1859 return _Py_INIT_NO_MEMORY();
Victor Stinner1f151112017-11-23 10:43:14 +01001860 }
Victor Stinner46972b72017-11-24 22:55:40 +01001861 return _Py_INIT_OK();
Victor Stinner1f151112017-11-23 10:43:14 +01001862 }
1863
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001864 int res = config_get_env_var_dup(config, &home, L"PYTHONHOME", "PYTHONHOME");
Victor Stinner1f151112017-11-23 10:43:14 +01001865 if (res < 0) {
Victor Stinner46972b72017-11-24 22:55:40 +01001866 return DECODE_LOCALE_ERR("PYTHONHOME", res);
Victor Stinner1f151112017-11-23 10:43:14 +01001867 }
Victor Stinner46972b72017-11-24 22:55:40 +01001868 config->home = home;
1869 return _Py_INIT_OK();
Victor Stinnerd4341102017-11-23 00:12:09 +01001870}
1871
1872
Victor Stinner358e5e12017-12-15 00:51:22 +01001873static _PyInitError
1874config_init_hash_seed(_PyCoreConfig *config)
1875{
1876 if (config->use_hash_seed < 0) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001877 const char *seed_text = config_get_env_var(config, "PYTHONHASHSEED");
Victor Stinner358e5e12017-12-15 00:51:22 +01001878 int use_hash_seed;
1879 unsigned long hash_seed;
1880 if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
1881 return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
1882 "or an integer in range [0; 4294967295]");
1883 }
1884 config->use_hash_seed = use_hash_seed;
1885 config->hash_seed = hash_seed;
1886 }
1887 return _Py_INIT_OK();
1888}
1889
1890
Victor Stinner9cfc0022017-12-20 19:36:46 +01001891static _PyInitError
1892config_init_utf8_mode(_PyCoreConfig *config)
Victor Stinner91106cd2017-12-13 12:29:09 +01001893{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001894 /* The option was already set in config.Py_UTF8Mode,
1895 by Py_LegacyWindowsFSEncodingFlag or PYTHONLEGACYWINDOWSFSENCODING. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001896 if (config->utf8_mode >= 0) {
1897 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001898 }
Victor Stinner91106cd2017-12-13 12:29:09 +01001899
Victor Stinner9cfc0022017-12-20 19:36:46 +01001900 const wchar_t *xopt = config_get_xoption(config, L"utf8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001901 if (xopt) {
1902 wchar_t *sep = wcschr(xopt, L'=');
1903 if (sep) {
1904 xopt = sep + 1;
1905 if (wcscmp(xopt, L"1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001906 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001907 }
1908 else if (wcscmp(xopt, L"0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001909 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001910 }
1911 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001912 return _Py_INIT_USER_ERR("invalid -X utf8 option value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001913 }
1914 }
1915 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001916 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001917 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001918 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001919 }
1920
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001921 const char *opt = config_get_env_var(config, "PYTHONUTF8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001922 if (opt) {
1923 if (strcmp(opt, "1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001924 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001925 }
1926 else if (strcmp(opt, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001927 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001928 }
1929 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001930 return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
1931 "variable value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001932 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001933 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001934 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001935
1936 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001937}
Victor Stinner46972b72017-11-24 22:55:40 +01001938
1939
Victor Stinner9cfc0022017-12-20 19:36:46 +01001940static _PyInitError
1941config_read_env_vars(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001942{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001943 config->allocator = config_get_env_var(config, "PYTHONMALLOC");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001944
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001945 if (config_get_env_var(config, "PYTHONDUMPREFS")) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001946 config->dump_refs = 1;
1947 }
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001948 if (config_get_env_var(config, "PYTHONMALLOCSTATS")) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001949 config->malloc_stats = 1;
Victor Stinner31a83932017-12-04 13:39:15 +01001950 }
1951
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001952 const char *env = config_get_env_var(config, "PYTHONCOERCECLOCALE");
Victor Stinner94540602017-12-16 04:54:22 +01001953 if (env) {
1954 if (strcmp(env, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001955 config->coerce_c_locale = 0;
Victor Stinner94540602017-12-16 04:54:22 +01001956 }
1957 else if (strcmp(env, "warn") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001958 config->coerce_c_locale_warn = 1;
Victor Stinner94540602017-12-16 04:54:22 +01001959 }
1960 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001961 config->coerce_c_locale = 1;
Victor Stinner94540602017-12-16 04:54:22 +01001962 }
1963 }
1964
Victor Stinner9cfc0022017-12-20 19:36:46 +01001965 wchar_t *path;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001966 int res = config_get_env_var_dup(config, &path, L"PYTHONPATH", "PYTHONPATH");
Victor Stinner9cfc0022017-12-20 19:36:46 +01001967 if (res < 0) {
Carl Meyer48575432018-05-19 16:48:22 -06001968 return DECODE_LOCALE_ERR("PYTHONPATH", res);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001969 }
1970 config->module_search_path_env = path;
1971
1972 _PyInitError err = config_init_hash_seed(config);
1973 if (_Py_INIT_FAILED(err)) {
1974 return err;
1975 }
1976
1977 return _Py_INIT_OK();
1978}
1979
1980
1981static _PyInitError
1982config_read_complex_options(_PyCoreConfig *config)
1983{
1984 /* More complex options configured by env var and -X option */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001985 if (config_get_env_var(config, "PYTHONFAULTHANDLER")
Victor Stinner9cfc0022017-12-20 19:36:46 +01001986 || config_get_xoption(config, L"faulthandler")) {
1987 config->faulthandler = 1;
1988 }
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001989 if (config_get_env_var(config, "PYTHONPROFILEIMPORTTIME")
Victor Stinner9cfc0022017-12-20 19:36:46 +01001990 || config_get_xoption(config, L"importtime")) {
1991 config->import_time = 1;
1992 }
1993 if (config_get_xoption(config, L"dev" ) ||
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001994 config_get_env_var(config, "PYTHONDEVMODE"))
Victor Stinner9cfc0022017-12-20 19:36:46 +01001995 {
1996 config->dev_mode = 1;
1997 config->faulthandler = 1;
1998 config->allocator = "debug";
1999 }
2000
2001 _PyInitError err = pymain_init_tracemalloc(config);
2002 if (_Py_INIT_FAILED(err)) {
2003 return err;
2004 }
Carl Meyerb193fa92018-06-15 22:40:56 -06002005
2006 err = pymain_init_pycache_prefix(config);
2007 if (_Py_INIT_FAILED(err)) {
2008 return err;
2009 }
2010
Victor Stinner9cfc0022017-12-20 19:36:46 +01002011 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002012}
2013
2014
Victor Stinnera7368ac2017-11-15 18:11:45 -08002015/* Parse command line options and environment variables.
2016 This code must not use Python runtime apart PyMem_Raw memory allocator.
2017
2018 Return 0 on success.
2019 Return 1 if Python is done and must exit.
2020 Set pymain->err and return -1 on error. */
2021static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002022pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
2023 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002024{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002025 _PyInitError err;
2026
Victor Stinner1dc6e392018-07-25 02:49:17 +02002027 int res = pymain_parse_cmdline(pymain, config, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01002028 if (res != 0) {
2029 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002030 }
2031
Victor Stinner9cfc0022017-12-20 19:36:46 +01002032 /* Get environment variables */
Victor Stinner1dc6e392018-07-25 02:49:17 +02002033 cmdline_get_env_flags(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002034
Victor Stinner1dc6e392018-07-25 02:49:17 +02002035 err = cmdline_init_env_warnoptions(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002036 if (_Py_INIT_FAILED(err)) {
2037 pymain->err = err;
2038 return -1;
2039 }
2040
2041#ifdef MS_WINDOWS
Victor Stinner53b7d4e2018-07-25 01:37:05 +02002042 if (config->legacy_windows_fs_encoding) {
2043 config->utf8_mode = 0;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002044 }
2045#endif
2046
Victor Stinner1dc6e392018-07-25 02:49:17 +02002047 if (pymain_init_core_argv(pymain, config, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01002048 return -1;
2049 }
2050
Victor Stinner53b7d4e2018-07-25 01:37:05 +02002051 err = _PyCoreConfig_Read(config);
Victor Stinner31a83932017-12-04 13:39:15 +01002052 if (_Py_INIT_FAILED(err)) {
2053 pymain->err = err;
2054 return -1;
2055 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002056 return 0;
2057}
2058
2059
Victor Stinner19760862017-12-20 01:41:59 +01002060/* Read the configuration, but initialize also the LC_CTYPE locale:
2061 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
Victor Stinnera7368ac2017-11-15 18:11:45 -08002062static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002063pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
2064 _PyCmdline *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002065{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002066 _PyCoreConfig save_config = _PyCoreConfig_INIT;
2067 char *oldloc = NULL;
Victor Stinner94540602017-12-16 04:54:22 +01002068 int res = -1;
2069
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002070 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
Victor Stinner94540602017-12-16 04:54:22 +01002071 if (oldloc == NULL) {
2072 pymain->err = _Py_INIT_NO_MEMORY();
2073 goto done;
2074 }
2075
2076 /* Reconfigure the locale to the default for this process */
2077 _Py_SetLocaleFromEnv(LC_ALL);
2078
2079 int locale_coerced = 0;
2080 int loops = 0;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002081
2082 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
2083 pymain->err = _Py_INIT_NO_MEMORY();
2084 goto done;
2085 }
Victor Stinner94540602017-12-16 04:54:22 +01002086
2087 while (1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002088 int utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002089 int encoding_changed = 0;
2090
2091 /* Watchdog to prevent an infinite loop */
2092 loops++;
2093 if (loops == 3) {
2094 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
2095 "reading the configuration");
2096 goto done;
2097 }
2098
Victor Stinner1dc6e392018-07-25 02:49:17 +02002099 if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01002100 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01002101 }
2102
Victor Stinner1dc6e392018-07-25 02:49:17 +02002103 int conf_res = pymain_read_conf_impl(pymain, config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002104 if (conf_res != 0) {
2105 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01002106 goto done;
2107 }
2108
2109 /* The legacy C locale assumes ASCII as the default text encoding, which
2110 * causes problems not only for the CPython runtime, but also other
2111 * components like GNU readline.
2112 *
2113 * Accordingly, when the CLI detects it, it attempts to coerce it to a
2114 * more capable UTF-8 based alternative.
2115 *
2116 * See the documentation of the PYTHONCOERCECLOCALE setting for more
2117 * details.
2118 */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002119 if (config->coerce_c_locale == 1 && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01002120 locale_coerced = 1;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002121 _Py_CoerceLegacyLocale(config);
Victor Stinner94540602017-12-16 04:54:22 +01002122 encoding_changed = 1;
2123 }
2124
2125 if (utf8_mode == -1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002126 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01002127 /* UTF-8 Mode enabled */
2128 encoding_changed = 1;
2129 }
2130 }
2131 else {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002132 if (config->utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01002133 encoding_changed = 1;
2134 }
2135 }
2136
2137 if (!encoding_changed) {
2138 break;
2139 }
2140
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002141 /* Reset the configuration before reading the configuration,
2142 except UTF-8 Mode. */
2143 int new_utf8_mode = config->utf8_mode;
2144 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
2145 pymain->err = _Py_INIT_NO_MEMORY();
2146 goto done;
2147 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002148 pymain_clear_cmdline(pymain, cmdline);
Victor Stinner6c5a4b32018-06-16 00:06:28 +02002149 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinner1dc6e392018-07-25 02:49:17 +02002150 config->utf8_mode = new_utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002151
2152 /* The encoding changed: read again the configuration
2153 with the new encoding */
2154 }
2155 res = 0;
2156
2157done:
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002158 _PyCoreConfig_Clear(&save_config);
Victor Stinner94540602017-12-16 04:54:22 +01002159 if (oldloc != NULL) {
2160 setlocale(LC_ALL, oldloc);
2161 PyMem_RawFree(oldloc);
2162 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002163
Victor Stinnera7368ac2017-11-15 18:11:45 -08002164 return res;
2165}
2166
Victor Stinner91106cd2017-12-13 12:29:09 +01002167
Victor Stinner9cfc0022017-12-20 19:36:46 +01002168static void
2169config_init_locale(_PyCoreConfig *config)
2170{
2171 if (config->utf8_mode >= 0 && config->coerce_c_locale >= 0) {
2172 return;
2173 }
2174
2175 if (_Py_LegacyLocaleDetected()) {
2176 /* POSIX locale: enable C locale coercion and UTF-8 Mode */
2177 if (config->utf8_mode < 0) {
2178 config->utf8_mode = 1;
2179 }
2180 if (config->coerce_c_locale < 0) {
2181 config->coerce_c_locale = 1;
2182 }
2183 return;
2184 }
2185
2186 /* By default, C locale coercion and UTF-8 Mode are disabled */
2187 if (config->coerce_c_locale < 0) {
2188 config->coerce_c_locale = 0;
2189 }
2190 if (config->utf8_mode < 0) {
2191 config->utf8_mode = 0;
2192 }
2193}
2194
2195
Victor Stinnerda273412017-12-15 01:46:02 +01002196/* Read configuration settings from standard locations
2197 *
2198 * This function doesn't make any changes to the interpreter state - it
2199 * merely populates any missing configuration settings. This allows an
2200 * embedding application to completely override a config option by
2201 * setting it before calling this function, or else modify the default
2202 * setting before passing the fully populated config to Py_EndInitialization.
2203 *
2204 * More advanced selective initialization tricks are possible by calling
2205 * this function multiple times with various preconfigured settings.
2206 */
2207
2208_PyInitError
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002209_PyCoreConfig_Read(_PyCoreConfig *config)
Victor Stinnerda273412017-12-15 01:46:02 +01002210{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002211 _PyInitError err;
2212
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002213 _PyCoreConfig_GetGlobalConfig(config);
2214
Victor Stinner9cfc0022017-12-20 19:36:46 +01002215 err = config_read_env_vars(config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002216 if (_Py_INIT_FAILED(err)) {
2217 return err;
2218 }
2219
Victor Stinner9cfc0022017-12-20 19:36:46 +01002220 /* -X options */
2221 if (config_get_xoption(config, L"showrefcount")) {
2222 config->show_ref_count = 1;
2223 }
2224 if (config_get_xoption(config, L"showalloccount")) {
2225 config->show_alloc_count = 1;
2226 }
2227
2228 err = config_read_complex_options(config);
2229 if (_Py_INIT_FAILED(err)) {
2230 return err;
2231 }
2232
2233 err = config_init_utf8_mode(config);
2234 if (_Py_INIT_FAILED(err)) {
2235 return err;
2236 }
2237
2238 err = config_init_home(config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002239 if (_Py_INIT_FAILED(err)) {
2240 return err;
2241 }
2242
Victor Stinner1dc6e392018-07-25 02:49:17 +02002243 if (config->program_name == NULL) {
2244 err = config_init_program_name(config);
2245 if (_Py_INIT_FAILED(err)) {
2246 return err;
2247 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002248 }
2249
Victor Stinner9cfc0022017-12-20 19:36:46 +01002250 config_init_locale(config);
Victor Stinnerda273412017-12-15 01:46:02 +01002251
Victor Stinner9cfc0022017-12-20 19:36:46 +01002252 /* Signal handlers are installed by default */
2253 if (config->install_signal_handlers < 0) {
2254 config->install_signal_handlers = 1;
Victor Stinner94540602017-12-16 04:54:22 +01002255 }
2256
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002257 if (config->_install_importlib) {
2258 err = _PyCoreConfig_InitPathConfig(config);
Victor Stinner8ded5b82018-01-24 17:03:28 +01002259 if (_Py_INIT_FAILED(err)) {
2260 return err;
2261 }
2262 }
Victor Stinnerda273412017-12-15 01:46:02 +01002263 return _Py_INIT_OK();
2264}
2265
2266
2267void
Victor Stinnerda273412017-12-15 01:46:02 +01002268_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
2269{
2270 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002271 Py_CLEAR(config->executable);
2272 Py_CLEAR(config->prefix);
2273 Py_CLEAR(config->base_prefix);
2274 Py_CLEAR(config->exec_prefix);
2275 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002276 Py_CLEAR(config->warnoptions);
2277 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002278 Py_CLEAR(config->module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06002279 Py_CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002280}
2281
2282
2283static PyObject*
2284config_copy_attr(PyObject *obj)
2285{
2286 if (PyUnicode_Check(obj)) {
2287 Py_INCREF(obj);
2288 return obj;
2289 }
2290 else if (PyList_Check(obj)) {
2291 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
2292 }
2293 else if (PyDict_Check(obj)) {
2294 /* The dict type is used for xoptions. Make the assumption that keys
2295 and values are immutables */
2296 return PyDict_Copy(obj);
2297 }
2298 else {
2299 PyErr_Format(PyExc_TypeError,
2300 "cannot copy config attribute of type %.200s",
2301 Py_TYPE(obj)->tp_name);
2302 return NULL;
2303 }
2304}
2305
2306
2307int
2308_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
2309 const _PyMainInterpreterConfig *config2)
2310{
2311 _PyMainInterpreterConfig_Clear(config);
2312
2313#define COPY_ATTR(ATTR) \
2314 do { \
2315 if (config2->ATTR != NULL) { \
2316 config->ATTR = config_copy_attr(config2->ATTR); \
2317 if (config->ATTR == NULL) { \
2318 return -1; \
2319 } \
2320 } \
2321 } while (0)
2322
2323 COPY_ATTR(argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002324 COPY_ATTR(executable);
2325 COPY_ATTR(prefix);
2326 COPY_ATTR(base_prefix);
2327 COPY_ATTR(exec_prefix);
2328 COPY_ATTR(base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002329 COPY_ATTR(warnoptions);
2330 COPY_ATTR(xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002331 COPY_ATTR(module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06002332 COPY_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002333#undef COPY_ATTR
2334 return 0;
2335}
2336
2337
2338
2339
Victor Stinner41264f12017-12-15 02:05:29 +01002340_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01002341_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
2342 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002343{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002344 if (main_config->install_signal_handlers < 0) {
2345 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002346 }
2347
Victor Stinner9cfc0022017-12-20 19:36:46 +01002348 if (main_config->xoptions == NULL) {
2349 main_config->xoptions = config_create_xoptions_dict(config);
2350 if (main_config->xoptions == NULL) {
2351 return _Py_INIT_NO_MEMORY();
2352 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002353 }
2354
Victor Stinner8ded5b82018-01-24 17:03:28 +01002355#define COPY_WSTR(ATTR) \
2356 do { \
Victor Stinner1dc6e392018-07-25 02:49:17 +02002357 if (main_config->ATTR == NULL && config->ATTR != NULL) { \
Victor Stinner8ded5b82018-01-24 17:03:28 +01002358 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
2359 if (main_config->ATTR == NULL) { \
2360 return _Py_INIT_NO_MEMORY(); \
2361 } \
2362 } \
2363 } while (0)
2364#define COPY_WSTRLIST(ATTR, LEN, LIST) \
2365 do { \
2366 if (ATTR == NULL) { \
2367 ATTR = wstrlist_as_pylist(LEN, LIST); \
2368 if (ATTR == NULL) { \
2369 return _Py_INIT_NO_MEMORY(); \
2370 } \
2371 } \
2372 } while (0)
2373
2374 COPY_WSTRLIST(main_config->warnoptions,
2375 config->nwarnoption, config->warnoptions);
2376 if (config->argc >= 0) {
2377 COPY_WSTRLIST(main_config->argv,
2378 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002379 }
2380
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002381 if (config->_install_importlib) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01002382 COPY_WSTR(executable);
2383 COPY_WSTR(prefix);
2384 COPY_WSTR(base_prefix);
2385 COPY_WSTR(exec_prefix);
2386 COPY_WSTR(base_exec_prefix);
2387
2388 COPY_WSTRLIST(main_config->module_search_path,
2389 config->nmodule_search_path, config->module_search_paths);
Carl Meyerb193fa92018-06-15 22:40:56 -06002390
2391 if (config->pycache_prefix != NULL) {
2392 COPY_WSTR(pycache_prefix);
2393 } else {
2394 main_config->pycache_prefix = NULL;
2395 }
2396
Victor Stinner9cfc0022017-12-20 19:36:46 +01002397 }
Victor Stinner41264f12017-12-15 02:05:29 +01002398
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002399 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01002400#undef COPY_WSTR
2401#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002402}
2403
2404
2405static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002406pymain_init_python_main(_PyMain *pymain, _PyCoreConfig *config,
2407 PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002408{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002409 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002410
Victor Stinner9cfc0022017-12-20 19:36:46 +01002411 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
Victor Stinner1dc6e392018-07-25 02:49:17 +02002412 err = _PyMainInterpreterConfig_Read(&main_config, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002413 if (!_Py_INIT_FAILED(err)) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02002414 err = _Py_InitializeMainInterpreter(interp, &main_config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002415 }
2416 _PyMainInterpreterConfig_Clear(&main_config);
2417
2418 if (_Py_INIT_FAILED(err)) {
2419 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002420 return -1;
2421 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002422 return 0;
2423}
Victor Stinnera7368ac2017-11-15 18:11:45 -08002424
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002425
2426static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02002427pymain_run_python(_PyMain *pymain, PyInterpreterState *interp)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002428{
Victor Stinnerd3b19192018-07-25 10:21:03 +02002429 int res = 0;
2430 _PyCoreConfig *config = &interp->core_config;
2431
2432 PyObject *main_importer_path = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002433 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002434 /* If filename is a package (ex: directory or ZIP file) which contains
2435 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +02002436 prepended to sys.path.
2437
2438 Otherwise, main_importer_path is set to NULL. */
2439 main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01002440 }
2441
Victor Stinnerd3b19192018-07-25 10:21:03 +02002442 if (main_importer_path != NULL) {
2443 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
2444 pymain->status = 1;
2445 goto done;
2446 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01002447 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02002448 else if (!config->isolated) {
2449 PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc,
2450 config->argv);
2451 if (path0 == NULL) {
2452 pymain->err = _Py_INIT_NO_MEMORY();
2453 res = -1;
2454 goto done;
2455 }
Victor Stinner19760862017-12-20 01:41:59 +01002456
Victor Stinnerd3b19192018-07-25 10:21:03 +02002457 if (pymain_sys_path_add_path0(interp, path0) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01002458 Py_DECREF(path0);
Victor Stinnerd3b19192018-07-25 10:21:03 +02002459 pymain->status = 1;
2460 goto done;
Victor Stinner19760862017-12-20 01:41:59 +01002461 }
2462 Py_DECREF(path0);
2463 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002464
Victor Stinner19760862017-12-20 01:41:59 +01002465 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08002466
2467 pymain_header(pymain);
2468 pymain_import_readline(pymain);
2469
Victor Stinnerca719ac2017-12-20 18:00:19 +01002470 if (pymain->command) {
2471 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002472 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002473 else if (pymain->module) {
2474 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002475 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02002476 else if (main_importer_path != NULL) {
2477 int sts = pymain_run_module(L"__main__", 0);
2478 pymain->status = (sts != 0);
2479 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002480 else {
Victor Stinner1dc6e392018-07-25 02:49:17 +02002481 pymain_run_filename(pymain, config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002482 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002483
Victor Stinner1dc6e392018-07-25 02:49:17 +02002484 pymain_repl(pymain, config, &cf);
Victor Stinnerd3b19192018-07-25 10:21:03 +02002485
2486done:
2487 Py_XDECREF(main_importer_path);
2488 return res;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002489}
2490
Victor Stinnera7368ac2017-11-15 18:11:45 -08002491
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002492static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002493pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
2494 _PyCmdline *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002495{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002496 pymain->err = _PyRuntime_Initialize();
2497 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002498 return -1;
2499 }
2500
Victor Stinner1dc6e392018-07-25 02:49:17 +02002501 int res = pymain_read_conf(pymain, config, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002502 if (res < 0) {
2503 return -1;
2504 }
2505 if (res > 0) {
2506 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01002507 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002508 }
2509
Victor Stinner94540602017-12-16 04:54:22 +01002510 if (cmdline->print_help) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02002511 pymain_usage(0, config->program);
Victor Stinner19760862017-12-20 01:41:59 +01002512 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002513 }
2514
2515 if (cmdline->print_version) {
2516 printf("Python %s\n",
2517 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01002518 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002519 }
2520
Victor Stinnerc4bca952017-12-19 23:48:17 +01002521 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002522 orig_argv = copy_wstrlist(pymain->argc, cmdline->argv);
2523 if (orig_argv == NULL) {
2524 pymain->err = _Py_INIT_NO_MEMORY();
2525 return -1;
2526 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01002527 orig_argc = pymain->argc;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002528
Victor Stinner1dc6e392018-07-25 02:49:17 +02002529 _PyInitError err = config_init_warnoptions(config, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002530 if (_Py_INIT_FAILED(err)) {
2531 pymain->err = err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002532 return -1;
2533 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002534 return 0;
2535}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00002536
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002537
Victor Stinnerca719ac2017-12-20 18:00:19 +01002538/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
2539 LC_CTYPE locale and Py_DecodeLocale().
2540
2541 Configuration:
2542
2543 * Command line arguments
2544 * Environment variables
2545 * Py_xxx global configuration variables
2546
Victor Stinner1dc6e392018-07-25 02:49:17 +02002547 _PyCmdline is a temporary structure used to prioritize these
Victor Stinnerca719ac2017-12-20 18:00:19 +01002548 variables. */
2549static int
Victor Stinner1dc6e392018-07-25 02:49:17 +02002550pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
Victor Stinnerca719ac2017-12-20 18:00:19 +01002551{
Victor Stinner31e99082017-12-20 23:41:38 +01002552 /* Force default allocator, since pymain_free() and pymain_clear_config()
2553 must use the same allocator than this function. */
2554 PyMemAllocatorEx old_alloc;
2555 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2556#ifdef Py_DEBUG
2557 PyMemAllocatorEx default_alloc;
2558 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
2559#endif
2560
Victor Stinner1dc6e392018-07-25 02:49:17 +02002561 _PyCmdline cmdline;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002562 memset(&cmdline, 0, sizeof(cmdline));
2563
Victor Stinner1dc6e392018-07-25 02:49:17 +02002564 int res = pymain_cmdline_impl(pymain, config, &cmdline);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002565
Victor Stinnerca719ac2017-12-20 18:00:19 +01002566 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01002567
2568#ifdef Py_DEBUG
2569 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
2570 PyMemAllocatorEx cur_alloc;
2571 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
2572 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
2573#endif
2574 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002575 return res;
2576}
2577
2578
Victor Stinner94540602017-12-16 04:54:22 +01002579static int
Victor Stinnerd3b19192018-07-25 10:21:03 +02002580pymain_init(_PyMain *pymain, PyInterpreterState **interp_p)
Victor Stinner94540602017-12-16 04:54:22 +01002581{
Victor Stinner1dc6e392018-07-25 02:49:17 +02002582 /* 754 requires that FP exceptions run in "no stop" mode by default,
2583 * and until C vendors implement C99's ways to control FP exceptions,
2584 * Python requires non-stop mode. Alas, some platforms enable FP
2585 * exceptions by default. Here we disable them.
2586 */
2587#ifdef __FreeBSD__
2588 fedisableexcept(FE_OVERFLOW);
2589#endif
Victor Stinner94540602017-12-16 04:54:22 +01002590
Victor Stinner1dc6e392018-07-25 02:49:17 +02002591 _PyCoreConfig local_config = _PyCoreConfig_INIT;
2592 _PyCoreConfig *config = &local_config;
2593 config->install_signal_handlers = 1;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02002594
Victor Stinner1dc6e392018-07-25 02:49:17 +02002595 _PyCoreConfig_GetGlobalConfig(config);
2596
2597 int cmd_res = pymain_cmdline(pymain, config);
2598 if (cmd_res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01002599 _Py_FatalInitError(pymain->err);
2600 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02002601 if (cmd_res == 1) {
2602 pymain_clear_config(config);
2603 return 1;
Victor Stinner19760862017-12-20 01:41:59 +01002604 }
2605
Victor Stinner1dc6e392018-07-25 02:49:17 +02002606 _PyCoreConfig_SetGlobalConfig(config);
Victor Stinner53b7d4e2018-07-25 01:37:05 +02002607
Victor Stinner1dc6e392018-07-25 02:49:17 +02002608 pymain_init_stdio(pymain, config);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002609
Victor Stinner1dc6e392018-07-25 02:49:17 +02002610 PyInterpreterState *interp;
2611 pymain->err = _Py_InitializeCore(&interp, config);
2612 if (_Py_INIT_FAILED(pymain->err)) {
2613 _Py_FatalInitError(pymain->err);
2614 }
Victor Stinnerd3b19192018-07-25 10:21:03 +02002615 *interp_p = interp;
Victor Stinner1dc6e392018-07-25 02:49:17 +02002616
2617 pymain_clear_config(config);
2618 config = &interp->core_config;
2619
2620 if (pymain_init_python_main(pymain, config, interp) < 0) {
2621 _Py_FatalInitError(pymain->err);
2622 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02002623 return 0;
2624}
2625
2626
2627static int
2628pymain_main(_PyMain *pymain)
2629{
Victor Stinnerd3b19192018-07-25 10:21:03 +02002630 PyInterpreterState *interp;
2631 int res = pymain_init(pymain, &interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +02002632 if (res != 1) {
Victor Stinnerd3b19192018-07-25 10:21:03 +02002633 if (pymain_run_python(pymain, interp) < 0) {
2634 _Py_FatalInitError(pymain->err);
2635 }
Victor Stinner1dc6e392018-07-25 02:49:17 +02002636
2637 if (Py_FinalizeEx() < 0) {
2638 /* Value unlikely to be confused with a non-error exit status or
2639 other special meaning */
2640 pymain->status = 120;
Victor Stinnerfb47bca2018-07-20 17:34:23 +02002641 }
Victor Stinner19760862017-12-20 01:41:59 +01002642 }
2643
Victor Stinner94540602017-12-16 04:54:22 +01002644 pymain_free(pymain);
2645
Victor Stinner94540602017-12-16 04:54:22 +01002646 return pymain->status;
2647}
2648
2649
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002650int
2651Py_Main(int argc, wchar_t **argv)
2652{
2653 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002654 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002655 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002656 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002657
Victor Stinner94540602017-12-16 04:54:22 +01002658 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00002659}
2660
Victor Stinner94540602017-12-16 04:54:22 +01002661
2662int
2663_Py_UnixMain(int argc, char **argv)
2664{
2665 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01002666 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002667 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01002668 pymain.bytes_argv = argv;
2669
2670 return pymain_main(&pymain);
2671}
2672
2673
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002674/* this is gonna seem *real weird*, but if you put some other code between
2675 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
2676 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00002677
Guido van Rossum667d7041995-08-04 04:20:48 +00002678/* Make the *original* argc/argv available to other modules.
2679 This is rare, but it is needed by the secureware extension. */
2680
2681void
Martin v. Löwis790465f2008-04-05 20:41:37 +00002682Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00002683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 *argc = orig_argc;
2685 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00002686}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002687
2688#ifdef __cplusplus
2689}
2690#endif