Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1 | /* Python interpreter main program */ |
| 2 | |
| 3 | #include "Python.h" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 4 | #include "osdefs.h" |
Benjamin Peterson | e425bd7 | 2017-12-14 23:48:12 -0800 | [diff] [blame] | 5 | #include "internal/pygetopt.h" |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 6 | #include "internal/pystate.h" |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 7 | |
Antoine Pitrou | 5651eaa | 2008-09-06 20:46:58 +0000 | [diff] [blame] | 8 | #include <locale.h> |
| 9 | |
Sjoerd Mullender | 9cf424b | 2002-08-09 13:35:18 +0000 | [diff] [blame] | 10 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
Victor Stinner | 6efcb6d | 2017-12-18 23:42:55 +0100 | [diff] [blame] | 11 | # include <windows.h> |
| 12 | # ifdef HAVE_IO_H |
| 13 | # include <io.h> |
| 14 | # endif |
| 15 | # ifdef HAVE_FCNTL_H |
| 16 | # include <fcntl.h> |
| 17 | # endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18 | #endif |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 19 | |
Martin v. Löwis | 945362c | 2007-08-30 14:57:25 +0000 | [diff] [blame] | 20 | #ifdef _MSC_VER |
Victor Stinner | 6efcb6d | 2017-12-18 23:42:55 +0100 | [diff] [blame] | 21 | # include <crtdbg.h> |
| 22 | #endif |
| 23 | |
| 24 | #ifdef __FreeBSD__ |
| 25 | # include <fenv.h> |
Martin v. Löwis | 945362c | 2007-08-30 14:57:25 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 28 | #if defined(MS_WINDOWS) |
Victor Stinner | 6efcb6d | 2017-12-18 23:42:55 +0100 | [diff] [blame] | 29 | # define PYTHONHOMEHELP "<prefix>\\python{major}{minor}" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 30 | #else |
Victor Stinner | 6efcb6d | 2017-12-18 23:42:55 +0100 | [diff] [blame] | 31 | # define PYTHONHOMEHELP "<prefix>/lib/pythonX.X" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 34 | #define COPYRIGHT \ |
Guido van Rossum | 36002d7 | 2001-07-18 16:59:46 +0000 | [diff] [blame] | 35 | "Type \"help\", \"copyright\", \"credits\" or \"license\" " \ |
| 36 | "for more information." |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 37 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 42 | #define DECODE_LOCALE_ERR(NAME, LEN) \ |
| 43 | (((LEN) == -2) \ |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 44 | ? _Py_INIT_USER_ERR("cannot decode " NAME) \ |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 45 | : _Py_INIT_NO_MEMORY()) |
| 46 | |
| 47 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
| 49 | #define WCSTOK wcstok_s |
| 50 | #else |
| 51 | #define WCSTOK wcstok |
| 52 | #endif |
| 53 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 54 | /* For Py_GetArgcArgv(); set by main() */ |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 55 | static wchar_t **orig_argv = NULL; |
| 56 | static int orig_argc = 0; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 58 | /* command line options */ |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 59 | #define BASE_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?" |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 61 | #define PROGRAM_OPTS BASE_OPTS |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 62 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 63 | static const _PyOS_LongOption longoptions[] = { |
| 64 | {L"check-hash-based-pycs", 1, 0}, |
| 65 | {NULL, 0, 0}, |
| 66 | }; |
| 67 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 68 | /* Short usage message (with %s for argv0) */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 69 | static const char usage_line[] = |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 70 | "usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 71 | |
| 72 | /* Long usage message, split into parts < 512 bytes */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 73 | static const char usage_1[] = "\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 74 | Options and arguments (and corresponding environment variables):\n\ |
Christian Heimes | 2ab3444 | 2008-09-03 20:31:07 +0000 | [diff] [blame] | 75 | -b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\ |
| 76 | and comparing bytes/bytearray with str. (-bb: issue errors)\n\ |
Xiang Zhang | 0710d75 | 2017-03-11 13:02:52 +0800 | [diff] [blame] | 77 | -B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 78 | -c cmd : program passed in as string (terminates option list)\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 79 | -d : debug output from parser; also PYTHONDEBUG=x\n\ |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 80 | -E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 81 | -h : print this help message and exit (also --help)\n\ |
Guido van Rossum | 61c345f | 2001-09-04 03:26:15 +0000 | [diff] [blame] | 82 | "; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 83 | static const char usage_2[] = "\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 84 | -i : inspect interactively after running script; forces a prompt even\n\ |
| 85 | if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\ |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 86 | -I : isolate Python from the user's environment (implies -E and -s)\n\ |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 87 | -m mod : run library module as a script (terminates option list)\n\ |
Cheryl Sabella | 186b606 | 2018-02-24 22:04:40 -0500 | [diff] [blame] | 88 | -O : remove assert and __debug__-dependent statements; add .opt-1 before\n\ |
| 89 | .pyc extension; also PYTHONOPTIMIZE=x\n\ |
| 90 | -OO : do -O changes and also discard docstrings; add .opt-2 before\n\ |
| 91 | .pyc extension\n\ |
Georg Brandl | 9d87119 | 2010-12-04 10:47:18 +0000 | [diff] [blame] | 92 | -q : don't print version and copyright messages on interactive startup\n\ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 93 | -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 94 | -S : don't imply 'import site' on initialization\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 95 | "; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 96 | static const char usage_3[] = "\ |
Berker Peksag | 7f58097 | 2017-10-13 15:16:31 +0300 | [diff] [blame] | 97 | -u : force the stdout and stderr streams to be unbuffered;\n\ |
| 98 | this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 99 | -v : verbose (trace import statements); also PYTHONVERBOSE=x\n\ |
| 100 | can be supplied multiple times to increase verbosity\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 101 | -V : print the Python version number and exit (also --version)\n\ |
INADA Naoki | 0e175a6 | 2016-11-21 20:57:14 +0900 | [diff] [blame] | 102 | when given twice, print more information about the build\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 103 | -W arg : warning control; arg is action:message:category:module:lineno\n\ |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 104 | also PYTHONWARNINGS=arg\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 105 | -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 106 | -X opt : set implementation-specific option\n\ |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 107 | --check-hash-based-pycs always|default|never:\n\ |
| 108 | control how Python invalidates hash-based .pyc files\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 109 | "; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 110 | static const char usage_4[] = "\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 111 | file : program read from script file\n\ |
| 112 | - : program read from stdin (default; interactive mode if a tty)\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 113 | arg ...: arguments passed to program in sys.argv[1:]\n\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 114 | Other environment variables:\n\ |
| 115 | PYTHONSTARTUP: file executed on interactive startup (no default)\n\ |
Serhiy Storchaka | 1ba0161 | 2015-12-30 09:28:19 +0200 | [diff] [blame] | 116 | PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 117 | default module search path. The result is sys.path.\n\ |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 118 | "; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 119 | static const char usage_5[] = |
Serhiy Storchaka | 1ba0161 | 2015-12-30 09:28:19 +0200 | [diff] [blame] | 120 | "PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n" |
Victor Stinner | 9802b39 | 2010-08-19 11:36:43 +0000 | [diff] [blame] | 121 | " The default module search path uses %s.\n" |
| 122 | "PYTHONCASEOK : ignore case in 'import' statements (Windows).\n" |
| 123 | "PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n" |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 124 | "PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n"; |
| 125 | static const char usage_6[] = |
| 126 | "PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n" |
| 127 | " to seed the hashes of str, bytes and datetime objects. It can also be\n" |
| 128 | " set to an integer in the range [0,4294967295] to get hash values with a\n" |
| 129 | " predictable seed.\n" |
| 130 | "PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n" |
| 131 | " on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n" |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 132 | " hooks.\n" |
Stéphane Wirtel | 7d1017d | 2017-06-12 13:30:33 +0200 | [diff] [blame] | 133 | "PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n" |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 134 | " coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n" |
Victor Stinner | 5e3806f | 2017-11-30 11:40:24 +0100 | [diff] [blame] | 135 | " locale coercion and locale compatibility warnings on stderr.\n" |
Stéphane Wirtel | b7fd738 | 2018-07-29 12:27:16 +0200 | [diff] [blame] | 136 | "PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n" |
| 137 | " debugger. It can be set to the callable of your debugger of choice.\n" |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 138 | "PYTHONDEVMODE: enable the development mode.\n" |
| 139 | "PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 140 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 141 | static void |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 142 | pymain_usage(int error, const wchar_t* program) |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 143 | { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 144 | FILE *f = error ? stderr : stdout; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | fprintf(f, usage_line, program); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 147 | if (error) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | fprintf(f, "Try `python -h' for more information.\n"); |
| 149 | else { |
| 150 | fputs(usage_1, f); |
| 151 | fputs(usage_2, f); |
| 152 | fputs(usage_3, f); |
Serhiy Storchaka | 1ba0161 | 2015-12-30 09:28:19 +0200 | [diff] [blame] | 153 | fprintf(f, usage_4, (wint_t)DELIM); |
| 154 | fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 155 | fputs(usage_6, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 159 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 160 | static void |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 161 | pymain_run_interactive_hook(void) |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 162 | { |
| 163 | PyObject *sys, *hook, *result; |
| 164 | sys = PyImport_ImportModule("sys"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 165 | if (sys == NULL) { |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 166 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 169 | hook = PyObject_GetAttrString(sys, "__interactivehook__"); |
| 170 | Py_DECREF(sys); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 171 | if (hook == NULL) { |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 172 | PyErr_Clear(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 173 | return; |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 174 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 175 | |
| 176 | result = _PyObject_CallNoArg(hook); |
| 177 | Py_DECREF(hook); |
| 178 | if (result == NULL) { |
| 179 | goto error; |
| 180 | } |
| 181 | Py_DECREF(result); |
| 182 | |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 183 | return; |
| 184 | |
| 185 | error: |
| 186 | PySys_WriteStderr("Failed calling sys.__interactivehook__\n"); |
| 187 | PyErr_Print(); |
| 188 | PyErr_Clear(); |
| 189 | } |
| 190 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 191 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 192 | static int |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 193 | pymain_run_module(const wchar_t *modname, int set_argv0) |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | PyObject *module, *runpy, *runmodule, *runargs, *result; |
| 196 | runpy = PyImport_ImportModule("runpy"); |
| 197 | if (runpy == NULL) { |
| 198 | fprintf(stderr, "Could not import runpy module\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame] | 199 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | return -1; |
| 201 | } |
| 202 | runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main"); |
| 203 | if (runmodule == NULL) { |
| 204 | fprintf(stderr, "Could not access runpy._run_module_as_main\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame] | 205 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | Py_DECREF(runpy); |
| 207 | return -1; |
| 208 | } |
| 209 | module = PyUnicode_FromWideChar(modname, wcslen(modname)); |
| 210 | if (module == NULL) { |
| 211 | fprintf(stderr, "Could not convert module name to unicode\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame] | 212 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | Py_DECREF(runpy); |
| 214 | Py_DECREF(runmodule); |
| 215 | return -1; |
| 216 | } |
| 217 | runargs = Py_BuildValue("(Oi)", module, set_argv0); |
| 218 | if (runargs == NULL) { |
| 219 | fprintf(stderr, |
| 220 | "Could not create arguments for runpy._run_module_as_main\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame] | 221 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | Py_DECREF(runpy); |
| 223 | Py_DECREF(runmodule); |
| 224 | Py_DECREF(module); |
| 225 | return -1; |
| 226 | } |
| 227 | result = PyObject_Call(runmodule, runargs, NULL); |
| 228 | if (result == NULL) { |
| 229 | PyErr_Print(); |
| 230 | } |
| 231 | Py_DECREF(runpy); |
| 232 | Py_DECREF(runmodule); |
| 233 | Py_DECREF(module); |
| 234 | Py_DECREF(runargs); |
| 235 | if (result == NULL) { |
| 236 | return -1; |
| 237 | } |
| 238 | Py_DECREF(result); |
| 239 | return 0; |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 240 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 241 | |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 242 | static PyObject * |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 243 | pymain_get_importer(const wchar_t *filename) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 244 | { |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 245 | PyObject *sys_path0 = NULL, *importer; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 246 | |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 247 | sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename)); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 248 | if (sys_path0 == NULL) { |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 249 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 250 | } |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 251 | |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 252 | importer = PyImport_GetImporter(sys_path0); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 253 | if (importer == NULL) { |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 254 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 255 | } |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 256 | |
Brett Cannon | aa93642 | 2012-04-27 15:30:58 -0400 | [diff] [blame] | 257 | if (importer == Py_None) { |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 258 | Py_DECREF(sys_path0); |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 259 | Py_DECREF(importer); |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 260 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 262 | |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 263 | Py_DECREF(importer); |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 264 | return sys_path0; |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 265 | |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 266 | error: |
| 267 | Py_XDECREF(sys_path0); |
| 268 | PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n"); |
| 269 | PyErr_Print(); |
| 270 | PyErr_Clear(); |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | static int |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 276 | pymain_run_command(wchar_t *command, PyCompilerFlags *cf) |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 277 | { |
| 278 | PyObject *unicode, *bytes; |
| 279 | int ret; |
| 280 | |
| 281 | unicode = PyUnicode_FromWideChar(command, -1); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 282 | if (unicode == NULL) { |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 283 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 286 | bytes = PyUnicode_AsUTF8String(unicode); |
| 287 | Py_DECREF(unicode); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 288 | if (bytes == NULL) { |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 289 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 292 | ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf); |
| 293 | Py_DECREF(bytes); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 294 | return (ret != 0); |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 295 | |
| 296 | error: |
Victor Stinner | 398356b | 2010-08-18 22:23:22 +0000 | [diff] [blame] | 297 | PySys_WriteStderr("Unable to decode the command from the command line:\n"); |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 298 | PyErr_Print(); |
| 299 | return 1; |
| 300 | } |
| 301 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 302 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 303 | /* Main program */ |
| 304 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 305 | typedef struct { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 306 | wchar_t **argv; |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 307 | int nwarnoption; /* Number of -W command line options */ |
| 308 | wchar_t **warnoptions; /* Command line -W options */ |
| 309 | int nenv_warnoption; /* Number of PYTHONWARNINGS environment variables */ |
| 310 | wchar_t **env_warnoptions; /* PYTHONWARNINGS environment variables */ |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 311 | int print_help; /* -h, -? options */ |
| 312 | int print_version; /* -V option */ |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 313 | } _PyCmdline; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 314 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 315 | /* Structure used by Py_Main() to pass data to subfunctions */ |
| 316 | typedef struct { |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 317 | /* Input arguments */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 318 | int argc; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 319 | int use_bytes_argv; |
| 320 | char **bytes_argv; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 321 | wchar_t **wchar_argv; |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 322 | |
| 323 | /* Exit status or "exit code": result of pymain_main() */ |
| 324 | int status; |
| 325 | /* Error message if a function failed */ |
| 326 | _PyInitError err; |
| 327 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 328 | /* non-zero is stdin is a TTY or if -i option is used */ |
| 329 | int stdin_is_interactive; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 330 | int skip_first_line; /* -x option */ |
| 331 | wchar_t *filename; /* Trailing arg without -c or -m */ |
| 332 | wchar_t *command; /* -c argument */ |
| 333 | wchar_t *module; /* -m argument */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 334 | } _PyMain; |
| 335 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 336 | #define _PyMain_INIT {.err = _Py_INIT_OK()} |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 337 | /* Note: _PyMain_INIT sets other fields to 0/NULL */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 338 | |
| 339 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 340 | /* Non-zero if filename, command (-c) or module (-m) is set |
| 341 | on the command line */ |
| 342 | #define RUN_CODE(pymain) \ |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 343 | (pymain->command != NULL || pymain->filename != NULL \ |
| 344 | || pymain->module != NULL) |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 345 | |
| 346 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 347 | static wchar_t* |
| 348 | pymain_wstrdup(_PyMain *pymain, const wchar_t *str) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 349 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 350 | wchar_t *str2 = _PyMem_RawWcsdup(str); |
| 351 | if (str2 == NULL) { |
| 352 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 353 | return NULL; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 354 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 355 | return str2; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Victor Stinner | 92a3c6f | 2017-12-06 18:12:59 +0100 | [diff] [blame] | 358 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 359 | static int |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 360 | pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config, |
| 361 | _PyCmdline *cmdline) |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 362 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 363 | assert(cmdline->argv == NULL); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 364 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 365 | if (pymain->use_bytes_argv) { |
| 366 | /* +1 for a the NULL terminator */ |
| 367 | size_t size = sizeof(wchar_t*) * (pymain->argc + 1); |
| 368 | wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size); |
| 369 | if (argv == NULL) { |
| 370 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 371 | return -1; |
| 372 | } |
| 373 | |
| 374 | for (int i = 0; i < pymain->argc; i++) { |
| 375 | size_t len; |
| 376 | wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len); |
| 377 | if (arg == NULL) { |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 378 | _Py_wstrlist_clear(i, argv); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 379 | pymain->err = DECODE_LOCALE_ERR("command line arguments", |
| 380 | (Py_ssize_t)len); |
| 381 | return -1; |
| 382 | } |
| 383 | argv[i] = arg; |
| 384 | } |
| 385 | argv[pymain->argc] = NULL; |
| 386 | |
| 387 | cmdline->argv = argv; |
| 388 | } |
| 389 | else { |
| 390 | cmdline->argv = pymain->wchar_argv; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 393 | wchar_t *program; |
| 394 | if (pymain->argc >= 1 && cmdline->argv != NULL) { |
| 395 | program = cmdline->argv[0]; |
| 396 | } |
| 397 | else { |
| 398 | program = L""; |
| 399 | } |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 400 | config->program = pymain_wstrdup(pymain, program); |
| 401 | if (config->program == NULL) { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 402 | return -1; |
| 403 | } |
| 404 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | static void |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 410 | pymain_clear_cmdline(_PyMain *pymain, _PyCmdline *cmdline) |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 411 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 412 | PyMemAllocatorEx old_alloc; |
| 413 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 414 | |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 415 | _Py_wstrlist_clear(cmdline->nwarnoption, cmdline->warnoptions); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 416 | cmdline->nwarnoption = 0; |
| 417 | cmdline->warnoptions = NULL; |
| 418 | |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 419 | _Py_wstrlist_clear(cmdline->nenv_warnoption, cmdline->env_warnoptions); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 420 | cmdline->nenv_warnoption = 0; |
| 421 | cmdline->env_warnoptions = NULL; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 422 | |
| 423 | if (pymain->use_bytes_argv && cmdline->argv != NULL) { |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 424 | _Py_wstrlist_clear(pymain->argc, cmdline->argv); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 425 | } |
| 426 | cmdline->argv = NULL; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 427 | |
| 428 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | static void |
| 433 | pymain_clear_pymain(_PyMain *pymain) |
| 434 | { |
| 435 | #define CLEAR(ATTR) \ |
| 436 | do { \ |
| 437 | PyMem_RawFree(ATTR); \ |
| 438 | ATTR = NULL; \ |
| 439 | } while (0) |
| 440 | |
| 441 | CLEAR(pymain->filename); |
| 442 | CLEAR(pymain->command); |
| 443 | CLEAR(pymain->module); |
| 444 | #undef CLEAR |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 447 | static void |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 448 | pymain_clear_config(_PyCoreConfig *config) |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 449 | { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 450 | /* Clear core config with the memory allocator |
| 451 | used by pymain_read_conf() */ |
| 452 | PyMemAllocatorEx old_alloc; |
| 453 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 454 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 455 | _PyCoreConfig_Clear(config); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 456 | |
| 457 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | static void |
Victor Stinner | d145775 | 2018-07-26 16:04:56 +0200 | [diff] [blame] | 462 | pymain_free(_PyMain *pymain) |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 463 | { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 464 | _PyImport_Fini2(); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 465 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 466 | /* Free global variables which cannot be freed in Py_Finalize(): |
| 467 | configuration options set before Py_Initialize() which should |
| 468 | remain valid after Py_Finalize(), since |
| 469 | Py_Initialize()-Py_Finalize() can be called multiple times. */ |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 470 | _PyPathConfig_ClearGlobal(); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 471 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 472 | /* Force the allocator used by pymain_read_conf() */ |
| 473 | PyMemAllocatorEx old_alloc; |
| 474 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 475 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 476 | pymain_clear_pymain(pymain); |
| 477 | |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 478 | _Py_wstrlist_clear(orig_argc, orig_argv); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 479 | orig_argc = 0; |
| 480 | orig_argv = NULL; |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 481 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 482 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 483 | |
| 484 | #ifdef __INSURE__ |
| 485 | /* Insure++ is a memory analysis tool that aids in discovering |
| 486 | * memory leaks and other memory problems. On Python exit, the |
| 487 | * interned string dictionaries are flagged as being in use at exit |
| 488 | * (which it is). Under normal circumstances, this is fine because |
| 489 | * the memory will be automatically reclaimed by the system. Under |
| 490 | * memory debugging, it's a huge source of useless noise, so we |
| 491 | * trade off slower shutdown for less distraction in the memory |
| 492 | * reports. -baw |
| 493 | */ |
| 494 | _Py_ReleaseInternedUnicodeStrings(); |
| 495 | #endif /* __INSURE__ */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 498 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 499 | static int |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 500 | pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 501 | { |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 502 | PyObject *sys_path; |
| 503 | PyObject *sysdict = interp->sysdict; |
| 504 | if (sysdict != NULL) { |
| 505 | sys_path = PyDict_GetItemString(sysdict, "path"); |
| 506 | } |
| 507 | else { |
| 508 | sys_path = NULL; |
| 509 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 510 | if (sys_path == NULL) { |
| 511 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path"); |
| 512 | goto error; |
| 513 | } |
| 514 | |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 515 | if (PyList_Insert(sys_path, 0, path0)) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 516 | goto error; |
| 517 | } |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 518 | return 0; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 519 | |
| 520 | error: |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 521 | PyErr_Print(); |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 522 | return -1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 526 | _PyInitError |
| 527 | _Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 528 | { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 529 | if (*len == INT_MAX) { |
| 530 | /* len+1 would overflow */ |
| 531 | return _Py_INIT_NO_MEMORY(); |
| 532 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 533 | wchar_t *str2 = _PyMem_RawWcsdup(str); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 534 | if (str2 == NULL) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 535 | return _Py_INIT_NO_MEMORY(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 538 | size_t size = (*len + 1) * sizeof(list[0]); |
| 539 | wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size); |
| 540 | if (list2 == NULL) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 541 | PyMem_RawFree(str2); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 542 | return _Py_INIT_NO_MEMORY(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 543 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 544 | list2[*len] = str2; |
| 545 | *list = list2; |
| 546 | (*len)++; |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 547 | return _Py_INIT_OK(); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | static int |
| 552 | pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str) |
| 553 | { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 554 | _PyInitError err = _Py_wstrlist_append(len, list, str); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 555 | if (_Py_INIT_FAILED(err)) { |
| 556 | pymain->err = err; |
| 557 | return -1; |
| 558 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | |
| 563 | /* Parse the command line arguments |
| 564 | Return 0 on success. |
| 565 | Return 1 if parsing failed. |
| 566 | Set pymain->err and return -1 on other errors. */ |
| 567 | static int |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 568 | pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config, |
| 569 | _PyCmdline *cmdline) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 570 | { |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 571 | _PyOS_ResetGetOpt(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 572 | do { |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 573 | int longindex = -1; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 574 | int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 575 | longoptions, &longindex); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 576 | if (c == EOF) { |
| 577 | break; |
| 578 | } |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | if (c == 'c') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | /* -c is the last option; following arguments |
| 582 | that look like options are left for the |
| 583 | command to interpret. */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 584 | size_t len = wcslen(_PyOS_optarg) + 1 + 1; |
| 585 | wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len); |
| 586 | if (command == NULL) { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 587 | pymain->err = _Py_INIT_NO_MEMORY(); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 588 | return -1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 589 | } |
Victor Stinner | 58d1683 | 2018-05-31 15:09:28 +0200 | [diff] [blame] | 590 | memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | command[len - 2] = '\n'; |
| 592 | command[len - 1] = 0; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 593 | pymain->command = command; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | break; |
| 595 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | if (c == 'm') { |
| 598 | /* -m is the last option; following arguments |
| 599 | that look like options are left for the |
| 600 | module to interpret. */ |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 601 | pymain->module = pymain_wstrdup(pymain, _PyOS_optarg); |
| 602 | if (pymain->module == NULL) { |
| 603 | return -1; |
| 604 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | break; |
| 606 | } |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | switch (c) { |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 609 | case 0: |
| 610 | // Handle long option. |
| 611 | assert(longindex == 0); // Only one long option now. |
| 612 | if (!wcscmp(_PyOS_optarg, L"always")) { |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 613 | config->_check_hash_pycs_mode = "always"; |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 614 | } else if (!wcscmp(_PyOS_optarg, L"never")) { |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 615 | config->_check_hash_pycs_mode = "never"; |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 616 | } else if (!wcscmp(_PyOS_optarg, L"default")) { |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 617 | config->_check_hash_pycs_mode = "default"; |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 618 | } else { |
| 619 | fprintf(stderr, "--check-hash-based-pycs must be one of " |
| 620 | "'default', 'always', or 'never'\n"); |
| 621 | return 1; |
| 622 | } |
| 623 | break; |
| 624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | case 'b': |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 626 | config->bytes_warning++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | case 'd': |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 630 | config->parser_debug++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | case 'i': |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 634 | config->inspect++; |
| 635 | config->interactive++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 637 | |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 638 | case 'I': |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 639 | config->isolated++; |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 640 | break; |
| 641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | /* case 'J': reserved for Jython */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | case 'O': |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 645 | config->optimization_level++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | break; |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | case 'B': |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 649 | config->write_bytecode = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | break; |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | case 's': |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 653 | config->user_site_directory = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | break; |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | case 'S': |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 657 | config->site_import = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | break; |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | case 'E': |
Victor Stinner | d145775 | 2018-07-26 16:04:56 +0200 | [diff] [blame] | 661 | config->use_environment = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | break; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | case 't': |
| 665 | /* ignored for backwards compatibility */ |
| 666 | break; |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | case 'u': |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 669 | config->buffered_stdio = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | case 'v': |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 673 | config->verbose++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | case 'x': |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 677 | pymain->skip_first_line = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | break; |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | case 'h': |
| 681 | case '?': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 682 | cmdline->print_help++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | break; |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | case 'V': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 686 | cmdline->print_version++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | break; |
Guido van Rossum | c15a9a1 | 2000-05-01 17:54:33 +0000 | [diff] [blame] | 688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | case 'W': |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 690 | if (pymain_wstrlist_append(pymain, |
| 691 | &cmdline->nwarnoption, |
| 692 | &cmdline->warnoptions, |
| 693 | _PyOS_optarg) < 0) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 694 | return -1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 695 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | break; |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 697 | |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 698 | case 'X': |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 699 | if (pymain_wstrlist_append(pymain, |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 700 | &config->nxoption, |
| 701 | &config->xoptions, |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 702 | _PyOS_optarg) < 0) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 703 | return -1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 704 | } |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 705 | break; |
| 706 | |
Georg Brandl | 9d87119 | 2010-12-04 10:47:18 +0000 | [diff] [blame] | 707 | case 'q': |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 708 | config->quiet++; |
Georg Brandl | 9d87119 | 2010-12-04 10:47:18 +0000 | [diff] [blame] | 709 | break; |
| 710 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 711 | case 'R': |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 712 | config->use_hash_seed = 0; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 713 | break; |
| 714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | /* This space reserved for other options */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | default: |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 718 | /* unknown argument: parsing failed */ |
| 719 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 721 | } while (1); |
| 722 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 723 | if (pymain->command == NULL && pymain->module == NULL |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 724 | && _PyOS_optind < pymain->argc |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 725 | && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 726 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 727 | pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]); |
| 728 | if (pymain->filename == NULL) { |
| 729 | return -1; |
| 730 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 732 | |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 733 | /* -c and -m options are exclusive */ |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 734 | assert(!(pymain->command != NULL && pymain->module != NULL)); |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 735 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 736 | return 0; |
| 737 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 738 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 739 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 740 | static int |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 741 | add_xoption(PyObject *opts, const wchar_t *s) |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 742 | { |
| 743 | PyObject *name, *value; |
| 744 | |
| 745 | const wchar_t *name_end = wcschr(s, L'='); |
| 746 | if (!name_end) { |
| 747 | name = PyUnicode_FromWideChar(s, -1); |
| 748 | value = Py_True; |
| 749 | Py_INCREF(value); |
| 750 | } |
| 751 | else { |
| 752 | name = PyUnicode_FromWideChar(s, name_end - s); |
| 753 | value = PyUnicode_FromWideChar(name_end + 1, -1); |
| 754 | } |
| 755 | if (name == NULL || value == NULL) { |
| 756 | goto error; |
| 757 | } |
| 758 | if (PyDict_SetItem(opts, name, value) < 0) { |
| 759 | goto error; |
| 760 | } |
| 761 | Py_DECREF(name); |
| 762 | Py_DECREF(value); |
| 763 | return 0; |
| 764 | |
| 765 | error: |
| 766 | Py_XDECREF(name); |
| 767 | Py_XDECREF(value); |
| 768 | return -1; |
| 769 | } |
| 770 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 771 | |
| 772 | static PyObject* |
| 773 | config_create_xoptions_dict(const _PyCoreConfig *config) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 774 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 775 | int nxoption = config->nxoption; |
| 776 | wchar_t **xoptions = config->xoptions; |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 777 | PyObject *dict = PyDict_New(); |
| 778 | if (dict == NULL) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 779 | return NULL; |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 780 | } |
| 781 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 782 | for (int i=0; i < nxoption; i++) { |
| 783 | wchar_t *option = xoptions[i]; |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 784 | if (add_xoption(dict, option) < 0) { |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 785 | Py_DECREF(dict); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 786 | return NULL; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 787 | } |
| 788 | } |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 789 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 790 | return dict; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 793 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 794 | static _PyInitError |
| 795 | config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options) |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 796 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 797 | for (int i = 0; i < len; i++) { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 798 | _PyInitError err = _Py_wstrlist_append(&config->nwarnoption, |
| 799 | &config->warnoptions, |
| 800 | options[i]); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 801 | if (_Py_INIT_FAILED(err)) { |
| 802 | return err; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 803 | } |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 804 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 805 | return _Py_INIT_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 806 | } |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 807 | |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 808 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 809 | static _PyInitError |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 810 | config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline) |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 811 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 812 | _PyInitError err; |
| 813 | |
| 814 | assert(config->nwarnoption == 0); |
| 815 | |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 816 | /* The priority order for warnings configuration is (highest precedence |
| 817 | * first): |
| 818 | * |
| 819 | * - the BytesWarning filter, if needed ('-b', '-bb') |
| 820 | * - any '-W' command line options; then |
| 821 | * - the 'PYTHONWARNINGS' environment variable; then |
| 822 | * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then |
| 823 | * - any implicit filters added by _warnings.c/warnings.py |
| 824 | * |
| 825 | * All settings except the last are passed to the warnings module via |
| 826 | * the `sys.warnoptions` list. Since the warnings module works on the basis |
| 827 | * of "the most recently added filter will be checked first", we add |
| 828 | * the lowest precedence entries first so that later entries override them. |
| 829 | */ |
| 830 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 831 | if (config->dev_mode) { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 832 | err = _Py_wstrlist_append(&config->nwarnoption, |
| 833 | &config->warnoptions, |
| 834 | L"default"); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 835 | if (_Py_INIT_FAILED(err)) { |
| 836 | return err; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 837 | } |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 838 | } |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 839 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 840 | err = config_add_warnings_optlist(config, |
| 841 | cmdline->nenv_warnoption, |
| 842 | cmdline->env_warnoptions); |
| 843 | if (_Py_INIT_FAILED(err)) { |
| 844 | return err; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 847 | err = config_add_warnings_optlist(config, |
| 848 | cmdline->nwarnoption, |
| 849 | cmdline->warnoptions); |
| 850 | if (_Py_INIT_FAILED(err)) { |
| 851 | return err; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c |
| 855 | * don't even try to emit a warning, so we skip setting the filter in that |
| 856 | * case. |
| 857 | */ |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 858 | if (config->bytes_warning) { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 859 | wchar_t *filter; |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 860 | if (config->bytes_warning> 1) { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 861 | filter = L"error::BytesWarning"; |
| 862 | } |
| 863 | else { |
| 864 | filter = L"default::BytesWarning"; |
| 865 | } |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 866 | err = _Py_wstrlist_append(&config->nwarnoption, |
| 867 | &config->warnoptions, |
| 868 | filter); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 869 | if (_Py_INIT_FAILED(err)) { |
| 870 | return err; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 871 | } |
| 872 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 873 | return _Py_INIT_OK(); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 877 | /* Get warning options from PYTHONWARNINGS environment variable. |
| 878 | Return 0 on success. |
| 879 | Set pymain->err and return -1 on error. */ |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 880 | static _PyInitError |
Victor Stinner | d145775 | 2018-07-26 16:04:56 +0200 | [diff] [blame] | 881 | cmdline_init_env_warnoptions(_PyMain *pymain, const _PyCoreConfig *config, |
| 882 | _PyCmdline *cmdline) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 883 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 884 | wchar_t *env; |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 885 | int res = _PyCoreConfig_GetEnvDup(config, &env, |
| 886 | L"PYTHONWARNINGS", "PYTHONWARNINGS"); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 887 | if (res < 0) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 888 | return DECODE_LOCALE_ERR("PYTHONWARNINGS", res); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 889 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 890 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 891 | if (env == NULL) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 892 | return _Py_INIT_OK(); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 893 | } |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 894 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 895 | |
| 896 | wchar_t *warning, *context = NULL; |
| 897 | for (warning = WCSTOK(env, L",", &context); |
| 898 | warning != NULL; |
| 899 | warning = WCSTOK(NULL, L",", &context)) |
| 900 | { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 901 | _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption, |
| 902 | &cmdline->env_warnoptions, |
| 903 | warning); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 904 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 905 | PyMem_RawFree(env); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 906 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 907 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 909 | PyMem_RawFree(env); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 910 | return _Py_INIT_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | |
| 914 | static void |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 915 | pymain_init_stdio(_PyMain *pymain, _PyCoreConfig *config) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 916 | { |
| 917 | pymain->stdin_is_interactive = (isatty(fileno(stdin)) |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 918 | || config->interactive); |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 919 | |
Sjoerd Mullender | 9cf424b | 2002-08-09 13:35:18 +0000 | [diff] [blame] | 920 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
Victor Stinner | 89e3436 | 2011-01-07 18:47:22 +0000 | [diff] [blame] | 921 | /* don't translate newlines (\r\n <=> \n) */ |
| 922 | _setmode(fileno(stdin), O_BINARY); |
| 923 | _setmode(fileno(stdout), O_BINARY); |
| 924 | _setmode(fileno(stderr), O_BINARY); |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 925 | #endif |
Victor Stinner | 89e3436 | 2011-01-07 18:47:22 +0000 | [diff] [blame] | 926 | |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 927 | if (!config->buffered_stdio) { |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 928 | #ifdef HAVE_SETVBUF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
| 930 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
| 931 | setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 932 | #else /* !HAVE_SETVBUF */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | setbuf(stdin, (char *)NULL); |
| 934 | setbuf(stdout, (char *)NULL); |
| 935 | setbuf(stderr, (char *)NULL); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 936 | #endif /* !HAVE_SETVBUF */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | } |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 938 | else if (config->interactive) { |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 939 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | /* Doesn't have to have line-buffered -- use unbuffered */ |
| 941 | /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */ |
| 942 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 943 | #else /* !MS_WINDOWS */ |
| 944 | #ifdef HAVE_SETVBUF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
| 946 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 947 | #endif /* HAVE_SETVBUF */ |
| 948 | #endif /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | /* Leave stderr alone - it should be unbuffered anyway. */ |
| 950 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 951 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 952 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 953 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 954 | static void |
| 955 | pymain_header(_PyMain *pymain) |
| 956 | { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 957 | if (Py_QuietFlag) { |
| 958 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 960 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 961 | if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 962 | return; |
| 963 | } |
| 964 | |
| 965 | fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform()); |
| 966 | if (!Py_NoSiteFlag) { |
| 967 | fprintf(stderr, "%s\n", COPYRIGHT); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 972 | static int |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 973 | pymain_init_core_argv(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline) |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 974 | { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 975 | /* Copy argv to be able to modify it (to force -c/-m) */ |
| 976 | int argc = pymain->argc - _PyOS_optind; |
| 977 | wchar_t **argv; |
| 978 | |
| 979 | if (argc <= 0 || cmdline->argv == NULL) { |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 980 | /* Ensure at least one (empty) argument is seen */ |
| 981 | static wchar_t *empty_argv[1] = {L""}; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 982 | argc = 1; |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 983 | argv = _Py_wstrlist_copy(1, empty_argv); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 984 | } |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 985 | else { |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 986 | argv = _Py_wstrlist_copy(argc, &cmdline->argv[_PyOS_optind]); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | if (argv == NULL) { |
| 990 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 991 | return -1; |
| 992 | } |
| 993 | |
| 994 | wchar_t *arg0 = NULL; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 995 | if (pymain->command != NULL) { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 996 | /* Force sys.argv[0] = '-c' */ |
| 997 | arg0 = L"-c"; |
| 998 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 999 | else if (pymain->module != NULL) { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1000 | /* Force sys.argv[0] = '-m'*/ |
| 1001 | arg0 = L"-m"; |
| 1002 | } |
| 1003 | if (arg0 != NULL) { |
| 1004 | arg0 = _PyMem_RawWcsdup(arg0); |
| 1005 | if (arg0 == NULL) { |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 1006 | _Py_wstrlist_clear(argc, argv); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1007 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 1008 | return -1; |
| 1009 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1010 | |
| 1011 | assert(argc >= 1); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1012 | PyMem_RawFree(argv[0]); |
| 1013 | argv[0] = arg0; |
| 1014 | } |
| 1015 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1016 | config->argc = argc; |
| 1017 | config->argv = argv; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1022 | static PyObject* |
| 1023 | wstrlist_as_pylist(int len, wchar_t **list) |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1024 | { |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1025 | assert(list != NULL || len < 1); |
| 1026 | |
| 1027 | PyObject *pylist = PyList_New(len); |
| 1028 | if (pylist == NULL) { |
| 1029 | return NULL; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1030 | } |
| 1031 | |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1032 | for (int i = 0; i < len; i++) { |
| 1033 | PyObject *v = PyUnicode_FromWideChar(list[i], -1); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1034 | if (v == NULL) { |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1035 | Py_DECREF(pylist); |
| 1036 | return NULL; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1037 | } |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1038 | PyList_SET_ITEM(pylist, i, v); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1039 | } |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1040 | return pylist; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1043 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1044 | static void |
| 1045 | pymain_import_readline(_PyMain *pymain) |
| 1046 | { |
| 1047 | if (Py_IsolatedFlag) { |
| 1048 | return; |
| 1049 | } |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1050 | if (!Py_InspectFlag && RUN_CODE(pymain)) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1051 | return; |
| 1052 | } |
| 1053 | if (!isatty(fileno(stdin))) { |
| 1054 | return; |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 1055 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1056 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1057 | PyObject *mod = PyImport_ImportModule("readline"); |
| 1058 | if (mod == NULL) { |
| 1059 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | } |
| 1061 | else { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1062 | Py_DECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1067 | static void |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1068 | pymain_run_startup(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf) |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1069 | { |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 1070 | const char *startup = _PyCoreConfig_GetEnv(config, "PYTHONSTARTUP"); |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1071 | if (startup == NULL) { |
| 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | FILE *fp = _Py_fopen(startup, "r"); |
| 1076 | if (fp == NULL) { |
| 1077 | int save_errno = errno; |
| 1078 | PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); |
| 1079 | errno = save_errno; |
| 1080 | |
| 1081 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
| 1082 | startup); |
| 1083 | PyErr_Print(); |
| 1084 | PyErr_Clear(); |
| 1085 | return; |
| 1086 | } |
| 1087 | |
| 1088 | (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); |
| 1089 | PyErr_Clear(); |
| 1090 | fclose(fp); |
| 1091 | } |
| 1092 | |
| 1093 | |
| 1094 | static void |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 1095 | pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1096 | { |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 1097 | const wchar_t *filename = pymain->filename; |
| 1098 | FILE *fp = _Py_wfopen(filename, L"r"); |
| 1099 | if (fp == NULL) { |
| 1100 | char *cfilename_buffer; |
| 1101 | const char *cfilename; |
| 1102 | int err = errno; |
| 1103 | cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL); |
| 1104 | if (cfilename_buffer != NULL) |
| 1105 | cfilename = cfilename_buffer; |
| 1106 | else |
| 1107 | cfilename = "<unprintable file name>"; |
| 1108 | fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n", |
| 1109 | config->program, cfilename, err, strerror(err)); |
| 1110 | PyMem_RawFree(cfilename_buffer); |
| 1111 | pymain->status = 2; |
| 1112 | return; |
| 1113 | } |
| 1114 | |
| 1115 | if (pymain->skip_first_line) { |
| 1116 | int ch; |
| 1117 | /* Push back first newline so line numbers remain the same */ |
| 1118 | while ((ch = getc(fp)) != EOF) { |
| 1119 | if (ch == '\n') { |
| 1120 | (void)ungetc(ch, fp); |
| 1121 | break; |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | struct _Py_stat_struct sb; |
| 1127 | if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) { |
| 1128 | fprintf(stderr, |
| 1129 | "%ls: '%ls' is a directory, cannot continue\n", |
| 1130 | config->program, filename); |
| 1131 | pymain->status = 1; |
Victor Stinner | d807862 | 2018-08-03 23:54:06 +0200 | [diff] [blame^] | 1132 | fclose(fp); |
| 1133 | return; |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /* call pending calls like signal handlers (SIGINT) */ |
| 1137 | if (Py_MakePendingCalls() == -1) { |
| 1138 | PyErr_Print(); |
| 1139 | pymain->status = 1; |
Victor Stinner | d807862 | 2018-08-03 23:54:06 +0200 | [diff] [blame^] | 1140 | fclose(fp); |
| 1141 | return; |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | PyObject *unicode, *bytes = NULL; |
| 1145 | const char *filename_str; |
| 1146 | |
| 1147 | unicode = PyUnicode_FromWideChar(filename, wcslen(filename)); |
| 1148 | if (unicode != NULL) { |
| 1149 | bytes = PyUnicode_EncodeFSDefault(unicode); |
| 1150 | Py_DECREF(unicode); |
| 1151 | } |
| 1152 | if (bytes != NULL) { |
| 1153 | filename_str = PyBytes_AsString(bytes); |
| 1154 | } |
| 1155 | else { |
| 1156 | PyErr_Clear(); |
| 1157 | filename_str = "<filename encoding error>"; |
| 1158 | } |
| 1159 | |
Victor Stinner | d807862 | 2018-08-03 23:54:06 +0200 | [diff] [blame^] | 1160 | /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */ |
| 1161 | int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf); |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 1162 | Py_XDECREF(bytes); |
| 1163 | pymain->status = (run != 0); |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | |
| 1167 | static void |
| 1168 | pymain_run_stdin(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf) |
| 1169 | { |
| 1170 | if (pymain->stdin_is_interactive) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1171 | Py_InspectFlag = 0; /* do exit on SystemExit */ |
Victor Stinner | a4d20b2 | 2018-08-01 02:57:45 +0200 | [diff] [blame] | 1172 | config->inspect = 0; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1173 | pymain_run_startup(pymain, config, cf); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1174 | pymain_run_interactive_hook(); |
| 1175 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1176 | |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 1177 | /* call pending calls like signal handlers (SIGINT) */ |
| 1178 | if (Py_MakePendingCalls() == -1) { |
| 1179 | PyErr_Print(); |
| 1180 | pymain->status = 1; |
| 1181 | return; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 1184 | int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf); |
| 1185 | pymain->status = (run != 0); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | |
| 1189 | static void |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1190 | pymain_repl(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1191 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | /* Check this environment variable at the end, to give programs the |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1193 | opportunity to set it from Python. */ |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 1194 | if (!Py_InspectFlag && _PyCoreConfig_GetEnv(config, "PYTHONINSPECT")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | Py_InspectFlag = 1; |
Victor Stinner | a4d20b2 | 2018-08-01 02:57:45 +0200 | [diff] [blame] | 1196 | config->inspect = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1198 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1199 | if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1200 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1202 | |
| 1203 | Py_InspectFlag = 0; |
Victor Stinner | a4d20b2 | 2018-08-01 02:57:45 +0200 | [diff] [blame] | 1204 | config->inspect = 0; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1205 | pymain_run_interactive_hook(); |
Victor Stinner | 33c377e | 2017-12-05 15:12:41 +0100 | [diff] [blame] | 1206 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1207 | int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1208 | pymain->status = (res != 0); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | |
| 1212 | /* Parse the command line. |
| 1213 | Handle --version and --help options directly. |
| 1214 | |
| 1215 | Return 1 if Python must exit. |
| 1216 | Return 0 on success. |
| 1217 | Set pymain->err and return -1 on failure. */ |
| 1218 | static int |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1219 | pymain_parse_cmdline(_PyMain *pymain, _PyCoreConfig *config, |
| 1220 | _PyCmdline *cmdline) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1221 | { |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1222 | int res = pymain_parse_cmdline_impl(pymain, config, cmdline); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1223 | if (res < 0) { |
| 1224 | return -1; |
| 1225 | } |
| 1226 | if (res) { |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1227 | pymain_usage(1, config->program); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1228 | pymain->status = 2; |
| 1229 | return 1; |
| 1230 | } |
| 1231 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1232 | if (pymain->command != NULL || pymain->module != NULL) { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1233 | /* Backup _PyOS_optind */ |
| 1234 | _PyOS_optind--; |
| 1235 | } |
| 1236 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1241 | /* Parse command line options and environment variables. |
| 1242 | This code must not use Python runtime apart PyMem_Raw memory allocator. |
| 1243 | |
| 1244 | Return 0 on success. |
| 1245 | Return 1 if Python is done and must exit. |
| 1246 | Set pymain->err and return -1 on error. */ |
| 1247 | static int |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1248 | pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config, |
| 1249 | _PyCmdline *cmdline) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1250 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1251 | _PyInitError err; |
| 1252 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1253 | int res = pymain_parse_cmdline(pymain, config, cmdline); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1254 | if (res != 0) { |
| 1255 | return res; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1258 | if (pymain_init_core_argv(pymain, config, cmdline) < 0) { |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1259 | return -1; |
| 1260 | } |
| 1261 | |
Victor Stinner | d145775 | 2018-07-26 16:04:56 +0200 | [diff] [blame] | 1262 | err = _PyCoreConfig_Read(config); |
| 1263 | if (_Py_INIT_FAILED(err)) { |
| 1264 | pymain->err = err; |
| 1265 | return -1; |
| 1266 | } |
| 1267 | |
| 1268 | assert(config->use_environment >= 0); |
| 1269 | if (config->use_environment) { |
Victor Stinner | ecf411c | 2018-07-26 02:37:22 +0200 | [diff] [blame] | 1270 | err = cmdline_init_env_warnoptions(pymain, config, cmdline); |
| 1271 | if (_Py_INIT_FAILED(err)) { |
| 1272 | pymain->err = err; |
| 1273 | return -1; |
| 1274 | } |
| 1275 | } |
| 1276 | |
Victor Stinner | d145775 | 2018-07-26 16:04:56 +0200 | [diff] [blame] | 1277 | err = config_init_warnoptions(config, cmdline); |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1278 | if (_Py_INIT_FAILED(err)) { |
| 1279 | pymain->err = err; |
| 1280 | return -1; |
| 1281 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1286 | /* Read the configuration, but initialize also the LC_CTYPE locale: |
| 1287 | enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */ |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1288 | static int |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1289 | pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config, |
| 1290 | _PyCmdline *cmdline) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1291 | { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1292 | _PyCoreConfig save_config = _PyCoreConfig_INIT; |
| 1293 | char *oldloc = NULL; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1294 | int res = -1; |
| 1295 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1296 | oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL)); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1297 | if (oldloc == NULL) { |
| 1298 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 1299 | goto done; |
| 1300 | } |
| 1301 | |
| 1302 | /* Reconfigure the locale to the default for this process */ |
| 1303 | _Py_SetLocaleFromEnv(LC_ALL); |
| 1304 | |
| 1305 | int locale_coerced = 0; |
| 1306 | int loops = 0; |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1307 | |
| 1308 | if (_PyCoreConfig_Copy(&save_config, config) < 0) { |
| 1309 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 1310 | goto done; |
| 1311 | } |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1312 | |
| 1313 | while (1) { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1314 | int utf8_mode = config->utf8_mode; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1315 | int encoding_changed = 0; |
| 1316 | |
| 1317 | /* Watchdog to prevent an infinite loop */ |
| 1318 | loops++; |
| 1319 | if (loops == 3) { |
| 1320 | pymain->err = _Py_INIT_ERR("Encoding changed twice while " |
| 1321 | "reading the configuration"); |
| 1322 | goto done; |
| 1323 | } |
| 1324 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1325 | if (pymain_init_cmdline_argv(pymain, config, cmdline) < 0) { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1326 | goto done; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1327 | } |
| 1328 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1329 | int conf_res = pymain_read_conf_impl(pymain, config, cmdline); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1330 | if (conf_res != 0) { |
| 1331 | res = conf_res; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1332 | goto done; |
| 1333 | } |
| 1334 | |
| 1335 | /* The legacy C locale assumes ASCII as the default text encoding, which |
| 1336 | * causes problems not only for the CPython runtime, but also other |
| 1337 | * components like GNU readline. |
| 1338 | * |
| 1339 | * Accordingly, when the CLI detects it, it attempts to coerce it to a |
| 1340 | * more capable UTF-8 based alternative. |
| 1341 | * |
| 1342 | * See the documentation of the PYTHONCOERCECLOCALE setting for more |
| 1343 | * details. |
| 1344 | */ |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1345 | if (config->coerce_c_locale == 1 && !locale_coerced) { |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1346 | locale_coerced = 1; |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1347 | _Py_CoerceLegacyLocale(config); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1348 | encoding_changed = 1; |
| 1349 | } |
| 1350 | |
| 1351 | if (utf8_mode == -1) { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1352 | if (config->utf8_mode == 1) { |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1353 | /* UTF-8 Mode enabled */ |
| 1354 | encoding_changed = 1; |
| 1355 | } |
| 1356 | } |
| 1357 | else { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1358 | if (config->utf8_mode != utf8_mode) { |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1359 | encoding_changed = 1; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | if (!encoding_changed) { |
| 1364 | break; |
| 1365 | } |
| 1366 | |
Victor Stinner | d145775 | 2018-07-26 16:04:56 +0200 | [diff] [blame] | 1367 | /* Reset the configuration before reading again the configuration, |
| 1368 | just keep UTF-8 Mode value. */ |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1369 | int new_utf8_mode = config->utf8_mode; |
| 1370 | if (_PyCoreConfig_Copy(config, &save_config) < 0) { |
| 1371 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 1372 | goto done; |
| 1373 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1374 | pymain_clear_cmdline(pymain, cmdline); |
Victor Stinner | 6c5a4b3 | 2018-06-16 00:06:28 +0200 | [diff] [blame] | 1375 | memset(cmdline, 0, sizeof(*cmdline)); |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1376 | config->utf8_mode = new_utf8_mode; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1377 | |
| 1378 | /* The encoding changed: read again the configuration |
| 1379 | with the new encoding */ |
| 1380 | } |
| 1381 | res = 0; |
| 1382 | |
| 1383 | done: |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1384 | _PyCoreConfig_Clear(&save_config); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1385 | if (oldloc != NULL) { |
| 1386 | setlocale(LC_ALL, oldloc); |
| 1387 | PyMem_RawFree(oldloc); |
| 1388 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1389 | return res; |
| 1390 | } |
| 1391 | |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1392 | |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1393 | void |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1394 | _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config) |
| 1395 | { |
| 1396 | Py_CLEAR(config->argv); |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 1397 | Py_CLEAR(config->executable); |
| 1398 | Py_CLEAR(config->prefix); |
| 1399 | Py_CLEAR(config->base_prefix); |
| 1400 | Py_CLEAR(config->exec_prefix); |
| 1401 | Py_CLEAR(config->base_exec_prefix); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1402 | Py_CLEAR(config->warnoptions); |
| 1403 | Py_CLEAR(config->xoptions); |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 1404 | Py_CLEAR(config->module_search_path); |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 1405 | Py_CLEAR(config->pycache_prefix); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | |
| 1409 | static PyObject* |
| 1410 | config_copy_attr(PyObject *obj) |
| 1411 | { |
| 1412 | if (PyUnicode_Check(obj)) { |
| 1413 | Py_INCREF(obj); |
| 1414 | return obj; |
| 1415 | } |
| 1416 | else if (PyList_Check(obj)) { |
| 1417 | return PyList_GetSlice(obj, 0, Py_SIZE(obj)); |
| 1418 | } |
| 1419 | else if (PyDict_Check(obj)) { |
| 1420 | /* The dict type is used for xoptions. Make the assumption that keys |
| 1421 | and values are immutables */ |
| 1422 | return PyDict_Copy(obj); |
| 1423 | } |
| 1424 | else { |
| 1425 | PyErr_Format(PyExc_TypeError, |
| 1426 | "cannot copy config attribute of type %.200s", |
| 1427 | Py_TYPE(obj)->tp_name); |
| 1428 | return NULL; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | |
| 1433 | int |
| 1434 | _PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config, |
| 1435 | const _PyMainInterpreterConfig *config2) |
| 1436 | { |
| 1437 | _PyMainInterpreterConfig_Clear(config); |
| 1438 | |
| 1439 | #define COPY_ATTR(ATTR) \ |
| 1440 | do { \ |
| 1441 | if (config2->ATTR != NULL) { \ |
| 1442 | config->ATTR = config_copy_attr(config2->ATTR); \ |
| 1443 | if (config->ATTR == NULL) { \ |
| 1444 | return -1; \ |
| 1445 | } \ |
| 1446 | } \ |
| 1447 | } while (0) |
| 1448 | |
| 1449 | COPY_ATTR(argv); |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 1450 | COPY_ATTR(executable); |
| 1451 | COPY_ATTR(prefix); |
| 1452 | COPY_ATTR(base_prefix); |
| 1453 | COPY_ATTR(exec_prefix); |
| 1454 | COPY_ATTR(base_exec_prefix); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1455 | COPY_ATTR(warnoptions); |
| 1456 | COPY_ATTR(xoptions); |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 1457 | COPY_ATTR(module_search_path); |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 1458 | COPY_ATTR(pycache_prefix); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1459 | #undef COPY_ATTR |
| 1460 | return 0; |
| 1461 | } |
| 1462 | |
| 1463 | |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 1464 | _PyInitError |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1465 | _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config, |
| 1466 | const _PyCoreConfig *config) |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1467 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1468 | if (main_config->install_signal_handlers < 0) { |
| 1469 | main_config->install_signal_handlers = config->install_signal_handlers; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1470 | } |
| 1471 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1472 | if (main_config->xoptions == NULL) { |
| 1473 | main_config->xoptions = config_create_xoptions_dict(config); |
| 1474 | if (main_config->xoptions == NULL) { |
| 1475 | return _Py_INIT_NO_MEMORY(); |
| 1476 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1477 | } |
| 1478 | |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1479 | #define COPY_WSTR(ATTR) \ |
| 1480 | do { \ |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1481 | if (main_config->ATTR == NULL && config->ATTR != NULL) { \ |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1482 | main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \ |
| 1483 | if (main_config->ATTR == NULL) { \ |
| 1484 | return _Py_INIT_NO_MEMORY(); \ |
| 1485 | } \ |
| 1486 | } \ |
| 1487 | } while (0) |
| 1488 | #define COPY_WSTRLIST(ATTR, LEN, LIST) \ |
| 1489 | do { \ |
| 1490 | if (ATTR == NULL) { \ |
| 1491 | ATTR = wstrlist_as_pylist(LEN, LIST); \ |
| 1492 | if (ATTR == NULL) { \ |
| 1493 | return _Py_INIT_NO_MEMORY(); \ |
| 1494 | } \ |
| 1495 | } \ |
| 1496 | } while (0) |
| 1497 | |
| 1498 | COPY_WSTRLIST(main_config->warnoptions, |
| 1499 | config->nwarnoption, config->warnoptions); |
| 1500 | if (config->argc >= 0) { |
| 1501 | COPY_WSTRLIST(main_config->argv, |
| 1502 | config->argc, config->argv); |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1503 | } |
| 1504 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 1505 | if (config->_install_importlib) { |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1506 | COPY_WSTR(executable); |
| 1507 | COPY_WSTR(prefix); |
| 1508 | COPY_WSTR(base_prefix); |
| 1509 | COPY_WSTR(exec_prefix); |
| 1510 | COPY_WSTR(base_exec_prefix); |
| 1511 | |
| 1512 | COPY_WSTRLIST(main_config->module_search_path, |
| 1513 | config->nmodule_search_path, config->module_search_paths); |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 1514 | |
| 1515 | if (config->pycache_prefix != NULL) { |
| 1516 | COPY_WSTR(pycache_prefix); |
| 1517 | } else { |
| 1518 | main_config->pycache_prefix = NULL; |
| 1519 | } |
| 1520 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1521 | } |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 1522 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1523 | return _Py_INIT_OK(); |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1524 | #undef COPY_WSTR |
| 1525 | #undef COPY_WSTRLIST |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | |
| 1529 | static int |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1530 | pymain_init_python_main(_PyMain *pymain, _PyCoreConfig *config, |
| 1531 | PyInterpreterState *interp) |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1532 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1533 | _PyInitError err; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1534 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1535 | _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1536 | err = _PyMainInterpreterConfig_Read(&main_config, config); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1537 | if (!_Py_INIT_FAILED(err)) { |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1538 | err = _Py_InitializeMainInterpreter(interp, &main_config); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1539 | } |
| 1540 | _PyMainInterpreterConfig_Clear(&main_config); |
| 1541 | |
| 1542 | if (_Py_INIT_FAILED(err)) { |
| 1543 | pymain->err = err; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1544 | return -1; |
| 1545 | } |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1546 | return 0; |
| 1547 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1548 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1549 | |
| 1550 | static int |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1551 | pymain_run_python(_PyMain *pymain, PyInterpreterState *interp) |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1552 | { |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1553 | int res = 0; |
| 1554 | _PyCoreConfig *config = &interp->core_config; |
| 1555 | |
| 1556 | PyObject *main_importer_path = NULL; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1557 | if (pymain->filename != NULL) { |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 1558 | /* If filename is a package (ex: directory or ZIP file) which contains |
| 1559 | __main__.py, main_importer_path is set to filename and will be |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1560 | prepended to sys.path. |
| 1561 | |
| 1562 | Otherwise, main_importer_path is set to NULL. */ |
| 1563 | main_importer_path = pymain_get_importer(pymain->filename); |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 1564 | } |
| 1565 | |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1566 | if (main_importer_path != NULL) { |
| 1567 | if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) { |
| 1568 | pymain->status = 1; |
| 1569 | goto done; |
| 1570 | } |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 1571 | } |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1572 | else if (!config->isolated) { |
| 1573 | PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc, |
| 1574 | config->argv); |
| 1575 | if (path0 == NULL) { |
| 1576 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 1577 | res = -1; |
| 1578 | goto done; |
| 1579 | } |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1580 | |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1581 | if (pymain_sys_path_add_path0(interp, path0) < 0) { |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1582 | Py_DECREF(path0); |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1583 | pymain->status = 1; |
| 1584 | goto done; |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1585 | } |
| 1586 | Py_DECREF(path0); |
| 1587 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1588 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1589 | PyCompilerFlags cf = {.cf_flags = 0}; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1590 | |
| 1591 | pymain_header(pymain); |
| 1592 | pymain_import_readline(pymain); |
| 1593 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1594 | if (pymain->command) { |
| 1595 | pymain->status = pymain_run_command(pymain->command, &cf); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1596 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1597 | else if (pymain->module) { |
| 1598 | pymain->status = (pymain_run_module(pymain->module, 1) != 0); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1599 | } |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1600 | else if (main_importer_path != NULL) { |
| 1601 | int sts = pymain_run_module(L"__main__", 0); |
| 1602 | pymain->status = (sts != 0); |
| 1603 | } |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 1604 | else if (pymain->filename != NULL) { |
| 1605 | pymain_run_file(pymain, config, &cf); |
| 1606 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1607 | else { |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 1608 | pymain_run_stdin(pymain, config, &cf); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1609 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1610 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1611 | pymain_repl(pymain, config, &cf); |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1612 | |
| 1613 | done: |
| 1614 | Py_XDECREF(main_importer_path); |
| 1615 | return res; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1618 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1619 | static int |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1620 | pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config, |
| 1621 | _PyCmdline *cmdline) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1622 | { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1623 | pymain->err = _PyRuntime_Initialize(); |
| 1624 | if (_Py_INIT_FAILED(pymain->err)) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1625 | return -1; |
| 1626 | } |
| 1627 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1628 | int res = pymain_read_conf(pymain, config, cmdline); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1629 | if (res < 0) { |
| 1630 | return -1; |
| 1631 | } |
| 1632 | if (res > 0) { |
| 1633 | /* --help or --version command: we are done */ |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1634 | return 1; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1635 | } |
| 1636 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1637 | if (cmdline->print_help) { |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1638 | pymain_usage(0, config->program); |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1639 | return 1; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | if (cmdline->print_version) { |
| 1643 | printf("Python %s\n", |
| 1644 | (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION); |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1645 | return 1; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1646 | } |
| 1647 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1648 | /* For Py_GetArgcArgv(). Cleared by pymain_free(). */ |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 1649 | orig_argv = _Py_wstrlist_copy(pymain->argc, cmdline->argv); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1650 | if (orig_argv == NULL) { |
| 1651 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 1652 | return -1; |
| 1653 | } |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1654 | orig_argc = pymain->argc; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1655 | return 0; |
| 1656 | } |
Barry Warsaw | 3e13b1e | 2001-02-23 16:46:39 +0000 | [diff] [blame] | 1657 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1658 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1659 | /* Read the configuration into _PyCoreConfig and _PyMain, initialize the |
| 1660 | LC_CTYPE locale and Py_DecodeLocale(). |
| 1661 | |
| 1662 | Configuration: |
| 1663 | |
| 1664 | * Command line arguments |
| 1665 | * Environment variables |
| 1666 | * Py_xxx global configuration variables |
| 1667 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1668 | _PyCmdline is a temporary structure used to prioritize these |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1669 | variables. */ |
| 1670 | static int |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1671 | pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config) |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1672 | { |
Victor Stinner | 31e9908 | 2017-12-20 23:41:38 +0100 | [diff] [blame] | 1673 | /* Force default allocator, since pymain_free() and pymain_clear_config() |
| 1674 | must use the same allocator than this function. */ |
| 1675 | PyMemAllocatorEx old_alloc; |
| 1676 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 1677 | #ifdef Py_DEBUG |
| 1678 | PyMemAllocatorEx default_alloc; |
| 1679 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc); |
| 1680 | #endif |
| 1681 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1682 | _PyCmdline cmdline; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1683 | memset(&cmdline, 0, sizeof(cmdline)); |
| 1684 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1685 | int res = pymain_cmdline_impl(pymain, config, &cmdline); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1686 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1687 | pymain_clear_cmdline(pymain, &cmdline); |
Victor Stinner | 31e9908 | 2017-12-20 23:41:38 +0100 | [diff] [blame] | 1688 | |
| 1689 | #ifdef Py_DEBUG |
| 1690 | /* Make sure that PYMEM_DOMAIN_RAW has not been modified */ |
| 1691 | PyMemAllocatorEx cur_alloc; |
| 1692 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc); |
| 1693 | assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0); |
| 1694 | #endif |
| 1695 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1696 | return res; |
| 1697 | } |
| 1698 | |
| 1699 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1700 | static int |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1701 | pymain_init(_PyMain *pymain, PyInterpreterState **interp_p) |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1702 | { |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1703 | /* 754 requires that FP exceptions run in "no stop" mode by default, |
| 1704 | * and until C vendors implement C99's ways to control FP exceptions, |
| 1705 | * Python requires non-stop mode. Alas, some platforms enable FP |
| 1706 | * exceptions by default. Here we disable them. |
| 1707 | */ |
| 1708 | #ifdef __FreeBSD__ |
| 1709 | fedisableexcept(FE_OVERFLOW); |
| 1710 | #endif |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1711 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1712 | _PyCoreConfig local_config = _PyCoreConfig_INIT; |
| 1713 | _PyCoreConfig *config = &local_config; |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 1714 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1715 | _PyCoreConfig_GetGlobalConfig(config); |
| 1716 | |
| 1717 | int cmd_res = pymain_cmdline(pymain, config); |
| 1718 | if (cmd_res < 0) { |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1719 | _Py_FatalInitError(pymain->err); |
| 1720 | } |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1721 | if (cmd_res == 1) { |
| 1722 | pymain_clear_config(config); |
| 1723 | return 1; |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1724 | } |
| 1725 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1726 | _PyCoreConfig_SetGlobalConfig(config); |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 1727 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1728 | pymain_init_stdio(pymain, config); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1729 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1730 | PyInterpreterState *interp; |
| 1731 | pymain->err = _Py_InitializeCore(&interp, config); |
| 1732 | if (_Py_INIT_FAILED(pymain->err)) { |
| 1733 | _Py_FatalInitError(pymain->err); |
| 1734 | } |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1735 | *interp_p = interp; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1736 | |
| 1737 | pymain_clear_config(config); |
| 1738 | config = &interp->core_config; |
| 1739 | |
| 1740 | if (pymain_init_python_main(pymain, config, interp) < 0) { |
| 1741 | _Py_FatalInitError(pymain->err); |
| 1742 | } |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1743 | return 0; |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | static int |
| 1748 | pymain_main(_PyMain *pymain) |
| 1749 | { |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1750 | PyInterpreterState *interp; |
| 1751 | int res = pymain_init(pymain, &interp); |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1752 | if (res != 1) { |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 1753 | if (pymain_run_python(pymain, interp) < 0) { |
| 1754 | _Py_FatalInitError(pymain->err); |
| 1755 | } |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1756 | |
| 1757 | if (Py_FinalizeEx() < 0) { |
| 1758 | /* Value unlikely to be confused with a non-error exit status or |
| 1759 | other special meaning */ |
| 1760 | pymain->status = 120; |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 1761 | } |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1762 | } |
| 1763 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1764 | pymain_free(pymain); |
| 1765 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1766 | return pymain->status; |
| 1767 | } |
| 1768 | |
| 1769 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1770 | int |
| 1771 | Py_Main(int argc, wchar_t **argv) |
| 1772 | { |
| 1773 | _PyMain pymain = _PyMain_INIT; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1774 | pymain.use_bytes_argv = 0; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1775 | pymain.argc = argc; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1776 | pymain.wchar_argv = argv; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1777 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1778 | return pymain_main(&pymain); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1781 | |
| 1782 | int |
| 1783 | _Py_UnixMain(int argc, char **argv) |
| 1784 | { |
| 1785 | _PyMain pymain = _PyMain_INIT; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1786 | pymain.use_bytes_argv = 1; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1787 | pymain.argc = argc; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 1788 | pymain.bytes_argv = argv; |
| 1789 | |
| 1790 | return pymain_main(&pymain); |
| 1791 | } |
| 1792 | |
| 1793 | |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 1794 | /* this is gonna seem *real weird*, but if you put some other code between |
| 1795 | Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the |
| 1796 | while statement in Misc/gdbinit:ppystack */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1797 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1798 | /* Make the *original* argc/argv available to other modules. |
| 1799 | This is rare, but it is needed by the secureware extension. */ |
| 1800 | |
| 1801 | void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1802 | Py_GetArgcArgv(int *argc, wchar_t ***argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1804 | *argc = orig_argc; |
| 1805 | *argv = orig_argv; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1806 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1807 | |
| 1808 | #ifdef __cplusplus |
| 1809 | } |
| 1810 | #endif |