blob: c9fd300e0344949f0b97764128324d101512205e [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 Stinner34be8072016-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 Stinnerca719ac2017-12-20 18:00:19 +0100418 int nwarnoption; /* Number of -W options */
419 wchar_t **warnoptions; /* -W options */
420 int nenv_warnoption; /* Number of PYTHONWARNINGS options */
421 wchar_t **env_warnoptions; /* PYTHONWARNINGS options */
Eric Snow6b4be192017-05-22 21:36:03 -0700422 int print_help; /* -h, -? options */
423 int print_version; /* -V option */
Victor Stinner6bf992a2017-12-06 17:26:10 +0100424 int bytes_warning; /* Py_BytesWarningFlag, -b */
425 int debug; /* Py_DebugFlag, -b, PYTHONDEBUG */
426 int inspect; /* Py_InspectFlag, -i, PYTHONINSPECT */
427 int interactive; /* Py_InteractiveFlag, -i */
Victor Stinner6bf992a2017-12-06 17:26:10 +0100428 int optimization_level; /* Py_OptimizeFlag, -O, PYTHONOPTIMIZE */
429 int dont_write_bytecode; /* Py_DontWriteBytecodeFlag, -B, PYTHONDONTWRITEBYTECODE */
430 int no_user_site_directory; /* Py_NoUserSiteDirectory, -I, -s, PYTHONNOUSERSITE */
Victor Stinner6bf992a2017-12-06 17:26:10 +0100431 int use_unbuffered_io; /* Py_UnbufferedStdioFlag, -u, PYTHONUNBUFFERED */
432 int verbosity; /* Py_VerboseFlag, -v, PYTHONVERBOSE */
433 int quiet_flag; /* Py_QuietFlag, -q */
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800434 const char *check_hash_pycs_mode; /* --check-hash-based-pycs */
Victor Stinner6bf992a2017-12-06 17:26:10 +0100435#ifdef MS_WINDOWS
436 int legacy_windows_fs_encoding; /* Py_LegacyWindowsFSEncodingFlag,
437 PYTHONLEGACYWINDOWSFSENCODING */
438 int legacy_windows_stdio; /* Py_LegacyWindowsStdioFlag,
439 PYTHONLEGACYWINDOWSSTDIO */
440#endif
Eric Snow6b4be192017-05-22 21:36:03 -0700441} _Py_CommandLineDetails;
442
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800443/* Structure used by Py_Main() to pass data to subfunctions */
444typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100445 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800446 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100447 int use_bytes_argv;
448 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100449 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100450
451 /* Exit status or "exit code": result of pymain_main() */
452 int status;
453 /* Error message if a function failed */
454 _PyInitError err;
455
Victor Stinner19760862017-12-20 01:41:59 +0100456 /* non-zero is stdin is a TTY or if -i option is used */
457 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100458 int skip_first_line; /* -x option */
459 wchar_t *filename; /* Trailing arg without -c or -m */
460 wchar_t *command; /* -c argument */
461 wchar_t *module; /* -m argument */
Victor Stinner19760862017-12-20 01:41:59 +0100462
Victor Stinner9cfc0022017-12-20 19:36:46 +0100463 _PyCoreConfig config;
Victor Stinner19760862017-12-20 01:41:59 +0100464
465 PyObject *main_importer_path;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800466} _PyMain;
467
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800468#define _PyMain_INIT \
Victor Stinner9cfc0022017-12-20 19:36:46 +0100469 {.config = _PyCoreConfig_INIT, \
Victor Stinnerd5dda982017-12-13 17:31:16 +0100470 .err = _Py_INIT_OK()}
471/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800472
473
Victor Stinner19760862017-12-20 01:41:59 +0100474/* Non-zero if filename, command (-c) or module (-m) is set
475 on the command line */
476#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100477 (pymain->command != NULL || pymain->filename != NULL \
478 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100479
480
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200481static void
482clear_wstrlist(int len, wchar_t **list)
483{
484 for (int i=0; i < len; i++) {
485 PyMem_RawFree(list[i]);
486 }
487 PyMem_RawFree(list);
488}
489
490
491static wchar_t**
492copy_wstrlist(int len, wchar_t **list)
493{
494 assert((len > 0 && list != NULL) || len == 0);
495 size_t size = len * sizeof(list[0]);
496 wchar_t **list_copy = PyMem_RawMalloc(size);
497 for (int i=0; i < len; i++) {
498 wchar_t* arg = _PyMem_RawWcsdup(list[i]);
499 if (arg == NULL) {
500 clear_wstrlist(i, list);
501 return NULL;
502 }
503 list_copy[i] = arg;
504 }
505 return list_copy;
506}
507
508
Victor Stinnerca719ac2017-12-20 18:00:19 +0100509static wchar_t*
510pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800511{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100512 wchar_t *str2 = _PyMem_RawWcsdup(str);
513 if (str2 == NULL) {
514 pymain->err = _Py_INIT_NO_MEMORY();
515 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800516 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100517 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800518}
519
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100520
Victor Stinnerc4bca952017-12-19 23:48:17 +0100521static int
Victor Stinnerca719ac2017-12-20 18:00:19 +0100522pymain_init_cmdline_argv(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100523{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100524 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100525
Victor Stinnerca719ac2017-12-20 18:00:19 +0100526 if (pymain->use_bytes_argv) {
527 /* +1 for a the NULL terminator */
528 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
529 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
530 if (argv == NULL) {
531 pymain->err = _Py_INIT_NO_MEMORY();
532 return -1;
533 }
534
535 for (int i = 0; i < pymain->argc; i++) {
536 size_t len;
537 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
538 if (arg == NULL) {
539 clear_wstrlist(i, argv);
540 pymain->err = DECODE_LOCALE_ERR("command line arguments",
541 (Py_ssize_t)len);
542 return -1;
543 }
544 argv[i] = arg;
545 }
546 argv[pymain->argc] = NULL;
547
548 cmdline->argv = argv;
549 }
550 else {
551 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100552 }
553
Victor Stinnerca719ac2017-12-20 18:00:19 +0100554 wchar_t *program;
555 if (pymain->argc >= 1 && cmdline->argv != NULL) {
556 program = cmdline->argv[0];
557 }
558 else {
559 program = L"";
560 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100561 pymain->config.program = pymain_wstrdup(pymain, program);
562 if (pymain->config.program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100563 return -1;
564 }
565
Victor Stinnerc4bca952017-12-19 23:48:17 +0100566 return 0;
567}
568
569
570static void
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200571_PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config)
572{
573 if (config->ignore_environment == -1) {
574 config->ignore_environment = Py_IgnoreEnvironmentFlag;
575 }
576 if (config->utf8_mode == -1) {
577 config->utf8_mode = Py_UTF8Mode;
578 }
579 if (config->isolated == -1) {
580 config->isolated = Py_IsolatedFlag;
581 }
582 if (config->site_import == -1) {
583 config->site_import = !Py_NoSiteFlag;
584 }
585}
586
587
588/* Get Py_xxx global configuration variables */
589static void
590pymain_get_global_config(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
591{
592 _PyCoreConfig_GetGlobalConfig(&pymain->config);
593
594 cmdline->bytes_warning = Py_BytesWarningFlag;
595 cmdline->debug = Py_DebugFlag;
596 cmdline->inspect = Py_InspectFlag;
597 cmdline->interactive = Py_InteractiveFlag;
598 cmdline->optimization_level = Py_OptimizeFlag;
599 cmdline->dont_write_bytecode = Py_DontWriteBytecodeFlag;
600 cmdline->no_user_site_directory = Py_NoUserSiteDirectory;
601 cmdline->use_unbuffered_io = Py_UnbufferedStdioFlag;
602 cmdline->verbosity = Py_VerboseFlag;
603 cmdline->quiet_flag = Py_QuietFlag;
604#ifdef MS_WINDOWS
605 cmdline->legacy_windows_fs_encoding = Py_LegacyWindowsFSEncodingFlag;
606 cmdline->legacy_windows_stdio = Py_LegacyWindowsStdioFlag;
607#endif
608 cmdline->check_hash_pycs_mode = _Py_CheckHashBasedPycsMode ;
609}
610
611
612/* Set Py_xxx global configuration variables from 'config' configuration. */
613void
614_PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config)
615{
616 if (config->ignore_environment != -1) {
617 Py_IgnoreEnvironmentFlag = config->ignore_environment;
618 }
619 if (config->utf8_mode != -1) {
620 Py_UTF8Mode = config->utf8_mode;
621 }
622 if (config->isolated != -1) {
623 Py_IsolatedFlag = config->isolated;
624 }
625 if (config->site_import != -1) {
626 Py_NoSiteFlag = !config->site_import;
627 }
628
629 /* Random or non-zero hash seed */
630 Py_HashRandomizationFlag = (config->use_hash_seed == 0 ||
631 config->hash_seed != 0);
632}
633
634
635/* Set Py_xxx global configuration variables */
636static void
637pymain_set_global_config(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
638{
639 _PyCoreConfig_SetGlobalConfig(&pymain->config);
640
641 Py_BytesWarningFlag = cmdline->bytes_warning;
642 Py_DebugFlag = cmdline->debug;
643 Py_InspectFlag = cmdline->inspect;
644 Py_InteractiveFlag = cmdline->interactive;
645 Py_OptimizeFlag = cmdline->optimization_level;
646 Py_DontWriteBytecodeFlag = cmdline->dont_write_bytecode;
647 Py_NoUserSiteDirectory = cmdline->no_user_site_directory;
648 Py_UnbufferedStdioFlag = cmdline->use_unbuffered_io;
649 Py_VerboseFlag = cmdline->verbosity;
650 Py_QuietFlag = cmdline->quiet_flag;
651 _Py_CheckHashBasedPycsMode = cmdline->check_hash_pycs_mode;
652#ifdef MS_WINDOWS
653 Py_LegacyWindowsFSEncodingFlag = cmdline->legacy_windows_fs_encoding;
654 Py_LegacyWindowsStdioFlag = cmdline->legacy_windows_stdio;
655#endif
656}
657
658
659/* Free memory allocated in config, but don't clear all attributes */
660void
661_PyCoreConfig_Clear(_PyCoreConfig *config)
662{
663#define CLEAR(ATTR) \
664 do { \
665 PyMem_RawFree(ATTR); \
666 ATTR = NULL; \
667 } while (0)
668#define CLEAR_WSTRLIST(LEN, LIST) \
669 do { \
670 clear_wstrlist(LEN, LIST); \
671 LEN = 0; \
672 LIST = NULL; \
673 } while (0)
674
675 CLEAR(config->pycache_prefix);
676 CLEAR(config->module_search_path_env);
677 CLEAR(config->home);
678 CLEAR(config->program_name);
679 CLEAR(config->program);
680
681 CLEAR_WSTRLIST(config->argc, config->argv);
682 config->argc = -1;
683
684 CLEAR_WSTRLIST(config->nwarnoption, config->warnoptions);
685 CLEAR_WSTRLIST(config->nxoption, config->xoptions);
686 CLEAR_WSTRLIST(config->nmodule_search_path, config->module_search_paths);
687 config->nmodule_search_path = -1;
688
689 CLEAR(config->executable);
690 CLEAR(config->prefix);
691 CLEAR(config->base_prefix);
692 CLEAR(config->exec_prefix);
693#ifdef MS_WINDOWS
694 CLEAR(config->dll_path);
695#endif
696 CLEAR(config->base_exec_prefix);
697#undef CLEAR
698#undef CLEAR_WSTRLIST
699}
700
701
702int
703_PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
704{
705 _PyCoreConfig_Clear(config);
706
707#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
708#define COPY_STR_ATTR(ATTR) \
709 do { \
710 if (config2->ATTR != NULL) { \
711 config->ATTR = _PyMem_RawWcsdup(config2->ATTR); \
712 if (config->ATTR == NULL) { \
713 return -1; \
714 } \
715 } \
716 } while (0)
717#define COPY_WSTRLIST(LEN, LIST) \
718 do { \
719 if (config2->LIST != NULL) { \
720 config->LIST = copy_wstrlist(config2->LEN, config2->LIST); \
721 if (config->LIST == NULL) { \
722 return -1; \
723 } \
724 } \
725 config->LEN = config2->LEN; \
726 } while (0)
727
728 COPY_ATTR(ignore_environment);
729 COPY_ATTR(use_hash_seed);
730 COPY_ATTR(hash_seed);
731 COPY_ATTR(_install_importlib);
732 COPY_ATTR(allocator);
733 COPY_ATTR(dev_mode);
734 COPY_ATTR(faulthandler);
735 COPY_ATTR(tracemalloc);
736 COPY_ATTR(import_time);
737 COPY_ATTR(show_ref_count);
738 COPY_ATTR(show_alloc_count);
739 COPY_ATTR(dump_refs);
740 COPY_ATTR(malloc_stats);
741 COPY_ATTR(utf8_mode);
742
743 COPY_STR_ATTR(pycache_prefix);
744 COPY_STR_ATTR(module_search_path_env);
745 COPY_STR_ATTR(home);
746 COPY_STR_ATTR(program_name);
747 COPY_STR_ATTR(program);
748
749 COPY_WSTRLIST(argc, argv);
750 COPY_WSTRLIST(nwarnoption, warnoptions);
751 COPY_WSTRLIST(nxoption, xoptions);
752 COPY_WSTRLIST(nmodule_search_path, module_search_paths);
753
754 COPY_STR_ATTR(executable);
755 COPY_STR_ATTR(prefix);
756 COPY_STR_ATTR(base_prefix);
757 COPY_STR_ATTR(exec_prefix);
758#ifdef MS_WINDOWS
759 COPY_STR_ATTR(dll_path);
760#endif
761 COPY_STR_ATTR(base_exec_prefix);
762
763 COPY_ATTR(isolated);
764 COPY_ATTR(site_import);
765
766#undef COPY_ATTR
767#undef COPY_STR_ATTR
768#undef COPY_WSTRLIST
769 return 0;
770}
771
772
773static void
Victor Stinnerca719ac2017-12-20 18:00:19 +0100774pymain_clear_cmdline(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100775{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100776 PyMemAllocatorEx old_alloc;
777 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100778
Victor Stinnerca719ac2017-12-20 18:00:19 +0100779 clear_wstrlist(cmdline->nwarnoption, cmdline->warnoptions);
780 cmdline->nwarnoption = 0;
781 cmdline->warnoptions = NULL;
782
783 clear_wstrlist(cmdline->nenv_warnoption, cmdline->env_warnoptions);
784 cmdline->nenv_warnoption = 0;
785 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100786
787 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100788 clear_wstrlist(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100789 }
790 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100791
792 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
793}
794
795
796static void
797pymain_clear_pymain(_PyMain *pymain)
798{
799#define CLEAR(ATTR) \
800 do { \
801 PyMem_RawFree(ATTR); \
802 ATTR = NULL; \
803 } while (0)
804
805 CLEAR(pymain->filename);
806 CLEAR(pymain->command);
807 CLEAR(pymain->module);
808#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100809}
810
Victor Stinnerc4bca952017-12-19 23:48:17 +0100811static void
Victor Stinner9cfc0022017-12-20 19:36:46 +0100812pymain_clear_config(_PyMain *pymain)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100813{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100814 /* Clear core config with the memory allocator
815 used by pymain_read_conf() */
816 PyMemAllocatorEx old_alloc;
817 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
818
Victor Stinner9cfc0022017-12-20 19:36:46 +0100819 _PyCoreConfig_Clear(&pymain->config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100820
821 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
822}
823
824
825static void
826pymain_free_python(_PyMain *pymain)
827{
828 Py_CLEAR(pymain->main_importer_path);
Victor Stinnerd4341102017-11-23 00:12:09 +0100829
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800830#ifdef __INSURE__
831 /* Insure++ is a memory analysis tool that aids in discovering
832 * memory leaks and other memory problems. On Python exit, the
833 * interned string dictionaries are flagged as being in use at exit
834 * (which it is). Under normal circumstances, this is fine because
835 * the memory will be automatically reclaimed by the system. Under
836 * memory debugging, it's a huge source of useless noise, so we
837 * trade off slower shutdown for less distraction in the memory
838 * reports. -baw
839 */
840 _Py_ReleaseInternedUnicodeStrings();
841#endif /* __INSURE__ */
842}
843
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100844
845static void
Victor Stinnerc4bca952017-12-19 23:48:17 +0100846pymain_free_raw(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100847{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100848 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100849
Victor Stinnerc4bca952017-12-19 23:48:17 +0100850 /* Free global variables which cannot be freed in Py_Finalize():
851 configuration options set before Py_Initialize() which should
852 remain valid after Py_Finalize(), since
853 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinnerb1147e42018-07-21 02:06:16 +0200854 _PyPathConfig_ClearGlobal();
Victor Stinner94540602017-12-16 04:54:22 +0100855
Victor Stinner31e99082017-12-20 23:41:38 +0100856 pymain_clear_config(pymain);
857
Victor Stinnerc4bca952017-12-19 23:48:17 +0100858 /* Force the allocator used by pymain_read_conf() */
859 PyMemAllocatorEx old_alloc;
860 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100861
Victor Stinnerca719ac2017-12-20 18:00:19 +0100862 pymain_clear_pymain(pymain);
863
864 clear_wstrlist(orig_argc, orig_argv);
865 orig_argc = 0;
866 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100867
Victor Stinnerc4bca952017-12-19 23:48:17 +0100868 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100869}
870
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100871
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800872static void
873pymain_free(_PyMain *pymain)
874{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100875 pymain_free_python(pymain);
876 pymain_free_raw(pymain);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800877}
878
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100879
Eric Snow6b4be192017-05-22 21:36:03 -0700880static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800881pymain_run_main_from_importer(_PyMain *pymain)
Guido van Rossum667d7041995-08-04 04:20:48 +0000882{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800883 /* Assume sys_path0 has already been checked by pymain_get_importer(),
884 * so put it in sys.path[0] and import __main__ */
Victor Stinner11a247d2017-12-13 21:05:57 +0100885 PyObject *sys_path = PySys_GetObject("path");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800886 if (sys_path == NULL) {
887 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
888 goto error;
889 }
890
Victor Stinner11a247d2017-12-13 21:05:57 +0100891 if (PyList_Insert(sys_path, 0, pymain->main_importer_path)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800892 goto error;
893 }
894
Victor Stinner11a247d2017-12-13 21:05:57 +0100895 int sts = pymain_run_module(L"__main__", 0);
896 return (sts != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800897
898error:
899 Py_CLEAR(pymain->main_importer_path);
900 PyErr_Print();
901 return 1;
902}
903
904
Victor Stinnerb1147e42018-07-21 02:06:16 +0200905_PyInitError
906_Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800907{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200908 if (*len == INT_MAX) {
909 /* len+1 would overflow */
910 return _Py_INIT_NO_MEMORY();
911 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100912 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800913 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100914 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800915 }
916
Victor Stinnerca719ac2017-12-20 18:00:19 +0100917 size_t size = (*len + 1) * sizeof(list[0]);
918 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
919 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800920 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100921 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800922 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100923 list2[*len] = str2;
924 *list = list2;
925 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100926 return _Py_INIT_OK();
927}
928
929
930static int
931pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
932{
Victor Stinnerb1147e42018-07-21 02:06:16 +0200933 _PyInitError err = _Py_wstrlist_append(len, list, str);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100934 if (_Py_INIT_FAILED(err)) {
935 pymain->err = err;
936 return -1;
937 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800938 return 0;
939}
940
941
942/* Parse the command line arguments
943 Return 0 on success.
944 Return 1 if parsing failed.
945 Set pymain->err and return -1 on other errors. */
946static int
Victor Stinnerca719ac2017-12-20 18:00:19 +0100947pymain_parse_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800948{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100949 _PyCoreConfig *config = &pymain->config;
950
Antoine Pitrou86838b02012-02-21 19:03:47 +0100951 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800952 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800953 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100954 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800955 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800956 if (c == EOF) {
957 break;
958 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 /* -c is the last option; following arguments
962 that look like options are left for the
963 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800964 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
965 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
966 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100967 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800968 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800969 }
Victor Stinner58d16832018-05-31 15:09:28 +0200970 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 command[len - 2] = '\n';
972 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100973 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 break;
975 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 if (c == 'm') {
978 /* -m is the last option; following arguments
979 that look like options are left for the
980 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100981 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
982 if (pymain->module == NULL) {
983 return -1;
984 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 break;
986 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800989 case 0:
990 // Handle long option.
991 assert(longindex == 0); // Only one long option now.
992 if (!wcscmp(_PyOS_optarg, L"always")) {
993 cmdline->check_hash_pycs_mode = "always";
994 } else if (!wcscmp(_PyOS_optarg, L"never")) {
995 cmdline->check_hash_pycs_mode = "never";
996 } else if (!wcscmp(_PyOS_optarg, L"default")) {
997 cmdline->check_hash_pycs_mode = "default";
998 } else {
999 fprintf(stderr, "--check-hash-based-pycs must be one of "
1000 "'default', 'always', or 'never'\n");
1001 return 1;
1002 }
1003 break;
1004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 case 'b':
Eric Snow6b4be192017-05-22 21:36:03 -07001006 cmdline->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 break;
Guido van Rossum667d7041995-08-04 04:20:48 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 case 'd':
Eric Snow6b4be192017-05-22 21:36:03 -07001010 cmdline->debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 break;
Guido van Rossum667d7041995-08-04 04:20:48 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 case 'i':
Eric Snow6b4be192017-05-22 21:36:03 -07001014 cmdline->inspect++;
1015 cmdline->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 break;
Guido van Rossum667d7041995-08-04 04:20:48 +00001017
Christian Heimesad73a9c2013-08-10 16:36:18 +02001018 case 'I':
Victor Stinner9cfc0022017-12-20 19:36:46 +01001019 config->ignore_environment++;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001020 config->isolated++;
Eric Snow6b4be192017-05-22 21:36:03 -07001021 cmdline->no_user_site_directory++;
Christian Heimesad73a9c2013-08-10 16:36:18 +02001022 break;
1023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 case 'O':
Eric Snow6b4be192017-05-22 21:36:03 -07001027 cmdline->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 break;
Guido van Rossum7614da61997-03-03 19:14:45 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 case 'B':
Eric Snow6b4be192017-05-22 21:36:03 -07001031 cmdline->dont_write_bytecode++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 break;
Christian Heimes790c8232008-01-07 21:14:23 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 case 's':
Eric Snow6b4be192017-05-22 21:36:03 -07001035 cmdline->no_user_site_directory++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 case 'S':
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001039 config->site_import = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 case 'E':
Victor Stinner9cfc0022017-12-20 19:36:46 +01001043 config->ignore_environment++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 case 't':
1047 /* ignored for backwards compatibility */
1048 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 case 'u':
Eric Snow6b4be192017-05-22 21:36:03 -07001051 cmdline->use_unbuffered_io = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 break;
Guido van Rossum667d7041995-08-04 04:20:48 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 case 'v':
Eric Snow6b4be192017-05-22 21:36:03 -07001055 cmdline->verbosity++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 break;
Guido van Rossum667d7041995-08-04 04:20:48 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +01001059 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 case 'h':
1063 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -07001064 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -07001068 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +01001072 if (pymain_wstrlist_append(pymain,
1073 &cmdline->nwarnoption,
1074 &cmdline->warnoptions,
1075 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001076 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001077 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +00001079
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001080 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +01001081 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +01001082 &config->nxoption,
1083 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +01001084 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001085 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001086 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +00001087 break;
1088
Georg Brandl9d871192010-12-04 10:47:18 +00001089 case 'q':
Eric Snow6b4be192017-05-22 21:36:03 -07001090 cmdline->quiet_flag++;
Georg Brandl9d871192010-12-04 10:47:18 +00001091 break;
1092
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001093 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +01001094 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001095 break;
1096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001100 /* unknown argument: parsing failed */
1101 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001103 } while (1);
1104
Victor Stinnerca719ac2017-12-20 18:00:19 +01001105 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001106 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +01001107 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001108 {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001109 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
1110 if (pymain->filename == NULL) {
1111 return -1;
1112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001114
Victor Stinnerd5dda982017-12-13 17:31:16 +01001115 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +01001116 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +01001117
Eric Snow6b4be192017-05-22 21:36:03 -07001118 return 0;
1119}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +00001120
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001121
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001122static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01001123add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +01001124{
1125 PyObject *name, *value;
1126
1127 const wchar_t *name_end = wcschr(s, L'=');
1128 if (!name_end) {
1129 name = PyUnicode_FromWideChar(s, -1);
1130 value = Py_True;
1131 Py_INCREF(value);
1132 }
1133 else {
1134 name = PyUnicode_FromWideChar(s, name_end - s);
1135 value = PyUnicode_FromWideChar(name_end + 1, -1);
1136 }
1137 if (name == NULL || value == NULL) {
1138 goto error;
1139 }
1140 if (PyDict_SetItem(opts, name, value) < 0) {
1141 goto error;
1142 }
1143 Py_DECREF(name);
1144 Py_DECREF(value);
1145 return 0;
1146
1147error:
1148 Py_XDECREF(name);
1149 Py_XDECREF(value);
1150 return -1;
1151}
1152
Victor Stinner9cfc0022017-12-20 19:36:46 +01001153
1154static PyObject*
1155config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001156{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001157 int nxoption = config->nxoption;
1158 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +01001159 PyObject *dict = PyDict_New();
1160 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001161 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +01001162 }
1163
Victor Stinnerca719ac2017-12-20 18:00:19 +01001164 for (int i=0; i < nxoption; i++) {
1165 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +01001166 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +01001167 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001168 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001169 }
1170 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001171
Victor Stinner9cfc0022017-12-20 19:36:46 +01001172 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -07001173}
1174
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001175
Victor Stinner9cfc0022017-12-20 19:36:46 +01001176static _PyInitError
1177config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -07001178{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001179 for (int i = 0; i < len; i++) {
Victor Stinnerb1147e42018-07-21 02:06:16 +02001180 _PyInitError err = _Py_wstrlist_append(&config->nwarnoption,
1181 &config->warnoptions,
1182 options[i]);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001183 if (_Py_INIT_FAILED(err)) {
1184 return err;
Eric Snow6b4be192017-05-22 21:36:03 -07001185 }
Eric Snow6b4be192017-05-22 21:36:03 -07001186 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001187 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001188}
Eric Snow6b4be192017-05-22 21:36:03 -07001189
Victor Stinner747f48e2017-12-12 22:59:48 +01001190
Victor Stinner9cfc0022017-12-20 19:36:46 +01001191static _PyInitError
1192config_init_warnoptions(_PyCoreConfig *config, _Py_CommandLineDetails *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +01001193{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001194 _PyInitError err;
1195
1196 assert(config->nwarnoption == 0);
1197
Victor Stinner747f48e2017-12-12 22:59:48 +01001198 /* The priority order for warnings configuration is (highest precedence
1199 * first):
1200 *
1201 * - the BytesWarning filter, if needed ('-b', '-bb')
1202 * - any '-W' command line options; then
1203 * - the 'PYTHONWARNINGS' environment variable; then
1204 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
1205 * - any implicit filters added by _warnings.c/warnings.py
1206 *
1207 * All settings except the last are passed to the warnings module via
1208 * the `sys.warnoptions` list. Since the warnings module works on the basis
1209 * of "the most recently added filter will be checked first", we add
1210 * the lowest precedence entries first so that later entries override them.
1211 */
1212
Victor Stinner9cfc0022017-12-20 19:36:46 +01001213 if (config->dev_mode) {
Victor Stinnerb1147e42018-07-21 02:06:16 +02001214 err = _Py_wstrlist_append(&config->nwarnoption,
1215 &config->warnoptions,
1216 L"default");
Victor Stinner9cfc0022017-12-20 19:36:46 +01001217 if (_Py_INIT_FAILED(err)) {
1218 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001219 }
Victor Stinner747f48e2017-12-12 22:59:48 +01001220 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001221
Victor Stinner9cfc0022017-12-20 19:36:46 +01001222 err = config_add_warnings_optlist(config,
1223 cmdline->nenv_warnoption,
1224 cmdline->env_warnoptions);
1225 if (_Py_INIT_FAILED(err)) {
1226 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001227 }
1228
Victor Stinner9cfc0022017-12-20 19:36:46 +01001229 err = config_add_warnings_optlist(config,
1230 cmdline->nwarnoption,
1231 cmdline->warnoptions);
1232 if (_Py_INIT_FAILED(err)) {
1233 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001234 }
1235
1236 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
1237 * don't even try to emit a warning, so we skip setting the filter in that
1238 * case.
1239 */
1240 if (cmdline->bytes_warning) {
1241 wchar_t *filter;
1242 if (cmdline->bytes_warning> 1) {
1243 filter = L"error::BytesWarning";
1244 }
1245 else {
1246 filter = L"default::BytesWarning";
1247 }
Victor Stinnerb1147e42018-07-21 02:06:16 +02001248 err = _Py_wstrlist_append(&config->nwarnoption,
1249 &config->warnoptions,
1250 filter);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001251 if (_Py_INIT_FAILED(err)) {
1252 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001253 }
1254 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001255 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001256}
1257
1258
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001259/* Get warning options from PYTHONWARNINGS environment variable.
1260 Return 0 on success.
1261 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001262static _PyInitError
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001263cmdline_init_env_warnoptions(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001264{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001265 if (pymain->config.ignore_environment) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001266 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +00001268
Victor Stinnerca719ac2017-12-20 18:00:19 +01001269 wchar_t *env;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001270 int res = config_get_env_var_dup(&pymain->config, &env,
1271 L"PYTHONWARNINGS", "PYTHONWARNINGS");
Victor Stinnerca719ac2017-12-20 18:00:19 +01001272 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001273 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001274 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001275
Victor Stinnerca719ac2017-12-20 18:00:19 +01001276 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001277 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001278 }
Philip Jenvey0805ca32010-04-07 04:04:10 +00001279
Victor Stinnerca719ac2017-12-20 18:00:19 +01001280
1281 wchar_t *warning, *context = NULL;
1282 for (warning = WCSTOK(env, L",", &context);
1283 warning != NULL;
1284 warning = WCSTOK(NULL, L",", &context))
1285 {
Victor Stinnerb1147e42018-07-21 02:06:16 +02001286 _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption,
1287 &cmdline->env_warnoptions,
1288 warning);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001289 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001290 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001291 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001292 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001294 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001295 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001296}
1297
1298
1299static void
1300pymain_init_stdio(_PyMain *pymain)
1301{
1302 pymain->stdin_is_interactive = (isatty(fileno(stdin))
1303 || Py_InteractiveFlag);
Guido van Rossum775af911997-02-14 19:50:32 +00001304
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +00001305#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +00001306 /* don't translate newlines (\r\n <=> \n) */
1307 _setmode(fileno(stdin), O_BINARY);
1308 _setmode(fileno(stdout), O_BINARY);
1309 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +00001310#endif
Victor Stinner89e34362011-01-07 18:47:22 +00001311
1312 if (Py_UnbufferedStdioFlag) {
Guido van Rossum22ffac11998-03-06 15:30:39 +00001313#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
1315 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
1316 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001317#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 setbuf(stdin, (char *)NULL);
1319 setbuf(stdout, (char *)NULL);
1320 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001321#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 }
1323 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +00001324#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 /* Doesn't have to have line-buffered -- use unbuffered */
1326 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
1327 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001328#else /* !MS_WINDOWS */
1329#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
1331 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001332#endif /* HAVE_SETVBUF */
1333#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 /* Leave stderr alone - it should be unbuffered anyway. */
1335 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001336}
Guido van Rossum667d7041995-08-04 04:20:48 +00001337
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001338
1339/* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
Victor Stinner31a83932017-12-04 13:39:15 +01001340 environment variables on macOS if available. */
1341static _PyInitError
Victor Stinnerca719ac2017-12-20 18:00:19 +01001342config_init_program_name(_PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001343{
Victor Stinner31a83932017-12-04 13:39:15 +01001344 assert(config->program_name == NULL);
1345
1346 /* If Py_SetProgramName() was called, use its value */
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001347 const wchar_t *program_name = _Py_path_config.program_name;
Victor Stinner31a83932017-12-04 13:39:15 +01001348 if (program_name != NULL) {
1349 config->program_name = _PyMem_RawWcsdup(program_name);
1350 if (config->program_name == NULL) {
1351 return _Py_INIT_NO_MEMORY();
1352 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001353 return _Py_INIT_OK();
Victor Stinner31a83932017-12-04 13:39:15 +01001354 }
1355
Just van Rossum2ac79ef2003-03-05 15:46:54 +00001356#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 /* On MacOS X, when the Python interpreter is embedded in an
1358 application bundle, it gets executed by a bootstrapping script
1359 that does os.execve() with an argv[0] that's different from the
1360 actual Python executable. This is needed to keep the Finder happy,
1361 or rather, to work around Apple's overly strict requirements of
1362 the process name. However, we still need a usable sys.executable,
1363 so the actual executable path is passed in an environment variable.
1364 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
1365 script. */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001366 const char *p = config_get_env_var(config, "PYTHONEXECUTABLE");
Victor Stinner6bf992a2017-12-06 17:26:10 +01001367 if (p != NULL) {
Victor Stinner31a83932017-12-04 13:39:15 +01001368 size_t len;
1369 wchar_t* program_name = Py_DecodeLocale(p, &len);
1370 if (program_name == NULL) {
1371 return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment "
1372 "variable", (Py_ssize_t)len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 }
Victor Stinner31a83932017-12-04 13:39:15 +01001374 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001375 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001376 }
Vinay Sajip90db6612012-07-17 17:33:46 +01001377#ifdef WITH_NEXT_FRAMEWORK
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001378 else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001379 const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
Vinay Sajip90db6612012-07-17 17:33:46 +01001380 if (pyvenv_launcher && *pyvenv_launcher) {
1381 /* Used by Mac/Tools/pythonw.c to forward
1382 * the argv0 of the stub executable
1383 */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001384 size_t len;
Victor Stinner31a83932017-12-04 13:39:15 +01001385 wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len);
1386 if (program_name == NULL) {
1387 return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment "
1388 "variable", (Py_ssize_t)len);
Vinay Sajip90db6612012-07-17 17:33:46 +01001389 }
Victor Stinner31a83932017-12-04 13:39:15 +01001390 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001391 return _Py_INIT_OK();
Vinay Sajip90db6612012-07-17 17:33:46 +01001392 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001394#endif /* WITH_NEXT_FRAMEWORK */
1395#endif /* __APPLE__ */
Victor Stinneraf5a8952017-12-02 10:11:32 +01001396
Victor Stinnerca719ac2017-12-20 18:00:19 +01001397 /* Use argv[0] by default, if available */
1398 if (config->program != NULL) {
1399 config->program_name = _PyMem_RawWcsdup(config->program);
1400 if (config->program_name == NULL) {
1401 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001402 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001403 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001404 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001405
1406 /* Last fall back: hardcoded string */
1407#ifdef MS_WINDOWS
1408 const wchar_t *default_program_name = L"python";
1409#else
1410 const wchar_t *default_program_name = L"python3";
1411#endif
1412 config->program_name = _PyMem_RawWcsdup(default_program_name);
1413 if (config->program_name == NULL) {
1414 return _Py_INIT_NO_MEMORY();
1415 }
1416 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001417}
1418
1419
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001420static void
1421pymain_header(_PyMain *pymain)
1422{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001423 if (Py_QuietFlag) {
1424 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 }
Guido van Rossum393661d2001-08-31 17:40:15 +00001426
Victor Stinner19760862017-12-20 01:41:59 +01001427 if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001428 return;
1429 }
1430
1431 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
1432 if (!Py_NoSiteFlag) {
1433 fprintf(stderr, "%s\n", COPYRIGHT);
1434 }
1435}
1436
1437
Victor Stinnerc4bca952017-12-19 23:48:17 +01001438static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001439pymain_init_core_argv(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001440{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001441 /* Copy argv to be able to modify it (to force -c/-m) */
1442 int argc = pymain->argc - _PyOS_optind;
1443 wchar_t **argv;
1444
1445 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +01001446 /* Ensure at least one (empty) argument is seen */
1447 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +01001448 argc = 1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001449 argv = copy_wstrlist(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +01001450 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001451 else {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001452 argv = copy_wstrlist(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001453 }
1454
1455 if (argv == NULL) {
1456 pymain->err = _Py_INIT_NO_MEMORY();
1457 return -1;
1458 }
1459
1460 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001461 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001462 /* Force sys.argv[0] = '-c' */
1463 arg0 = L"-c";
1464 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001465 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001466 /* Force sys.argv[0] = '-m'*/
1467 arg0 = L"-m";
1468 }
1469 if (arg0 != NULL) {
1470 arg0 = _PyMem_RawWcsdup(arg0);
1471 if (arg0 == NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001472 clear_wstrlist(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001473 pymain->err = _Py_INIT_NO_MEMORY();
1474 return -1;
1475 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001476
1477 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001478 PyMem_RawFree(argv[0]);
1479 argv[0] = arg0;
1480 }
1481
Victor Stinner9cfc0022017-12-20 19:36:46 +01001482 pymain->config.argc = argc;
1483 pymain->config.argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001484 return 0;
1485}
1486
1487
Victor Stinner8ded5b82018-01-24 17:03:28 +01001488static PyObject*
1489wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001490{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001491 assert(list != NULL || len < 1);
1492
1493 PyObject *pylist = PyList_New(len);
1494 if (pylist == NULL) {
1495 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001496 }
1497
Victor Stinner8ded5b82018-01-24 17:03:28 +01001498 for (int i = 0; i < len; i++) {
1499 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001500 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001501 Py_DECREF(pylist);
1502 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001503 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001504 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001505 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001506 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001507}
1508
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001509
Victor Stinner11a247d2017-12-13 21:05:57 +01001510static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01001511pymain_compute_path0(_PyMain *pymain, PyObject **path0)
Victor Stinner11a247d2017-12-13 21:05:57 +01001512{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001513 if (pymain->main_importer_path != NULL) {
1514 /* Let pymain_run_main_from_importer() adjust sys.path[0] later */
Victor Stinner19760862017-12-20 01:41:59 +01001515 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001516 return 0;
1517 }
1518
1519 if (Py_IsolatedFlag) {
Victor Stinner19760862017-12-20 01:41:59 +01001520 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001521 return 0;
1522 }
1523
Victor Stinner9cfc0022017-12-20 19:36:46 +01001524 *path0 = _PyPathConfig_ComputeArgv0(pymain->config.argc,
1525 pymain->config.argv);
Victor Stinner19760862017-12-20 01:41:59 +01001526 if (*path0 == NULL) {
1527 pymain->err = _Py_INIT_NO_MEMORY();
1528 return -1;
1529 }
1530 return 0;
1531}
1532
1533
1534static int
1535pymain_update_sys_path(_PyMain *pymain, PyObject *path0)
1536{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001537 /* Prepend argv[0] to sys.path.
1538 If argv[0] is a symlink, use the real path. */
1539 PyObject *sys_path = PySys_GetObject("path");
1540 if (sys_path == NULL) {
1541 pymain->err = _Py_INIT_ERR("can't get sys.path");
Victor Stinnerd5dda982017-12-13 17:31:16 +01001542 return -1;
1543 }
1544
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001545 /* Prepend path0 to sys.path */
1546 if (PyList_Insert(sys_path, 0, path0) < 0) {
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001547 pymain->err = _Py_INIT_ERR("sys.path.insert(0, path0) failed");
1548 return -1;
1549 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001550 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001551}
1552
1553
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001554static void
1555pymain_import_readline(_PyMain *pymain)
1556{
1557 if (Py_IsolatedFlag) {
1558 return;
1559 }
Victor Stinner19760862017-12-20 01:41:59 +01001560 if (!Py_InspectFlag && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001561 return;
1562 }
1563 if (!isatty(fileno(stdin))) {
1564 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001565 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001566
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001567 PyObject *mod = PyImport_ImportModule("readline");
1568 if (mod == NULL) {
1569 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 }
1571 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001572 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001574}
1575
1576
1577static FILE*
1578pymain_open_filename(_PyMain *pymain)
1579{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001580 FILE* fp;
1581
Victor Stinnerca719ac2017-12-20 18:00:19 +01001582 fp = _Py_wfopen(pymain->filename, L"r");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001583 if (fp == NULL) {
1584 char *cfilename_buffer;
1585 const char *cfilename;
1586 int err = errno;
Victor Stinner9dd76202017-12-21 16:20:32 +01001587 cfilename_buffer = _Py_EncodeLocaleRaw(pymain->filename, NULL);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001588 if (cfilename_buffer != NULL)
1589 cfilename = cfilename_buffer;
1590 else
1591 cfilename = "<unprintable file name>";
1592 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinner9cfc0022017-12-20 19:36:46 +01001593 pymain->config.program, cfilename, err, strerror(err));
Victor Stinner9dd76202017-12-21 16:20:32 +01001594 PyMem_RawFree(cfilename_buffer);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001595 pymain->status = 2;
1596 return NULL;
1597 }
1598
Victor Stinnerca719ac2017-12-20 18:00:19 +01001599 if (pymain->skip_first_line) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001600 int ch;
1601 /* Push back first newline so line numbers
1602 remain the same */
1603 while ((ch = getc(fp)) != EOF) {
1604 if (ch == '\n') {
1605 (void)ungetc(ch, fp);
1606 break;
1607 }
1608 }
1609 }
1610
1611 struct _Py_stat_struct sb;
1612 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 &&
1613 S_ISDIR(sb.st_mode)) {
1614 fprintf(stderr,
1615 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinner9cfc0022017-12-20 19:36:46 +01001616 pymain->config.program, pymain->filename);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001617 fclose(fp);
1618 pymain->status = 1;
1619 return NULL;
1620 }
1621
1622 return fp;
1623}
1624
1625
1626static void
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001627pymain_run_startup(_PyMain *pymain, PyCompilerFlags *cf)
1628{
1629 const char *startup = config_get_env_var(&pymain->config, "PYTHONSTARTUP");
1630 if (startup == NULL) {
1631 return;
1632 }
1633
1634 FILE *fp = _Py_fopen(startup, "r");
1635 if (fp == NULL) {
1636 int save_errno = errno;
1637 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
1638 errno = save_errno;
1639
1640 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
1641 startup);
1642 PyErr_Print();
1643 PyErr_Clear();
1644 return;
1645 }
1646
1647 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
1648 PyErr_Clear();
1649 fclose(fp);
1650}
1651
1652
1653static void
Victor Stinner19760862017-12-20 01:41:59 +01001654pymain_run_filename(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001655{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001656 if (pymain->filename == NULL && pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001657 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001658 pymain_run_startup(pymain, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001659 pymain_run_interactive_hook();
1660 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001661
1662 if (pymain->main_importer_path != NULL) {
1663 pymain->status = pymain_run_main_from_importer(pymain);
1664 return;
1665 }
1666
1667 FILE *fp;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001668 if (pymain->filename != NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001669 fp = pymain_open_filename(pymain);
1670 if (fp == NULL) {
1671 return;
1672 }
1673 }
1674 else {
1675 fp = stdin;
1676 }
1677
Victor Stinnerca719ac2017-12-20 18:00:19 +01001678 pymain->status = pymain_run_file(fp, pymain->filename, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001679}
1680
1681
1682static void
Victor Stinner19760862017-12-20 01:41:59 +01001683pymain_repl(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001686 opportunity to set it from Python. */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001687 if (!Py_InspectFlag && config_get_env_var(&pymain->config, "PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 Py_InspectFlag = 1;
1689 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001690
Victor Stinner19760862017-12-20 01:41:59 +01001691 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001692 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001694
1695 Py_InspectFlag = 0;
1696 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001697
Victor Stinner19760862017-12-20 01:41:59 +01001698 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001699 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001700}
1701
1702
1703/* Parse the command line.
1704 Handle --version and --help options directly.
1705
1706 Return 1 if Python must exit.
1707 Return 0 on success.
1708 Set pymain->err and return -1 on failure. */
1709static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001710pymain_parse_cmdline(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001711{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001712 int res = pymain_parse_cmdline_impl(pymain, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001713 if (res < 0) {
1714 return -1;
1715 }
1716 if (res) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001717 pymain_usage(1, pymain->config.program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001718 pymain->status = 2;
1719 return 1;
1720 }
1721
Victor Stinnerca719ac2017-12-20 18:00:19 +01001722 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001723 /* Backup _PyOS_optind */
1724 _PyOS_optind--;
1725 }
1726
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001727 return 0;
1728}
1729
1730
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001731static const wchar_t*
Victor Stinner9cfc0022017-12-20 19:36:46 +01001732config_get_xoption(_PyCoreConfig *config, wchar_t *name)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001733{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001734 int nxoption = config->nxoption;
1735 wchar_t **xoptions = config->xoptions;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001736 for (int i=0; i < nxoption; i++) {
1737 wchar_t *option = xoptions[i];
Victor Stinnera7368ac2017-11-15 18:11:45 -08001738 size_t len;
1739 wchar_t *sep = wcschr(option, L'=');
1740 if (sep != NULL) {
1741 len = (sep - option);
1742 }
1743 else {
1744 len = wcslen(option);
1745 }
1746 if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
1747 return option;
1748 }
1749 }
1750 return NULL;
1751}
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001752
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001753
Victor Stinnera7368ac2017-11-15 18:11:45 -08001754static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001755pymain_str_to_int(const char *str, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001756{
1757 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001758 const char *endptr = str;
1759 long value = strtol(str, (char **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001760 if (*endptr != '\0' || errno == ERANGE) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001761 return -1;
1762 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001763 if (value < INT_MIN || value > INT_MAX) {
1764 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001765 }
1766
Victor Stinnera7368ac2017-11-15 18:11:45 -08001767 *result = (int)value;
1768 return 0;
1769}
1770
1771
1772static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001773pymain_wstr_to_int(const wchar_t *wstr, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001774{
1775 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001776 const wchar_t *endptr = wstr;
1777 long value = wcstol(wstr, (wchar_t **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001778 if (*endptr != '\0' || errno == ERANGE) {
1779 return -1;
1780 }
1781 if (value < INT_MIN || value > INT_MAX) {
1782 return -1;
1783 }
1784
1785 *result = (int)value;
1786 return 0;
1787}
1788
1789
Victor Stinner9cfc0022017-12-20 19:36:46 +01001790static _PyInitError
1791pymain_init_tracemalloc(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001792{
1793 int nframe;
1794 int valid;
1795
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001796 const char *env = config_get_env_var(config, "PYTHONTRACEMALLOC");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001797 if (env) {
1798 if (!pymain_str_to_int(env, &nframe)) {
1799 valid = (nframe >= 1);
1800 }
1801 else {
1802 valid = 0;
1803 }
1804 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001805 return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number "
1806 "of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001807 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001808 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001809 }
1810
Victor Stinner9cfc0022017-12-20 19:36:46 +01001811 const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001812 if (xoption) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001813 const wchar_t *sep = wcschr(xoption, L'=');
Victor Stinnera7368ac2017-11-15 18:11:45 -08001814 if (sep) {
1815 if (!pymain_wstr_to_int(sep + 1, &nframe)) {
1816 valid = (nframe >= 1);
1817 }
1818 else {
1819 valid = 0;
1820 }
1821 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001822 return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: "
1823 "invalid number of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001824 }
1825 }
1826 else {
1827 /* -X tracemalloc behaves as -X tracemalloc=1 */
1828 nframe = 1;
1829 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001830 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001831 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001832 return _Py_INIT_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001833}
1834
1835
Carl Meyerb193fa92018-06-15 22:40:56 -06001836static _PyInitError
1837pymain_init_pycache_prefix(_PyCoreConfig *config)
1838{
1839 wchar_t *env;
1840
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001841 int res = config_get_env_var_dup(config, &env,
1842 L"PYTHONPYCACHEPREFIX", "PYTHONPYCACHEPREFIX");
Carl Meyerb193fa92018-06-15 22:40:56 -06001843 if (res < 0) {
1844 return DECODE_LOCALE_ERR("PYTHONPYCACHEPREFIX", res);
1845 } else if (env) {
1846 config->pycache_prefix = env;
1847 }
1848
1849 const wchar_t *xoption = config_get_xoption(config, L"pycache_prefix");
1850 if (xoption) {
1851 const wchar_t *sep = wcschr(xoption, L'=');
1852 if (sep && wcslen(sep) > 1) {
1853 config->pycache_prefix = _PyMem_RawWcsdup(sep + 1);
1854 if (config->pycache_prefix == NULL) {
1855 return _Py_INIT_NO_MEMORY();
1856 }
1857 } else {
1858 // -X pycache_prefix= can cancel the env var
1859 config->pycache_prefix = NULL;
1860 }
1861 }
1862
1863 return _Py_INIT_OK();
1864}
1865
1866
Victor Stinnera7368ac2017-11-15 18:11:45 -08001867static void
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001868get_env_flag(_PyCoreConfig *config, int *flag, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001869{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001870 const char *var = config_get_env_var(config, name);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001871 if (!var) {
1872 return;
1873 }
1874 int value;
1875 if (pymain_str_to_int(var, &value) < 0 || value < 0) {
1876 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
1877 value = 1;
1878 }
1879 if (*flag < value) {
1880 *flag = value;
1881 }
1882}
1883
1884
1885static void
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001886cmdline_get_env_flags(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001887{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001888 _PyCoreConfig *config = &pymain->config;
1889 get_env_flag(config, &cmdline->debug, "PYTHONDEBUG");
1890 get_env_flag(config, &cmdline->verbosity, "PYTHONVERBOSE");
1891 get_env_flag(config, &cmdline->optimization_level, "PYTHONOPTIMIZE");
1892 get_env_flag(config, &cmdline->inspect, "PYTHONINSPECT");
1893 get_env_flag(config, &cmdline->dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
1894 get_env_flag(config, &cmdline->no_user_site_directory, "PYTHONNOUSERSITE");
1895 get_env_flag(config, &cmdline->use_unbuffered_io, "PYTHONUNBUFFERED");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001896#ifdef MS_WINDOWS
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001897 get_env_flag(config, &cmdline->legacy_windows_fs_encoding,
Victor Stinner9cfc0022017-12-20 19:36:46 +01001898 "PYTHONLEGACYWINDOWSFSENCODING");
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001899 get_env_flag(config, &cmdline->legacy_windows_stdio,
Victor Stinner9cfc0022017-12-20 19:36:46 +01001900 "PYTHONLEGACYWINDOWSSTDIO");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001901#endif
1902}
1903
1904
Victor Stinner46972b72017-11-24 22:55:40 +01001905static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001906config_init_home(_PyCoreConfig *config)
Victor Stinner1f151112017-11-23 10:43:14 +01001907{
1908 wchar_t *home;
1909
Victor Stinner31a83932017-12-04 13:39:15 +01001910 /* If Py_SetPythonHome() was called, use its value */
1911 home = _Py_path_config.home;
Victor Stinner1f151112017-11-23 10:43:14 +01001912 if (home) {
Victor Stinner46972b72017-11-24 22:55:40 +01001913 config->home = _PyMem_RawWcsdup(home);
1914 if (config->home == NULL) {
1915 return _Py_INIT_NO_MEMORY();
Victor Stinner1f151112017-11-23 10:43:14 +01001916 }
Victor Stinner46972b72017-11-24 22:55:40 +01001917 return _Py_INIT_OK();
Victor Stinner1f151112017-11-23 10:43:14 +01001918 }
1919
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001920 int res = config_get_env_var_dup(config, &home, L"PYTHONHOME", "PYTHONHOME");
Victor Stinner1f151112017-11-23 10:43:14 +01001921 if (res < 0) {
Victor Stinner46972b72017-11-24 22:55:40 +01001922 return DECODE_LOCALE_ERR("PYTHONHOME", res);
Victor Stinner1f151112017-11-23 10:43:14 +01001923 }
Victor Stinner46972b72017-11-24 22:55:40 +01001924 config->home = home;
1925 return _Py_INIT_OK();
Victor Stinnerd4341102017-11-23 00:12:09 +01001926}
1927
1928
Victor Stinner358e5e12017-12-15 00:51:22 +01001929static _PyInitError
1930config_init_hash_seed(_PyCoreConfig *config)
1931{
1932 if (config->use_hash_seed < 0) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001933 const char *seed_text = config_get_env_var(config, "PYTHONHASHSEED");
Victor Stinner358e5e12017-12-15 00:51:22 +01001934 int use_hash_seed;
1935 unsigned long hash_seed;
1936 if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
1937 return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
1938 "or an integer in range [0; 4294967295]");
1939 }
1940 config->use_hash_seed = use_hash_seed;
1941 config->hash_seed = hash_seed;
1942 }
1943 return _Py_INIT_OK();
1944}
1945
1946
Victor Stinner9cfc0022017-12-20 19:36:46 +01001947static _PyInitError
1948config_init_utf8_mode(_PyCoreConfig *config)
Victor Stinner91106cd2017-12-13 12:29:09 +01001949{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001950 /* The option was already set in config.Py_UTF8Mode,
1951 by Py_LegacyWindowsFSEncodingFlag or PYTHONLEGACYWINDOWSFSENCODING. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001952 if (config->utf8_mode >= 0) {
1953 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001954 }
Victor Stinner91106cd2017-12-13 12:29:09 +01001955
Victor Stinner9cfc0022017-12-20 19:36:46 +01001956 const wchar_t *xopt = config_get_xoption(config, L"utf8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001957 if (xopt) {
1958 wchar_t *sep = wcschr(xopt, L'=');
1959 if (sep) {
1960 xopt = sep + 1;
1961 if (wcscmp(xopt, L"1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001962 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001963 }
1964 else if (wcscmp(xopt, L"0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001965 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001966 }
1967 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001968 return _Py_INIT_USER_ERR("invalid -X utf8 option value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001969 }
1970 }
1971 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001972 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001973 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001974 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001975 }
1976
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001977 const char *opt = config_get_env_var(config, "PYTHONUTF8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001978 if (opt) {
1979 if (strcmp(opt, "1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001980 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001981 }
1982 else if (strcmp(opt, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001983 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001984 }
1985 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001986 return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
1987 "variable value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001988 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001989 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001990 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001991
1992 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001993}
Victor Stinner46972b72017-11-24 22:55:40 +01001994
1995
Victor Stinner9cfc0022017-12-20 19:36:46 +01001996static _PyInitError
1997config_read_env_vars(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001998{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02001999 config->allocator = config_get_env_var(config, "PYTHONMALLOC");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002000
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002001 if (config_get_env_var(config, "PYTHONDUMPREFS")) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002002 config->dump_refs = 1;
2003 }
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002004 if (config_get_env_var(config, "PYTHONMALLOCSTATS")) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002005 config->malloc_stats = 1;
Victor Stinner31a83932017-12-04 13:39:15 +01002006 }
2007
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002008 const char *env = config_get_env_var(config, "PYTHONCOERCECLOCALE");
Victor Stinner94540602017-12-16 04:54:22 +01002009 if (env) {
2010 if (strcmp(env, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002011 config->coerce_c_locale = 0;
Victor Stinner94540602017-12-16 04:54:22 +01002012 }
2013 else if (strcmp(env, "warn") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002014 config->coerce_c_locale_warn = 1;
Victor Stinner94540602017-12-16 04:54:22 +01002015 }
2016 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002017 config->coerce_c_locale = 1;
Victor Stinner94540602017-12-16 04:54:22 +01002018 }
2019 }
2020
Victor Stinner9cfc0022017-12-20 19:36:46 +01002021 wchar_t *path;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002022 int res = config_get_env_var_dup(config, &path, L"PYTHONPATH", "PYTHONPATH");
Victor Stinner9cfc0022017-12-20 19:36:46 +01002023 if (res < 0) {
Carl Meyer48575432018-05-19 16:48:22 -06002024 return DECODE_LOCALE_ERR("PYTHONPATH", res);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002025 }
2026 config->module_search_path_env = path;
2027
2028 _PyInitError err = config_init_hash_seed(config);
2029 if (_Py_INIT_FAILED(err)) {
2030 return err;
2031 }
2032
2033 return _Py_INIT_OK();
2034}
2035
2036
2037static _PyInitError
2038config_read_complex_options(_PyCoreConfig *config)
2039{
2040 /* More complex options configured by env var and -X option */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002041 if (config_get_env_var(config, "PYTHONFAULTHANDLER")
Victor Stinner9cfc0022017-12-20 19:36:46 +01002042 || config_get_xoption(config, L"faulthandler")) {
2043 config->faulthandler = 1;
2044 }
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002045 if (config_get_env_var(config, "PYTHONPROFILEIMPORTTIME")
Victor Stinner9cfc0022017-12-20 19:36:46 +01002046 || config_get_xoption(config, L"importtime")) {
2047 config->import_time = 1;
2048 }
2049 if (config_get_xoption(config, L"dev" ) ||
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002050 config_get_env_var(config, "PYTHONDEVMODE"))
Victor Stinner9cfc0022017-12-20 19:36:46 +01002051 {
2052 config->dev_mode = 1;
2053 config->faulthandler = 1;
2054 config->allocator = "debug";
2055 }
2056
2057 _PyInitError err = pymain_init_tracemalloc(config);
2058 if (_Py_INIT_FAILED(err)) {
2059 return err;
2060 }
Carl Meyerb193fa92018-06-15 22:40:56 -06002061
2062 err = pymain_init_pycache_prefix(config);
2063 if (_Py_INIT_FAILED(err)) {
2064 return err;
2065 }
2066
Victor Stinner9cfc0022017-12-20 19:36:46 +01002067 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002068}
2069
2070
Victor Stinnera7368ac2017-11-15 18:11:45 -08002071/* Parse command line options and environment variables.
2072 This code must not use Python runtime apart PyMem_Raw memory allocator.
2073
2074 Return 0 on success.
2075 Return 1 if Python is done and must exit.
2076 Set pymain->err and return -1 on error. */
2077static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01002078pymain_read_conf_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002079{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002080 _PyInitError err;
2081
Victor Stinnerca719ac2017-12-20 18:00:19 +01002082 int res = pymain_parse_cmdline(pymain, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01002083 if (res != 0) {
2084 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002085 }
2086
Victor Stinner9cfc0022017-12-20 19:36:46 +01002087 /* Get environment variables */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002088 cmdline_get_env_flags(pymain, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002089
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002090 err = cmdline_init_env_warnoptions(pymain, cmdline);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002091 if (_Py_INIT_FAILED(err)) {
2092 pymain->err = err;
2093 return -1;
2094 }
2095
2096#ifdef MS_WINDOWS
2097 if (cmdline->legacy_windows_fs_encoding) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002098 pymain->config.utf8_mode = 0;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002099 }
2100#endif
2101
Victor Stinnerca719ac2017-12-20 18:00:19 +01002102 if (pymain_init_core_argv(pymain, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01002103 return -1;
2104 }
2105
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002106 err = _PyCoreConfig_Read(&pymain->config);
Victor Stinner2b822a02018-01-25 09:18:36 +01002107
Victor Stinner31a83932017-12-04 13:39:15 +01002108 if (_Py_INIT_FAILED(err)) {
2109 pymain->err = err;
2110 return -1;
2111 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002112 return 0;
2113}
2114
2115
Victor Stinner19760862017-12-20 01:41:59 +01002116/* Read the configuration, but initialize also the LC_CTYPE locale:
2117 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
Victor Stinnera7368ac2017-11-15 18:11:45 -08002118static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01002119pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08002120{
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002121 _PyCoreConfig *config = &pymain->config;
2122 _PyCoreConfig save_config = _PyCoreConfig_INIT;
2123 char *oldloc = NULL;
Victor Stinner94540602017-12-16 04:54:22 +01002124 int res = -1;
2125
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002126 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
Victor Stinner94540602017-12-16 04:54:22 +01002127 if (oldloc == NULL) {
2128 pymain->err = _Py_INIT_NO_MEMORY();
2129 goto done;
2130 }
2131
2132 /* Reconfigure the locale to the default for this process */
2133 _Py_SetLocaleFromEnv(LC_ALL);
2134
2135 int locale_coerced = 0;
2136 int loops = 0;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002137
2138 if (_PyCoreConfig_Copy(&save_config, config) < 0) {
2139 pymain->err = _Py_INIT_NO_MEMORY();
2140 goto done;
2141 }
Victor Stinner94540602017-12-16 04:54:22 +01002142
2143 while (1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002144 int utf8_mode = config->utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002145 int encoding_changed = 0;
2146
2147 /* Watchdog to prevent an infinite loop */
2148 loops++;
2149 if (loops == 3) {
2150 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
2151 "reading the configuration");
2152 goto done;
2153 }
2154
Victor Stinnerca719ac2017-12-20 18:00:19 +01002155 if (pymain_init_cmdline_argv(pymain, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01002156 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01002157 }
2158
Victor Stinner9cfc0022017-12-20 19:36:46 +01002159 int conf_res = pymain_read_conf_impl(pymain, cmdline);
2160 if (conf_res != 0) {
2161 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01002162 goto done;
2163 }
2164
2165 /* The legacy C locale assumes ASCII as the default text encoding, which
2166 * causes problems not only for the CPython runtime, but also other
2167 * components like GNU readline.
2168 *
2169 * Accordingly, when the CLI detects it, it attempts to coerce it to a
2170 * more capable UTF-8 based alternative.
2171 *
2172 * See the documentation of the PYTHONCOERCECLOCALE setting for more
2173 * details.
2174 */
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002175 if (config->coerce_c_locale == 1 && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01002176 locale_coerced = 1;
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002177 _Py_CoerceLegacyLocale(config);
Victor Stinner94540602017-12-16 04:54:22 +01002178 encoding_changed = 1;
2179 }
2180
2181 if (utf8_mode == -1) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002182 if (config->utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01002183 /* UTF-8 Mode enabled */
2184 encoding_changed = 1;
2185 }
2186 }
2187 else {
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002188 if (config->utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01002189 encoding_changed = 1;
2190 }
2191 }
2192
2193 if (!encoding_changed) {
2194 break;
2195 }
2196
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002197 /* Reset the configuration before reading the configuration,
2198 except UTF-8 Mode. */
2199 int new_utf8_mode = config->utf8_mode;
2200 if (_PyCoreConfig_Copy(config, &save_config) < 0) {
2201 pymain->err = _Py_INIT_NO_MEMORY();
2202 goto done;
2203 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002204 pymain_clear_cmdline(pymain, cmdline);
Victor Stinner6c5a4b32018-06-16 00:06:28 +02002205 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinnerca719ac2017-12-20 18:00:19 +01002206 pymain_get_global_config(pymain, cmdline);
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002207 pymain->config.utf8_mode = new_utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002208
2209 /* The encoding changed: read again the configuration
2210 with the new encoding */
2211 }
2212 res = 0;
2213
2214done:
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002215 _PyCoreConfig_Clear(&save_config);
Victor Stinner94540602017-12-16 04:54:22 +01002216 if (oldloc != NULL) {
2217 setlocale(LC_ALL, oldloc);
2218 PyMem_RawFree(oldloc);
2219 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002220
Victor Stinnera7368ac2017-11-15 18:11:45 -08002221 return res;
2222}
2223
Victor Stinner91106cd2017-12-13 12:29:09 +01002224
Victor Stinner9cfc0022017-12-20 19:36:46 +01002225static void
2226config_init_locale(_PyCoreConfig *config)
2227{
2228 if (config->utf8_mode >= 0 && config->coerce_c_locale >= 0) {
2229 return;
2230 }
2231
2232 if (_Py_LegacyLocaleDetected()) {
2233 /* POSIX locale: enable C locale coercion and UTF-8 Mode */
2234 if (config->utf8_mode < 0) {
2235 config->utf8_mode = 1;
2236 }
2237 if (config->coerce_c_locale < 0) {
2238 config->coerce_c_locale = 1;
2239 }
2240 return;
2241 }
2242
2243 /* By default, C locale coercion and UTF-8 Mode are disabled */
2244 if (config->coerce_c_locale < 0) {
2245 config->coerce_c_locale = 0;
2246 }
2247 if (config->utf8_mode < 0) {
2248 config->utf8_mode = 0;
2249 }
2250}
2251
2252
Victor Stinnerda273412017-12-15 01:46:02 +01002253/* Read configuration settings from standard locations
2254 *
2255 * This function doesn't make any changes to the interpreter state - it
2256 * merely populates any missing configuration settings. This allows an
2257 * embedding application to completely override a config option by
2258 * setting it before calling this function, or else modify the default
2259 * setting before passing the fully populated config to Py_EndInitialization.
2260 *
2261 * More advanced selective initialization tricks are possible by calling
2262 * this function multiple times with various preconfigured settings.
2263 */
2264
2265_PyInitError
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002266_PyCoreConfig_Read(_PyCoreConfig *config)
Victor Stinnerda273412017-12-15 01:46:02 +01002267{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002268 _PyInitError err;
2269
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002270 _PyCoreConfig_GetGlobalConfig(config);
2271
Victor Stinner9cfc0022017-12-20 19:36:46 +01002272 err = config_read_env_vars(config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002273 if (_Py_INIT_FAILED(err)) {
2274 return err;
2275 }
2276
Victor Stinner9cfc0022017-12-20 19:36:46 +01002277 /* -X options */
2278 if (config_get_xoption(config, L"showrefcount")) {
2279 config->show_ref_count = 1;
2280 }
2281 if (config_get_xoption(config, L"showalloccount")) {
2282 config->show_alloc_count = 1;
2283 }
2284
2285 err = config_read_complex_options(config);
2286 if (_Py_INIT_FAILED(err)) {
2287 return err;
2288 }
2289
2290 err = config_init_utf8_mode(config);
2291 if (_Py_INIT_FAILED(err)) {
2292 return err;
2293 }
2294
2295 err = config_init_home(config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002296 if (_Py_INIT_FAILED(err)) {
2297 return err;
2298 }
2299
2300 err = config_init_program_name(config);
2301 if (_Py_INIT_FAILED(err)) {
2302 return err;
2303 }
2304
Victor Stinner9cfc0022017-12-20 19:36:46 +01002305 config_init_locale(config);
Victor Stinnerda273412017-12-15 01:46:02 +01002306
Victor Stinner9cfc0022017-12-20 19:36:46 +01002307 /* Signal handlers are installed by default */
2308 if (config->install_signal_handlers < 0) {
2309 config->install_signal_handlers = 1;
Victor Stinner94540602017-12-16 04:54:22 +01002310 }
2311
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002312 if (config->_install_importlib) {
2313 err = _PyCoreConfig_InitPathConfig(config);
Victor Stinner8ded5b82018-01-24 17:03:28 +01002314 if (_Py_INIT_FAILED(err)) {
2315 return err;
2316 }
2317 }
Victor Stinnerda273412017-12-15 01:46:02 +01002318 return _Py_INIT_OK();
2319}
2320
2321
2322void
Victor Stinnerda273412017-12-15 01:46:02 +01002323_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
2324{
2325 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002326 Py_CLEAR(config->executable);
2327 Py_CLEAR(config->prefix);
2328 Py_CLEAR(config->base_prefix);
2329 Py_CLEAR(config->exec_prefix);
2330 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002331 Py_CLEAR(config->warnoptions);
2332 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002333 Py_CLEAR(config->module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06002334 Py_CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002335}
2336
2337
2338static PyObject*
2339config_copy_attr(PyObject *obj)
2340{
2341 if (PyUnicode_Check(obj)) {
2342 Py_INCREF(obj);
2343 return obj;
2344 }
2345 else if (PyList_Check(obj)) {
2346 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
2347 }
2348 else if (PyDict_Check(obj)) {
2349 /* The dict type is used for xoptions. Make the assumption that keys
2350 and values are immutables */
2351 return PyDict_Copy(obj);
2352 }
2353 else {
2354 PyErr_Format(PyExc_TypeError,
2355 "cannot copy config attribute of type %.200s",
2356 Py_TYPE(obj)->tp_name);
2357 return NULL;
2358 }
2359}
2360
2361
2362int
2363_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
2364 const _PyMainInterpreterConfig *config2)
2365{
2366 _PyMainInterpreterConfig_Clear(config);
2367
2368#define COPY_ATTR(ATTR) \
2369 do { \
2370 if (config2->ATTR != NULL) { \
2371 config->ATTR = config_copy_attr(config2->ATTR); \
2372 if (config->ATTR == NULL) { \
2373 return -1; \
2374 } \
2375 } \
2376 } while (0)
2377
2378 COPY_ATTR(argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002379 COPY_ATTR(executable);
2380 COPY_ATTR(prefix);
2381 COPY_ATTR(base_prefix);
2382 COPY_ATTR(exec_prefix);
2383 COPY_ATTR(base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002384 COPY_ATTR(warnoptions);
2385 COPY_ATTR(xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002386 COPY_ATTR(module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06002387 COPY_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002388#undef COPY_ATTR
2389 return 0;
2390}
2391
2392
2393
2394
Victor Stinner41264f12017-12-15 02:05:29 +01002395_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01002396_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
2397 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002398{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002399 if (main_config->install_signal_handlers < 0) {
2400 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002401 }
2402
Victor Stinner9cfc0022017-12-20 19:36:46 +01002403 if (main_config->xoptions == NULL) {
2404 main_config->xoptions = config_create_xoptions_dict(config);
2405 if (main_config->xoptions == NULL) {
2406 return _Py_INIT_NO_MEMORY();
2407 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002408 }
2409
Victor Stinner8ded5b82018-01-24 17:03:28 +01002410#define COPY_WSTR(ATTR) \
2411 do { \
2412 if (main_config->ATTR == NULL) { \
2413 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
2414 if (main_config->ATTR == NULL) { \
2415 return _Py_INIT_NO_MEMORY(); \
2416 } \
2417 } \
2418 } while (0)
2419#define COPY_WSTRLIST(ATTR, LEN, LIST) \
2420 do { \
2421 if (ATTR == NULL) { \
2422 ATTR = wstrlist_as_pylist(LEN, LIST); \
2423 if (ATTR == NULL) { \
2424 return _Py_INIT_NO_MEMORY(); \
2425 } \
2426 } \
2427 } while (0)
2428
2429 COPY_WSTRLIST(main_config->warnoptions,
2430 config->nwarnoption, config->warnoptions);
2431 if (config->argc >= 0) {
2432 COPY_WSTRLIST(main_config->argv,
2433 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002434 }
2435
Victor Stinnerd19d8d52018-07-24 13:55:48 +02002436 if (config->_install_importlib) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01002437 COPY_WSTR(executable);
2438 COPY_WSTR(prefix);
2439 COPY_WSTR(base_prefix);
2440 COPY_WSTR(exec_prefix);
2441 COPY_WSTR(base_exec_prefix);
2442
2443 COPY_WSTRLIST(main_config->module_search_path,
2444 config->nmodule_search_path, config->module_search_paths);
Carl Meyerb193fa92018-06-15 22:40:56 -06002445
2446 if (config->pycache_prefix != NULL) {
2447 COPY_WSTR(pycache_prefix);
2448 } else {
2449 main_config->pycache_prefix = NULL;
2450 }
2451
Victor Stinner9cfc0022017-12-20 19:36:46 +01002452 }
Victor Stinner41264f12017-12-15 02:05:29 +01002453
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002454 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01002455#undef COPY_WSTR
2456#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002457}
2458
2459
2460static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01002461pymain_init_python_main(_PyMain *pymain)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002462{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002463 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002464
Victor Stinner9cfc0022017-12-20 19:36:46 +01002465 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
2466 err = _PyMainInterpreterConfig_Read(&main_config, &pymain->config);
2467 if (!_Py_INIT_FAILED(err)) {
2468 err = _Py_InitializeMainInterpreter(&main_config);
2469 }
2470 _PyMainInterpreterConfig_Clear(&main_config);
2471
2472 if (_Py_INIT_FAILED(err)) {
2473 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002474 return -1;
2475 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002476 return 0;
2477}
Victor Stinnera7368ac2017-11-15 18:11:45 -08002478
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002479
2480static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01002481pymain_init_sys_path(_PyMain *pymain)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002482{
Victor Stinnerca719ac2017-12-20 18:00:19 +01002483 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002484 /* If filename is a package (ex: directory or ZIP file) which contains
2485 __main__.py, main_importer_path is set to filename and will be
2486 prepended to sys.path by pymain_run_main_from_importer(). Otherwise,
2487 main_importer_path is set to NULL. */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002488 pymain->main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01002489 }
2490
Victor Stinner19760862017-12-20 01:41:59 +01002491 PyObject *path0;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002492 if (pymain_compute_path0(pymain, &path0) < 0) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002493 return -1;
2494 }
Victor Stinner19760862017-12-20 01:41:59 +01002495
Victor Stinner9cfc0022017-12-20 19:36:46 +01002496 pymain_clear_config(pymain);
Victor Stinner19760862017-12-20 01:41:59 +01002497
2498 if (path0 != NULL) {
2499 if (pymain_update_sys_path(pymain, path0) < 0) {
2500 Py_DECREF(path0);
2501 return -1;
2502 }
2503 Py_DECREF(path0);
2504 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002505 return 0;
2506}
2507
2508
2509static void
2510pymain_run_python(_PyMain *pymain)
2511{
Victor Stinner19760862017-12-20 01:41:59 +01002512 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08002513
2514 pymain_header(pymain);
2515 pymain_import_readline(pymain);
2516
Victor Stinnerca719ac2017-12-20 18:00:19 +01002517 if (pymain->command) {
2518 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002519 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002520 else if (pymain->module) {
2521 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002522 }
2523 else {
Victor Stinner19760862017-12-20 01:41:59 +01002524 pymain_run_filename(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002525 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002526
Victor Stinner19760862017-12-20 01:41:59 +01002527 pymain_repl(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002528}
2529
2530
Victor Stinnerc4bca952017-12-19 23:48:17 +01002531static void
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002532pymain_init(_PyMain *pymain)
2533{
Victor Stinner94540602017-12-16 04:54:22 +01002534 /* 754 requires that FP exceptions run in "no stop" mode by default,
2535 * and until C vendors implement C99's ways to control FP exceptions,
2536 * Python requires non-stop mode. Alas, some platforms enable FP
2537 * exceptions by default. Here we disable them.
2538 */
2539#ifdef __FreeBSD__
2540 fedisableexcept(FE_OVERFLOW);
2541#endif
2542
Victor Stinnere32e79f2017-11-23 01:49:45 +01002543 pymain->config.install_signal_handlers = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002544}
2545
Victor Stinnera7368ac2017-11-15 18:11:45 -08002546
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002547static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01002548pymain_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002549{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002550 pymain->err = _PyRuntime_Initialize();
2551 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002552 return -1;
2553 }
2554
Victor Stinnerca719ac2017-12-20 18:00:19 +01002555 int res = pymain_read_conf(pymain, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002556 if (res < 0) {
2557 return -1;
2558 }
2559 if (res > 0) {
2560 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01002561 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002562 }
2563
Victor Stinner94540602017-12-16 04:54:22 +01002564 if (cmdline->print_help) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002565 pymain_usage(0, pymain->config.program);
Victor Stinner19760862017-12-20 01:41:59 +01002566 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002567 }
2568
2569 if (cmdline->print_version) {
2570 printf("Python %s\n",
2571 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01002572 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002573 }
2574
Victor Stinnerc4bca952017-12-19 23:48:17 +01002575 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002576 orig_argv = copy_wstrlist(pymain->argc, cmdline->argv);
2577 if (orig_argv == NULL) {
2578 pymain->err = _Py_INIT_NO_MEMORY();
2579 return -1;
2580 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01002581 orig_argc = pymain->argc;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002582
Victor Stinner9cfc0022017-12-20 19:36:46 +01002583 _PyInitError err = config_init_warnoptions(&pymain->config, cmdline);
2584 if (_Py_INIT_FAILED(err)) {
2585 pymain->err = err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002586 return -1;
2587 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002588 return 0;
2589}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00002590
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002591
Victor Stinnerca719ac2017-12-20 18:00:19 +01002592/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
2593 LC_CTYPE locale and Py_DecodeLocale().
2594
2595 Configuration:
2596
2597 * Command line arguments
2598 * Environment variables
2599 * Py_xxx global configuration variables
2600
2601 _Py_CommandLineDetails is a temporary structure used to prioritize these
2602 variables. */
2603static int
2604pymain_cmdline(_PyMain *pymain)
2605{
Victor Stinner31e99082017-12-20 23:41:38 +01002606 /* Force default allocator, since pymain_free() and pymain_clear_config()
2607 must use the same allocator than this function. */
2608 PyMemAllocatorEx old_alloc;
2609 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2610#ifdef Py_DEBUG
2611 PyMemAllocatorEx default_alloc;
2612 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
2613#endif
2614
Victor Stinnerca719ac2017-12-20 18:00:19 +01002615 _Py_CommandLineDetails cmdline;
2616 memset(&cmdline, 0, sizeof(cmdline));
2617
2618 pymain_get_global_config(pymain, &cmdline);
2619
2620 int res = pymain_cmdline_impl(pymain, &cmdline);
2621
2622 pymain_set_global_config(pymain, &cmdline);
2623
2624 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01002625
2626#ifdef Py_DEBUG
2627 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
2628 PyMemAllocatorEx cur_alloc;
2629 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
2630 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
2631#endif
2632 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002633 return res;
2634}
2635
2636
Victor Stinner94540602017-12-16 04:54:22 +01002637static int
2638pymain_main(_PyMain *pymain)
2639{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002640 pymain_init(pymain);
Victor Stinner94540602017-12-16 04:54:22 +01002641
Victor Stinnerca719ac2017-12-20 18:00:19 +01002642 int res = pymain_cmdline(pymain);
Victor Stinner19760862017-12-20 01:41:59 +01002643 if (res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01002644 _Py_FatalInitError(pymain->err);
2645 }
Victor Stinner19760862017-12-20 01:41:59 +01002646 if (res == 1) {
2647 goto done;
2648 }
2649
Victor Stinner9cfc0022017-12-20 19:36:46 +01002650 pymain_init_stdio(pymain);
2651
Victor Stinnerfb47bca2018-07-20 17:34:23 +02002652 /* bpo-34008: For backward compatibility reasons, calling Py_Main() after
2653 Py_Initialize() ignores the new configuration. */
2654 if (!_PyRuntime.initialized) {
2655 pymain->err = _Py_InitializeCore(&pymain->config);
2656 if (_Py_INIT_FAILED(pymain->err)) {
2657 _Py_FatalInitError(pymain->err);
2658 }
Victor Stinner19760862017-12-20 01:41:59 +01002659 }
2660
2661 if (pymain_init_python_main(pymain) < 0) {
2662 _Py_FatalInitError(pymain->err);
2663 }
2664
Victor Stinner9cfc0022017-12-20 19:36:46 +01002665 if (pymain_init_sys_path(pymain) < 0) {
2666 _Py_FatalInitError(pymain->err);
2667 }
2668
Victor Stinner19760862017-12-20 01:41:59 +01002669 pymain_run_python(pymain);
2670
2671 if (Py_FinalizeEx() < 0) {
2672 /* Value unlikely to be confused with a non-error exit status or
2673 other special meaning */
2674 pymain->status = 120;
2675 }
2676
2677done:
Victor Stinner94540602017-12-16 04:54:22 +01002678 pymain_free(pymain);
2679
Victor Stinner94540602017-12-16 04:54:22 +01002680 return pymain->status;
2681}
2682
2683
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002684int
2685Py_Main(int argc, wchar_t **argv)
2686{
2687 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002688 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002689 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002690 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002691
Victor Stinner94540602017-12-16 04:54:22 +01002692 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00002693}
2694
Victor Stinner94540602017-12-16 04:54:22 +01002695
2696int
2697_Py_UnixMain(int argc, char **argv)
2698{
2699 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01002700 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002701 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01002702 pymain.bytes_argv = argv;
2703
2704 return pymain_main(&pymain);
2705}
2706
2707
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002708/* this is gonna seem *real weird*, but if you put some other code between
2709 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
2710 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00002711
Guido van Rossum667d7041995-08-04 04:20:48 +00002712/* Make the *original* argc/argv available to other modules.
2713 This is rare, but it is needed by the secureware extension. */
2714
2715void
Martin v. Löwis790465f2008-04-05 20:41:37 +00002716Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00002717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 *argc = orig_argc;
2719 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00002720}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002721
2722#ifdef __cplusplus
2723}
2724#endif