blob: 1ce075f98e36da11433cfb2813784292bc9366e8 [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\
Thomas Wouters89f507f2006-12-13 04:49:30 +000099-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +0000100-OO : remove doc-strings in addition to the -O optimizations\n\
Georg Brandl9d871192010-12-04 10:47:18 +0000101-q : don't print version and copyright messages on interactive startup\n\
Christian Heimes8dc226f2008-05-06 23:45:46 +0000102-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000103-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000104";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200105static const char usage_3[] = "\
Berker Peksag7f580972017-10-13 15:16:31 +0300106-u : force the stdout and stderr streams to be unbuffered;\n\
107 this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000108-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
109 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000110-V : print the Python version number and exit (also --version)\n\
INADA Naoki0e175a62016-11-21 20:57:14 +0900111 when given twice, print more information about the build\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenvey0805ca32010-04-07 04:04:10 +0000113 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +0000114-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000115-X opt : set implementation-specific option\n\
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800116--check-hash-based-pycs always|default|never:\n\
117 control how Python invalidates hash-based .pyc files\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +0000118";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200119static const char usage_4[] = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +0000120file : program read from script file\n\
121- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000122arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000123Other environment variables:\n\
124PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200125PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000126 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +0000127";
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200128static const char usage_5[] =
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200129"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
Victor Stinner9802b392010-08-19 11:36:43 +0000130" The default module search path uses %s.\n"
131"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
132"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
Victor Stinner34be8072016-03-14 12:04:26 +0100133"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
134static const char usage_6[] =
135"PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
136" to seed the hashes of str, bytes and datetime objects. It can also be\n"
137" set to an integer in the range [0,4294967295] to get hash values with a\n"
138" predictable seed.\n"
139"PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
140" on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000141" hooks.\n"
Stéphane Wirtel7d1017d2017-06-12 13:30:33 +0200142"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
Nick Coghlaneb817952017-06-18 12:29:42 +1000143" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
Victor Stinner5e3806f2017-11-30 11:40:24 +0100144" locale coercion and locale compatibility warnings on stderr.\n"
145"PYTHONDEVMODE: enable the development mode.\n";
Guido van Rossum667d7041995-08-04 04:20:48 +0000146
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800147static void
Victor Stinnera7368ac2017-11-15 18:11:45 -0800148pymain_usage(int error, const wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000149{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800150 FILE *f = error ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 fprintf(f, usage_line, program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800153 if (error)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 fprintf(f, "Try `python -h' for more information.\n");
155 else {
156 fputs(usage_1, f);
157 fputs(usage_2, f);
158 fputs(usage_3, f);
Serhiy Storchaka1ba01612015-12-30 09:28:19 +0200159 fprintf(f, usage_4, (wint_t)DELIM);
160 fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100161 fputs(usage_6, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000163}
164
Victor Stinnera7368ac2017-11-15 18:11:45 -0800165
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200166static const char*
Victor Stinner9cfc0022017-12-20 19:36:46 +0100167config_get_env_var(const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -0800168{
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200169 const char *var = Py_GETENV(name);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800170 if (var && var[0] != '\0') {
171 return var;
172 }
173 else {
174 return NULL;
175 }
176}
177
178
Victor Stinnerca719ac2017-12-20 18:00:19 +0100179static int
180config_get_env_var_dup(wchar_t **dest, wchar_t *wname, char *name)
181{
182 if (Py_IgnoreEnvironmentFlag) {
183 *dest = NULL;
184 return 0;
185 }
186
187#ifdef MS_WINDOWS
188 const wchar_t *var = _wgetenv(wname);
189 if (!var || var[0] == '\0') {
190 *dest = NULL;
191 return 0;
192 }
193
194 wchar_t *copy = _PyMem_RawWcsdup(var);
195 if (copy == NULL) {
196 return -1;
197 }
198
199 *dest = copy;
200#else
201 const char *var = getenv(name);
202 if (!var || var[0] == '\0') {
203 *dest = NULL;
204 return 0;
205 }
206
207 size_t len;
208 wchar_t *wvar = Py_DecodeLocale(var, &len);
209 if (!wvar) {
210 if (len == (size_t)-2) {
211 return -2;
212 }
213 else {
214 return -1;
215 }
216 }
217 *dest = wvar;
218#endif
219 return 0;
220}
221
222
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800223static void
Victor Stinner33c377e2017-12-05 15:12:41 +0100224pymain_run_startup(PyCompilerFlags *cf)
Martin v. Löwis6caea372003-11-18 19:46:25 +0000225{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100226 const char *startup = config_get_env_var("PYTHONSTARTUP");
Victor Stinner6bf992a2017-12-06 17:26:10 +0100227 if (startup == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800228 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800230
231 FILE *fp = _Py_fopen(startup, "r");
232 if (fp == NULL) {
233 int save_errno = errno;
234 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
235 errno = save_errno;
236
237 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
238 startup);
239 PyErr_Print();
240 PyErr_Clear();
241 return;
242 }
243
244 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
245 PyErr_Clear();
246 fclose(fp);
Martin v. Löwis6caea372003-11-18 19:46:25 +0000247}
248
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800249static void
250pymain_run_interactive_hook(void)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200251{
252 PyObject *sys, *hook, *result;
253 sys = PyImport_ImportModule("sys");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800254 if (sys == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200255 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800256 }
257
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200258 hook = PyObject_GetAttrString(sys, "__interactivehook__");
259 Py_DECREF(sys);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800260 if (hook == NULL) {
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200261 PyErr_Clear();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800262 return;
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200263 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800264
265 result = _PyObject_CallNoArg(hook);
266 Py_DECREF(hook);
267 if (result == NULL) {
268 goto error;
269 }
270 Py_DECREF(result);
271
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200272 return;
273
274error:
275 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
276 PyErr_Print();
277 PyErr_Clear();
278}
279
Thomas Woutersa9773292006-04-21 09:43:23 +0000280
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800281static int
Victor Stinnerc4bca952017-12-19 23:48:17 +0100282pymain_run_module(const wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 PyObject *module, *runpy, *runmodule, *runargs, *result;
285 runpy = PyImport_ImportModule("runpy");
286 if (runpy == NULL) {
287 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200288 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 return -1;
290 }
291 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
292 if (runmodule == NULL) {
293 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200294 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 Py_DECREF(runpy);
296 return -1;
297 }
298 module = PyUnicode_FromWideChar(modname, wcslen(modname));
299 if (module == NULL) {
300 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200301 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 Py_DECREF(runpy);
303 Py_DECREF(runmodule);
304 return -1;
305 }
306 runargs = Py_BuildValue("(Oi)", module, set_argv0);
307 if (runargs == NULL) {
308 fprintf(stderr,
309 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner7d36e4f2013-04-10 00:27:23 +0200310 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_DECREF(runpy);
312 Py_DECREF(runmodule);
313 Py_DECREF(module);
314 return -1;
315 }
316 result = PyObject_Call(runmodule, runargs, NULL);
317 if (result == NULL) {
318 PyErr_Print();
319 }
320 Py_DECREF(runpy);
321 Py_DECREF(runmodule);
322 Py_DECREF(module);
323 Py_DECREF(runargs);
324 if (result == NULL) {
325 return -1;
326 }
327 Py_DECREF(result);
328 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000329}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000330
Nick Coghland2977a32017-03-12 20:38:32 +1000331static PyObject *
Victor Stinnerc4bca952017-12-19 23:48:17 +0100332pymain_get_importer(const wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000333{
Nick Coghland2977a32017-03-12 20:38:32 +1000334 PyObject *sys_path0 = NULL, *importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000335
Nick Coghland2977a32017-03-12 20:38:32 +1000336 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800337 if (sys_path0 == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000338 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800339 }
Victor Stinner4726e402010-10-06 23:24:57 +0000340
Nick Coghland2977a32017-03-12 20:38:32 +1000341 importer = PyImport_GetImporter(sys_path0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800342 if (importer == NULL) {
Victor Stinner4726e402010-10-06 23:24:57 +0000343 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800344 }
Victor Stinner4726e402010-10-06 23:24:57 +0000345
Brett Cannonaa936422012-04-27 15:30:58 -0400346 if (importer == Py_None) {
Nick Coghland2977a32017-03-12 20:38:32 +1000347 Py_DECREF(sys_path0);
Victor Stinner4726e402010-10-06 23:24:57 +0000348 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000349 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800351
Victor Stinner4726e402010-10-06 23:24:57 +0000352 Py_DECREF(importer);
Nick Coghland2977a32017-03-12 20:38:32 +1000353 return sys_path0;
Victor Stinner4726e402010-10-06 23:24:57 +0000354
Nick Coghland2977a32017-03-12 20:38:32 +1000355error:
356 Py_XDECREF(sys_path0);
357 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
358 PyErr_Print();
359 PyErr_Clear();
360 return NULL;
361}
362
363
364static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800365pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
Victor Stinnera62207c2010-08-07 10:57:17 +0000366{
367 PyObject *unicode, *bytes;
368 int ret;
369
370 unicode = PyUnicode_FromWideChar(command, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800371 if (unicode == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000372 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800373 }
374
Victor Stinnera62207c2010-08-07 10:57:17 +0000375 bytes = PyUnicode_AsUTF8String(unicode);
376 Py_DECREF(unicode);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800377 if (bytes == NULL) {
Victor Stinnera62207c2010-08-07 10:57:17 +0000378 goto error;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800379 }
380
Victor Stinnera62207c2010-08-07 10:57:17 +0000381 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
382 Py_DECREF(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800383 return (ret != 0);
Victor Stinnera62207c2010-08-07 10:57:17 +0000384
385error:
Victor Stinner398356b2010-08-18 22:23:22 +0000386 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinnera62207c2010-08-07 10:57:17 +0000387 PyErr_Print();
388 return 1;
389}
390
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800391
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000392static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800393pymain_run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf)
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000394{
395 PyObject *unicode, *bytes = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200396 const char *filename_str;
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000397 int run;
398
399 /* call pending calls like signal handlers (SIGINT) */
400 if (Py_MakePendingCalls() == -1) {
401 PyErr_Print();
402 return 1;
403 }
404
405 if (filename) {
406 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
407 if (unicode != NULL) {
Victor Stinnere0f32682010-10-17 19:34:51 +0000408 bytes = PyUnicode_EncodeFSDefault(unicode);
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000409 Py_DECREF(unicode);
410 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800411 if (bytes != NULL) {
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000412 filename_str = PyBytes_AsString(bytes);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800413 }
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000414 else {
415 PyErr_Clear();
Victor Stinnere0f32682010-10-17 19:34:51 +0000416 filename_str = "<encoding error>";
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000417 }
418 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800419 else {
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000420 filename_str = "<stdin>";
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800421 }
Victor Stinner0a3ddad2010-08-07 16:34:25 +0000422
423 run = PyRun_AnyFileExFlags(fp, filename_str, filename != NULL, p_cf);
424 Py_XDECREF(bytes);
425 return run != 0;
426}
427
Christian Heimes9cd17752007-11-18 19:35:23 +0000428
Guido van Rossum667d7041995-08-04 04:20:48 +0000429/* Main program */
430
Eric Snow6b4be192017-05-22 21:36:03 -0700431typedef struct {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100432 wchar_t **argv;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100433 int nwarnoption; /* Number of -W options */
434 wchar_t **warnoptions; /* -W options */
435 int nenv_warnoption; /* Number of PYTHONWARNINGS options */
436 wchar_t **env_warnoptions; /* PYTHONWARNINGS options */
Eric Snow6b4be192017-05-22 21:36:03 -0700437 int print_help; /* -h, -? options */
438 int print_version; /* -V option */
Victor Stinner6bf992a2017-12-06 17:26:10 +0100439 int bytes_warning; /* Py_BytesWarningFlag, -b */
440 int debug; /* Py_DebugFlag, -b, PYTHONDEBUG */
441 int inspect; /* Py_InspectFlag, -i, PYTHONINSPECT */
442 int interactive; /* Py_InteractiveFlag, -i */
443 int isolated; /* Py_IsolatedFlag, -I */
444 int optimization_level; /* Py_OptimizeFlag, -O, PYTHONOPTIMIZE */
445 int dont_write_bytecode; /* Py_DontWriteBytecodeFlag, -B, PYTHONDONTWRITEBYTECODE */
446 int no_user_site_directory; /* Py_NoUserSiteDirectory, -I, -s, PYTHONNOUSERSITE */
447 int no_site_import; /* Py_NoSiteFlag, -S */
448 int use_unbuffered_io; /* Py_UnbufferedStdioFlag, -u, PYTHONUNBUFFERED */
449 int verbosity; /* Py_VerboseFlag, -v, PYTHONVERBOSE */
450 int quiet_flag; /* Py_QuietFlag, -q */
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800451 const char *check_hash_pycs_mode; /* --check-hash-based-pycs */
Victor Stinner6bf992a2017-12-06 17:26:10 +0100452#ifdef MS_WINDOWS
453 int legacy_windows_fs_encoding; /* Py_LegacyWindowsFSEncodingFlag,
454 PYTHONLEGACYWINDOWSFSENCODING */
455 int legacy_windows_stdio; /* Py_LegacyWindowsStdioFlag,
456 PYTHONLEGACYWINDOWSSTDIO */
457#endif
Eric Snow6b4be192017-05-22 21:36:03 -0700458} _Py_CommandLineDetails;
459
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800460/* Structure used by Py_Main() to pass data to subfunctions */
461typedef struct {
Victor Stinner19760862017-12-20 01:41:59 +0100462 /* Input arguments */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800463 int argc;
Victor Stinner94540602017-12-16 04:54:22 +0100464 int use_bytes_argv;
465 char **bytes_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100466 wchar_t **wchar_argv;
Victor Stinner19760862017-12-20 01:41:59 +0100467
468 /* Exit status or "exit code": result of pymain_main() */
469 int status;
470 /* Error message if a function failed */
471 _PyInitError err;
472
Victor Stinner19760862017-12-20 01:41:59 +0100473 /* non-zero is stdin is a TTY or if -i option is used */
474 int stdin_is_interactive;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100475 int skip_first_line; /* -x option */
476 wchar_t *filename; /* Trailing arg without -c or -m */
477 wchar_t *command; /* -c argument */
478 wchar_t *module; /* -m argument */
Victor Stinner19760862017-12-20 01:41:59 +0100479
Victor Stinner9cfc0022017-12-20 19:36:46 +0100480 _PyCoreConfig config;
Victor Stinner19760862017-12-20 01:41:59 +0100481
482 PyObject *main_importer_path;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800483} _PyMain;
484
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800485#define _PyMain_INIT \
Victor Stinner9cfc0022017-12-20 19:36:46 +0100486 {.config = _PyCoreConfig_INIT, \
Victor Stinnerd5dda982017-12-13 17:31:16 +0100487 .err = _Py_INIT_OK()}
488/* Note: _PyMain_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800489
490
Victor Stinner19760862017-12-20 01:41:59 +0100491/* Non-zero if filename, command (-c) or module (-m) is set
492 on the command line */
493#define RUN_CODE(pymain) \
Victor Stinnerca719ac2017-12-20 18:00:19 +0100494 (pymain->command != NULL || pymain->filename != NULL \
495 || pymain->module != NULL)
Victor Stinner19760862017-12-20 01:41:59 +0100496
497
Victor Stinnerca719ac2017-12-20 18:00:19 +0100498static wchar_t*
499pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800500{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100501 wchar_t *str2 = _PyMem_RawWcsdup(str);
502 if (str2 == NULL) {
503 pymain->err = _Py_INIT_NO_MEMORY();
504 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800505 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100506 return str2;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800507}
508
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100509
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800510static void
Victor Stinnerca719ac2017-12-20 18:00:19 +0100511clear_wstrlist(int len, wchar_t **list)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800512{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100513 for (int i=0; i < len; i++) {
514 PyMem_RawFree(list[i]);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100515 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100516 PyMem_RawFree(list);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100517}
518
519
520static int
Victor Stinnerca719ac2017-12-20 18:00:19 +0100521pymain_init_cmdline_argv(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100522{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100523 assert(cmdline->argv == NULL);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100524
Victor Stinnerca719ac2017-12-20 18:00:19 +0100525 if (pymain->use_bytes_argv) {
526 /* +1 for a the NULL terminator */
527 size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
528 wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
529 if (argv == NULL) {
530 pymain->err = _Py_INIT_NO_MEMORY();
531 return -1;
532 }
533
534 for (int i = 0; i < pymain->argc; i++) {
535 size_t len;
536 wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
537 if (arg == NULL) {
538 clear_wstrlist(i, argv);
539 pymain->err = DECODE_LOCALE_ERR("command line arguments",
540 (Py_ssize_t)len);
541 return -1;
542 }
543 argv[i] = arg;
544 }
545 argv[pymain->argc] = NULL;
546
547 cmdline->argv = argv;
548 }
549 else {
550 cmdline->argv = pymain->wchar_argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100551 }
552
Victor Stinnerca719ac2017-12-20 18:00:19 +0100553 wchar_t *program;
554 if (pymain->argc >= 1 && cmdline->argv != NULL) {
555 program = cmdline->argv[0];
556 }
557 else {
558 program = L"";
559 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100560 pymain->config.program = pymain_wstrdup(pymain, program);
561 if (pymain->config.program == NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +0100562 return -1;
563 }
564
Victor Stinnerc4bca952017-12-19 23:48:17 +0100565 return 0;
566}
567
568
569static void
Victor Stinnerca719ac2017-12-20 18:00:19 +0100570pymain_clear_cmdline(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100571{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100572 PyMemAllocatorEx old_alloc;
573 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100574
Victor Stinnerca719ac2017-12-20 18:00:19 +0100575 clear_wstrlist(cmdline->nwarnoption, cmdline->warnoptions);
576 cmdline->nwarnoption = 0;
577 cmdline->warnoptions = NULL;
578
579 clear_wstrlist(cmdline->nenv_warnoption, cmdline->env_warnoptions);
580 cmdline->nenv_warnoption = 0;
581 cmdline->env_warnoptions = NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100582
583 if (pymain->use_bytes_argv && cmdline->argv != NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100584 clear_wstrlist(pymain->argc, cmdline->argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100585 }
586 cmdline->argv = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100587
588 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
589}
590
591
592static void
593pymain_clear_pymain(_PyMain *pymain)
594{
595#define CLEAR(ATTR) \
596 do { \
597 PyMem_RawFree(ATTR); \
598 ATTR = NULL; \
599 } while (0)
600
601 CLEAR(pymain->filename);
602 CLEAR(pymain->command);
603 CLEAR(pymain->module);
604#undef CLEAR
Victor Stinnerc4bca952017-12-19 23:48:17 +0100605}
606
Victor Stinnerc4bca952017-12-19 23:48:17 +0100607static void
Victor Stinner9cfc0022017-12-20 19:36:46 +0100608pymain_clear_config(_PyMain *pymain)
Victor Stinnerc4bca952017-12-19 23:48:17 +0100609{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100610 /* Clear core config with the memory allocator
611 used by pymain_read_conf() */
612 PyMemAllocatorEx old_alloc;
613 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
614
Victor Stinner9cfc0022017-12-20 19:36:46 +0100615 _PyCoreConfig_Clear(&pymain->config);
Victor Stinnerc4bca952017-12-19 23:48:17 +0100616
617 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
618}
619
620
621static void
622pymain_free_python(_PyMain *pymain)
623{
624 Py_CLEAR(pymain->main_importer_path);
Victor Stinnerd4341102017-11-23 00:12:09 +0100625
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800626#ifdef __INSURE__
627 /* Insure++ is a memory analysis tool that aids in discovering
628 * memory leaks and other memory problems. On Python exit, the
629 * interned string dictionaries are flagged as being in use at exit
630 * (which it is). Under normal circumstances, this is fine because
631 * the memory will be automatically reclaimed by the system. Under
632 * memory debugging, it's a huge source of useless noise, so we
633 * trade off slower shutdown for less distraction in the memory
634 * reports. -baw
635 */
636 _Py_ReleaseInternedUnicodeStrings();
637#endif /* __INSURE__ */
638}
639
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100640
641static void
Victor Stinnerc4bca952017-12-19 23:48:17 +0100642pymain_free_raw(_PyMain *pymain)
Victor Stinner94540602017-12-16 04:54:22 +0100643{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100644 _PyImport_Fini2();
Victor Stinner94540602017-12-16 04:54:22 +0100645
Victor Stinnerc4bca952017-12-19 23:48:17 +0100646 /* Free global variables which cannot be freed in Py_Finalize():
647 configuration options set before Py_Initialize() which should
648 remain valid after Py_Finalize(), since
649 Py_Initialize()-Py_Finalize() can be called multiple times. */
650 _PyPathConfig_Clear(&_Py_path_config);
Victor Stinner94540602017-12-16 04:54:22 +0100651
Victor Stinner31e99082017-12-20 23:41:38 +0100652 pymain_clear_config(pymain);
653
Victor Stinnerc4bca952017-12-19 23:48:17 +0100654 /* Force the allocator used by pymain_read_conf() */
655 PyMemAllocatorEx old_alloc;
656 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner94540602017-12-16 04:54:22 +0100657
Victor Stinnerca719ac2017-12-20 18:00:19 +0100658 pymain_clear_pymain(pymain);
659
660 clear_wstrlist(orig_argc, orig_argv);
661 orig_argc = 0;
662 orig_argv = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100663
Victor Stinnerc4bca952017-12-19 23:48:17 +0100664 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner92a3c6f2017-12-06 18:12:59 +0100665}
666
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100667
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800668static void
669pymain_free(_PyMain *pymain)
670{
Victor Stinnerc4bca952017-12-19 23:48:17 +0100671 pymain_free_python(pymain);
672 pymain_free_raw(pymain);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800673}
674
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100675
Eric Snow6b4be192017-05-22 21:36:03 -0700676static int
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800677pymain_run_main_from_importer(_PyMain *pymain)
Guido van Rossum667d7041995-08-04 04:20:48 +0000678{
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800679 /* Assume sys_path0 has already been checked by pymain_get_importer(),
680 * so put it in sys.path[0] and import __main__ */
Victor Stinner11a247d2017-12-13 21:05:57 +0100681 PyObject *sys_path = PySys_GetObject("path");
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800682 if (sys_path == NULL) {
683 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
684 goto error;
685 }
686
Victor Stinner11a247d2017-12-13 21:05:57 +0100687 if (PyList_Insert(sys_path, 0, pymain->main_importer_path)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800688 goto error;
689 }
690
Victor Stinner11a247d2017-12-13 21:05:57 +0100691 int sts = pymain_run_module(L"__main__", 0);
692 return (sts != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800693
694error:
695 Py_CLEAR(pymain->main_importer_path);
696 PyErr_Print();
697 return 1;
698}
699
700
Victor Stinner9cfc0022017-12-20 19:36:46 +0100701static _PyInitError
702wstrlist_append(int *len, wchar_t ***list, const wchar_t *str)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800703{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100704 wchar_t *str2 = _PyMem_RawWcsdup(str);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800705 if (str2 == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100706 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800707 }
708
Victor Stinnerca719ac2017-12-20 18:00:19 +0100709 size_t size = (*len + 1) * sizeof(list[0]);
710 wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size);
711 if (list2 == NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800712 PyMem_RawFree(str2);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100713 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800714 }
Victor Stinnerca719ac2017-12-20 18:00:19 +0100715 list2[*len] = str2;
716 *list = list2;
717 (*len)++;
Victor Stinner9cfc0022017-12-20 19:36:46 +0100718 return _Py_INIT_OK();
719}
720
721
722static int
723pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str)
724{
725 _PyInitError err = wstrlist_append(len, list, str);
726 if (_Py_INIT_FAILED(err)) {
727 pymain->err = err;
728 return -1;
729 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800730 return 0;
731}
732
733
734/* Parse the command line arguments
735 Return 0 on success.
736 Return 1 if parsing failed.
737 Set pymain->err and return -1 on other errors. */
738static int
Victor Stinnerca719ac2017-12-20 18:00:19 +0100739pymain_parse_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800740{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100741 _PyCoreConfig *config = &pymain->config;
742
Antoine Pitrou86838b02012-02-21 19:03:47 +0100743 _PyOS_ResetGetOpt();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800744 do {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800745 int longindex = -1;
Victor Stinnerc4bca952017-12-19 23:48:17 +0100746 int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800747 longoptions, &longindex);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800748 if (c == EOF) {
749 break;
750 }
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 if (c == 'c') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 /* -c is the last option; following arguments
754 that look like options are left for the
755 command to interpret. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800756 size_t len = wcslen(_PyOS_optarg) + 1 + 1;
757 wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
758 if (command == NULL) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100759 pymain->err = _Py_INIT_NO_MEMORY();
Victor Stinnera7368ac2017-11-15 18:11:45 -0800760 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800761 }
762 memcpy(command, _PyOS_optarg, len * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 command[len - 2] = '\n';
764 command[len - 1] = 0;
Victor Stinnerca719ac2017-12-20 18:00:19 +0100765 pymain->command = command;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 break;
767 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (c == 'm') {
770 /* -m is the last option; following arguments
771 that look like options are left for the
772 module to interpret. */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100773 pymain->module = pymain_wstrdup(pymain, _PyOS_optarg);
774 if (pymain->module == NULL) {
775 return -1;
776 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 break;
778 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 switch (c) {
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800781 case 0:
782 // Handle long option.
783 assert(longindex == 0); // Only one long option now.
784 if (!wcscmp(_PyOS_optarg, L"always")) {
785 cmdline->check_hash_pycs_mode = "always";
786 } else if (!wcscmp(_PyOS_optarg, L"never")) {
787 cmdline->check_hash_pycs_mode = "never";
788 } else if (!wcscmp(_PyOS_optarg, L"default")) {
789 cmdline->check_hash_pycs_mode = "default";
790 } else {
791 fprintf(stderr, "--check-hash-based-pycs must be one of "
792 "'default', 'always', or 'never'\n");
793 return 1;
794 }
795 break;
796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 case 'b':
Eric Snow6b4be192017-05-22 21:36:03 -0700798 cmdline->bytes_warning++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 case 'd':
Eric Snow6b4be192017-05-22 21:36:03 -0700802 cmdline->debug++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 case 'i':
Eric Snow6b4be192017-05-22 21:36:03 -0700806 cmdline->inspect++;
807 cmdline->interactive++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000809
Christian Heimesad73a9c2013-08-10 16:36:18 +0200810 case 'I':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100811 config->ignore_environment++;
Eric Snow6b4be192017-05-22 21:36:03 -0700812 cmdline->isolated++;
813 cmdline->no_user_site_directory++;
Christian Heimesad73a9c2013-08-10 16:36:18 +0200814 break;
815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 case 'O':
Eric Snow6b4be192017-05-22 21:36:03 -0700819 cmdline->optimization_level++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 case 'B':
Eric Snow6b4be192017-05-22 21:36:03 -0700823 cmdline->dont_write_bytecode++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 case 's':
Eric Snow6b4be192017-05-22 21:36:03 -0700827 cmdline->no_user_site_directory++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 case 'S':
Eric Snow6b4be192017-05-22 21:36:03 -0700831 cmdline->no_site_import++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 case 'E':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100835 config->ignore_environment++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 case 't':
839 /* ignored for backwards compatibility */
840 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 case 'u':
Eric Snow6b4be192017-05-22 21:36:03 -0700843 cmdline->use_unbuffered_io = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 case 'v':
Eric Snow6b4be192017-05-22 21:36:03 -0700847 cmdline->verbosity++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 case 'x':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100851 pymain->skip_first_line = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 case 'h':
855 case '?':
Eric Snow6b4be192017-05-22 21:36:03 -0700856 cmdline->print_help++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 case 'V':
Eric Snow6b4be192017-05-22 21:36:03 -0700860 cmdline->print_version++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 case 'W':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100864 if (pymain_wstrlist_append(pymain,
865 &cmdline->nwarnoption,
866 &cmdline->warnoptions,
867 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800868 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800869 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000871
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000872 case 'X':
Victor Stinnerca719ac2017-12-20 18:00:19 +0100873 if (pymain_wstrlist_append(pymain,
Victor Stinner9cfc0022017-12-20 19:36:46 +0100874 &config->nxoption,
875 &config->xoptions,
Victor Stinnerca719ac2017-12-20 18:00:19 +0100876 _PyOS_optarg) < 0) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800877 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800878 }
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000879 break;
880
Georg Brandl9d871192010-12-04 10:47:18 +0000881 case 'q':
Eric Snow6b4be192017-05-22 21:36:03 -0700882 cmdline->quiet_flag++;
Georg Brandl9d871192010-12-04 10:47:18 +0000883 break;
884
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100885 case 'R':
Victor Stinner9cfc0022017-12-20 19:36:46 +0100886 config->use_hash_seed = 0;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100887 break;
888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 default:
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800892 /* unknown argument: parsing failed */
893 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800895 } while (1);
896
Victor Stinnerca719ac2017-12-20 18:00:19 +0100897 if (pymain->command == NULL && pymain->module == NULL
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800898 && _PyOS_optind < pymain->argc
Victor Stinnerc4bca952017-12-19 23:48:17 +0100899 && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800900 {
Victor Stinnerca719ac2017-12-20 18:00:19 +0100901 pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]);
902 if (pymain->filename == NULL) {
903 return -1;
904 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000906
Victor Stinnerd5dda982017-12-13 17:31:16 +0100907 /* -c and -m options are exclusive */
Victor Stinnerca719ac2017-12-20 18:00:19 +0100908 assert(!(pymain->command != NULL && pymain->module != NULL));
Victor Stinnerd5dda982017-12-13 17:31:16 +0100909
Eric Snow6b4be192017-05-22 21:36:03 -0700910 return 0;
911}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000912
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800913
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800914static int
Victor Stinner9cfc0022017-12-20 19:36:46 +0100915add_xoption(PyObject *opts, const wchar_t *s)
Victor Stinner374c6e12017-12-14 12:05:26 +0100916{
917 PyObject *name, *value;
918
919 const wchar_t *name_end = wcschr(s, L'=');
920 if (!name_end) {
921 name = PyUnicode_FromWideChar(s, -1);
922 value = Py_True;
923 Py_INCREF(value);
924 }
925 else {
926 name = PyUnicode_FromWideChar(s, name_end - s);
927 value = PyUnicode_FromWideChar(name_end + 1, -1);
928 }
929 if (name == NULL || value == NULL) {
930 goto error;
931 }
932 if (PyDict_SetItem(opts, name, value) < 0) {
933 goto error;
934 }
935 Py_DECREF(name);
936 Py_DECREF(value);
937 return 0;
938
939error:
940 Py_XDECREF(name);
941 Py_XDECREF(value);
942 return -1;
943}
944
Victor Stinner9cfc0022017-12-20 19:36:46 +0100945
946static PyObject*
947config_create_xoptions_dict(const _PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800948{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100949 int nxoption = config->nxoption;
950 wchar_t **xoptions = config->xoptions;
Victor Stinner374c6e12017-12-14 12:05:26 +0100951 PyObject *dict = PyDict_New();
952 if (dict == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100953 return NULL;
Victor Stinner374c6e12017-12-14 12:05:26 +0100954 }
955
Victor Stinnerca719ac2017-12-20 18:00:19 +0100956 for (int i=0; i < nxoption; i++) {
957 wchar_t *option = xoptions[i];
Victor Stinner9cfc0022017-12-20 19:36:46 +0100958 if (add_xoption(dict, option) < 0) {
Victor Stinner374c6e12017-12-14 12:05:26 +0100959 Py_DECREF(dict);
Victor Stinner9cfc0022017-12-20 19:36:46 +0100960 return NULL;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800961 }
962 }
Victor Stinner374c6e12017-12-14 12:05:26 +0100963
Victor Stinner9cfc0022017-12-20 19:36:46 +0100964 return dict;
Eric Snow6b4be192017-05-22 21:36:03 -0700965}
966
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800967
Victor Stinner9cfc0022017-12-20 19:36:46 +0100968static _PyInitError
969config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options)
Eric Snow6b4be192017-05-22 21:36:03 -0700970{
Victor Stinnerca719ac2017-12-20 18:00:19 +0100971 for (int i = 0; i < len; i++) {
Victor Stinner9cfc0022017-12-20 19:36:46 +0100972 _PyInitError err = wstrlist_append(&config->nwarnoption,
973 &config->warnoptions,
974 options[i]);
975 if (_Py_INIT_FAILED(err)) {
976 return err;
Eric Snow6b4be192017-05-22 21:36:03 -0700977 }
Eric Snow6b4be192017-05-22 21:36:03 -0700978 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100979 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800980}
Eric Snow6b4be192017-05-22 21:36:03 -0700981
Victor Stinner747f48e2017-12-12 22:59:48 +0100982
Victor Stinner9cfc0022017-12-20 19:36:46 +0100983static _PyInitError
984config_init_warnoptions(_PyCoreConfig *config, _Py_CommandLineDetails *cmdline)
Victor Stinner747f48e2017-12-12 22:59:48 +0100985{
Victor Stinner9cfc0022017-12-20 19:36:46 +0100986 _PyInitError err;
987
988 assert(config->nwarnoption == 0);
989
Victor Stinner747f48e2017-12-12 22:59:48 +0100990 /* The priority order for warnings configuration is (highest precedence
991 * first):
992 *
993 * - the BytesWarning filter, if needed ('-b', '-bb')
994 * - any '-W' command line options; then
995 * - the 'PYTHONWARNINGS' environment variable; then
996 * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
997 * - any implicit filters added by _warnings.c/warnings.py
998 *
999 * All settings except the last are passed to the warnings module via
1000 * the `sys.warnoptions` list. Since the warnings module works on the basis
1001 * of "the most recently added filter will be checked first", we add
1002 * the lowest precedence entries first so that later entries override them.
1003 */
1004
Victor Stinner9cfc0022017-12-20 19:36:46 +01001005 if (config->dev_mode) {
1006 err = wstrlist_append(&config->nwarnoption,
1007 &config->warnoptions,
1008 L"default");
1009 if (_Py_INIT_FAILED(err)) {
1010 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001011 }
Victor Stinner747f48e2017-12-12 22:59:48 +01001012 }
Victor Stinner374c6e12017-12-14 12:05:26 +01001013
Victor Stinner9cfc0022017-12-20 19:36:46 +01001014 err = config_add_warnings_optlist(config,
1015 cmdline->nenv_warnoption,
1016 cmdline->env_warnoptions);
1017 if (_Py_INIT_FAILED(err)) {
1018 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001019 }
1020
Victor Stinner9cfc0022017-12-20 19:36:46 +01001021 err = config_add_warnings_optlist(config,
1022 cmdline->nwarnoption,
1023 cmdline->warnoptions);
1024 if (_Py_INIT_FAILED(err)) {
1025 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001026 }
1027
1028 /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
1029 * don't even try to emit a warning, so we skip setting the filter in that
1030 * case.
1031 */
1032 if (cmdline->bytes_warning) {
1033 wchar_t *filter;
1034 if (cmdline->bytes_warning> 1) {
1035 filter = L"error::BytesWarning";
1036 }
1037 else {
1038 filter = L"default::BytesWarning";
1039 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001040 err = wstrlist_append(&config->nwarnoption,
1041 &config->warnoptions,
1042 filter);
1043 if (_Py_INIT_FAILED(err)) {
1044 return err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001045 }
1046 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001047 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001048}
1049
1050
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001051/* Get warning options from PYTHONWARNINGS environment variable.
1052 Return 0 on success.
1053 Set pymain->err and return -1 on error. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001054static _PyInitError
1055cmdline_init_env_warnoptions(_Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001056{
1057 if (Py_IgnoreEnvironmentFlag) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001058 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +00001060
Victor Stinnerca719ac2017-12-20 18:00:19 +01001061 wchar_t *env;
1062 int res = config_get_env_var_dup(&env, L"PYTHONWARNINGS", "PYTHONWARNINGS");
1063 if (res < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001064 return DECODE_LOCALE_ERR("PYTHONWARNINGS", res);
Victor Stinnerca719ac2017-12-20 18:00:19 +01001065 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001066
Victor Stinnerca719ac2017-12-20 18:00:19 +01001067 if (env == NULL) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001068 return _Py_INIT_OK();
Victor Stinnerca719ac2017-12-20 18:00:19 +01001069 }
Philip Jenvey0805ca32010-04-07 04:04:10 +00001070
Victor Stinnerca719ac2017-12-20 18:00:19 +01001071
1072 wchar_t *warning, *context = NULL;
1073 for (warning = WCSTOK(env, L",", &context);
1074 warning != NULL;
1075 warning = WCSTOK(NULL, L",", &context))
1076 {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001077 _PyInitError err = wstrlist_append(&cmdline->nenv_warnoption,
1078 &cmdline->env_warnoptions,
1079 warning);
1080 if (_Py_INIT_FAILED(err)) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001081 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001082 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001083 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001085 PyMem_RawFree(env);
Victor Stinner9cfc0022017-12-20 19:36:46 +01001086 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001087}
1088
1089
1090static void
1091pymain_init_stdio(_PyMain *pymain)
1092{
1093 pymain->stdin_is_interactive = (isatty(fileno(stdin))
1094 || Py_InteractiveFlag);
Guido van Rossum775af911997-02-14 19:50:32 +00001095
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +00001096#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Victor Stinner89e34362011-01-07 18:47:22 +00001097 /* don't translate newlines (\r\n <=> \n) */
1098 _setmode(fileno(stdin), O_BINARY);
1099 _setmode(fileno(stdout), O_BINARY);
1100 _setmode(fileno(stderr), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +00001101#endif
Victor Stinner89e34362011-01-07 18:47:22 +00001102
1103 if (Py_UnbufferedStdioFlag) {
Guido van Rossum22ffac11998-03-06 15:30:39 +00001104#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
1106 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
1107 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001108#else /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 setbuf(stdin, (char *)NULL);
1110 setbuf(stdout, (char *)NULL);
1111 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001112#endif /* !HAVE_SETVBUF */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 }
1114 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +00001115#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* Doesn't have to have line-buffered -- use unbuffered */
1117 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
1118 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001119#else /* !MS_WINDOWS */
1120#ifdef HAVE_SETVBUF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
1122 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +00001123#endif /* HAVE_SETVBUF */
1124#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* Leave stderr alone - it should be unbuffered anyway. */
1126 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001127}
Guido van Rossum667d7041995-08-04 04:20:48 +00001128
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001129
1130/* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
Victor Stinner31a83932017-12-04 13:39:15 +01001131 environment variables on macOS if available. */
1132static _PyInitError
Victor Stinnerca719ac2017-12-20 18:00:19 +01001133config_init_program_name(_PyCoreConfig *config)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001134{
Victor Stinner31a83932017-12-04 13:39:15 +01001135 assert(config->program_name == NULL);
1136
1137 /* If Py_SetProgramName() was called, use its value */
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001138 const wchar_t *program_name = _Py_path_config.program_name;
Victor Stinner31a83932017-12-04 13:39:15 +01001139 if (program_name != NULL) {
1140 config->program_name = _PyMem_RawWcsdup(program_name);
1141 if (config->program_name == NULL) {
1142 return _Py_INIT_NO_MEMORY();
1143 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001144 return _Py_INIT_OK();
Victor Stinner31a83932017-12-04 13:39:15 +01001145 }
1146
Just van Rossum2ac79ef2003-03-05 15:46:54 +00001147#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 /* On MacOS X, when the Python interpreter is embedded in an
1149 application bundle, it gets executed by a bootstrapping script
1150 that does os.execve() with an argv[0] that's different from the
1151 actual Python executable. This is needed to keep the Finder happy,
1152 or rather, to work around Apple's overly strict requirements of
1153 the process name. However, we still need a usable sys.executable,
1154 so the actual executable path is passed in an environment variable.
1155 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
1156 script. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001157 const char *p = config_get_env_var("PYTHONEXECUTABLE");
Victor Stinner6bf992a2017-12-06 17:26:10 +01001158 if (p != NULL) {
Victor Stinner31a83932017-12-04 13:39:15 +01001159 size_t len;
1160 wchar_t* program_name = Py_DecodeLocale(p, &len);
1161 if (program_name == NULL) {
1162 return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment "
1163 "variable", (Py_ssize_t)len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 }
Victor Stinner31a83932017-12-04 13:39:15 +01001165 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001166 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001167 }
Vinay Sajip90db6612012-07-17 17:33:46 +01001168#ifdef WITH_NEXT_FRAMEWORK
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001169 else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001170 const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
Vinay Sajip90db6612012-07-17 17:33:46 +01001171 if (pyvenv_launcher && *pyvenv_launcher) {
1172 /* Used by Mac/Tools/pythonw.c to forward
1173 * the argv0 of the stub executable
1174 */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001175 size_t len;
Victor Stinner31a83932017-12-04 13:39:15 +01001176 wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len);
1177 if (program_name == NULL) {
1178 return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment "
1179 "variable", (Py_ssize_t)len);
Vinay Sajip90db6612012-07-17 17:33:46 +01001180 }
Victor Stinner31a83932017-12-04 13:39:15 +01001181 config->program_name = program_name;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001182 return _Py_INIT_OK();
Vinay Sajip90db6612012-07-17 17:33:46 +01001183 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001185#endif /* WITH_NEXT_FRAMEWORK */
1186#endif /* __APPLE__ */
Victor Stinneraf5a8952017-12-02 10:11:32 +01001187
Victor Stinnerca719ac2017-12-20 18:00:19 +01001188 /* Use argv[0] by default, if available */
1189 if (config->program != NULL) {
1190 config->program_name = _PyMem_RawWcsdup(config->program);
1191 if (config->program_name == NULL) {
1192 return _Py_INIT_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001193 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001194 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001195 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001196
1197 /* Last fall back: hardcoded string */
1198#ifdef MS_WINDOWS
1199 const wchar_t *default_program_name = L"python";
1200#else
1201 const wchar_t *default_program_name = L"python3";
1202#endif
1203 config->program_name = _PyMem_RawWcsdup(default_program_name);
1204 if (config->program_name == NULL) {
1205 return _Py_INIT_NO_MEMORY();
1206 }
1207 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001208}
1209
1210
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001211static void
1212pymain_header(_PyMain *pymain)
1213{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001214 if (Py_QuietFlag) {
1215 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 }
Guido van Rossum393661d2001-08-31 17:40:15 +00001217
Victor Stinner19760862017-12-20 01:41:59 +01001218 if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001219 return;
1220 }
1221
1222 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
1223 if (!Py_NoSiteFlag) {
1224 fprintf(stderr, "%s\n", COPYRIGHT);
1225 }
1226}
1227
1228
Victor Stinnerc4bca952017-12-19 23:48:17 +01001229static wchar_t**
Victor Stinnerca719ac2017-12-20 18:00:19 +01001230copy_wstrlist(int len, wchar_t **list)
Victor Stinner11a247d2017-12-13 21:05:57 +01001231{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001232 assert((len > 0 && list != NULL) || len == 0);
1233 size_t size = len * sizeof(list[0]);
1234 wchar_t **list_copy = PyMem_RawMalloc(size);
1235 for (int i=0; i < len; i++) {
1236 wchar_t* arg = _PyMem_RawWcsdup(list[i]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001237 if (arg == NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001238 clear_wstrlist(i, list);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001239 return NULL;
1240 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001241 list_copy[i] = arg;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001242 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001243 return list_copy;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001244}
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001245
Victor Stinnerc4bca952017-12-19 23:48:17 +01001246
1247static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001248pymain_init_core_argv(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001249{
Victor Stinnerc4bca952017-12-19 23:48:17 +01001250 /* Copy argv to be able to modify it (to force -c/-m) */
1251 int argc = pymain->argc - _PyOS_optind;
1252 wchar_t **argv;
1253
1254 if (argc <= 0 || cmdline->argv == NULL) {
Victor Stinner11a247d2017-12-13 21:05:57 +01001255 /* Ensure at least one (empty) argument is seen */
1256 static wchar_t *empty_argv[1] = {L""};
Victor Stinner11a247d2017-12-13 21:05:57 +01001257 argc = 1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001258 argv = copy_wstrlist(1, empty_argv);
Victor Stinner11a247d2017-12-13 21:05:57 +01001259 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01001260 else {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001261 argv = copy_wstrlist(argc, &cmdline->argv[_PyOS_optind]);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001262 }
1263
1264 if (argv == NULL) {
1265 pymain->err = _Py_INIT_NO_MEMORY();
1266 return -1;
1267 }
1268
1269 wchar_t *arg0 = NULL;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001270 if (pymain->command != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001271 /* Force sys.argv[0] = '-c' */
1272 arg0 = L"-c";
1273 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001274 else if (pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001275 /* Force sys.argv[0] = '-m'*/
1276 arg0 = L"-m";
1277 }
1278 if (arg0 != NULL) {
1279 arg0 = _PyMem_RawWcsdup(arg0);
1280 if (arg0 == NULL) {
Victor Stinnerca719ac2017-12-20 18:00:19 +01001281 clear_wstrlist(argc, argv);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001282 pymain->err = _Py_INIT_NO_MEMORY();
1283 return -1;
1284 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01001285
1286 assert(argc >= 1);
Victor Stinnerc4bca952017-12-19 23:48:17 +01001287 PyMem_RawFree(argv[0]);
1288 argv[0] = arg0;
1289 }
1290
Victor Stinner9cfc0022017-12-20 19:36:46 +01001291 pymain->config.argc = argc;
1292 pymain->config.argv = argv;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001293 return 0;
1294}
1295
1296
Victor Stinner8ded5b82018-01-24 17:03:28 +01001297static PyObject*
1298wstrlist_as_pylist(int len, wchar_t **list)
Victor Stinnerc4bca952017-12-19 23:48:17 +01001299{
Victor Stinner8ded5b82018-01-24 17:03:28 +01001300 assert(list != NULL || len < 1);
1301
1302 PyObject *pylist = PyList_New(len);
1303 if (pylist == NULL) {
1304 return NULL;
Victor Stinnerc4bca952017-12-19 23:48:17 +01001305 }
1306
Victor Stinner8ded5b82018-01-24 17:03:28 +01001307 for (int i = 0; i < len; i++) {
1308 PyObject *v = PyUnicode_FromWideChar(list[i], -1);
Victor Stinner11a247d2017-12-13 21:05:57 +01001309 if (v == NULL) {
Victor Stinner8ded5b82018-01-24 17:03:28 +01001310 Py_DECREF(pylist);
1311 return NULL;
Victor Stinner11a247d2017-12-13 21:05:57 +01001312 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001313 PyList_SET_ITEM(pylist, i, v);
Victor Stinner11a247d2017-12-13 21:05:57 +01001314 }
Victor Stinner8ded5b82018-01-24 17:03:28 +01001315 return pylist;
Victor Stinner11a247d2017-12-13 21:05:57 +01001316}
1317
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001318
Victor Stinner11a247d2017-12-13 21:05:57 +01001319static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01001320pymain_compute_path0(_PyMain *pymain, PyObject **path0)
Victor Stinner11a247d2017-12-13 21:05:57 +01001321{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001322 if (pymain->main_importer_path != NULL) {
1323 /* Let pymain_run_main_from_importer() adjust sys.path[0] later */
Victor Stinner19760862017-12-20 01:41:59 +01001324 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001325 return 0;
1326 }
1327
1328 if (Py_IsolatedFlag) {
Victor Stinner19760862017-12-20 01:41:59 +01001329 *path0 = NULL;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001330 return 0;
1331 }
1332
Victor Stinner9cfc0022017-12-20 19:36:46 +01001333 *path0 = _PyPathConfig_ComputeArgv0(pymain->config.argc,
1334 pymain->config.argv);
Victor Stinner19760862017-12-20 01:41:59 +01001335 if (*path0 == NULL) {
1336 pymain->err = _Py_INIT_NO_MEMORY();
1337 return -1;
1338 }
1339 return 0;
1340}
1341
1342
1343static int
1344pymain_update_sys_path(_PyMain *pymain, PyObject *path0)
1345{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001346 /* Prepend argv[0] to sys.path.
1347 If argv[0] is a symlink, use the real path. */
1348 PyObject *sys_path = PySys_GetObject("path");
1349 if (sys_path == NULL) {
1350 pymain->err = _Py_INIT_ERR("can't get sys.path");
Victor Stinnerd5dda982017-12-13 17:31:16 +01001351 return -1;
1352 }
1353
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001354 /* Prepend path0 to sys.path */
1355 if (PyList_Insert(sys_path, 0, path0) < 0) {
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001356 pymain->err = _Py_INIT_ERR("sys.path.insert(0, path0) failed");
1357 return -1;
1358 }
Victor Stinnerd5dda982017-12-13 17:31:16 +01001359 return 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001360}
1361
1362
Victor Stinner6bf992a2017-12-06 17:26:10 +01001363/* Get Py_xxx global configuration variables */
1364static void
Victor Stinnerca719ac2017-12-20 18:00:19 +01001365pymain_get_global_config(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinner6bf992a2017-12-06 17:26:10 +01001366{
Victor Stinner91106cd2017-12-13 12:29:09 +01001367 cmdline->bytes_warning = Py_BytesWarningFlag;
1368 cmdline->debug = Py_DebugFlag;
1369 cmdline->inspect = Py_InspectFlag;
1370 cmdline->interactive = Py_InteractiveFlag;
1371 cmdline->isolated = Py_IsolatedFlag;
1372 cmdline->optimization_level = Py_OptimizeFlag;
1373 cmdline->dont_write_bytecode = Py_DontWriteBytecodeFlag;
1374 cmdline->no_user_site_directory = Py_NoUserSiteDirectory;
1375 cmdline->no_site_import = Py_NoSiteFlag;
1376 cmdline->use_unbuffered_io = Py_UnbufferedStdioFlag;
1377 cmdline->verbosity = Py_VerboseFlag;
1378 cmdline->quiet_flag = Py_QuietFlag;
1379#ifdef MS_WINDOWS
1380 cmdline->legacy_windows_fs_encoding = Py_LegacyWindowsFSEncodingFlag;
1381 cmdline->legacy_windows_stdio = Py_LegacyWindowsStdioFlag;
1382#endif
1383 cmdline->check_hash_pycs_mode = _Py_CheckHashBasedPycsMode ;
1384
Victor Stinner9cfc0022017-12-20 19:36:46 +01001385 pymain->config.ignore_environment = Py_IgnoreEnvironmentFlag;
1386 pymain->config.utf8_mode = Py_UTF8Mode;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001387}
1388
1389
Victor Stinner19760862017-12-20 01:41:59 +01001390/* Set Py_xxx global configuration variables */
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001391static void
Victor Stinnerca719ac2017-12-20 18:00:19 +01001392pymain_set_global_config(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001393{
Victor Stinner91106cd2017-12-13 12:29:09 +01001394 Py_BytesWarningFlag = cmdline->bytes_warning;
1395 Py_DebugFlag = cmdline->debug;
1396 Py_InspectFlag = cmdline->inspect;
1397 Py_InteractiveFlag = cmdline->interactive;
1398 Py_IsolatedFlag = cmdline->isolated;
1399 Py_OptimizeFlag = cmdline->optimization_level;
1400 Py_DontWriteBytecodeFlag = cmdline->dont_write_bytecode;
1401 Py_NoUserSiteDirectory = cmdline->no_user_site_directory;
1402 Py_NoSiteFlag = cmdline->no_site_import;
1403 Py_UnbufferedStdioFlag = cmdline->use_unbuffered_io;
1404 Py_VerboseFlag = cmdline->verbosity;
1405 Py_QuietFlag = cmdline->quiet_flag;
1406 _Py_CheckHashBasedPycsMode = cmdline->check_hash_pycs_mode;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001407#ifdef MS_WINDOWS
Victor Stinner91106cd2017-12-13 12:29:09 +01001408 Py_LegacyWindowsFSEncodingFlag = cmdline->legacy_windows_fs_encoding;
1409 Py_LegacyWindowsStdioFlag = cmdline->legacy_windows_stdio;
Victor Stinner6bf992a2017-12-06 17:26:10 +01001410#endif
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001411
Victor Stinner9cfc0022017-12-20 19:36:46 +01001412 Py_IgnoreEnvironmentFlag = pymain->config.ignore_environment;
1413 Py_UTF8Mode = pymain->config.utf8_mode;
Victor Stinner358e5e12017-12-15 00:51:22 +01001414
1415 /* Random or non-zero hash seed */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001416 Py_HashRandomizationFlag = (pymain->config.use_hash_seed == 0 ||
1417 pymain->config.hash_seed != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001418}
1419
1420
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001421static void
1422pymain_import_readline(_PyMain *pymain)
1423{
1424 if (Py_IsolatedFlag) {
1425 return;
1426 }
Victor Stinner19760862017-12-20 01:41:59 +01001427 if (!Py_InspectFlag && RUN_CODE(pymain)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001428 return;
1429 }
1430 if (!isatty(fileno(stdin))) {
1431 return;
Nick Coghland2977a32017-03-12 20:38:32 +10001432 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001433
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001434 PyObject *mod = PyImport_ImportModule("readline");
1435 if (mod == NULL) {
1436 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 }
1438 else {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001439 Py_DECREF(mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001441}
1442
1443
1444static FILE*
1445pymain_open_filename(_PyMain *pymain)
1446{
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001447 FILE* fp;
1448
Victor Stinnerca719ac2017-12-20 18:00:19 +01001449 fp = _Py_wfopen(pymain->filename, L"r");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001450 if (fp == NULL) {
1451 char *cfilename_buffer;
1452 const char *cfilename;
1453 int err = errno;
Victor Stinner9dd76202017-12-21 16:20:32 +01001454 cfilename_buffer = _Py_EncodeLocaleRaw(pymain->filename, NULL);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001455 if (cfilename_buffer != NULL)
1456 cfilename = cfilename_buffer;
1457 else
1458 cfilename = "<unprintable file name>";
1459 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinner9cfc0022017-12-20 19:36:46 +01001460 pymain->config.program, cfilename, err, strerror(err));
Victor Stinner9dd76202017-12-21 16:20:32 +01001461 PyMem_RawFree(cfilename_buffer);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001462 pymain->status = 2;
1463 return NULL;
1464 }
1465
Victor Stinnerca719ac2017-12-20 18:00:19 +01001466 if (pymain->skip_first_line) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001467 int ch;
1468 /* Push back first newline so line numbers
1469 remain the same */
1470 while ((ch = getc(fp)) != EOF) {
1471 if (ch == '\n') {
1472 (void)ungetc(ch, fp);
1473 break;
1474 }
1475 }
1476 }
1477
1478 struct _Py_stat_struct sb;
1479 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 &&
1480 S_ISDIR(sb.st_mode)) {
1481 fprintf(stderr,
1482 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinner9cfc0022017-12-20 19:36:46 +01001483 pymain->config.program, pymain->filename);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001484 fclose(fp);
1485 pymain->status = 1;
1486 return NULL;
1487 }
1488
1489 return fp;
1490}
1491
1492
1493static void
Victor Stinner19760862017-12-20 01:41:59 +01001494pymain_run_filename(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001495{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001496 if (pymain->filename == NULL && pymain->stdin_is_interactive) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001497 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner19760862017-12-20 01:41:59 +01001498 pymain_run_startup(cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001499 pymain_run_interactive_hook();
1500 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001501
1502 if (pymain->main_importer_path != NULL) {
1503 pymain->status = pymain_run_main_from_importer(pymain);
1504 return;
1505 }
1506
1507 FILE *fp;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001508 if (pymain->filename != NULL) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001509 fp = pymain_open_filename(pymain);
1510 if (fp == NULL) {
1511 return;
1512 }
1513 }
1514 else {
1515 fp = stdin;
1516 }
1517
Victor Stinnerca719ac2017-12-20 18:00:19 +01001518 pymain->status = pymain_run_file(fp, pymain->filename, cf);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001519}
1520
1521
1522static void
Victor Stinner19760862017-12-20 01:41:59 +01001523pymain_repl(_PyMain *pymain, PyCompilerFlags *cf)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 /* Check this environment variable at the end, to give programs the
Victor Stinnera7368ac2017-11-15 18:11:45 -08001526 opportunity to set it from Python. */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001527 if (!Py_InspectFlag && config_get_env_var("PYTHONINSPECT")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 Py_InspectFlag = 1;
1529 }
Guido van Rossum667d7041995-08-04 04:20:48 +00001530
Victor Stinner19760862017-12-20 01:41:59 +01001531 if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) {
Victor Stinnera7368ac2017-11-15 18:11:45 -08001532 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001534
1535 Py_InspectFlag = 0;
1536 pymain_run_interactive_hook();
Victor Stinner33c377e2017-12-05 15:12:41 +01001537
Victor Stinner19760862017-12-20 01:41:59 +01001538 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001539 pymain->status = (res != 0);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001540}
1541
1542
1543/* Parse the command line.
1544 Handle --version and --help options directly.
1545
1546 Return 1 if Python must exit.
1547 Return 0 on success.
1548 Set pymain->err and return -1 on failure. */
1549static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001550pymain_parse_cmdline(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001551{
Victor Stinnerca719ac2017-12-20 18:00:19 +01001552 int res = pymain_parse_cmdline_impl(pymain, cmdline);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001553 if (res < 0) {
1554 return -1;
1555 }
1556 if (res) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001557 pymain_usage(1, pymain->config.program);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001558 pymain->status = 2;
1559 return 1;
1560 }
1561
Victor Stinnerca719ac2017-12-20 18:00:19 +01001562 if (pymain->command != NULL || pymain->module != NULL) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001563 /* Backup _PyOS_optind */
1564 _PyOS_optind--;
1565 }
1566
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001567 return 0;
1568}
1569
1570
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001571static const wchar_t*
Victor Stinner9cfc0022017-12-20 19:36:46 +01001572config_get_xoption(_PyCoreConfig *config, wchar_t *name)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001573{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001574 int nxoption = config->nxoption;
1575 wchar_t **xoptions = config->xoptions;
Victor Stinnerca719ac2017-12-20 18:00:19 +01001576 for (int i=0; i < nxoption; i++) {
1577 wchar_t *option = xoptions[i];
Victor Stinnera7368ac2017-11-15 18:11:45 -08001578 size_t len;
1579 wchar_t *sep = wcschr(option, L'=');
1580 if (sep != NULL) {
1581 len = (sep - option);
1582 }
1583 else {
1584 len = wcslen(option);
1585 }
1586 if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
1587 return option;
1588 }
1589 }
1590 return NULL;
1591}
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001592
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001593
Victor Stinnera7368ac2017-11-15 18:11:45 -08001594static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001595pymain_str_to_int(const char *str, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001596{
1597 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001598 const char *endptr = str;
1599 long value = strtol(str, (char **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001600 if (*endptr != '\0' || errno == ERANGE) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001601 return -1;
1602 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001603 if (value < INT_MIN || value > INT_MAX) {
1604 return -1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001605 }
1606
Victor Stinnera7368ac2017-11-15 18:11:45 -08001607 *result = (int)value;
1608 return 0;
1609}
1610
1611
1612static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001613pymain_wstr_to_int(const wchar_t *wstr, int *result)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001614{
1615 errno = 0;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001616 const wchar_t *endptr = wstr;
1617 long value = wcstol(wstr, (wchar_t **)&endptr, 10);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001618 if (*endptr != '\0' || errno == ERANGE) {
1619 return -1;
1620 }
1621 if (value < INT_MIN || value > INT_MAX) {
1622 return -1;
1623 }
1624
1625 *result = (int)value;
1626 return 0;
1627}
1628
1629
Victor Stinner9cfc0022017-12-20 19:36:46 +01001630static _PyInitError
1631pymain_init_tracemalloc(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001632{
1633 int nframe;
1634 int valid;
1635
Victor Stinner9cfc0022017-12-20 19:36:46 +01001636 const char *env = config_get_env_var("PYTHONTRACEMALLOC");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001637 if (env) {
1638 if (!pymain_str_to_int(env, &nframe)) {
1639 valid = (nframe >= 1);
1640 }
1641 else {
1642 valid = 0;
1643 }
1644 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001645 return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number "
1646 "of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001647 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001648 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001649 }
1650
Victor Stinner9cfc0022017-12-20 19:36:46 +01001651 const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001652 if (xoption) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001653 const wchar_t *sep = wcschr(xoption, L'=');
Victor Stinnera7368ac2017-11-15 18:11:45 -08001654 if (sep) {
1655 if (!pymain_wstr_to_int(sep + 1, &nframe)) {
1656 valid = (nframe >= 1);
1657 }
1658 else {
1659 valid = 0;
1660 }
1661 if (!valid) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001662 return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: "
1663 "invalid number of frames");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001664 }
1665 }
1666 else {
1667 /* -X tracemalloc behaves as -X tracemalloc=1 */
1668 nframe = 1;
1669 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001670 config->tracemalloc = nframe;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001671 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001672 return _Py_INIT_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001673}
1674
1675
1676static void
Victor Stinner9cfc0022017-12-20 19:36:46 +01001677get_env_flag(int *flag, const char *name)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001678{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001679 const char *var = config_get_env_var(name);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001680 if (!var) {
1681 return;
1682 }
1683 int value;
1684 if (pymain_str_to_int(var, &value) < 0 || value < 0) {
1685 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
1686 value = 1;
1687 }
1688 if (*flag < value) {
1689 *flag = value;
1690 }
1691}
1692
1693
1694static void
Victor Stinner9cfc0022017-12-20 19:36:46 +01001695cmdline_get_env_flags(_Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001696{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001697 get_env_flag(&cmdline->debug, "PYTHONDEBUG");
1698 get_env_flag(&cmdline->verbosity, "PYTHONVERBOSE");
1699 get_env_flag(&cmdline->optimization_level, "PYTHONOPTIMIZE");
1700 get_env_flag(&cmdline->inspect, "PYTHONINSPECT");
1701 get_env_flag(&cmdline->dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
1702 get_env_flag(&cmdline->no_user_site_directory, "PYTHONNOUSERSITE");
1703 get_env_flag(&cmdline->use_unbuffered_io, "PYTHONUNBUFFERED");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001704#ifdef MS_WINDOWS
Victor Stinner9cfc0022017-12-20 19:36:46 +01001705 get_env_flag(&cmdline->legacy_windows_fs_encoding,
1706 "PYTHONLEGACYWINDOWSFSENCODING");
1707 get_env_flag(&cmdline->legacy_windows_stdio,
1708 "PYTHONLEGACYWINDOWSSTDIO");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001709#endif
1710}
1711
1712
Victor Stinner46972b72017-11-24 22:55:40 +01001713static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001714config_init_home(_PyCoreConfig *config)
Victor Stinner1f151112017-11-23 10:43:14 +01001715{
1716 wchar_t *home;
1717
Victor Stinner31a83932017-12-04 13:39:15 +01001718 /* If Py_SetPythonHome() was called, use its value */
1719 home = _Py_path_config.home;
Victor Stinner1f151112017-11-23 10:43:14 +01001720 if (home) {
Victor Stinner46972b72017-11-24 22:55:40 +01001721 config->home = _PyMem_RawWcsdup(home);
1722 if (config->home == NULL) {
1723 return _Py_INIT_NO_MEMORY();
Victor Stinner1f151112017-11-23 10:43:14 +01001724 }
Victor Stinner46972b72017-11-24 22:55:40 +01001725 return _Py_INIT_OK();
Victor Stinner1f151112017-11-23 10:43:14 +01001726 }
1727
Victor Stinner46972b72017-11-24 22:55:40 +01001728 int res = config_get_env_var_dup(&home, L"PYTHONHOME", "PYTHONHOME");
Victor Stinner1f151112017-11-23 10:43:14 +01001729 if (res < 0) {
Victor Stinner46972b72017-11-24 22:55:40 +01001730 return DECODE_LOCALE_ERR("PYTHONHOME", res);
Victor Stinner1f151112017-11-23 10:43:14 +01001731 }
Victor Stinner46972b72017-11-24 22:55:40 +01001732 config->home = home;
1733 return _Py_INIT_OK();
Victor Stinnerd4341102017-11-23 00:12:09 +01001734}
1735
1736
Victor Stinner358e5e12017-12-15 00:51:22 +01001737static _PyInitError
1738config_init_hash_seed(_PyCoreConfig *config)
1739{
1740 if (config->use_hash_seed < 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001741 const char *seed_text = config_get_env_var("PYTHONHASHSEED");
Victor Stinner358e5e12017-12-15 00:51:22 +01001742 int use_hash_seed;
1743 unsigned long hash_seed;
1744 if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
1745 return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" "
1746 "or an integer in range [0; 4294967295]");
1747 }
1748 config->use_hash_seed = use_hash_seed;
1749 config->hash_seed = hash_seed;
1750 }
1751 return _Py_INIT_OK();
1752}
1753
1754
Victor Stinner9cfc0022017-12-20 19:36:46 +01001755static _PyInitError
1756config_init_utf8_mode(_PyCoreConfig *config)
Victor Stinner91106cd2017-12-13 12:29:09 +01001757{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001758 /* The option was already set by Py_UTF8Mode,
1759 Py_LegacyWindowsFSEncodingFlag or PYTHONLEGACYWINDOWSFSENCODING. */
1760 if (config->utf8_mode >= 0) {
1761 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001762 }
Victor Stinner91106cd2017-12-13 12:29:09 +01001763
Victor Stinner9cfc0022017-12-20 19:36:46 +01001764 const wchar_t *xopt = config_get_xoption(config, L"utf8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001765 if (xopt) {
1766 wchar_t *sep = wcschr(xopt, L'=');
1767 if (sep) {
1768 xopt = sep + 1;
1769 if (wcscmp(xopt, L"1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001770 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001771 }
1772 else if (wcscmp(xopt, L"0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001773 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001774 }
1775 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001776 return _Py_INIT_USER_ERR("invalid -X utf8 option value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001777 }
1778 }
1779 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001780 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001781 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001782 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001783 }
1784
Victor Stinner9cfc0022017-12-20 19:36:46 +01001785 const char *opt = config_get_env_var("PYTHONUTF8");
Victor Stinner91106cd2017-12-13 12:29:09 +01001786 if (opt) {
1787 if (strcmp(opt, "1") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001788 config->utf8_mode = 1;
Victor Stinner91106cd2017-12-13 12:29:09 +01001789 }
1790 else if (strcmp(opt, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001791 config->utf8_mode = 0;
Victor Stinner91106cd2017-12-13 12:29:09 +01001792 }
1793 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001794 return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
1795 "variable value");
Victor Stinner91106cd2017-12-13 12:29:09 +01001796 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001797 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001798 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01001799
1800 return _Py_INIT_OK();
Victor Stinner91106cd2017-12-13 12:29:09 +01001801}
Victor Stinner46972b72017-11-24 22:55:40 +01001802
1803
Victor Stinner9cfc0022017-12-20 19:36:46 +01001804static _PyInitError
1805config_read_env_vars(_PyCoreConfig *config)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001806{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001807 config->allocator = config_get_env_var("PYTHONMALLOC");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001808
Victor Stinner9cfc0022017-12-20 19:36:46 +01001809 if (config_get_env_var("PYTHONDUMPREFS")) {
1810 config->dump_refs = 1;
1811 }
1812 if (config_get_env_var("PYTHONMALLOCSTATS")) {
1813 config->malloc_stats = 1;
Victor Stinner31a83932017-12-04 13:39:15 +01001814 }
1815
Victor Stinner9cfc0022017-12-20 19:36:46 +01001816 const char *env = config_get_env_var("PYTHONCOERCECLOCALE");
Victor Stinner94540602017-12-16 04:54:22 +01001817 if (env) {
1818 if (strcmp(env, "0") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001819 config->coerce_c_locale = 0;
Victor Stinner94540602017-12-16 04:54:22 +01001820 }
1821 else if (strcmp(env, "warn") == 0) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001822 config->coerce_c_locale_warn = 1;
Victor Stinner94540602017-12-16 04:54:22 +01001823 }
1824 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001825 config->coerce_c_locale = 1;
Victor Stinner94540602017-12-16 04:54:22 +01001826 }
1827 }
1828
Victor Stinner9cfc0022017-12-20 19:36:46 +01001829 wchar_t *path;
1830 int res = config_get_env_var_dup(&path, L"PYTHONPATH", "PYTHONPATH");
1831 if (res < 0) {
1832 return DECODE_LOCALE_ERR("PYTHONHOME", res);
1833 }
1834 config->module_search_path_env = path;
1835
1836 _PyInitError err = config_init_hash_seed(config);
1837 if (_Py_INIT_FAILED(err)) {
1838 return err;
1839 }
1840
1841 return _Py_INIT_OK();
1842}
1843
1844
1845static _PyInitError
1846config_read_complex_options(_PyCoreConfig *config)
1847{
1848 /* More complex options configured by env var and -X option */
1849 if (config_get_env_var("PYTHONFAULTHANDLER")
1850 || config_get_xoption(config, L"faulthandler")) {
1851 config->faulthandler = 1;
1852 }
1853 if (config_get_env_var("PYTHONPROFILEIMPORTTIME")
1854 || config_get_xoption(config, L"importtime")) {
1855 config->import_time = 1;
1856 }
1857 if (config_get_xoption(config, L"dev" ) ||
1858 config_get_env_var("PYTHONDEVMODE"))
1859 {
1860 config->dev_mode = 1;
1861 config->faulthandler = 1;
1862 config->allocator = "debug";
1863 }
1864
1865 _PyInitError err = pymain_init_tracemalloc(config);
1866 if (_Py_INIT_FAILED(err)) {
1867 return err;
1868 }
1869 return _Py_INIT_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001870}
1871
1872
Victor Stinnera7368ac2017-11-15 18:11:45 -08001873/* Parse command line options and environment variables.
1874 This code must not use Python runtime apart PyMem_Raw memory allocator.
1875
1876 Return 0 on success.
1877 Return 1 if Python is done and must exit.
1878 Set pymain->err and return -1 on error. */
1879static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001880pymain_read_conf_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001881{
Victor Stinner9cfc0022017-12-20 19:36:46 +01001882 _PyInitError err;
1883
Victor Stinnerca719ac2017-12-20 18:00:19 +01001884 int res = pymain_parse_cmdline(pymain, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01001885 if (res != 0) {
1886 return res;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001887 }
1888
Victor Stinner9cfc0022017-12-20 19:36:46 +01001889 /* Set Py_IgnoreEnvironmentFlag for Py_GETENV() */
1890 _PyCoreConfig *config = &pymain->config;
1891 Py_IgnoreEnvironmentFlag = config->ignore_environment;
1892
1893 /* Get environment variables */
1894 cmdline_get_env_flags(cmdline);
1895
1896 err = cmdline_init_env_warnoptions(cmdline);
1897 if (_Py_INIT_FAILED(err)) {
1898 pymain->err = err;
1899 return -1;
1900 }
1901
1902#ifdef MS_WINDOWS
1903 if (cmdline->legacy_windows_fs_encoding) {
1904 config->utf8_mode = 0;
1905 }
1906#endif
1907
Victor Stinnerca719ac2017-12-20 18:00:19 +01001908 if (pymain_init_core_argv(pymain, cmdline) < 0) {
Victor Stinner19760862017-12-20 01:41:59 +01001909 return -1;
1910 }
1911
Victor Stinner2b822a02018-01-25 09:18:36 +01001912 /* On Windows, _PyPathConfig_Init() modifies Py_IsolatedFlag and
1913 Py_NoSiteFlag variables if a "._pth" file is found. */
1914 int init_isolated = Py_IsolatedFlag;
1915 int init_no_site = Py_NoSiteFlag;
1916 Py_IsolatedFlag = cmdline->isolated;
1917 Py_NoSiteFlag = cmdline->no_site_import;
Victor Stinner8ded5b82018-01-24 17:03:28 +01001918
Victor Stinner9cfc0022017-12-20 19:36:46 +01001919 err = _PyCoreConfig_Read(config);
Victor Stinner2b822a02018-01-25 09:18:36 +01001920
1921 cmdline->isolated = Py_IsolatedFlag;
1922 cmdline->no_site_import = Py_NoSiteFlag;
1923 Py_IsolatedFlag = init_isolated;
1924 Py_NoSiteFlag = init_no_site;
1925
Victor Stinner31a83932017-12-04 13:39:15 +01001926 if (_Py_INIT_FAILED(err)) {
1927 pymain->err = err;
1928 return -1;
1929 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08001930 return 0;
1931}
1932
1933
Victor Stinner19760862017-12-20 01:41:59 +01001934/* Read the configuration, but initialize also the LC_CTYPE locale:
1935 enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
Victor Stinnera7368ac2017-11-15 18:11:45 -08001936static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01001937pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001938{
Victor Stinner94540602017-12-16 04:54:22 +01001939 int res = -1;
1940
Victor Stinner94540602017-12-16 04:54:22 +01001941 char *oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
1942 if (oldloc == NULL) {
1943 pymain->err = _Py_INIT_NO_MEMORY();
1944 goto done;
1945 }
1946
1947 /* Reconfigure the locale to the default for this process */
1948 _Py_SetLocaleFromEnv(LC_ALL);
1949
1950 int locale_coerced = 0;
1951 int loops = 0;
Victor Stinner9cfc0022017-12-20 19:36:46 +01001952 int init_ignore_env = pymain->config.ignore_environment;
Victor Stinner94540602017-12-16 04:54:22 +01001953
1954 while (1) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001955 int utf8_mode = pymain->config.utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01001956 int encoding_changed = 0;
1957
1958 /* Watchdog to prevent an infinite loop */
1959 loops++;
1960 if (loops == 3) {
1961 pymain->err = _Py_INIT_ERR("Encoding changed twice while "
1962 "reading the configuration");
1963 goto done;
1964 }
1965
Victor Stinnerca719ac2017-12-20 18:00:19 +01001966 if (pymain_init_cmdline_argv(pymain, cmdline) < 0) {
Victor Stinnerc4bca952017-12-19 23:48:17 +01001967 goto done;
Victor Stinner94540602017-12-16 04:54:22 +01001968 }
1969
Victor Stinner9cfc0022017-12-20 19:36:46 +01001970 int conf_res = pymain_read_conf_impl(pymain, cmdline);
1971 if (conf_res != 0) {
1972 res = conf_res;
Victor Stinner94540602017-12-16 04:54:22 +01001973 goto done;
1974 }
1975
1976 /* The legacy C locale assumes ASCII as the default text encoding, which
1977 * causes problems not only for the CPython runtime, but also other
1978 * components like GNU readline.
1979 *
1980 * Accordingly, when the CLI detects it, it attempts to coerce it to a
1981 * more capable UTF-8 based alternative.
1982 *
1983 * See the documentation of the PYTHONCOERCECLOCALE setting for more
1984 * details.
1985 */
Victor Stinner9cfc0022017-12-20 19:36:46 +01001986 if (pymain->config.coerce_c_locale == 1 && !locale_coerced) {
Victor Stinner94540602017-12-16 04:54:22 +01001987 locale_coerced = 1;
Victor Stinner9cfc0022017-12-20 19:36:46 +01001988 _Py_CoerceLegacyLocale(&pymain->config);
Victor Stinner94540602017-12-16 04:54:22 +01001989 encoding_changed = 1;
1990 }
1991
1992 if (utf8_mode == -1) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001993 if (pymain->config.utf8_mode == 1) {
Victor Stinner94540602017-12-16 04:54:22 +01001994 /* UTF-8 Mode enabled */
1995 encoding_changed = 1;
1996 }
1997 }
1998 else {
Victor Stinner9cfc0022017-12-20 19:36:46 +01001999 if (pymain->config.utf8_mode != utf8_mode) {
Victor Stinner94540602017-12-16 04:54:22 +01002000 encoding_changed = 1;
2001 }
2002 }
2003
2004 if (!encoding_changed) {
2005 break;
2006 }
2007
2008 /* Reset the configuration, except UTF-8 Mode. Set Py_UTF8Mode for
2009 Py_DecodeLocale(). Reset Py_IgnoreEnvironmentFlag, modified by
Victor Stinner8ded5b82018-01-24 17:03:28 +01002010 pymain_read_conf_impl(). Reset Py_IsolatedFlag and Py_NoSiteFlag
2011 modified by _PyCoreConfig_Read(). */
Victor Stinner9cfc0022017-12-20 19:36:46 +01002012 Py_UTF8Mode = pymain->config.utf8_mode;
Victor Stinner94540602017-12-16 04:54:22 +01002013 Py_IgnoreEnvironmentFlag = init_ignore_env;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002014 _PyCoreConfig_Clear(&pymain->config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002015 pymain_clear_cmdline(pymain, cmdline);
2016 pymain_get_global_config(pymain, cmdline);
Victor Stinner94540602017-12-16 04:54:22 +01002017
2018 /* The encoding changed: read again the configuration
2019 with the new encoding */
2020 }
2021 res = 0;
2022
2023done:
2024 if (oldloc != NULL) {
2025 setlocale(LC_ALL, oldloc);
2026 PyMem_RawFree(oldloc);
2027 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002028
Victor Stinnera7368ac2017-11-15 18:11:45 -08002029 return res;
2030}
2031
Victor Stinner91106cd2017-12-13 12:29:09 +01002032
Victor Stinner9cfc0022017-12-20 19:36:46 +01002033static void
2034config_init_locale(_PyCoreConfig *config)
2035{
2036 if (config->utf8_mode >= 0 && config->coerce_c_locale >= 0) {
2037 return;
2038 }
2039
2040 if (_Py_LegacyLocaleDetected()) {
2041 /* POSIX locale: enable C locale coercion and UTF-8 Mode */
2042 if (config->utf8_mode < 0) {
2043 config->utf8_mode = 1;
2044 }
2045 if (config->coerce_c_locale < 0) {
2046 config->coerce_c_locale = 1;
2047 }
2048 return;
2049 }
2050
2051 /* By default, C locale coercion and UTF-8 Mode are disabled */
2052 if (config->coerce_c_locale < 0) {
2053 config->coerce_c_locale = 0;
2054 }
2055 if (config->utf8_mode < 0) {
2056 config->utf8_mode = 0;
2057 }
2058}
2059
2060
Victor Stinner8ded5b82018-01-24 17:03:28 +01002061static _PyInitError
2062config_init_module_search_paths(_PyCoreConfig *config)
2063{
2064 assert(config->module_search_paths == NULL);
2065 assert(config->nmodule_search_path < 0);
2066
2067 config->nmodule_search_path = 0;
2068
2069 const wchar_t *sys_path = Py_GetPath();
2070 const wchar_t delim = DELIM;
2071 const wchar_t *p = sys_path;
2072 while (1) {
2073 p = wcschr(sys_path, delim);
2074 if (p == NULL) {
2075 p = sys_path + wcslen(sys_path); /* End of string */
2076 }
2077
2078 size_t path_len = (p - sys_path);
2079 wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t));
2080 if (path == NULL) {
2081 return _Py_INIT_NO_MEMORY();
2082 }
2083 memcpy(path, sys_path, path_len * sizeof(wchar_t));
2084 path[path_len] = L'\0';
2085
2086 _PyInitError err = wstrlist_append(&config->nmodule_search_path,
2087 &config->module_search_paths,
2088 path);
2089 PyMem_RawFree(path);
2090 if (_Py_INIT_FAILED(err)) {
2091 return err;
2092 }
2093
2094 if (*p == '\0') {
2095 break;
2096 }
2097 sys_path = p + 1;
2098 }
2099 return _Py_INIT_OK();
2100}
2101
2102
2103static _PyInitError
2104config_init_path_config(_PyCoreConfig *config)
2105{
2106 _PyInitError err = _PyPathConfig_Init(config);
2107 if (_Py_INIT_FAILED(err)) {
2108 return err;
2109 }
2110
2111 if (config->nmodule_search_path < 0) {
2112 err = config_init_module_search_paths(config);
2113 if (_Py_INIT_FAILED(err)) {
2114 return err;
2115 }
2116 }
2117
2118 if (config->executable == NULL) {
2119 config->executable = _PyMem_RawWcsdup(Py_GetProgramFullPath());
2120 if (config->executable == NULL) {
2121 return _Py_INIT_NO_MEMORY();
2122 }
2123 }
2124
2125 if (config->prefix == NULL) {
2126 config->prefix = _PyMem_RawWcsdup(Py_GetPrefix());
2127 if (config->prefix == NULL) {
2128 return _Py_INIT_NO_MEMORY();
2129 }
2130 }
2131
2132 if (config->exec_prefix == NULL) {
2133 config->exec_prefix = _PyMem_RawWcsdup(Py_GetExecPrefix());
2134 if (config->exec_prefix == NULL) {
2135 return _Py_INIT_NO_MEMORY();
2136 }
2137 }
2138
2139 if (config->base_prefix == NULL) {
2140 config->base_prefix = _PyMem_RawWcsdup(config->prefix);
2141 if (config->base_prefix == NULL) {
2142 return _Py_INIT_NO_MEMORY();
2143 }
2144 }
2145
2146 if (config->base_exec_prefix == NULL) {
2147 config->base_exec_prefix = _PyMem_RawWcsdup(config->exec_prefix);
2148 if (config->base_exec_prefix == NULL) {
2149 return _Py_INIT_NO_MEMORY();
2150 }
2151 }
2152
2153 return _Py_INIT_OK();
2154}
2155
Victor Stinnerda273412017-12-15 01:46:02 +01002156/* Read configuration settings from standard locations
2157 *
2158 * This function doesn't make any changes to the interpreter state - it
2159 * merely populates any missing configuration settings. This allows an
2160 * embedding application to completely override a config option by
2161 * setting it before calling this function, or else modify the default
2162 * setting before passing the fully populated config to Py_EndInitialization.
2163 *
2164 * More advanced selective initialization tricks are possible by calling
2165 * this function multiple times with various preconfigured settings.
2166 */
2167
2168_PyInitError
2169_PyCoreConfig_Read(_PyCoreConfig *config)
2170{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002171 _PyInitError err;
2172
2173 err = config_read_env_vars(config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002174 if (_Py_INIT_FAILED(err)) {
2175 return err;
2176 }
2177
Victor Stinner9cfc0022017-12-20 19:36:46 +01002178 /* -X options */
2179 if (config_get_xoption(config, L"showrefcount")) {
2180 config->show_ref_count = 1;
2181 }
2182 if (config_get_xoption(config, L"showalloccount")) {
2183 config->show_alloc_count = 1;
2184 }
2185
2186 err = config_read_complex_options(config);
2187 if (_Py_INIT_FAILED(err)) {
2188 return err;
2189 }
2190
2191 err = config_init_utf8_mode(config);
2192 if (_Py_INIT_FAILED(err)) {
2193 return err;
2194 }
2195
2196 err = config_init_home(config);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002197 if (_Py_INIT_FAILED(err)) {
2198 return err;
2199 }
2200
2201 err = config_init_program_name(config);
2202 if (_Py_INIT_FAILED(err)) {
2203 return err;
2204 }
2205
Victor Stinner9cfc0022017-12-20 19:36:46 +01002206 config_init_locale(config);
Victor Stinnerda273412017-12-15 01:46:02 +01002207
Victor Stinner9cfc0022017-12-20 19:36:46 +01002208 /* Signal handlers are installed by default */
2209 if (config->install_signal_handlers < 0) {
2210 config->install_signal_handlers = 1;
Victor Stinner94540602017-12-16 04:54:22 +01002211 }
2212
Victor Stinner8ded5b82018-01-24 17:03:28 +01002213 if (!config->_disable_importlib) {
2214 err = config_init_path_config(config);
2215 if (_Py_INIT_FAILED(err)) {
2216 return err;
2217 }
2218 }
Victor Stinnerda273412017-12-15 01:46:02 +01002219 return _Py_INIT_OK();
2220}
2221
2222
2223void
2224_PyCoreConfig_Clear(_PyCoreConfig *config)
2225{
2226#define CLEAR(ATTR) \
2227 do { \
2228 PyMem_RawFree(ATTR); \
2229 ATTR = NULL; \
2230 } while (0)
Victor Stinner8ded5b82018-01-24 17:03:28 +01002231#define CLEAR_WSTRLIST(LEN, LIST) \
2232 do { \
2233 clear_wstrlist(LEN, LIST); \
2234 LEN = 0; \
2235 LIST = NULL; \
2236 } while (0)
Victor Stinnerda273412017-12-15 01:46:02 +01002237
2238 CLEAR(config->module_search_path_env);
2239 CLEAR(config->home);
2240 CLEAR(config->program_name);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002241 CLEAR(config->program);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002242
Victor Stinner8ded5b82018-01-24 17:03:28 +01002243 CLEAR_WSTRLIST(config->argc, config->argv);
2244 config->argc = -1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002245
Victor Stinner8ded5b82018-01-24 17:03:28 +01002246 CLEAR_WSTRLIST(config->nwarnoption, config->warnoptions);
2247 CLEAR_WSTRLIST(config->nxoption, config->xoptions);
2248 CLEAR_WSTRLIST(config->nmodule_search_path, config->module_search_paths);
2249 config->nmodule_search_path = -1;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002250
Victor Stinner8ded5b82018-01-24 17:03:28 +01002251 CLEAR(config->executable);
2252 CLEAR(config->prefix);
2253 CLEAR(config->base_prefix);
2254 CLEAR(config->exec_prefix);
2255 CLEAR(config->base_exec_prefix);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002256#undef CLEAR
Victor Stinner8ded5b82018-01-24 17:03:28 +01002257#undef CLEAR_WSTRLIST
Victor Stinnerda273412017-12-15 01:46:02 +01002258}
2259
2260
2261int
2262_PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
2263{
2264 _PyCoreConfig_Clear(config);
2265
2266#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
Victor Stinnerc4bca952017-12-19 23:48:17 +01002267#define COPY_STR_ATTR(ATTR) \
2268 do { \
2269 if (config2->ATTR != NULL) { \
2270 config->ATTR = _PyMem_RawWcsdup(config2->ATTR); \
2271 if (config->ATTR == NULL) { \
2272 return -1; \
2273 } \
2274 } \
2275 } while (0)
Victor Stinner8ded5b82018-01-24 17:03:28 +01002276#define COPY_WSTRLIST(LEN, LIST) \
2277 do { \
2278 if (config2->LIST != NULL) { \
2279 config->LIST = copy_wstrlist(config2->LEN, config2->LIST); \
2280 if (config->LIST == NULL) { \
2281 return -1; \
2282 } \
2283 } \
2284 config->LEN = config2->LEN; \
2285 } while (0)
Victor Stinnerc4bca952017-12-19 23:48:17 +01002286
Victor Stinnerda273412017-12-15 01:46:02 +01002287 COPY_ATTR(ignore_environment);
2288 COPY_ATTR(use_hash_seed);
2289 COPY_ATTR(hash_seed);
2290 COPY_ATTR(_disable_importlib);
2291 COPY_ATTR(allocator);
2292 COPY_ATTR(dev_mode);
2293 COPY_ATTR(faulthandler);
2294 COPY_ATTR(tracemalloc);
2295 COPY_ATTR(import_time);
2296 COPY_ATTR(show_ref_count);
2297 COPY_ATTR(show_alloc_count);
2298 COPY_ATTR(dump_refs);
2299 COPY_ATTR(malloc_stats);
2300 COPY_ATTR(utf8_mode);
Victor Stinnerda273412017-12-15 01:46:02 +01002301
2302 COPY_STR_ATTR(module_search_path_env);
2303 COPY_STR_ATTR(home);
2304 COPY_STR_ATTR(program_name);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002305 COPY_STR_ATTR(program);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002306
Victor Stinner8ded5b82018-01-24 17:03:28 +01002307 COPY_WSTRLIST(argc, argv);
2308 COPY_WSTRLIST(nwarnoption, warnoptions);
2309 COPY_WSTRLIST(nxoption, xoptions);
2310 COPY_WSTRLIST(nmodule_search_path, module_search_paths);
Victor Stinnerc4bca952017-12-19 23:48:17 +01002311
Victor Stinner8ded5b82018-01-24 17:03:28 +01002312 COPY_STR_ATTR(executable);
2313 COPY_STR_ATTR(prefix);
2314 COPY_STR_ATTR(base_prefix);
2315 COPY_STR_ATTR(exec_prefix);
2316 COPY_STR_ATTR(base_exec_prefix);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002317
Victor Stinnerc4bca952017-12-19 23:48:17 +01002318#undef COPY_ATTR
Victor Stinnerda273412017-12-15 01:46:02 +01002319#undef COPY_STR_ATTR
Victor Stinner8ded5b82018-01-24 17:03:28 +01002320#undef COPY_WSTRLIST
Victor Stinnerda273412017-12-15 01:46:02 +01002321 return 0;
2322}
2323
2324
2325void
2326_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
2327{
2328 Py_CLEAR(config->argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002329 Py_CLEAR(config->executable);
2330 Py_CLEAR(config->prefix);
2331 Py_CLEAR(config->base_prefix);
2332 Py_CLEAR(config->exec_prefix);
2333 Py_CLEAR(config->base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002334 Py_CLEAR(config->warnoptions);
2335 Py_CLEAR(config->xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002336 Py_CLEAR(config->module_search_path);
Victor Stinnerda273412017-12-15 01:46:02 +01002337}
2338
2339
2340static PyObject*
2341config_copy_attr(PyObject *obj)
2342{
2343 if (PyUnicode_Check(obj)) {
2344 Py_INCREF(obj);
2345 return obj;
2346 }
2347 else if (PyList_Check(obj)) {
2348 return PyList_GetSlice(obj, 0, Py_SIZE(obj));
2349 }
2350 else if (PyDict_Check(obj)) {
2351 /* The dict type is used for xoptions. Make the assumption that keys
2352 and values are immutables */
2353 return PyDict_Copy(obj);
2354 }
2355 else {
2356 PyErr_Format(PyExc_TypeError,
2357 "cannot copy config attribute of type %.200s",
2358 Py_TYPE(obj)->tp_name);
2359 return NULL;
2360 }
2361}
2362
2363
2364int
2365_PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
2366 const _PyMainInterpreterConfig *config2)
2367{
2368 _PyMainInterpreterConfig_Clear(config);
2369
2370#define COPY_ATTR(ATTR) \
2371 do { \
2372 if (config2->ATTR != NULL) { \
2373 config->ATTR = config_copy_attr(config2->ATTR); \
2374 if (config->ATTR == NULL) { \
2375 return -1; \
2376 } \
2377 } \
2378 } while (0)
2379
2380 COPY_ATTR(argv);
Victor Stinner41264f12017-12-15 02:05:29 +01002381 COPY_ATTR(executable);
2382 COPY_ATTR(prefix);
2383 COPY_ATTR(base_prefix);
2384 COPY_ATTR(exec_prefix);
2385 COPY_ATTR(base_exec_prefix);
Victor Stinnerda273412017-12-15 01:46:02 +01002386 COPY_ATTR(warnoptions);
2387 COPY_ATTR(xoptions);
Victor Stinner41264f12017-12-15 02:05:29 +01002388 COPY_ATTR(module_search_path);
Victor Stinnerda273412017-12-15 01:46:02 +01002389#undef COPY_ATTR
2390 return 0;
2391}
2392
2393
2394
2395
Victor Stinner41264f12017-12-15 02:05:29 +01002396_PyInitError
Victor Stinner9cfc0022017-12-20 19:36:46 +01002397_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
2398 const _PyCoreConfig *config)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002399{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002400 if (main_config->install_signal_handlers < 0) {
2401 main_config->install_signal_handlers = config->install_signal_handlers;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002402 }
2403
Victor Stinner9cfc0022017-12-20 19:36:46 +01002404 if (main_config->xoptions == NULL) {
2405 main_config->xoptions = config_create_xoptions_dict(config);
2406 if (main_config->xoptions == NULL) {
2407 return _Py_INIT_NO_MEMORY();
2408 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002409 }
2410
Victor Stinner8ded5b82018-01-24 17:03:28 +01002411#define COPY_WSTR(ATTR) \
2412 do { \
2413 if (main_config->ATTR == NULL) { \
2414 main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \
2415 if (main_config->ATTR == NULL) { \
2416 return _Py_INIT_NO_MEMORY(); \
2417 } \
2418 } \
2419 } while (0)
2420#define COPY_WSTRLIST(ATTR, LEN, LIST) \
2421 do { \
2422 if (ATTR == NULL) { \
2423 ATTR = wstrlist_as_pylist(LEN, LIST); \
2424 if (ATTR == NULL) { \
2425 return _Py_INIT_NO_MEMORY(); \
2426 } \
2427 } \
2428 } while (0)
2429
2430 COPY_WSTRLIST(main_config->warnoptions,
2431 config->nwarnoption, config->warnoptions);
2432 if (config->argc >= 0) {
2433 COPY_WSTRLIST(main_config->argv,
2434 config->argc, config->argv);
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002435 }
2436
Victor Stinner8ded5b82018-01-24 17:03:28 +01002437 if (!config->_disable_importlib) {
2438 COPY_WSTR(executable);
2439 COPY_WSTR(prefix);
2440 COPY_WSTR(base_prefix);
2441 COPY_WSTR(exec_prefix);
2442 COPY_WSTR(base_exec_prefix);
2443
2444 COPY_WSTRLIST(main_config->module_search_path,
2445 config->nmodule_search_path, config->module_search_paths);
Victor Stinner9cfc0022017-12-20 19:36:46 +01002446 }
Victor Stinner41264f12017-12-15 02:05:29 +01002447
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002448 return _Py_INIT_OK();
Victor Stinner8ded5b82018-01-24 17:03:28 +01002449#undef COPY_WSTR
2450#undef COPY_WSTRLIST
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002451}
2452
2453
2454static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01002455pymain_init_python_main(_PyMain *pymain)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002456{
Victor Stinner9cfc0022017-12-20 19:36:46 +01002457 _PyInitError err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002458
Victor Stinner9cfc0022017-12-20 19:36:46 +01002459 _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT;
2460 err = _PyMainInterpreterConfig_Read(&main_config, &pymain->config);
2461 if (!_Py_INIT_FAILED(err)) {
2462 err = _Py_InitializeMainInterpreter(&main_config);
2463 }
2464 _PyMainInterpreterConfig_Clear(&main_config);
2465
2466 if (_Py_INIT_FAILED(err)) {
2467 pymain->err = err;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002468 return -1;
2469 }
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002470 return 0;
2471}
Victor Stinnera7368ac2017-11-15 18:11:45 -08002472
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002473
2474static int
Victor Stinner9cfc0022017-12-20 19:36:46 +01002475pymain_init_sys_path(_PyMain *pymain)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01002476{
Victor Stinnerca719ac2017-12-20 18:00:19 +01002477 if (pymain->filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002478 /* If filename is a package (ex: directory or ZIP file) which contains
2479 __main__.py, main_importer_path is set to filename and will be
2480 prepended to sys.path by pymain_run_main_from_importer(). Otherwise,
2481 main_importer_path is set to NULL. */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002482 pymain->main_importer_path = pymain_get_importer(pymain->filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +01002483 }
2484
Victor Stinner19760862017-12-20 01:41:59 +01002485 PyObject *path0;
Victor Stinner9cfc0022017-12-20 19:36:46 +01002486 if (pymain_compute_path0(pymain, &path0) < 0) {
Victor Stinnerd5dda982017-12-13 17:31:16 +01002487 return -1;
2488 }
Victor Stinner19760862017-12-20 01:41:59 +01002489
Victor Stinner9cfc0022017-12-20 19:36:46 +01002490 pymain_clear_config(pymain);
Victor Stinner19760862017-12-20 01:41:59 +01002491
2492 if (path0 != NULL) {
2493 if (pymain_update_sys_path(pymain, path0) < 0) {
2494 Py_DECREF(path0);
2495 return -1;
2496 }
2497 Py_DECREF(path0);
2498 }
Victor Stinnera7368ac2017-11-15 18:11:45 -08002499 return 0;
2500}
2501
2502
2503static void
2504pymain_run_python(_PyMain *pymain)
2505{
Victor Stinner19760862017-12-20 01:41:59 +01002506 PyCompilerFlags cf = {.cf_flags = 0};
Victor Stinnera7368ac2017-11-15 18:11:45 -08002507
2508 pymain_header(pymain);
2509 pymain_import_readline(pymain);
2510
Victor Stinnerca719ac2017-12-20 18:00:19 +01002511 if (pymain->command) {
2512 pymain->status = pymain_run_command(pymain->command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002513 }
Victor Stinnerca719ac2017-12-20 18:00:19 +01002514 else if (pymain->module) {
2515 pymain->status = (pymain_run_module(pymain->module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002516 }
2517 else {
Victor Stinner19760862017-12-20 01:41:59 +01002518 pymain_run_filename(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002519 }
Victor Stinner9cfc0022017-12-20 19:36:46 +01002520
Victor Stinner19760862017-12-20 01:41:59 +01002521 pymain_repl(pymain, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002522}
2523
2524
Victor Stinnerc4bca952017-12-19 23:48:17 +01002525static void
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002526pymain_init(_PyMain *pymain)
2527{
Victor Stinner94540602017-12-16 04:54:22 +01002528 /* 754 requires that FP exceptions run in "no stop" mode by default,
2529 * and until C vendors implement C99's ways to control FP exceptions,
2530 * Python requires non-stop mode. Alas, some platforms enable FP
2531 * exceptions by default. Here we disable them.
2532 */
2533#ifdef __FreeBSD__
2534 fedisableexcept(FE_OVERFLOW);
2535#endif
2536
Victor Stinner9cfc0022017-12-20 19:36:46 +01002537 pymain->config._disable_importlib = 0;
Victor Stinnere32e79f2017-11-23 01:49:45 +01002538 pymain->config.install_signal_handlers = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002539}
2540
Victor Stinnera7368ac2017-11-15 18:11:45 -08002541
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002542static int
Victor Stinnerca719ac2017-12-20 18:00:19 +01002543pymain_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002544{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002545 pymain->err = _PyRuntime_Initialize();
2546 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002547 return -1;
2548 }
2549
Victor Stinnerca719ac2017-12-20 18:00:19 +01002550 int res = pymain_read_conf(pymain, cmdline);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002551 if (res < 0) {
2552 return -1;
2553 }
2554 if (res > 0) {
2555 /* --help or --version command: we are done */
Victor Stinner19760862017-12-20 01:41:59 +01002556 return 1;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002557 }
2558
Victor Stinner94540602017-12-16 04:54:22 +01002559 if (cmdline->print_help) {
Victor Stinner9cfc0022017-12-20 19:36:46 +01002560 pymain_usage(0, pymain->config.program);
Victor Stinner19760862017-12-20 01:41:59 +01002561 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002562 }
2563
2564 if (cmdline->print_version) {
2565 printf("Python %s\n",
2566 (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION);
Victor Stinner19760862017-12-20 01:41:59 +01002567 return 1;
Victor Stinner94540602017-12-16 04:54:22 +01002568 }
2569
Victor Stinnerc4bca952017-12-19 23:48:17 +01002570 /* For Py_GetArgcArgv(). Cleared by pymain_free(). */
Victor Stinnerca719ac2017-12-20 18:00:19 +01002571 orig_argv = copy_wstrlist(pymain->argc, cmdline->argv);
2572 if (orig_argv == NULL) {
2573 pymain->err = _Py_INIT_NO_MEMORY();
2574 return -1;
2575 }
Victor Stinnerc4bca952017-12-19 23:48:17 +01002576 orig_argc = pymain->argc;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002577
Victor Stinner9cfc0022017-12-20 19:36:46 +01002578 _PyInitError err = config_init_warnoptions(&pymain->config, cmdline);
2579 if (_Py_INIT_FAILED(err)) {
2580 pymain->err = err;
Victor Stinnerca719ac2017-12-20 18:00:19 +01002581 return -1;
2582 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002583 return 0;
2584}
Barry Warsaw3e13b1e2001-02-23 16:46:39 +00002585
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002586
Victor Stinnerca719ac2017-12-20 18:00:19 +01002587/* Read the configuration into _PyCoreConfig and _PyMain, initialize the
2588 LC_CTYPE locale and Py_DecodeLocale().
2589
2590 Configuration:
2591
2592 * Command line arguments
2593 * Environment variables
2594 * Py_xxx global configuration variables
2595
2596 _Py_CommandLineDetails is a temporary structure used to prioritize these
2597 variables. */
2598static int
2599pymain_cmdline(_PyMain *pymain)
2600{
Victor Stinner31e99082017-12-20 23:41:38 +01002601 /* Force default allocator, since pymain_free() and pymain_clear_config()
2602 must use the same allocator than this function. */
2603 PyMemAllocatorEx old_alloc;
2604 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
2605#ifdef Py_DEBUG
2606 PyMemAllocatorEx default_alloc;
2607 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
2608#endif
2609
Victor Stinnerca719ac2017-12-20 18:00:19 +01002610 _Py_CommandLineDetails cmdline;
2611 memset(&cmdline, 0, sizeof(cmdline));
2612
2613 pymain_get_global_config(pymain, &cmdline);
2614
2615 int res = pymain_cmdline_impl(pymain, &cmdline);
2616
2617 pymain_set_global_config(pymain, &cmdline);
2618
2619 pymain_clear_cmdline(pymain, &cmdline);
Victor Stinner31e99082017-12-20 23:41:38 +01002620
2621#ifdef Py_DEBUG
2622 /* Make sure that PYMEM_DOMAIN_RAW has not been modified */
2623 PyMemAllocatorEx cur_alloc;
2624 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
2625 assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
2626#endif
2627 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerca719ac2017-12-20 18:00:19 +01002628 return res;
2629}
2630
2631
Victor Stinner94540602017-12-16 04:54:22 +01002632static int
2633pymain_main(_PyMain *pymain)
2634{
Victor Stinnerc4bca952017-12-19 23:48:17 +01002635 pymain_init(pymain);
Victor Stinner94540602017-12-16 04:54:22 +01002636
Victor Stinnerca719ac2017-12-20 18:00:19 +01002637 int res = pymain_cmdline(pymain);
Victor Stinner19760862017-12-20 01:41:59 +01002638 if (res < 0) {
Victor Stinner94540602017-12-16 04:54:22 +01002639 _Py_FatalInitError(pymain->err);
2640 }
Victor Stinner19760862017-12-20 01:41:59 +01002641 if (res == 1) {
2642 goto done;
2643 }
2644
Victor Stinner9cfc0022017-12-20 19:36:46 +01002645 pymain_init_stdio(pymain);
2646
2647 pymain->err = _Py_InitializeCore(&pymain->config);
2648 if (_Py_INIT_FAILED(pymain->err)) {
Victor Stinner19760862017-12-20 01:41:59 +01002649 _Py_FatalInitError(pymain->err);
2650 }
2651
2652 if (pymain_init_python_main(pymain) < 0) {
2653 _Py_FatalInitError(pymain->err);
2654 }
2655
Victor Stinner9cfc0022017-12-20 19:36:46 +01002656 if (pymain_init_sys_path(pymain) < 0) {
2657 _Py_FatalInitError(pymain->err);
2658 }
2659
Victor Stinner19760862017-12-20 01:41:59 +01002660 pymain_run_python(pymain);
2661
2662 if (Py_FinalizeEx() < 0) {
2663 /* Value unlikely to be confused with a non-error exit status or
2664 other special meaning */
2665 pymain->status = 120;
2666 }
2667
2668done:
Victor Stinner94540602017-12-16 04:54:22 +01002669 pymain_free(pymain);
2670
Victor Stinner94540602017-12-16 04:54:22 +01002671 return pymain->status;
2672}
2673
2674
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002675int
2676Py_Main(int argc, wchar_t **argv)
2677{
2678 _PyMain pymain = _PyMain_INIT;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002679 pymain.use_bytes_argv = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002680 pymain.argc = argc;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002681 pymain.wchar_argv = argv;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002682
Victor Stinner94540602017-12-16 04:54:22 +01002683 return pymain_main(&pymain);
Guido van Rossum667d7041995-08-04 04:20:48 +00002684}
2685
Victor Stinner94540602017-12-16 04:54:22 +01002686
2687int
2688_Py_UnixMain(int argc, char **argv)
2689{
2690 _PyMain pymain = _PyMain_INIT;
Victor Stinner94540602017-12-16 04:54:22 +01002691 pymain.use_bytes_argv = 1;
Victor Stinnerc4bca952017-12-19 23:48:17 +01002692 pymain.argc = argc;
Victor Stinner94540602017-12-16 04:54:22 +01002693 pymain.bytes_argv = argv;
2694
2695 return pymain_main(&pymain);
2696}
2697
2698
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002699/* this is gonna seem *real weird*, but if you put some other code between
2700 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
2701 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +00002702
Guido van Rossum667d7041995-08-04 04:20:48 +00002703/* Make the *original* argc/argv available to other modules.
2704 This is rare, but it is needed by the secureware extension. */
2705
2706void
Martin v. Löwis790465f2008-04-05 20:41:37 +00002707Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +00002708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 *argc = orig_argc;
2710 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +00002711}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002712
2713#ifdef __cplusplus
2714}
2715#endif