blob: 6067d5eccffdeff1ed5795922d3c7d234706eb83 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Guido van Rossuma075ce11997-12-05 21:56:45 +00004#include "osdefs.h"
Benjamin Peterson42aa93b2017-12-09 10:26:52 -08005#include "internal/import.h"
Benjamin Petersone425bd72017-12-14 23:48:12 -08006#include "internal/pygetopt.h"
Victor Stinnerf7e5b562017-11-15 15:48:08 -08007#include "internal/pystate.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00008
Antoine Pitrou5651eaa2008-09-06 20:46:58 +00009#include <locale.h>
10
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000011#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010012# include <windows.h>
13# ifdef HAVE_IO_H
14# include <io.h>
15# endif
16# ifdef HAVE_FCNTL_H
17# include <fcntl.h>
18# endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000019#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000020
Martin v. Löwis945362c2007-08-30 14:57:25 +000021#ifdef _MSC_VER
Victor Stinner6efcb6d2017-12-18 23:42:55 +010022# include <crtdbg.h>
23#endif
24
25#ifdef __FreeBSD__
26# include <fenv.h>
Martin v. Löwis945362c2007-08-30 14:57:25 +000027#endif
28
Jesus Ceaab70e2a2012-10-05 01:48:08 +020029#if defined(MS_WINDOWS)
Victor Stinner6efcb6d2017-12-18 23:42:55 +010030# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
Guido van Rossuma075ce11997-12-05 21:56:45 +000031#else
Victor Stinner6efcb6d2017-12-18 23:42:55 +010032# define PYTHONHOMEHELP "<prefix>/lib/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000033#endif
34
Guido van Rossuma22865e2000-09-05 04:41:18 +000035#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000036 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
37 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000038
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039#ifdef __cplusplus
40extern "C" {
41#endif
42
Victor Stinner46972b72017-11-24 22:55:40 +010043#define DECODE_LOCALE_ERR(NAME, LEN) \
44 (((LEN) == -2) \
Victor Stinner94540602017-12-16 04:54:22 +010045 ? _Py_INIT_USER_ERR("cannot decode " NAME) \
Victor Stinner46972b72017-11-24 22:55:40 +010046 : _Py_INIT_NO_MEMORY())
47
48
Victor Stinner0327bde2017-11-23 17:03:20 +010049#define SET_DECODE_ERROR(NAME, LEN) \
50 do { \
51 if ((LEN) == (size_t)-2) { \
Victor Stinner94540602017-12-16 04:54:22 +010052 pymain->err = _Py_INIT_USER_ERR("cannot decode " NAME); \
Victor Stinner0327bde2017-11-23 17:03:20 +010053 } \
54 else { \
55 pymain->err = _Py_INIT_NO_MEMORY(); \
56 } \
57 } while (0)
58
Victor Stinnerca719ac2017-12-20 18:00:19 +010059#ifdef MS_WINDOWS
60#define WCSTOK wcstok_s
61#else
62#define WCSTOK wcstok
63#endif
64
Guido van Rossumac56b031996-07-21 02:33:38 +000065/* For Py_GetArgcArgv(); set by main() */
Victor Stinner94540602017-12-16 04:54:22 +010066static wchar_t **orig_argv = NULL;
67static int orig_argc = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +000068
Guido van Rossumbceccf52001-04-10 22:07:43 +000069/* command line options */
Christian Heimesad73a9c2013-08-10 16:36:18 +020070#define BASE_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000071
Guido van Rossumbceccf52001-04-10 22:07:43 +000072#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000073
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080074static const _PyOS_LongOption longoptions[] = {
75 {L"check-hash-based-pycs", 1, 0},
76 {NULL, 0, 0},
77};
78
Guido van Rossum667d7041995-08-04 04:20:48 +000079/* Short usage message (with %s for argv0) */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020080static const char usage_line[] =
Martin v. Löwis790465f2008-04-05 20:41:37 +000081"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000082
83/* Long usage message, split into parts < 512 bytes */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020084static const char usage_1[] = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000085Options and arguments (and corresponding environment variables):\n\
Christian Heimes2ab34442008-09-03 20:31:07 +000086-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
87 and comparing bytes/bytearray with str. (-bb: issue errors)\n\
Xiang Zhang0710d752017-03-11 13:02:52 +080088-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000089-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000090-d : debug output from parser; also PYTHONDEBUG=x\n\
Christian Heimes790c8232008-01-07 21:14:23 +000091-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000093";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020094static const char usage_2[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000095-i : inspect interactively after running script; forces a prompt even\n\
96 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Christian Heimesad73a9c2013-08-10 16:36:18 +020097-I : isolate Python from the user's environment (implies -E and -s)\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000098-m mod : run library module as a script (terminates option list)\n\
Cheryl Sabella186b6062018-02-24 22:04:40 -050099-O : remove assert and __debug__-dependent statements; add .opt-1 before\n\
100 .pyc extension; also PYTHONOPTIMIZE=x\n\
101-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
102 .pyc extension\n\
Georg Brandl9d871192010-12-04 10:47:18 +0000103-q : don't print version and copyright messages on interactive startup\n\
Christian Heimes8dc226f2008-05-06 23:45:46 +0000104-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000105-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000106";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200107static const char usage_3[] = "\
Berker Peksag7f580972017-10-13 15:16:31 +0300108-u : force the stdout and stderr streams to be unbuffered;\n\
109 this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
111 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000112-V : print the Python version number and exit (also --version)\n\
INADA Naoki0e175a62016-11-21 20:57:14 +0900113 when given twice, print more information about the build\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenvey0805ca32010-04-07 04:04:10 +0000115 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000116-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000117-X opt : set implementation-specific option\n\
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800118--check-hash-based-pycs always|default|never:\n\
119 control how Python invalidates hash-based .pyc files\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000120";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200121static const char usage_4[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +0000122file : program read from script file\n\
123- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000125Other environment variables:\n\
126PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200127PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000128 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +0000129";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200130static const char usage_5[] =
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200131"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
Victor Stinner9802b392010-08-19 11:36:43 +0000132" The default module search path uses %s.\n"
133"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
134"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
Victor Stinner34be807c2016-03-14 12:04:26 +0100135"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
136static const char usage_6[] =
137"PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
138" to seed the hashes of str, bytes and datetime objects. It can also be\n"
139" set to an integer in the range [0,4294967295] to get hash values with a\n"
140" predictable seed.\n"
141"PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
142" on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000143" hooks.\n"
Stéphane Wirtel7d1017d2017-06-12 13:30:33 +0200144"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000145" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
Victor Stinner5e3806f2017-11-30 11:40:24 +0100146" locale coercion and locale compatibility warnings on stderr.\n"
Carl Meyerb193fa92018-06-15 22:40:56 -0600147"PYTHONDEVMODE: enable the development mode.\n"
148"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
Guido van Rossum667d7041995-08-04 04:20:48 +0000149
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800150static void
Victor Stinnera7368ac2017-11-15 18:11:45 -0800151pymain_usage(int error, const wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000152{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800153 FILE *f = error ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 fprintf(f, usage_line, program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800156 if (error)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 fprintf(f, "Try `python -h' for more information.\n");
158 else {
159 fputs(usage_1, f);
160 fputs(usage_2, f);
161 fputs(usage_3, f);
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200162 fprintf(f, usage_4, (wint_t)DELIM);
163 fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100164 fputs(usage_6, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000166}
167
Victor Stinnera7368ac2017-11-15 18:11:45 -0800168
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200169static const char*
Victor Stinner9cfc0022017-12-20 19:36:46 +0100170config_get_env_var(const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -0800171{
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200172 const char *var = Py_GETENV(name);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800173 if (var && var[0] != '\0') {
174 return var;
175 }
176 else {
177 return NULL;
178 }
179}
180
181
Victor Stinnerca719ac2017-12-20 18:00:19 +0100182static int
183config_get_env_var_dup(wchar_t **dest, wchar_t *wname, char *name)
184{
185 if (Py_IgnoreEnvironmentFlag) {
186 *dest = NULL;
187 return 0;
188 }
189
190#ifdef MS_WINDOWS
191 const wchar_t *var = _wgetenv(wname);
192 if (!var || var[0] == '\0') {
193 *dest = NULL;
194 return 0;
195 }
196
197 wchar_t *copy = _PyMem_RawWcsdup(var);
198 if (copy == NULL) {
199 return -1;
200 }
201
202 *dest = copy;
203#else
204 const char *var = getenv(name);
205 if (!var || var[0] == '\0') {
206 *dest = NULL;
207 return 0;
208 }
209
210 size_t len;
211 wchar_t *wvar = Py_DecodeLocale(var, &len);
212 if (!wvar) {
213 if (len == (size_t)-2) {
214 return -2;
215 }
216 else {
217 return -1;
218 }
219 }
220 *dest = wvar;
221#endif
222 return 0;
223}
224
225
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800226static void
Victor Stinner33c377e2017-12-05 15:12:41 +0100227pymain_run_startup(PyCompilerFlags *cf)
Martin v. Löwis6caea372003-11-18 19:46:25 +0000228{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100229 const char *startup = config_get_env_var("PYTHONSTARTUP");
Victor Stinner6bf992a2017-12-06 17:26:10 +0100230 if (startup == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800231 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800233
234 FILE *fp = _Py_fopen(startup, "r");
235 if (fp == NULL) {
236 int save_errno = errno;
237 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
238 errno = save_errno;
239
240 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
241 startup);
242 PyErr_Print();
243 PyErr_Clear();
244 return;
245 }
246
247 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
248 PyErr_Clear();
249 fclose(fp);
Martin v. Löwis6caea372003-11-18 19:46:25 +0000250}
251
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800252static void
253pymain_run_interactive_hook(void)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200254{
255 PyObject *sys, *hook, *result;
256 sys = PyImport_ImportModule("sys");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800257 if (sys == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200258 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800259 }
260
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200261 hook = PyObject_GetAttrString(sys, "__interactivehook__");
262 Py_DECREF(sys);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800263 if (hook == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200264 PyErr_Clear();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800265 return;
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200266 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800267
268 result = _PyObject_CallNoArg(hook);
269 Py_DECREF(hook);
270 if (result == NULL) {
271 goto error;
272 }
273 Py_DECREF(result);
274
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200275 return;
276
277error:
278 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
279 PyErr_Print();
280 PyErr_Clear();
281}
282
Thomas Woutersa9773292006-04-21 09:43:23 +0000283
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800284static int
Victor Stinnerc4bca952017-12-19 23:48:17 +0100285pymain_run_module(const wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 PyObject *module, *runpy, *runmodule, *runargs, *result;
288 runpy = PyImport_ImportModule("runpy");
289 if (runpy == NULL) {
290 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200291 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 return -1;
293 }
294 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
295 if (runmodule == NULL) {
296 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200297 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 Py_DECREF(runpy);
299 return -1;
300 }
301 module = PyUnicode_FromWideChar(modname, wcslen(modname));
302 if (module == NULL) {
303 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200304 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 Py_DECREF(runpy);
306 Py_DECREF(runmodule);
307 return -1;
308 }
309 runargs = Py_BuildValue("(Oi)", module, set_argv0);
310 if (runargs == NULL) {
311 fprintf(stderr,
312 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200313 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 Py_DECREF(runpy);
315 Py_DECREF(runmodule);
316 Py_DECREF(module);
317 return -1;
318 }
319 result = PyObject_Call(runmodule, runargs, NULL);
320 if (result == NULL) {
321 PyErr_Print();
322 }
323 Py_DECREF(runpy);
324 Py_DECREF(runmodule);
325 Py_DECREF(module);
326 Py_DECREF(runargs);
327 if (result == NULL) {
328 return -1;
329 }
330 Py_DECREF(result);
331 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000332}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000333
Nick Coghland2977a32017-03-12 20:38:32 +1000334static PyObject *
Victor Stinnerc4bca952017-12-19 23:48:17 +0100335pymain_get_importer(const wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000336{
Nick Coghland2977a32017-03-12 20:38:32 +1000337 PyObject *sys_path0 = NULL, *importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000338
Nick Coghland2977a32017-03-12 20:38:32 +1000339 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800340 if (sys_path0 == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000341 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800342 }
Victor Stinner4726e402010-10-06 23:24:57 +0000343
Nick Coghland2977a32017-03-12 20:38:32 +1000344 importer = PyImport_GetImporter(sys_path0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800345 if (importer == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000346 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800347 }
Victor Stinner4726e402010-10-06 23:24:57 +0000348
Brett Cannonaa936422012-04-27 15:30:58 -0400349 if (importer == Py_None) {
Nick Coghland2977a32017-03-12 20:38:32 +1000350 Py_DECREF(sys_path0);
Victor Stinner4726e402010-10-06 23:24:57 +0000351 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000352 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800354
Victor Stinner4726e402010-10-06 23:24:57 +0000355 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000356 return sys_path0;
Victor Stinner4726e402010-10-06 23:24:57 +0000357
Nick Coghland2977a32017-03-12 20:38:32 +1000358error:
359 Py_XDECREF(sys_path0);
360 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
361 PyErr_Print();
362 PyErr_Clear();
363 return NULL;
364}
365
366
367static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800368pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
Victor Stinnera62207c2010-08-07 10:57:17 +0000369{
370 PyObject *unicode, *bytes;
371 int ret;
372
373 unicode = PyUnicode_FromWideChar(command, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800374 if (unicode == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000375 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800376 }
377
Victor Stinnera62207c2010-08-07 10:57:17 +0000378 bytes = PyUnicode_AsUTF8String(unicode);
379 Py_DECREF(unicode);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800380 if (bytes == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000381 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800382 }
383
Victor Stinnera62207c2010-08-07 10:57:17 +0000384 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
385 Py_DECREF(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800386 return (ret != 0);
Victor Stinnera62207c2010-08-07 10:57:17 +0000387
388error:
Victor Stinner398356b2010-08-18 22:23:22 +0000389 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000390 PyErr_Print();
391 return 1;
392}
393
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800394
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000395static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800396pymain_run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf)
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000397{
398 PyObject *unicode, *bytes = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200399 const char *filename_str;
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000400 int run;
401
402 /* call pending calls like signal handlers (SIGINT) */
403 if (Py_MakePendingCalls() == -1) {
404 PyErr_Print();
405 return 1;
406 }
407
408 if (filename) {
409 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
410 if (unicode != NULL) {
Victor Stinnere0f32682010-10-17 19:34:51 +0000411 bytes = PyUnicode_EncodeFSDefault(unicode);
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000412 Py_DECREF(unicode);
413 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800414 if (bytes != NULL) {
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000415 filename_str = PyBytes_AsString(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800416 }
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000417 else {
418 PyErr_Clear();
Victor Stinnere0f32682010-10-17 19:34:51 +0000419 filename_str = "<encoding error>";
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000420 }
421 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800422 else {
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000423 filename_str = "<stdin>";
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800424 }
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000425
426 run = PyRun_AnyFileExFlags(fp, filename_str, filename != NULL, p_cf);
427 Py_XDECREF(bytes);
428 return run != 0;
429}
430
Christian Heimes9cd17752007-11-18 19:35:23 +0000431
Guido van Rossum667d7041995-08-04 04:20:48 +0000432/* Main program */
433
Eric Snow6b4be192017-05-22 21:36:03 -0700434typedef struct {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100435 wchar_t **argv;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100436 int nwarnoption; /* Number of -W options */
437 wchar_t **warnoptions; /* -W options */
438 int nenv_warnoption; /* Number of PYTHONWARNINGS options */
439 wchar_t **env_warnoptions; /* PYTHONWARNINGS options */
Eric Snow6b4be192017-05-22 21:36:03 -0700440 int print_help; /* -h, -? options */
441 int print_version; /* -V option */
Victor Stinner6bf992a2017-12-06 17:26:10 +0100442 int bytes_warning; /* Py_BytesWarningFlag, -b */
443 int debug; /* Py_DebugFlag, -b, PYTHONDEBUG */
444 int inspect; /* Py_InspectFlag, -i, PYTHONINSPECT */
445 int interactive; /* Py_InteractiveFlag, -i */
446 int isolated; /* Py_IsolatedFlag, -I */
447 int optimization_level; /* Py_OptimizeFlag, -O, PYTHONOPTIMIZE */
448 int dont_write_bytecode; /* Py_DontWriteBytecodeFlag, -B, PYTHONDONTWRITEBYTECODE */
449 int no_user_site_directory; /* Py_NoUserSiteDirectory, -I, -s, PYTHONNOUSERSITE */
450 int no_site_import; /* Py_NoSiteFlag, -S */
451 int use_unbuffered_io; /* Py_UnbufferedStdioFlag, -u, PYTHONUNBUFFERED */
452 int verbosity; /* Py_VerboseFlag, -v, PYTHONVERBOSE */
453 int quiet_flag; /* Py_QuietFlag, -q */
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800454 const char *check_hash_pycs_mode; /* --check-hash-based-pycs */
Victor Stinner6bf992a2017-12-06 17:26:10 +0100455#ifdef MS_WINDOWS
456 int legacy_windows_fs_encoding; /* Py_LegacyWindowsFSEncodingFlag,
457 PYTHONLEGACYWINDOWSFSENCODING */
458 int legacy_windows_stdio; /* Py_LegacyWindowsStdioFlag,
459 PYTHONLEGACYWINDOWSSTDIO */
460#endif
Eric Snow6b4be192017-05-22 21:36:03 -0700461} _Py_CommandLineDetails;
462
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800463/* Structure used by Py_Main() to pass data to subfunctions */
464typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100465 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800466 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100467 int use_bytes_argv;
468 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100469 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100470
471 /* Exit status or "exit code": result of pymain_main() */
472 int status;
473 /* Error message if a function failed */
474 _PyInitError err;
475
Victor Stinner19760862017-12-20 01:41:59 +0100476 /* non-zero is stdin is a TTY or if -i option is used */
477 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100478 int skip_first_line; /* -x option */
479 wchar_t *filename; /* Trailing arg without -c or -m */
480 wchar_t *command; /* -c argument */
481 wchar_t *module; /* -m argument */
Victor Stinner19760862017-12-20 01:41:59 +0100482
Victor Stinner9cfc0022017-12-20 19:36:46 +0100483 _PyCoreConfig config;
Victor Stinner19760862017-12-20 01:41:59 +0100484
485 PyObject *main_importer_path;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800486} _PyMain;
487
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800488#define _PyMain_INIT \
Victor Stinner9cfc0022017-12-20 19:36:46 +0100489 {.config = _PyCoreConfig_INIT, \
Victor Stinnerd5dda982017-12-13 17:31:16 +0100490 .err = _Py_INIT_OK()}
491/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800492
493
Victor Stinner19760862017-12-20 01:41:59 +0100494/* Non-zero if filename, command (-c) or module (-m) is set
495 on the command line */
496#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100497 (pymain->command != NULL || pymain->filename != NULL \
498 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100499
500
Victor Stinnerca719ac2017-12-20 18:00:19 +0100501static wchar_t*
502pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800503{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100504 wchar_t *str2 = _PyMem_RawWcsdup(str);
505 if (str2 == NULL) {
506 pymain->err = _Py_INIT_NO_MEMORY();
507 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800508 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100509 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800510}
511
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100512
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800513static void
Victor Stinnerca719ac2017-12-20 18:00:19 +0100514clear_wstrlist(int len, wchar_t **list)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800515{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100516 for (int i=0; i < len; i++) {
517 PyMem_RawFree(list[i]);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100518 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100519 PyMem_RawFree(list);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100520}
521
522
523static int
Victor Stinnerca719ac2017-12-20 18:00:19 +0100524pymain_init_cmdline_argv(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100525{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100526 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100527
Victor Stinnerca719ac2017-12-20 18:00:19 +0100528 if (pymain->use_bytes_argv) {
529 /* +1 for a the NULL terminator */
530 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
531 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
532 if (argv == NULL) {
533 pymain->err = _Py_INIT_NO_MEMORY();
534 return -1;
535 }
536
537 for (int i = 0; i < pymain->argc; i++) {
538 size_t len;
539 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
540 if (arg == NULL) {
541 clear_wstrlist(i, argv);
542 pymain->err = DECODE_LOCALE_ERR("command line arguments",
543 (Py_ssize_t)len);
544 return -1;
545 }
546 argv[i] = arg;
547 }
548 argv[pymain->argc] = NULL;
549
550 cmdline->argv = argv;
551 }
552 else {
553 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100554 }
555
Victor Stinnerca719ac2017-12-20 18:00:19 +0100556 wchar_t *program;
557 if (pymain->argc >= 1 && cmdline->argv != NULL) {
558 program = cmdline->argv[0];
559 }
560 else {
561 program = L"";
562 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100563 pymain->config.program = pymain_wstrdup(pymain, program);
564 if (pymain->config.program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100565 return -1;
566 }
567
Victor Stinnerc4bca952017-12-19 23:48:17 +0100568 return 0;
569}
570
571
572static void
Victor Stinnerca719ac2017-12-20 18:00:19 +0100573pymain_clear_cmdline(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100574{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100575 PyMemAllocatorEx old_alloc;
576 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100577
Victor Stinnerca719ac2017-12-20 18:00:19 +0100578 clear_wstrlist(cmdline->nwarnoption, cmdline->warnoptions);
579 cmdline->nwarnoption = 0;
580 cmdline->warnoptions = NULL;
581
582 clear_wstrlist(cmdline->nenv_warnoption, cmdline->env_warnoptions);
583 cmdline->nenv_warnoption = 0;
584 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100585
586 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100587 clear_wstrlist(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100588 }
589 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100590
591 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
592}
593
594
595static void
596pymain_clear_pymain(_PyMain *pymain)
597{
598#define CLEAR(ATTR) \
599 do { \
600 PyMem_RawFree(ATTR); \
601 ATTR = NULL; \
602 } while (0)
603
604 CLEAR(pymain->filename);
605 CLEAR(pymain->command);
606 CLEAR(pymain->module);
607#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100608}
609
Victor Stinnerc4bca952017-12-19 23:48:17 +0100610static void
Victor Stinner9cfc0022017-12-20 19:36:46 +0100611pymain_clear_config(_PyMain *pymain)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100612{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100613 /* Clear core config with the memory allocator
614 used by pymain_read_conf() */
615 PyMemAllocatorEx old_alloc;
616 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
617
Victor Stinner9cfc0022017-12-20 19:36:46 +0100618 _PyCoreConfig_Clear(&pymain->config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100619
620 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
621}
622
623
624static void
625pymain_free_python(_PyMain *pymain)
626{
627 Py_CLEAR(pymain->main_importer_path);
Victor Stinnerd4341102017-11-23 00:12:09 +0100628
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800629#ifdef __INSURE__
630 /* Insure++ is a memory analysis tool that aids in discovering
631 * memory leaks and other memory problems. On Python exit, the
632 * interned string dictionaries are flagged as being in use at exit
633 * (which it is). Under normal circumstances, this is fine because
634 * the memory will be automatically reclaimed by the system. Under
635 * memory debugging, it's a huge source of useless noise, so we
636 * trade off slower shutdown for less distraction in the memory
637 * reports. -baw
638 */
639 _Py_ReleaseInternedUnicodeStrings();
640#endif /* __INSURE__ */
641}
642
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100643
644static void
Victor Stinnerc4bca952017-12-19 23:48:17 +0100645pymain_free_raw(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100646{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100647 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100648
Victor Stinnerc4bca952017-12-19 23:48:17 +0100649 /* Free global variables which cannot be freed in Py_Finalize():
650 configuration options set before Py_Initialize() which should
651 remain valid after Py_Finalize(), since
652 Py_Initialize()-Py_Finalize() can be called multiple times. */
653 _PyPathConfig_Clear(&_Py_path_config);
Victor Stinner94540602017-12-16 04:54:22 +0100654
Victor Stinner31e99082017-12-20 23:41:38 +0100655 pymain_clear_config(pymain);
656
Victor Stinnerc4bca952017-12-19 23:48:17 +0100657 /* Force the allocator used by pymain_read_conf() */
658 PyMemAllocatorEx old_alloc;
659 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100660
Victor Stinnerca719ac2017-12-20 18:00:19 +0100661 pymain_clear_pymain(pymain);
662
663 clear_wstrlist(orig_argc, orig_argv);
664 orig_argc = 0;
665 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100666
Victor Stinnerc4bca952017-12-19 23:48:17 +0100667 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100668}
669
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100670
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800671static void
672pymain_free(_PyMain *pymain)
673{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100674 pymain_free_python(pymain);
675 pymain_free_raw(pymain);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800676}
677
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100678
Eric Snow6b4be192017-05-22 21:36:03 -0700679static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800680pymain_run_main_from_importer(_PyMain *pymain)
Guido van Rossum667d7041995-08-04 04:20:48 +0000681{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800682 /* Assume sys_path0 has already been checked by pymain_get_importer(),
683 * so put it in sys.path[0] and import __main__ */
Victor Stinner11a247d2017-12-13 21:05:57 +0100684 PyObject *sys_path = PySys_GetObject("path");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800685 if (sys_path == NULL) {
686 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
687 goto error;
688 }
689
Victor Stinner11a247d2017-12-13 21:05:57 +0100690 if (PyList_Insert(sys_path, 0, pymain->main_importer_path)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800691 goto error;
692 }
693
Victor Stinner11a247d2017-12-13 21:05:57 +0100694 int sts = pymain_run_module(L"__main__", 0);
695 return (sts != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800696
697error:
698 Py_CLEAR(pymain->main_importer_path);
699 PyErr_Print();
700 return 1;
701}
702
703
Victor Stinner9cfc0022017-12-20 19:36:46 +0100704static _PyInitError
705wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800706{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100707 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800708 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100709 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800710 }
711
Victor Stinnerca719ac2017-12-20 18:00:19 +0100712 size_t size = (*len + 1) * sizeof(list[0]);
713 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
714 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800715 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100716 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800717 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100718 list2[*len] = str2;
719 *list = list2;
720 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100721 return _Py_INIT_OK();
722}
723
724
725static int
726pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
727{
728 _PyInitError err = wstrlist_append(len, list, str);
729 if (_Py_INIT_FAILED(err)) {
730 pymain->err = err;
731 return -1;
732 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800733 return 0;
734}
735
736
737/* Parse the command line arguments
738 Return 0 on success.
739 Return 1 if parsing failed.
740 Set pymain->err and return -1 on other errors. */
741static int
Victor Stinnerca719ac2017-12-20 18:00:19 +0100742pymain_parse_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800743{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100744 _PyCoreConfig *config = &pymain->config;
745
Antoine Pitrou86838b02012-02-21 19:03:47 +0100746 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800747 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800748 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100749 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800750 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800751 if (c == EOF) {
752 break;
753 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 /* -c is the last option; following arguments
757 that look like options are left for the
758 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800759 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
760 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
761 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100762 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800763 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800764 }
Victor Stinner58d16832018-05-31 15:09:28 +0200765 memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 command[len - 2] = '\n';
767 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100768 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 break;
770 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 if (c == 'm') {
773 /* -m is the last option; following arguments
774 that look like options are left for the
775 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100776 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
777 if (pymain->module == NULL) {
778 return -1;
779 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 break;
781 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800784 case 0:
785 // Handle long option.
786 assert(longindex == 0); // Only one long option now.
787 if (!wcscmp(_PyOS_optarg, L"always")) {
788 cmdline->check_hash_pycs_mode = "always";
789 } else if (!wcscmp(_PyOS_optarg, L"never")) {
790 cmdline->check_hash_pycs_mode = "never";
791 } else if (!wcscmp(_PyOS_optarg, L"default")) {
792 cmdline->check_hash_pycs_mode = "default";
793 } else {
794 fprintf(stderr, "--check-hash-based-pycs must be one of "
795 "'default', 'always', or 'never'\n");
796 return 1;
797 }
798 break;
799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 case 'b':
Eric Snow6b4be192017-05-22 21:36:03 -0700801 cmdline->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 case 'd':
Eric Snow6b4be192017-05-22 21:36:03 -0700805 cmdline->debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 case 'i':
Eric Snow6b4be192017-05-22 21:36:03 -0700809 cmdline->inspect++;
810 cmdline->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000812
Christian Heimesad73a9c2013-08-10 16:36:18 +0200813 case 'I':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100814 config->ignore_environment++;
Eric Snow6b4be192017-05-22 21:36:03 -0700815 cmdline->isolated++;
816 cmdline->no_user_site_directory++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200817 break;
818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 case 'O':
Eric Snow6b4be192017-05-22 21:36:03 -0700822 cmdline->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 case 'B':
Eric Snow6b4be192017-05-22 21:36:03 -0700826 cmdline->dont_write_bytecode++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 case 's':
Eric Snow6b4be192017-05-22 21:36:03 -0700830 cmdline->no_user_site_directory++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 case 'S':
Eric Snow6b4be192017-05-22 21:36:03 -0700834 cmdline->no_site_import++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 case 'E':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100838 config->ignore_environment++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 case 't':
842 /* ignored for backwards compatibility */
843 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 case 'u':
Eric Snow6b4be192017-05-22 21:36:03 -0700846 cmdline->use_unbuffered_io = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 case 'v':
Eric Snow6b4be192017-05-22 21:36:03 -0700850 cmdline->verbosity++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100854 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 case 'h':
858 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -0700859 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -0700863 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100867 if (pymain_wstrlist_append(pymain,
868 &cmdline->nwarnoption,
869 &cmdline->warnoptions,
870 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800871 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800872 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000874
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000875 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100876 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +0100877 &config->nxoption,
878 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +0100879 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800880 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800881 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000882 break;
883
Georg Brandl9d871192010-12-04 10:47:18 +0000884 case 'q':
Eric Snow6b4be192017-05-22 21:36:03 -0700885 cmdline->quiet_flag++;
Georg Brandl9d871192010-12-04 10:47:18 +0000886 break;
887
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100888 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100889 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100890 break;
891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800895 /* unknown argument: parsing failed */
896 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800898 } while (1);
899
Victor Stinnerca719ac2017-12-20 18:00:19 +0100900 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800901 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +0100902 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800903 {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100904 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
905 if (pymain->filename == NULL) {
906 return -1;
907 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000909
Victor Stinnerd5dda982017-12-13 17:31:16 +0100910 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100911 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +0100912
Eric Snow6b4be192017-05-22 21:36:03 -0700913 return 0;
914}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000915
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800916
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800917static int
Victor Stinner9cfc0022017-12-20 19:36:46 +0100918add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +0100919{
920 PyObject *name, *value;
921
922 const wchar_t *name_end = wcschr(s, L'=');
923 if (!name_end) {
924 name = PyUnicode_FromWideChar(s, -1);
925 value = Py_True;
926 Py_INCREF(value);
927 }
928 else {
929 name = PyUnicode_FromWideChar(s, name_end - s);
930 value = PyUnicode_FromWideChar(name_end + 1, -1);
931 }
932 if (name == NULL || value == NULL) {
933 goto error;
934 }
935 if (PyDict_SetItem(opts, name, value) < 0) {
936 goto error;
937 }
938 Py_DECREF(name);
939 Py_DECREF(value);
940 return 0;
941
942error:
943 Py_XDECREF(name);
944 Py_XDECREF(value);
945 return -1;
946}
947
Victor Stinner9cfc0022017-12-20 19:36:46 +0100948
949static PyObject*
950config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800951{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100952 int nxoption = config->nxoption;
953 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +0100954 PyObject *dict = PyDict_New();
955 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100956 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +0100957 }
958
Victor Stinnerca719ac2017-12-20 18:00:19 +0100959 for (int i=0; i < nxoption; i++) {
960 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +0100961 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +0100962 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100963 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800964 }
965 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100966
Victor Stinner9cfc0022017-12-20 19:36:46 +0100967 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -0700968}
969
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800970
Victor Stinner9cfc0022017-12-20 19:36:46 +0100971static _PyInitError
972config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -0700973{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100974 for (int i = 0; i < len; i++) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100975 _PyInitError err = wstrlist_append(&config->nwarnoption,
976 &config->warnoptions,
977 options[i]);
978 if (_Py_INIT_FAILED(err)) {
979 return err;
Eric Snow6b4be192017-05-22 21:36:03 -0700980 }
Eric Snow6b4be192017-05-22 21:36:03 -0700981 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100982 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800983}
Eric Snow6b4be192017-05-22 21:36:03 -0700984
Victor Stinner747f48e2017-12-12 22:59:48 +0100985
Victor Stinner9cfc0022017-12-20 19:36:46 +0100986static _PyInitError
987config_init_warnoptions(_PyCoreConfig *config, _Py_CommandLineDetails *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +0100988{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100989 _PyInitError err;
990
991 assert(config->nwarnoption == 0);
992
Victor Stinner747f48e2017-12-12 22:59:48 +0100993 /* The priority order for warnings configuration is (highest precedence
994 * first):
995 *
996 * - the BytesWarning filter, if needed ('-b', '-bb')
997 * - any '-W' command line options; then
998 * - the 'PYTHONWARNINGS' environment variable; then
999 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
1000 * - any implicit filters added by _warnings.c/warnings.py
1001 *
1002 * All settings except the last are passed to the warnings module via
1003 * the `sys.warnoptions` list. Since the warnings module works on the basis
1004 * of "the most recently added filter will be checked first", we add
1005 * the lowest precedence entries first so that later entries override them.
1006 */
1007
Victor Stinner9cfc0022017-12-20 19:36:46 +01001008 if (config->dev_mode) {
1009 err = wstrlist_append(&config->nwarnoption,
1010 &config->warnoptions,
1011 L"default");
1012 if (_Py_INIT_FAILED(err)) {
1013 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001014 }
Victor Stinner747f48e2017-12-12 22:59:48 +01001015 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001016
Victor Stinner9cfc0022017-12-20 19:36:46 +01001017 err = config_add_warnings_optlist(config,
1018 cmdline->nenv_warnoption,
1019 cmdline->env_warnoptions);
1020 if (_Py_INIT_FAILED(err)) {
1021 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001022 }
1023
Victor Stinner9cfc0022017-12-20 19:36:46 +01001024 err = config_add_warnings_optlist(config,
1025 cmdline->nwarnoption,
1026 cmdline->warnoptions);
1027 if (_Py_INIT_FAILED(err)) {
1028 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001029 }
1030
1031 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
1032 * don't even try to emit a warning, so we skip setting the filter in that
1033 * case.
1034 */
1035 if (cmdline->bytes_warning) {
1036 wchar_t *filter;
1037 if (cmdline->bytes_warning> 1) {
1038 filter = L"error::BytesWarning";
1039 }
1040 else {
1041 filter = L"default::BytesWarning";
1042 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001043 err = wstrlist_append(&config->nwarnoption,
1044 &config->warnoptions,
1045 filter);
1046 if (_Py_INIT_FAILED(err)) {
1047 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001048 }
1049 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001050 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001051}
1052
1053
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001054/* Get warning options from PYTHONWARNINGS environment variable.
1055 Return 0 on success.
1056 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001057static _PyInitError
1058cmdline_init_env_warnoptions(_Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001059{
1060 if (Py_IgnoreEnvironmentFlag) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001061 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +00001063
Victor Stinnerca719ac2017-12-20 18:00:19 +01001064 wchar_t *env;
1065 int res = config_get_env_var_dup(&env, L"PYTHONWARNINGS", "PYTHONWARNINGS");
1066 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001067 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001068 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001069
Victor Stinnerca719ac2017-12-20 18:00:19 +01001070 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001071 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001072 }
Philip Jenvey0805ca32010-04-07 04:04:10 +00001073
Victor Stinnerca719ac2017-12-20 18:00:19 +01001074
1075 wchar_t *warning, *context = NULL;
1076 for (warning = WCSTOK(env, L",", &context);
1077 warning != NULL;
1078 warning = WCSTOK(NULL, L",", &context))
1079 {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001080 _PyInitError err = wstrlist_append(&cmdline->nenv_warnoption,
1081 &cmdline->env_warnoptions,
1082 warning);
1083 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001084 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001085 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001086 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001088 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001089 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001090}
1091
1092
1093static void
1094pymain_init_stdio(_PyMain *pymain)
1095{
1096 pymain->stdin_is_interactive = (isatty(fileno(stdin))
1097 || Py_InteractiveFlag);
Guido van Rossum775af911997-02-14 19:50:32 +00001098
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +00001099#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +00001100 /* don't translate newlines (\r\n <=> \n) */
1101 _setmode(fileno(stdin), O_BINARY);
1102 _setmode(fileno(stdout), O_BINARY);
1103 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +00001104#endif
Victor Stinner89e34362011-01-07 18:47:22 +00001105
1106 if (Py_UnbufferedStdioFlag) {
Guido van Rossum22ffac11998-03-06 15:30:39 +00001107#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
1109 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
1110 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001111#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 setbuf(stdin, (char *)NULL);
1113 setbuf(stdout, (char *)NULL);
1114 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001115#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 }
1117 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +00001118#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 /* Doesn't have to have line-buffered -- use unbuffered */
1120 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
1121 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001122#else /* !MS_WINDOWS */
1123#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
1125 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001126#endif /* HAVE_SETVBUF */
1127#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 /* Leave stderr alone - it should be unbuffered anyway. */
1129 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001130}
Guido van Rossum667d7041995-08-04 04:20:48 +00001131
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001132
1133/* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
Victor Stinner31a83932017-12-04 13:39:15 +01001134 environment variables on macOS if available. */
1135static _PyInitError
Victor Stinnerca719ac2017-12-20 18:00:19 +01001136config_init_program_name(_PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001137{
Victor Stinner31a83932017-12-04 13:39:15 +01001138 assert(config->program_name == NULL);
1139
1140 /* If Py_SetProgramName() was called, use its value */
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001141 const wchar_t *program_name = _Py_path_config.program_name;
Victor Stinner31a83932017-12-04 13:39:15 +01001142 if (program_name != NULL) {
1143 config->program_name = _PyMem_RawWcsdup(program_name);
1144 if (config->program_name == NULL) {
1145 return _Py_INIT_NO_MEMORY();
1146 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001147 return _Py_INIT_OK();
Victor Stinner31a83932017-12-04 13:39:15 +01001148 }
1149
Just van Rossum2ac79ef2003-03-05 15:46:54 +00001150#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 /* On MacOS X, when the Python interpreter is embedded in an
1152 application bundle, it gets executed by a bootstrapping script
1153 that does os.execve() with an argv[0] that's different from the
1154 actual Python executable. This is needed to keep the Finder happy,
1155 or rather, to work around Apple's overly strict requirements of
1156 the process name. However, we still need a usable sys.executable,
1157 so the actual executable path is passed in an environment variable.
1158 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
1159 script. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001160 const char *p = config_get_env_var("PYTHONEXECUTABLE");
Victor Stinner6bf992a2017-12-06 17:26:10 +01001161 if (p != NULL) {
Victor Stinner31a83932017-12-04 13:39:15 +01001162 size_t len;
1163 wchar_t* program_name = Py_DecodeLocale(p, &len);
1164 if (program_name == NULL) {
1165 return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment "
1166 "variable", (Py_ssize_t)len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 }
Victor Stinner31a83932017-12-04 13:39:15 +01001168 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001169 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001170 }
Vinay Sajip90db6612012-07-17 17:33:46 +01001171#ifdef WITH_NEXT_FRAMEWORK
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001172 else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001173 const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
Vinay Sajip90db6612012-07-17 17:33:46 +01001174 if (pyvenv_launcher && *pyvenv_launcher) {
1175 /* Used by Mac/Tools/pythonw.c to forward
1176 * the argv0 of the stub executable
1177 */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001178 size_t len;
Victor Stinner31a83932017-12-04 13:39:15 +01001179 wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len);
1180 if (program_name == NULL) {
1181 return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment "
1182 "variable", (Py_ssize_t)len);
Vinay Sajip90db6612012-07-17 17:33:46 +01001183 }
Victor Stinner31a83932017-12-04 13:39:15 +01001184 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001185 return _Py_INIT_OK();
Vinay Sajip90db6612012-07-17 17:33:46 +01001186 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001188#endif /* WITH_NEXT_FRAMEWORK */
1189#endif /* __APPLE__ */
Victor Stinneraf5a8952017-12-02 10:11:32 +01001190
Victor Stinnerca719ac2017-12-20 18:00:19 +01001191 /* Use argv[0] by default, if available */
1192 if (config->program != NULL) {
1193 config->program_name = _PyMem_RawWcsdup(config->program);
1194 if (config->program_name == NULL) {
1195 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001196 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001197 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001198 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001199
1200 /* Last fall back: hardcoded string */
1201#ifdef MS_WINDOWS
1202 const wchar_t *default_program_name = L"python";
1203#else
1204 const wchar_t *default_program_name = L"python3";
1205#endif
1206 config->program_name = _PyMem_RawWcsdup(default_program_name);
1207 if (config->program_name == NULL) {
1208 return _Py_INIT_NO_MEMORY();
1209 }
1210 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001211}
1212
1213
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001214static void
1215pymain_header(_PyMain *pymain)
1216{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001217 if (Py_QuietFlag) {
1218 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 }
Guido van Rossum393661d2001-08-31 17:40:15 +00001220
Victor Stinner19760862017-12-20 01:41:59 +01001221 if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001222 return;
1223 }
1224
1225 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
1226 if (!Py_NoSiteFlag) {
1227 fprintf(stderr, "%s\n", COPYRIGHT);
1228 }
1229}
1230
1231
Victor Stinnerc4bca952017-12-19 23:48:17 +01001232static wchar_t**
Victor Stinnerca719ac2017-12-20 18:00:19 +01001233copy_wstrlist(int len, wchar_t **list)
Victor Stinner11a247d2017-12-13 21:05:57 +01001234{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001235 assert((len > 0 && list != NULL) || len == 0);
1236 size_t size = len * sizeof(list[0]);
1237 wchar_t **list_copy = PyMem_RawMalloc(size);
1238 for (int i=0; i < len; i++) {
1239 wchar_t* arg = _PyMem_RawWcsdup(list[i]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001240 if (arg == NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001241 clear_wstrlist(i, list);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001242 return NULL;
1243 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001244 list_copy[i] = arg;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001245 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001246 return list_copy;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001247}
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001248
Victor Stinnerc4bca952017-12-19 23:48:17 +01001249
1250static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001251pymain_init_core_argv(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001252{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001253 /* Copy argv to be able to modify it (to force -c/-m) */
1254 int argc = pymain->argc - _PyOS_optind;
1255 wchar_t **argv;
1256
1257 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +01001258 /* Ensure at least one (empty) argument is seen */
1259 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +01001260 argc = 1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001261 argv = copy_wstrlist(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +01001262 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001263 else {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001264 argv = copy_wstrlist(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001265 }
1266
1267 if (argv == NULL) {
1268 pymain->err = _Py_INIT_NO_MEMORY();
1269 return -1;
1270 }
1271
1272 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001273 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001274 /* Force sys.argv[0] = '-c' */
1275 arg0 = L"-c";
1276 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001277 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001278 /* Force sys.argv[0] = '-m'*/
1279 arg0 = L"-m";
1280 }
1281 if (arg0 != NULL) {
1282 arg0 = _PyMem_RawWcsdup(arg0);
1283 if (arg0 == NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001284 clear_wstrlist(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001285 pymain->err = _Py_INIT_NO_MEMORY();
1286 return -1;
1287 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001288
1289 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001290 PyMem_RawFree(argv[0]);
1291 argv[0] = arg0;
1292 }
1293
Victor Stinner9cfc0022017-12-20 19:36:46 +01001294 pymain->config.argc = argc;
1295 pymain->config.argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001296 return 0;
1297}
1298
1299
Victor Stinner8ded5b82018-01-24 17:03:28 +01001300static PyObject*
1301wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001302{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001303 assert(list != NULL || len < 1);
1304
1305 PyObject *pylist = PyList_New(len);
1306 if (pylist == NULL) {
1307 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001308 }
1309
Victor Stinner8ded5b82018-01-24 17:03:28 +01001310 for (int i = 0; i < len; i++) {
1311 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001312 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001313 Py_DECREF(pylist);
1314 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001315 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001316 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001317 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001318 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001319}
1320
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001321
Victor Stinner11a247d2017-12-13 21:05:57 +01001322static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01001323pymain_compute_path0(_PyMain *pymain, PyObject **path0)
Victor Stinner11a247d2017-12-13 21:05:57 +01001324{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001325 if (pymain->main_importer_path != NULL) {
1326 /* Let pymain_run_main_from_importer() adjust sys.path[0] later */
Victor Stinner19760862017-12-20 01:41:59 +01001327 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001328 return 0;
1329 }
1330
1331 if (Py_IsolatedFlag) {
Victor Stinner19760862017-12-20 01:41:59 +01001332 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001333 return 0;
1334 }
1335
Victor Stinner9cfc0022017-12-20 19:36:46 +01001336 *path0 = _PyPathConfig_ComputeArgv0(pymain->config.argc,
1337 pymain->config.argv);
Victor Stinner19760862017-12-20 01:41:59 +01001338 if (*path0 == NULL) {
1339 pymain->err = _Py_INIT_NO_MEMORY();
1340 return -1;
1341 }
1342 return 0;
1343}
1344
1345
1346static int
1347pymain_update_sys_path(_PyMain *pymain, PyObject *path0)
1348{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001349 /* Prepend argv[0] to sys.path.
1350 If argv[0] is a symlink, use the real path. */
1351 PyObject *sys_path = PySys_GetObject("path");
1352 if (sys_path == NULL) {
1353 pymain->err = _Py_INIT_ERR("can't get sys.path");
Victor Stinnerd5dda982017-12-13 17:31:16 +01001354 return -1;
1355 }
1356
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001357 /* Prepend path0 to sys.path */
1358 if (PyList_Insert(sys_path, 0, path0) < 0) {
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001359 pymain->err = _Py_INIT_ERR("sys.path.insert(0, path0) failed");
1360 return -1;
1361 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001362 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001363}
1364
1365
Victor Stinner6bf992a2017-12-06 17:26:10 +01001366/* Get Py_xxx global configuration variables */
1367static void
Victor Stinnerca719ac2017-12-20 18:00:19 +01001368pymain_get_global_config(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinner6bf992a2017-12-06 17:26:10 +01001369{
Victor Stinner91106cd2017-12-13 12:29:09 +01001370 cmdline->bytes_warning = Py_BytesWarningFlag;
1371 cmdline->debug = Py_DebugFlag;
1372 cmdline->inspect = Py_InspectFlag;
1373 cmdline->interactive = Py_InteractiveFlag;
1374 cmdline->isolated = Py_IsolatedFlag;
1375 cmdline->optimization_level = Py_OptimizeFlag;
1376 cmdline->dont_write_bytecode = Py_DontWriteBytecodeFlag;
1377 cmdline->no_user_site_directory = Py_NoUserSiteDirectory;
1378 cmdline->no_site_import = Py_NoSiteFlag;
1379 cmdline->use_unbuffered_io = Py_UnbufferedStdioFlag;
1380 cmdline->verbosity = Py_VerboseFlag;
1381 cmdline->quiet_flag = Py_QuietFlag;
1382#ifdef MS_WINDOWS
1383 cmdline->legacy_windows_fs_encoding = Py_LegacyWindowsFSEncodingFlag;
1384 cmdline->legacy_windows_stdio = Py_LegacyWindowsStdioFlag;
1385#endif
1386 cmdline->check_hash_pycs_mode = _Py_CheckHashBasedPycsMode ;
1387
Victor Stinner9cfc0022017-12-20 19:36:46 +01001388 pymain->config.ignore_environment = Py_IgnoreEnvironmentFlag;
1389 pymain->config.utf8_mode = Py_UTF8Mode;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001390}
1391
1392
Victor Stinner19760862017-12-20 01:41:59 +01001393/* Set Py_xxx global configuration variables */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001394static void
Victor Stinnerca719ac2017-12-20 18:00:19 +01001395pymain_set_global_config(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001396{
Victor Stinner91106cd2017-12-13 12:29:09 +01001397 Py_BytesWarningFlag = cmdline->bytes_warning;
1398 Py_DebugFlag = cmdline->debug;
1399 Py_InspectFlag = cmdline->inspect;
1400 Py_InteractiveFlag = cmdline->interactive;
1401 Py_IsolatedFlag = cmdline->isolated;
1402 Py_OptimizeFlag = cmdline->optimization_level;
1403 Py_DontWriteBytecodeFlag = cmdline->dont_write_bytecode;
1404 Py_NoUserSiteDirectory = cmdline->no_user_site_directory;
1405 Py_NoSiteFlag = cmdline->no_site_import;
1406 Py_UnbufferedStdioFlag = cmdline->use_unbuffered_io;
1407 Py_VerboseFlag = cmdline->verbosity;
1408 Py_QuietFlag = cmdline->quiet_flag;
1409 _Py_CheckHashBasedPycsMode = cmdline->check_hash_pycs_mode;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001410#ifdef MS_WINDOWS
Victor Stinner91106cd2017-12-13 12:29:09 +01001411 Py_LegacyWindowsFSEncodingFlag = cmdline->legacy_windows_fs_encoding;
1412 Py_LegacyWindowsStdioFlag = cmdline->legacy_windows_stdio;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001413#endif
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001414
Victor Stinner9cfc0022017-12-20 19:36:46 +01001415 Py_IgnoreEnvironmentFlag = pymain->config.ignore_environment;
1416 Py_UTF8Mode = pymain->config.utf8_mode;
Victor Stinner358e5e12017-12-15 00:51:22 +01001417
1418 /* Random or non-zero hash seed */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001419 Py_HashRandomizationFlag = (pymain->config.use_hash_seed == 0 ||
1420 pymain->config.hash_seed != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001421}
1422
1423
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001424static void
1425pymain_import_readline(_PyMain *pymain)
1426{
1427 if (Py_IsolatedFlag) {
1428 return;
1429 }
Victor Stinner19760862017-12-20 01:41:59 +01001430 if (!Py_InspectFlag && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001431 return;
1432 }
1433 if (!isatty(fileno(stdin))) {
1434 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001435 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001436
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001437 PyObject *mod = PyImport_ImportModule("readline");
1438 if (mod == NULL) {
1439 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 }
1441 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001442 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001444}
1445
1446
1447static FILE*
1448pymain_open_filename(_PyMain *pymain)
1449{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001450 FILE* fp;
1451
Victor Stinnerca719ac2017-12-20 18:00:19 +01001452 fp = _Py_wfopen(pymain->filename, L"r");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001453 if (fp == NULL) {
1454 char *cfilename_buffer;
1455 const char *cfilename;
1456 int err = errno;
Victor Stinner9dd76202017-12-21 16:20:32 +01001457 cfilename_buffer = _Py_EncodeLocaleRaw(pymain->filename, NULL);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001458 if (cfilename_buffer != NULL)
1459 cfilename = cfilename_buffer;
1460 else
1461 cfilename = "<unprintable file name>";
1462 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinner9cfc0022017-12-20 19:36:46 +01001463 pymain->config.program, cfilename, err, strerror(err));
Victor Stinner9dd76202017-12-21 16:20:32 +01001464 PyMem_RawFree(cfilename_buffer);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001465 pymain->status = 2;
1466 return NULL;
1467 }
1468
Victor Stinnerca719ac2017-12-20 18:00:19 +01001469 if (pymain->skip_first_line) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001470 int ch;
1471 /* Push back first newline so line numbers
1472 remain the same */
1473 while ((ch = getc(fp)) != EOF) {
1474 if (ch == '\n') {
1475 (void)ungetc(ch, fp);
1476 break;
1477 }
1478 }
1479 }
1480
1481 struct _Py_stat_struct sb;
1482 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 &&
1483 S_ISDIR(sb.st_mode)) {
1484 fprintf(stderr,
1485 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinner9cfc0022017-12-20 19:36:46 +01001486 pymain->config.program, pymain->filename);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001487 fclose(fp);
1488 pymain->status = 1;
1489 return NULL;
1490 }
1491
1492 return fp;
1493}
1494
1495
1496static void
Victor Stinner19760862017-12-20 01:41:59 +01001497pymain_run_filename(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001498{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001499 if (pymain->filename == NULL && pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001500 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner19760862017-12-20 01:41:59 +01001501 pymain_run_startup(cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001502 pymain_run_interactive_hook();
1503 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001504
1505 if (pymain->main_importer_path != NULL) {
1506 pymain->status = pymain_run_main_from_importer(pymain);
1507 return;
1508 }
1509
1510 FILE *fp;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001511 if (pymain->filename != NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001512 fp = pymain_open_filename(pymain);
1513 if (fp == NULL) {
1514 return;
1515 }
1516 }
1517 else {
1518 fp = stdin;
1519 }
1520
Victor Stinnerca719ac2017-12-20 18:00:19 +01001521 pymain->status = pymain_run_file(fp, pymain->filename, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001522}
1523
1524
1525static void
Victor Stinner19760862017-12-20 01:41:59 +01001526pymain_repl(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001529 opportunity to set it from Python. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001530 if (!Py_InspectFlag && config_get_env_var("PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 Py_InspectFlag = 1;
1532 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001533
Victor Stinner19760862017-12-20 01:41:59 +01001534 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001535 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001537
1538 Py_InspectFlag = 0;
1539 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001540
Victor Stinner19760862017-12-20 01:41:59 +01001541 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001542 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001543}
1544
1545
1546/* Parse the command line.
1547 Handle --version and --help options directly.
1548
1549 Return 1 if Python must exit.
1550 Return 0 on success.
1551 Set pymain->err and return -1 on failure. */
1552static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001553pymain_parse_cmdline(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001554{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001555 int res = pymain_parse_cmdline_impl(pymain, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001556 if (res < 0) {
1557 return -1;
1558 }
1559 if (res) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001560 pymain_usage(1, pymain->config.program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001561 pymain->status = 2;
1562 return 1;
1563 }
1564
Victor Stinnerca719ac2017-12-20 18:00:19 +01001565 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001566 /* Backup _PyOS_optind */
1567 _PyOS_optind--;
1568 }
1569
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001570 return 0;
1571}
1572
1573
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001574static const wchar_t*
Victor Stinner9cfc0022017-12-20 19:36:46 +01001575config_get_xoption(_PyCoreConfig *config, wchar_t *name)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001576{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001577 int nxoption = config->nxoption;
1578 wchar_t **xoptions = config->xoptions;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001579 for (int i=0; i < nxoption; i++) {
1580 wchar_t *option = xoptions[i];
Victor Stinnera7368ac2017-11-15 18:11:45 -08001581 size_t len;
1582 wchar_t *sep = wcschr(option, L'=');
1583 if (sep != NULL) {
1584 len = (sep - option);
1585 }
1586 else {
1587 len = wcslen(option);
1588 }
1589 if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
1590 return option;
1591 }
1592 }
1593 return NULL;
1594}
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001595
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001596
Victor Stinnera7368ac2017-11-15 18:11:45 -08001597static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001598pymain_str_to_int(const char *str, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001599{
1600 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001601 const char *endptr = str;
1602 long value = strtol(str, (char **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001603 if (*endptr != '\0' || errno == ERANGE) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001604 return -1;
1605 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001606 if (value < INT_MIN || value > INT_MAX) {
1607 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001608 }
1609
Victor Stinnera7368ac2017-11-15 18:11:45 -08001610 *result = (int)value;
1611 return 0;
1612}
1613
1614
1615static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001616pymain_wstr_to_int(const wchar_t *wstr, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001617{
1618 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001619 const wchar_t *endptr = wstr;
1620 long value = wcstol(wstr, (wchar_t **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001621 if (*endptr != '\0' || errno == ERANGE) {
1622 return -1;
1623 }
1624 if (value < INT_MIN || value > INT_MAX) {
1625 return -1;
1626 }
1627
1628 *result = (int)value;
1629 return 0;
1630}
1631
1632
Victor Stinner9cfc0022017-12-20 19:36:46 +01001633static _PyInitError
1634pymain_init_tracemalloc(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001635{
1636 int nframe;
1637 int valid;
1638
Victor Stinner9cfc0022017-12-20 19:36:46 +01001639 const char *env = config_get_env_var("PYTHONTRACEMALLOC");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001640 if (env) {
1641 if (!pymain_str_to_int(env, &nframe)) {
1642 valid = (nframe >= 1);
1643 }
1644 else {
1645 valid = 0;
1646 }
1647 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001648 return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number "
1649 "of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001650 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001651 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001652 }
1653
Victor Stinner9cfc0022017-12-20 19:36:46 +01001654 const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001655 if (xoption) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001656 const wchar_t *sep = wcschr(xoption, L'=');
Victor Stinnera7368ac2017-11-15 18:11:45 -08001657 if (sep) {
1658 if (!pymain_wstr_to_int(sep + 1, &nframe)) {
1659 valid = (nframe >= 1);
1660 }
1661 else {
1662 valid = 0;
1663 }
1664 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001665 return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: "
1666 "invalid number of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001667 }
1668 }
1669 else {
1670 /* -X tracemalloc behaves as -X tracemalloc=1 */
1671 nframe = 1;
1672 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001673 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001674 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001675 return _Py_INIT_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001676}
1677
1678
Carl Meyerb193fa92018-06-15 22:40:56 -06001679static _PyInitError
1680pymain_init_pycache_prefix(_PyCoreConfig *config)
1681{
1682 wchar_t *env;
1683
1684 int res = config_get_env_var_dup(
1685 &env, L"PYTHONPYCACHEPREFIX", "PYTHONPYCACHEPREFIX");
1686 if (res < 0) {
1687 return DECODE_LOCALE_ERR("PYTHONPYCACHEPREFIX", res);
1688 } else if (env) {
1689 config->pycache_prefix = env;
1690 }
1691
1692 const wchar_t *xoption = config_get_xoption(config, L"pycache_prefix");
1693 if (xoption) {
1694 const wchar_t *sep = wcschr(xoption, L'=');
1695 if (sep && wcslen(sep) > 1) {
1696 config->pycache_prefix = _PyMem_RawWcsdup(sep + 1);
1697 if (config->pycache_prefix == NULL) {
1698 return _Py_INIT_NO_MEMORY();
1699 }
1700 } else {
1701 // -X pycache_prefix= can cancel the env var
1702 config->pycache_prefix = NULL;
1703 }
1704 }
1705
1706 return _Py_INIT_OK();
1707}
1708
1709
Victor Stinnera7368ac2017-11-15 18:11:45 -08001710static void
Victor Stinner9cfc0022017-12-20 19:36:46 +01001711get_env_flag(int *flag, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001712{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001713 const char *var = config_get_env_var(name);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001714 if (!var) {
1715 return;
1716 }
1717 int value;
1718 if (pymain_str_to_int(var, &value) < 0 || value < 0) {
1719 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
1720 value = 1;
1721 }
1722 if (*flag < value) {
1723 *flag = value;
1724 }
1725}
1726
1727
1728static void
Victor Stinner9cfc0022017-12-20 19:36:46 +01001729cmdline_get_env_flags(_Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001730{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001731 get_env_flag(&cmdline->debug, "PYTHONDEBUG");
1732 get_env_flag(&cmdline->verbosity, "PYTHONVERBOSE");
1733 get_env_flag(&cmdline->optimization_level, "PYTHONOPTIMIZE");
1734 get_env_flag(&cmdline->inspect, "PYTHONINSPECT");
1735 get_env_flag(&cmdline->dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
1736 get_env_flag(&cmdline->no_user_site_directory, "PYTHONNOUSERSITE");
1737 get_env_flag(&cmdline->use_unbuffered_io, "PYTHONUNBUFFERED");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001738#ifdef MS_WINDOWS
Victor Stinner9cfc0022017-12-20 19:36:46 +01001739 get_env_flag(&cmdline->legacy_windows_fs_encoding,
1740 "PYTHONLEGACYWINDOWSFSENCODING");
1741 get_env_flag(&cmdline->legacy_windows_stdio,
1742 "PYTHONLEGACYWINDOWSSTDIO");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001743#endif
1744}
1745
1746
Victor Stinner46972b72017-11-24 22:55:40 +01001747static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001748config_init_home(_PyCoreConfig *config)
Victor Stinner1f151112017-11-23 10:43:14 +01001749{
1750 wchar_t *home;
1751
Victor Stinner31a83932017-12-04 13:39:15 +01001752 /* If Py_SetPythonHome() was called, use its value */
1753 home = _Py_path_config.home;
Victor Stinner1f151112017-11-23 10:43:14 +01001754 if (home) {
Victor Stinner46972b72017-11-24 22:55:40 +01001755 config->home = _PyMem_RawWcsdup(home);
1756 if (config->home == NULL) {
1757 return _Py_INIT_NO_MEMORY();
Victor Stinner1f151112017-11-23 10:43:14 +01001758 }
Victor Stinner46972b72017-11-24 22:55:40 +01001759 return _Py_INIT_OK();
Victor Stinner1f151112017-11-23 10:43:14 +01001760 }
1761
Victor Stinner46972b72017-11-24 22:55:40 +01001762 int res = config_get_env_var_dup(&home, L"PYTHONHOME", "PYTHONHOME");
Victor Stinner1f151112017-11-23 10:43:14 +01001763 if (res < 0) {
Victor Stinner46972b72017-11-24 22:55:40 +01001764 return DECODE_LOCALE_ERR("PYTHONHOME", res);
Victor Stinner1f151112017-11-23 10:43:14 +01001765 }
Victor Stinner46972b72017-11-24 22:55:40 +01001766 config->home = home;
1767 return _Py_INIT_OK();
Victor Stinnerd4341102017-11-23 00:12:09 +01001768}
1769
1770
Victor Stinner358e5e12017-12-15 00:51:22 +01001771static _PyInitError
1772config_init_hash_seed(_PyCoreConfig *config)
1773{
1774 if (config->use_hash_seed < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001775 const char *seed_text = config_get_env_var("PYTHONHASHSEED");
Victor Stinner358e5e12017-12-15 00:51:22 +01001776 int use_hash_seed;
1777 unsigned long hash_seed;
1778 if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
1779 return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
1780 "or an integer in range [0; 4294967295]");
1781 }
1782 config->use_hash_seed = use_hash_seed;
1783 config->hash_seed = hash_seed;
1784 }
1785 return _Py_INIT_OK();
1786}
1787
1788
Victor Stinner9cfc0022017-12-20 19:36:46 +01001789static _PyInitError
1790config_init_utf8_mode(_PyCoreConfig *config)
Victor Stinner91106cd2017-12-13 12:29:09 +01001791{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001792 /* The option was already set by Py_UTF8Mode,
1793 Py_LegacyWindowsFSEncodingFlag or PYTHONLEGACYWINDOWSFSENCODING. */
1794 if (config->utf8_mode >= 0) {
1795 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001796 }
Victor Stinner91106cd2017-12-13 12:29:09 +01001797
Victor Stinner9cfc0022017-12-20 19:36:46 +01001798 const wchar_t *xopt = config_get_xoption(config, L"utf8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001799 if (xopt) {
1800 wchar_t *sep = wcschr(xopt, L'=');
1801 if (sep) {
1802 xopt = sep + 1;
1803 if (wcscmp(xopt, L"1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001804 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001805 }
1806 else if (wcscmp(xopt, L"0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001807 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001808 }
1809 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001810 return _Py_INIT_USER_ERR("invalid -X utf8 option value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001811 }
1812 }
1813 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001814 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001815 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001816 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001817 }
1818
Victor Stinner9cfc0022017-12-20 19:36:46 +01001819 const char *opt = config_get_env_var("PYTHONUTF8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001820 if (opt) {
1821 if (strcmp(opt, "1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001822 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001823 }
1824 else if (strcmp(opt, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001825 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001826 }
1827 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001828 return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
1829 "variable value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001830 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001831 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001832 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001833
1834 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001835}
Victor Stinner46972b72017-11-24 22:55:40 +01001836
1837
Victor Stinner9cfc0022017-12-20 19:36:46 +01001838static _PyInitError
1839config_read_env_vars(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001840{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001841 config->allocator = config_get_env_var("PYTHONMALLOC");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001842
Victor Stinner9cfc0022017-12-20 19:36:46 +01001843 if (config_get_env_var("PYTHONDUMPREFS")) {
1844 config->dump_refs = 1;
1845 }
1846 if (config_get_env_var("PYTHONMALLOCSTATS")) {
1847 config->malloc_stats = 1;
Victor Stinner31a83932017-12-04 13:39:15 +01001848 }
1849
Victor Stinner9cfc0022017-12-20 19:36:46 +01001850 const char *env = config_get_env_var("PYTHONCOERCECLOCALE");
Victor Stinner94540602017-12-16 04:54:22 +01001851 if (env) {
1852 if (strcmp(env, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001853 config->coerce_c_locale = 0;
Victor Stinner94540602017-12-16 04:54:22 +01001854 }
1855 else if (strcmp(env, "warn") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001856 config->coerce_c_locale_warn = 1;
Victor Stinner94540602017-12-16 04:54:22 +01001857 }
1858 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001859 config->coerce_c_locale = 1;
Victor Stinner94540602017-12-16 04:54:22 +01001860 }
1861 }
1862
Victor Stinner9cfc0022017-12-20 19:36:46 +01001863 wchar_t *path;
1864 int res = config_get_env_var_dup(&path, L"PYTHONPATH", "PYTHONPATH");
1865 if (res < 0) {
Carl Meyer48575432018-05-19 16:48:22 -06001866 return DECODE_LOCALE_ERR("PYTHONPATH", res);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001867 }
1868 config->module_search_path_env = path;
1869
1870 _PyInitError err = config_init_hash_seed(config);
1871 if (_Py_INIT_FAILED(err)) {
1872 return err;
1873 }
1874
1875 return _Py_INIT_OK();
1876}
1877
1878
1879static _PyInitError
1880config_read_complex_options(_PyCoreConfig *config)
1881{
1882 /* More complex options configured by env var and -X option */
1883 if (config_get_env_var("PYTHONFAULTHANDLER")
1884 || config_get_xoption(config, L"faulthandler")) {
1885 config->faulthandler = 1;
1886 }
1887 if (config_get_env_var("PYTHONPROFILEIMPORTTIME")
1888 || config_get_xoption(config, L"importtime")) {
1889 config->import_time = 1;
1890 }
1891 if (config_get_xoption(config, L"dev" ) ||
1892 config_get_env_var("PYTHONDEVMODE"))
1893 {
1894 config->dev_mode = 1;
1895 config->faulthandler = 1;
1896 config->allocator = "debug";
1897 }
1898
1899 _PyInitError err = pymain_init_tracemalloc(config);
1900 if (_Py_INIT_FAILED(err)) {
1901 return err;
1902 }
Carl Meyerb193fa92018-06-15 22:40:56 -06001903
1904 err = pymain_init_pycache_prefix(config);
1905 if (_Py_INIT_FAILED(err)) {
1906 return err;
1907 }
1908
Victor Stinner9cfc0022017-12-20 19:36:46 +01001909 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001910}
1911
1912
Victor Stinnera7368ac2017-11-15 18:11:45 -08001913/* Parse command line options and environment variables.
1914 This code must not use Python runtime apart PyMem_Raw memory allocator.
1915
1916 Return 0 on success.
1917 Return 1 if Python is done and must exit.
1918 Set pymain->err and return -1 on error. */
1919static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001920pymain_read_conf_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001921{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001922 _PyInitError err;
1923
Victor Stinnerca719ac2017-12-20 18:00:19 +01001924 int res = pymain_parse_cmdline(pymain, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01001925 if (res != 0) {
1926 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001927 }
1928
Victor Stinner9cfc0022017-12-20 19:36:46 +01001929 /* Set Py_IgnoreEnvironmentFlag for Py_GETENV() */
1930 _PyCoreConfig *config = &pymain->config;
1931 Py_IgnoreEnvironmentFlag = config->ignore_environment;
1932
1933 /* Get environment variables */
1934 cmdline_get_env_flags(cmdline);
1935
1936 err = cmdline_init_env_warnoptions(cmdline);
1937 if (_Py_INIT_FAILED(err)) {
1938 pymain->err = err;
1939 return -1;
1940 }
1941
1942#ifdef MS_WINDOWS
1943 if (cmdline->legacy_windows_fs_encoding) {
1944 config->utf8_mode = 0;
1945 }
1946#endif
1947
Victor Stinnerca719ac2017-12-20 18:00:19 +01001948 if (pymain_init_core_argv(pymain, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001949 return -1;
1950 }
1951
Victor Stinner2b822a02018-01-25 09:18:36 +01001952 /* On Windows, _PyPathConfig_Init() modifies Py_IsolatedFlag and
1953 Py_NoSiteFlag variables if a "._pth" file is found. */
1954 int init_isolated = Py_IsolatedFlag;
1955 int init_no_site = Py_NoSiteFlag;
1956 Py_IsolatedFlag = cmdline->isolated;
1957 Py_NoSiteFlag = cmdline->no_site_import;
Victor Stinner8ded5b82018-01-24 17:03:28 +01001958
Victor Stinner9cfc0022017-12-20 19:36:46 +01001959 err = _PyCoreConfig_Read(config);
Victor Stinner2b822a02018-01-25 09:18:36 +01001960
1961 cmdline->isolated = Py_IsolatedFlag;
1962 cmdline->no_site_import = Py_NoSiteFlag;
1963 Py_IsolatedFlag = init_isolated;
1964 Py_NoSiteFlag = init_no_site;
1965
Victor Stinner31a83932017-12-04 13:39:15 +01001966 if (_Py_INIT_FAILED(err)) {
1967 pymain->err = err;
1968 return -1;
1969 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001970 return 0;
1971}
1972
1973
Victor Stinner19760862017-12-20 01:41:59 +01001974/* Read the configuration, but initialize also the LC_CTYPE locale:
1975 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
Victor Stinnera7368ac2017-11-15 18:11:45 -08001976static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001977pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001978{
Victor Stinner94540602017-12-16 04:54:22 +01001979 int res = -1;
1980
Victor Stinner94540602017-12-16 04:54:22 +01001981 char *oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
1982 if (oldloc == NULL) {
1983 pymain->err = _Py_INIT_NO_MEMORY();
1984 goto done;
1985 }
1986
1987 /* Reconfigure the locale to the default for this process */
1988 _Py_SetLocaleFromEnv(LC_ALL);
1989
1990 int locale_coerced = 0;
1991 int loops = 0;
Victor Stinner9cfc0022017-12-20 19:36:46 +01001992 int init_ignore_env = pymain->config.ignore_environment;
Victor Stinner94540602017-12-16 04:54:22 +01001993
1994 while (1) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001995 int utf8_mode = pymain->config.utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01001996 int encoding_changed = 0;
1997
1998 /* Watchdog to prevent an infinite loop */
1999 loops++;
2000 if (loops == 3) {
2001 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
2002 "reading the configuration");
2003 goto done;
2004 }
2005
Victor Stinnerca719ac2017-12-20 18:00:19 +01002006 if (pymain_init_cmdline_argv(pymain, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01002007 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01002008 }
2009
Victor Stinner9cfc0022017-12-20 19:36:46 +01002010 int conf_res = pymain_read_conf_impl(pymain, cmdline);
2011 if (conf_res != 0) {
2012 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01002013 goto done;
2014 }
2015
2016 /* The legacy C locale assumes ASCII as the default text encoding, which
2017 * causes problems not only for the CPython runtime, but also other
2018 * components like GNU readline.
2019 *
2020 * Accordingly, when the CLI detects it, it attempts to coerce it to a
2021 * more capable UTF-8 based alternative.
2022 *
2023 * See the documentation of the PYTHONCOERCECLOCALE setting for more
2024 * details.
2025 */
Victor Stinner9cfc0022017-12-20 19:36:46 +01002026 if (pymain->config.coerce_c_locale == 1 && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01002027 locale_coerced = 1;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002028 _Py_CoerceLegacyLocale(&pymain->config);
Victor Stinner94540602017-12-16 04:54:22 +01002029 encoding_changed = 1;
2030 }
2031
2032 if (utf8_mode == -1) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002033 if (pymain->config.utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01002034 /* UTF-8 Mode enabled */
2035 encoding_changed = 1;
2036 }
2037 }
2038 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002039 if (pymain->config.utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01002040 encoding_changed = 1;
2041 }
2042 }
2043
2044 if (!encoding_changed) {
2045 break;
2046 }
2047
2048 /* Reset the configuration, except UTF-8 Mode. Set Py_UTF8Mode for
2049 Py_DecodeLocale(). Reset Py_IgnoreEnvironmentFlag, modified by
Victor Stinner8ded5b82018-01-24 17:03:28 +01002050 pymain_read_conf_impl(). Reset Py_IsolatedFlag and Py_NoSiteFlag
2051 modified by _PyCoreConfig_Read(). */
Victor Stinner9cfc0022017-12-20 19:36:46 +01002052 Py_UTF8Mode = pymain->config.utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002053 Py_IgnoreEnvironmentFlag = init_ignore_env;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002054 _PyCoreConfig_Clear(&pymain->config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002055 pymain_clear_cmdline(pymain, cmdline);
Victor Stinner6c5a4b32018-06-16 00:06:28 +02002056 memset(cmdline, 0, sizeof(*cmdline));
Victor Stinnerca719ac2017-12-20 18:00:19 +01002057 pymain_get_global_config(pymain, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01002058
2059 /* The encoding changed: read again the configuration
2060 with the new encoding */
2061 }
2062 res = 0;
2063
2064done:
2065 if (oldloc != NULL) {
2066 setlocale(LC_ALL, oldloc);
2067 PyMem_RawFree(oldloc);
2068 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002069
Victor Stinnera7368ac2017-11-15 18:11:45 -08002070 return res;
2071}
2072
Victor Stinner91106cd2017-12-13 12:29:09 +01002073
Victor Stinner9cfc0022017-12-20 19:36:46 +01002074static void
2075config_init_locale(_PyCoreConfig *config)
2076{
2077 if (config->utf8_mode >= 0 && config->coerce_c_locale >= 0) {
2078 return;
2079 }
2080
2081 if (_Py_LegacyLocaleDetected()) {
2082 /* POSIX locale: enable C locale coercion and UTF-8 Mode */
2083 if (config->utf8_mode < 0) {
2084 config->utf8_mode = 1;
2085 }
2086 if (config->coerce_c_locale < 0) {
2087 config->coerce_c_locale = 1;
2088 }
2089 return;
2090 }
2091
2092 /* By default, C locale coercion and UTF-8 Mode are disabled */
2093 if (config->coerce_c_locale < 0) {
2094 config->coerce_c_locale = 0;
2095 }
2096 if (config->utf8_mode < 0) {
2097 config->utf8_mode = 0;
2098 }
2099}
2100
2101
Victor Stinner8ded5b82018-01-24 17:03:28 +01002102static _PyInitError
2103config_init_module_search_paths(_PyCoreConfig *config)
2104{
2105 assert(config->module_search_paths == NULL);
2106 assert(config->nmodule_search_path < 0);
2107
2108 config->nmodule_search_path = 0;
2109
2110 const wchar_t *sys_path = Py_GetPath();
2111 const wchar_t delim = DELIM;
2112 const wchar_t *p = sys_path;
2113 while (1) {
2114 p = wcschr(sys_path, delim);
2115 if (p == NULL) {
2116 p = sys_path + wcslen(sys_path); /* End of string */
2117 }
2118
2119 size_t path_len = (p - sys_path);
2120 wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t));
2121 if (path == NULL) {
2122 return _Py_INIT_NO_MEMORY();
2123 }
2124 memcpy(path, sys_path, path_len * sizeof(wchar_t));
2125 path[path_len] = L'\0';
2126
2127 _PyInitError err = wstrlist_append(&config->nmodule_search_path,
2128 &config->module_search_paths,
2129 path);
2130 PyMem_RawFree(path);
2131 if (_Py_INIT_FAILED(err)) {
2132 return err;
2133 }
2134
2135 if (*p == '\0') {
2136 break;
2137 }
2138 sys_path = p + 1;
2139 }
2140 return _Py_INIT_OK();
2141}
2142
2143
2144static _PyInitError
2145config_init_path_config(_PyCoreConfig *config)
2146{
2147 _PyInitError err = _PyPathConfig_Init(config);
2148 if (_Py_INIT_FAILED(err)) {
2149 return err;
2150 }
2151
2152 if (config->nmodule_search_path < 0) {
2153 err = config_init_module_search_paths(config);
2154 if (_Py_INIT_FAILED(err)) {
2155 return err;
2156 }
2157 }
2158
2159 if (config->executable == NULL) {
2160 config->executable = _PyMem_RawWcsdup(Py_GetProgramFullPath());
2161 if (config->executable == NULL) {
2162 return _Py_INIT_NO_MEMORY();
2163 }
2164 }
2165
2166 if (config->prefix == NULL) {
2167 config->prefix = _PyMem_RawWcsdup(Py_GetPrefix());
2168 if (config->prefix == NULL) {
2169 return _Py_INIT_NO_MEMORY();
2170 }
2171 }
2172
2173 if (config->exec_prefix == NULL) {
2174 config->exec_prefix = _PyMem_RawWcsdup(Py_GetExecPrefix());
2175 if (config->exec_prefix == NULL) {
2176 return _Py_INIT_NO_MEMORY();
2177 }
2178 }
2179
2180 if (config->base_prefix == NULL) {
2181 config->base_prefix = _PyMem_RawWcsdup(config->prefix);
2182 if (config->base_prefix == NULL) {
2183 return _Py_INIT_NO_MEMORY();
2184 }
2185 }
2186
2187 if (config->base_exec_prefix == NULL) {
2188 config->base_exec_prefix = _PyMem_RawWcsdup(config->exec_prefix);
2189 if (config->base_exec_prefix == NULL) {
2190 return _Py_INIT_NO_MEMORY();
2191 }
2192 }
2193
2194 return _Py_INIT_OK();
2195}
2196
Victor Stinnerda273412017-12-15 01:46:02 +01002197/* Read configuration settings from standard locations
2198 *
2199 * This function doesn't make any changes to the interpreter state - it
2200 * merely populates any missing configuration settings. This allows an
2201 * embedding application to completely override a config option by
2202 * setting it before calling this function, or else modify the default
2203 * setting before passing the fully populated config to Py_EndInitialization.
2204 *
2205 * More advanced selective initialization tricks are possible by calling
2206 * this function multiple times with various preconfigured settings.
2207 */
2208
2209_PyInitError
2210_PyCoreConfig_Read(_PyCoreConfig *config)
2211{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002212 _PyInitError err;
2213
2214 err = config_read_env_vars(config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002215 if (_Py_INIT_FAILED(err)) {
2216 return err;
2217 }
2218
Victor Stinner9cfc0022017-12-20 19:36:46 +01002219 /* -X options */
2220 if (config_get_xoption(config, L"showrefcount")) {
2221 config->show_ref_count = 1;
2222 }
2223 if (config_get_xoption(config, L"showalloccount")) {
2224 config->show_alloc_count = 1;
2225 }
2226
2227 err = config_read_complex_options(config);
2228 if (_Py_INIT_FAILED(err)) {
2229 return err;
2230 }
2231
2232 err = config_init_utf8_mode(config);
2233 if (_Py_INIT_FAILED(err)) {
2234 return err;
2235 }
2236
2237 err = config_init_home(config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002238 if (_Py_INIT_FAILED(err)) {
2239 return err;
2240 }
2241
2242 err = config_init_program_name(config);
2243 if (_Py_INIT_FAILED(err)) {
2244 return err;
2245 }
2246
Victor Stinner9cfc0022017-12-20 19:36:46 +01002247 config_init_locale(config);
Victor Stinnerda273412017-12-15 01:46:02 +01002248
Victor Stinner9cfc0022017-12-20 19:36:46 +01002249 /* Signal handlers are installed by default */
2250 if (config->install_signal_handlers < 0) {
2251 config->install_signal_handlers = 1;
Victor Stinner94540602017-12-16 04:54:22 +01002252 }
2253
Victor Stinner8ded5b82018-01-24 17:03:28 +01002254 if (!config->_disable_importlib) {
2255 err = config_init_path_config(config);
2256 if (_Py_INIT_FAILED(err)) {
2257 return err;
2258 }
2259 }
Victor Stinnerda273412017-12-15 01:46:02 +01002260 return _Py_INIT_OK();
2261}
2262
2263
2264void
2265_PyCoreConfig_Clear(_PyCoreConfig *config)
2266{
2267#define CLEAR(ATTR) \
2268 do { \
2269 PyMem_RawFree(ATTR); \
2270 ATTR = NULL; \
2271 } while (0)
Victor Stinner8ded5b82018-01-24 17:03:28 +01002272#define CLEAR_WSTRLIST(LEN, LIST) \
2273 do { \
2274 clear_wstrlist(LEN, LIST); \
2275 LEN = 0; \
2276 LIST = NULL; \
2277 } while (0)
Victor Stinnerda273412017-12-15 01:46:02 +01002278
Carl Meyerb193fa92018-06-15 22:40:56 -06002279 CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002280 CLEAR(config->module_search_path_env);
2281 CLEAR(config->home);
2282 CLEAR(config->program_name);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002283 CLEAR(config->program);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002284
Victor Stinner8ded5b82018-01-24 17:03:28 +01002285 CLEAR_WSTRLIST(config->argc, config->argv);
2286 config->argc = -1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002287
Victor Stinner8ded5b82018-01-24 17:03:28 +01002288 CLEAR_WSTRLIST(config->nwarnoption, config->warnoptions);
2289 CLEAR_WSTRLIST(config->nxoption, config->xoptions);
2290 CLEAR_WSTRLIST(config->nmodule_search_path, config->module_search_paths);
2291 config->nmodule_search_path = -1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002292
Victor Stinner8ded5b82018-01-24 17:03:28 +01002293 CLEAR(config->executable);
2294 CLEAR(config->prefix);
2295 CLEAR(config->base_prefix);
2296 CLEAR(config->exec_prefix);
2297 CLEAR(config->base_exec_prefix);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002298#undef CLEAR
Victor Stinner8ded5b82018-01-24 17:03:28 +01002299#undef CLEAR_WSTRLIST
Victor Stinnerda273412017-12-15 01:46:02 +01002300}
2301
2302
2303int
2304_PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
2305{
2306 _PyCoreConfig_Clear(config);
2307
2308#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
Victor Stinnerc4bca952017-12-19 23:48:17 +01002309#define COPY_STR_ATTR(ATTR) \
2310 do { \
2311 if (config2->ATTR != NULL) { \
2312 config->ATTR = _PyMem_RawWcsdup(config2->ATTR); \
2313 if (config->ATTR == NULL) { \
2314 return -1; \
2315 } \
2316 } \
2317 } while (0)
Victor Stinner8ded5b82018-01-24 17:03:28 +01002318#define COPY_WSTRLIST(LEN, LIST) \
2319 do { \
2320 if (config2->LIST != NULL) { \
2321 config->LIST = copy_wstrlist(config2->LEN, config2->LIST); \
2322 if (config->LIST == NULL) { \
2323 return -1; \
2324 } \
2325 } \
2326 config->LEN = config2->LEN; \
2327 } while (0)
Victor Stinnerc4bca952017-12-19 23:48:17 +01002328
Victor Stinnerda273412017-12-15 01:46:02 +01002329 COPY_ATTR(ignore_environment);
2330 COPY_ATTR(use_hash_seed);
2331 COPY_ATTR(hash_seed);
2332 COPY_ATTR(_disable_importlib);
2333 COPY_ATTR(allocator);
2334 COPY_ATTR(dev_mode);
2335 COPY_ATTR(faulthandler);
2336 COPY_ATTR(tracemalloc);
2337 COPY_ATTR(import_time);
2338 COPY_ATTR(show_ref_count);
2339 COPY_ATTR(show_alloc_count);
2340 COPY_ATTR(dump_refs);
2341 COPY_ATTR(malloc_stats);
2342 COPY_ATTR(utf8_mode);
Victor Stinnerda273412017-12-15 01:46:02 +01002343
Carl Meyerb193fa92018-06-15 22:40:56 -06002344 COPY_STR_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002345 COPY_STR_ATTR(module_search_path_env);
2346 COPY_STR_ATTR(home);
2347 COPY_STR_ATTR(program_name);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002348 COPY_STR_ATTR(program);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002349
Victor Stinner8ded5b82018-01-24 17:03:28 +01002350 COPY_WSTRLIST(argc, argv);
2351 COPY_WSTRLIST(nwarnoption, warnoptions);
2352 COPY_WSTRLIST(nxoption, xoptions);
2353 COPY_WSTRLIST(nmodule_search_path, module_search_paths);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002354
Victor Stinner8ded5b82018-01-24 17:03:28 +01002355 COPY_STR_ATTR(executable);
2356 COPY_STR_ATTR(prefix);
2357 COPY_STR_ATTR(base_prefix);
2358 COPY_STR_ATTR(exec_prefix);
2359 COPY_STR_ATTR(base_exec_prefix);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002360
Victor Stinnerc4bca952017-12-19 23:48:17 +01002361#undef COPY_ATTR
Victor Stinnerda273412017-12-15 01:46:02 +01002362#undef COPY_STR_ATTR
Victor Stinner8ded5b82018-01-24 17:03:28 +01002363#undef COPY_WSTRLIST
Victor Stinnerda273412017-12-15 01:46:02 +01002364 return 0;
2365}
2366
2367
2368void
2369_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
2370{
2371 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002372 Py_CLEAR(config->executable);
2373 Py_CLEAR(config->prefix);
2374 Py_CLEAR(config->base_prefix);
2375 Py_CLEAR(config->exec_prefix);
2376 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002377 Py_CLEAR(config->warnoptions);
2378 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002379 Py_CLEAR(config->module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06002380 Py_CLEAR(config->pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002381}
2382
2383
2384static PyObject*
2385config_copy_attr(PyObject *obj)
2386{
2387 if (PyUnicode_Check(obj)) {
2388 Py_INCREF(obj);
2389 return obj;
2390 }
2391 else if (PyList_Check(obj)) {
2392 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
2393 }
2394 else if (PyDict_Check(obj)) {
2395 /* The dict type is used for xoptions. Make the assumption that keys
2396 and values are immutables */
2397 return PyDict_Copy(obj);
2398 }
2399 else {
2400 PyErr_Format(PyExc_TypeError,
2401 "cannot copy config attribute of type %.200s",
2402 Py_TYPE(obj)->tp_name);
2403 return NULL;
2404 }
2405}
2406
2407
2408int
2409_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
2410 const _PyMainInterpreterConfig *config2)
2411{
2412 _PyMainInterpreterConfig_Clear(config);
2413
2414#define COPY_ATTR(ATTR) \
2415 do { \
2416 if (config2->ATTR != NULL) { \
2417 config->ATTR = config_copy_attr(config2->ATTR); \
2418 if (config->ATTR == NULL) { \
2419 return -1; \
2420 } \
2421 } \
2422 } while (0)
2423
2424 COPY_ATTR(argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002425 COPY_ATTR(executable);
2426 COPY_ATTR(prefix);
2427 COPY_ATTR(base_prefix);
2428 COPY_ATTR(exec_prefix);
2429 COPY_ATTR(base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002430 COPY_ATTR(warnoptions);
2431 COPY_ATTR(xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002432 COPY_ATTR(module_search_path);
Carl Meyerb193fa92018-06-15 22:40:56 -06002433 COPY_ATTR(pycache_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002434#undef COPY_ATTR
2435 return 0;
2436}
2437
2438
2439
2440
Victor Stinner41264f12017-12-15 02:05:29 +01002441_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01002442_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
2443 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002444{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002445 if (main_config->install_signal_handlers < 0) {
2446 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002447 }
2448
Victor Stinner9cfc0022017-12-20 19:36:46 +01002449 if (main_config->xoptions == NULL) {
2450 main_config->xoptions = config_create_xoptions_dict(config);
2451 if (main_config->xoptions == NULL) {
2452 return _Py_INIT_NO_MEMORY();
2453 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002454 }
2455
Victor Stinner8ded5b82018-01-24 17:03:28 +01002456#define COPY_WSTR(ATTR) \
2457 do { \
2458 if (main_config->ATTR == NULL) { \
2459 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
2460 if (main_config->ATTR == NULL) { \
2461 return _Py_INIT_NO_MEMORY(); \
2462 } \
2463 } \
2464 } while (0)
2465#define COPY_WSTRLIST(ATTR, LEN, LIST) \
2466 do { \
2467 if (ATTR == NULL) { \
2468 ATTR = wstrlist_as_pylist(LEN, LIST); \
2469 if (ATTR == NULL) { \
2470 return _Py_INIT_NO_MEMORY(); \
2471 } \
2472 } \
2473 } while (0)
2474
2475 COPY_WSTRLIST(main_config->warnoptions,
2476 config->nwarnoption, config->warnoptions);
2477 if (config->argc >= 0) {
2478 COPY_WSTRLIST(main_config->argv,
2479 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002480 }
2481
Victor Stinner8ded5b82018-01-24 17:03:28 +01002482 if (!config->_disable_importlib) {
2483 COPY_WSTR(executable);
2484 COPY_WSTR(prefix);
2485 COPY_WSTR(base_prefix);
2486 COPY_WSTR(exec_prefix);
2487 COPY_WSTR(base_exec_prefix);
2488
2489 COPY_WSTRLIST(main_config->module_search_path,
2490 config->nmodule_search_path, config->module_search_paths);
Carl Meyerb193fa92018-06-15 22:40:56 -06002491
2492 if (config->pycache_prefix != NULL) {
2493 COPY_WSTR(pycache_prefix);
2494 } else {
2495 main_config->pycache_prefix = NULL;
2496 }
2497
Victor Stinner9cfc0022017-12-20 19:36:46 +01002498 }
Victor Stinner41264f12017-12-15 02:05:29 +01002499
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002500 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01002501#undef COPY_WSTR
2502#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002503}
2504
2505
2506static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01002507pymain_init_python_main(_PyMain *pymain)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002508{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002509 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002510
Victor Stinner9cfc0022017-12-20 19:36:46 +01002511 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
2512 err = _PyMainInterpreterConfig_Read(&main_config, &pymain->config);
2513 if (!_Py_INIT_FAILED(err)) {
2514 err = _Py_InitializeMainInterpreter(&main_config);
2515 }
2516 _PyMainInterpreterConfig_Clear(&main_config);
2517
2518 if (_Py_INIT_FAILED(err)) {
2519 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002520 return -1;
2521 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002522 return 0;
2523}
Victor Stinnera7368ac2017-11-15 18:11:45 -08002524
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002525
2526static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01002527pymain_init_sys_path(_PyMain *pymain)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002528{
Victor Stinnerca719ac2017-12-20 18:00:19 +01002529 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002530 /* If filename is a package (ex: directory or ZIP file) which contains
2531 __main__.py, main_importer_path is set to filename and will be
2532 prepended to sys.path by pymain_run_main_from_importer(). Otherwise,
2533 main_importer_path is set to NULL. */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002534 pymain->main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01002535 }
2536
Victor Stinner19760862017-12-20 01:41:59 +01002537 PyObject *path0;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002538 if (pymain_compute_path0(pymain, &path0) < 0) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002539 return -1;
2540 }
Victor Stinner19760862017-12-20 01:41:59 +01002541
Victor Stinner9cfc0022017-12-20 19:36:46 +01002542 pymain_clear_config(pymain);
Victor Stinner19760862017-12-20 01:41:59 +01002543
2544 if (path0 != NULL) {
2545 if (pymain_update_sys_path(pymain, path0) < 0) {
2546 Py_DECREF(path0);
2547 return -1;
2548 }
2549 Py_DECREF(path0);
2550 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002551 return 0;
2552}
2553
2554
2555static void
2556pymain_run_python(_PyMain *pymain)
2557{
Victor Stinner19760862017-12-20 01:41:59 +01002558 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08002559
2560 pymain_header(pymain);
2561 pymain_import_readline(pymain);
2562
Victor Stinnerca719ac2017-12-20 18:00:19 +01002563 if (pymain->command) {
2564 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002565 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002566 else if (pymain->module) {
2567 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002568 }
2569 else {
Victor Stinner19760862017-12-20 01:41:59 +01002570 pymain_run_filename(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002571 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002572
Victor Stinner19760862017-12-20 01:41:59 +01002573 pymain_repl(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002574}
2575
2576
Victor Stinnerc4bca952017-12-19 23:48:17 +01002577static void
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002578pymain_init(_PyMain *pymain)
2579{
Victor Stinner94540602017-12-16 04:54:22 +01002580 /* 754 requires that FP exceptions run in "no stop" mode by default,
2581 * and until C vendors implement C99's ways to control FP exceptions,
2582 * Python requires non-stop mode. Alas, some platforms enable FP
2583 * exceptions by default. Here we disable them.
2584 */
2585#ifdef __FreeBSD__
2586 fedisableexcept(FE_OVERFLOW);
2587#endif
2588
Victor Stinner9cfc0022017-12-20 19:36:46 +01002589 pymain->config._disable_importlib = 0;
Victor Stinnere32e79f2017-11-23 01:49:45 +01002590 pymain->config.install_signal_handlers = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002591}
2592
Victor Stinnera7368ac2017-11-15 18:11:45 -08002593
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002594static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01002595pymain_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002596{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002597 pymain->err = _PyRuntime_Initialize();
2598 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002599 return -1;
2600 }
2601
Victor Stinnerca719ac2017-12-20 18:00:19 +01002602 int res = pymain_read_conf(pymain, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002603 if (res < 0) {
2604 return -1;
2605 }
2606 if (res > 0) {
2607 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01002608 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002609 }
2610
Victor Stinner94540602017-12-16 04:54:22 +01002611 if (cmdline->print_help) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002612 pymain_usage(0, pymain->config.program);
Victor Stinner19760862017-12-20 01:41:59 +01002613 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002614 }
2615
2616 if (cmdline->print_version) {
2617 printf("Python %s\n",
2618 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01002619 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002620 }
2621
Victor Stinnerc4bca952017-12-19 23:48:17 +01002622 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002623 orig_argv = copy_wstrlist(pymain->argc, cmdline->argv);
2624 if (orig_argv == NULL) {
2625 pymain->err = _Py_INIT_NO_MEMORY();
2626 return -1;
2627 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01002628 orig_argc = pymain->argc;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002629
Victor Stinner9cfc0022017-12-20 19:36:46 +01002630 _PyInitError err = config_init_warnoptions(&pymain->config, cmdline);
2631 if (_Py_INIT_FAILED(err)) {
2632 pymain->err = err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002633 return -1;
2634 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002635 return 0;
2636}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00002637
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002638
Victor Stinnerca719ac2017-12-20 18:00:19 +01002639/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
2640 LC_CTYPE locale and Py_DecodeLocale().
2641
2642 Configuration:
2643
2644 * Command line arguments
2645 * Environment variables
2646 * Py_xxx global configuration variables
2647
2648 _Py_CommandLineDetails is a temporary structure used to prioritize these
2649 variables. */
2650static int
2651pymain_cmdline(_PyMain *pymain)
2652{
Victor Stinner31e99082017-12-20 23:41:38 +01002653 /* Force default allocator, since pymain_free() and pymain_clear_config()
2654 must use the same allocator than this function. */
2655 PyMemAllocatorEx old_alloc;
2656 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2657#ifdef Py_DEBUG
2658 PyMemAllocatorEx default_alloc;
2659 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
2660#endif
2661
Victor Stinnerca719ac2017-12-20 18:00:19 +01002662 _Py_CommandLineDetails cmdline;
2663 memset(&cmdline, 0, sizeof(cmdline));
2664
2665 pymain_get_global_config(pymain, &cmdline);
2666
2667 int res = pymain_cmdline_impl(pymain, &cmdline);
2668
2669 pymain_set_global_config(pymain, &cmdline);
2670
2671 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01002672
2673#ifdef Py_DEBUG
2674 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
2675 PyMemAllocatorEx cur_alloc;
2676 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
2677 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
2678#endif
2679 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002680 return res;
2681}
2682
2683
Victor Stinner94540602017-12-16 04:54:22 +01002684static int
2685pymain_main(_PyMain *pymain)
2686{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002687 pymain_init(pymain);
Victor Stinner94540602017-12-16 04:54:22 +01002688
Victor Stinnerca719ac2017-12-20 18:00:19 +01002689 int res = pymain_cmdline(pymain);
Victor Stinner19760862017-12-20 01:41:59 +01002690 if (res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01002691 _Py_FatalInitError(pymain->err);
2692 }
Victor Stinner19760862017-12-20 01:41:59 +01002693 if (res == 1) {
2694 goto done;
2695 }
2696
Victor Stinner9cfc0022017-12-20 19:36:46 +01002697 pymain_init_stdio(pymain);
2698
2699 pymain->err = _Py_InitializeCore(&pymain->config);
2700 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinner19760862017-12-20 01:41:59 +01002701 _Py_FatalInitError(pymain->err);
2702 }
2703
2704 if (pymain_init_python_main(pymain) < 0) {
2705 _Py_FatalInitError(pymain->err);
2706 }
2707
Victor Stinner9cfc0022017-12-20 19:36:46 +01002708 if (pymain_init_sys_path(pymain) < 0) {
2709 _Py_FatalInitError(pymain->err);
2710 }
2711
Victor Stinner19760862017-12-20 01:41:59 +01002712 pymain_run_python(pymain);
2713
2714 if (Py_FinalizeEx() < 0) {
2715 /* Value unlikely to be confused with a non-error exit status or
2716 other special meaning */
2717 pymain->status = 120;
2718 }
2719
2720done:
Victor Stinner94540602017-12-16 04:54:22 +01002721 pymain_free(pymain);
2722
Victor Stinner94540602017-12-16 04:54:22 +01002723 return pymain->status;
2724}
2725
2726
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002727int
2728Py_Main(int argc, wchar_t **argv)
2729{
2730 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002731 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002732 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002733 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002734
Victor Stinner94540602017-12-16 04:54:22 +01002735 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00002736}
2737
Victor Stinner94540602017-12-16 04:54:22 +01002738
2739int
2740_Py_UnixMain(int argc, char **argv)
2741{
2742 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01002743 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002744 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01002745 pymain.bytes_argv = argv;
2746
2747 return pymain_main(&pymain);
2748}
2749
2750
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002751/* this is gonna seem *real weird*, but if you put some other code between
2752 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
2753 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00002754
Guido van Rossum667d7041995-08-04 04:20:48 +00002755/* Make the *original* argc/argv available to other modules.
2756 This is rare, but it is needed by the secureware extension. */
2757
2758void
Martin v. Löwis790465f2008-04-05 20:41:37 +00002759Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00002760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 *argc = orig_argc;
2762 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00002763}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002764
2765#ifdef __cplusplus
2766}
2767#endif