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 | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 5 | #include "internal/import.h" |
Benjamin Peterson | e425bd7 | 2017-12-14 23:48:12 -0800 | [diff] [blame] | 6 | #include "internal/pygetopt.h" |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 7 | #include "internal/pystate.h" |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 8 | |
Antoine Pitrou | 5651eaa | 2008-09-06 20:46:58 +0000 | [diff] [blame] | 9 | #include <locale.h> |
| 10 | |
Sjoerd Mullender | 9cf424b | 2002-08-09 13:35:18 +0000 | [diff] [blame] | 11 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
Victor Stinner | 6efcb6d | 2017-12-18 23:42:55 +0100 | [diff] [blame] | 12 | # include <windows.h> |
| 13 | # ifdef HAVE_IO_H |
| 14 | # include <io.h> |
| 15 | # endif |
| 16 | # ifdef HAVE_FCNTL_H |
| 17 | # include <fcntl.h> |
| 18 | # endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 19 | #endif |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 20 | |
Martin v. Löwis | 945362c | 2007-08-30 14:57:25 +0000 | [diff] [blame] | 21 | #ifdef _MSC_VER |
Victor Stinner | 6efcb6d | 2017-12-18 23:42:55 +0100 | [diff] [blame] | 22 | # include <crtdbg.h> |
| 23 | #endif |
| 24 | |
| 25 | #ifdef __FreeBSD__ |
| 26 | # include <fenv.h> |
Martin v. Löwis | 945362c | 2007-08-30 14:57:25 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 29 | #if defined(MS_WINDOWS) |
Victor Stinner | 6efcb6d | 2017-12-18 23:42:55 +0100 | [diff] [blame] | 30 | # define PYTHONHOMEHELP "<prefix>\\python{major}{minor}" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 31 | #else |
Victor Stinner | 6efcb6d | 2017-12-18 23:42:55 +0100 | [diff] [blame] | 32 | # define PYTHONHOMEHELP "<prefix>/lib/pythonX.X" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 35 | #define COPYRIGHT \ |
Guido van Rossum | 36002d7 | 2001-07-18 16:59:46 +0000 | [diff] [blame] | 36 | "Type \"help\", \"copyright\", \"credits\" or \"license\" " \ |
| 37 | "for more information." |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 38 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 43 | #define DECODE_LOCALE_ERR(NAME, LEN) \ |
| 44 | (((LEN) == -2) \ |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 45 | ? _Py_INIT_USER_ERR("cannot decode " NAME) \ |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 46 | : _Py_INIT_NO_MEMORY()) |
| 47 | |
| 48 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 49 | #define SET_DECODE_ERROR(NAME, LEN) \ |
| 50 | do { \ |
| 51 | if ((LEN) == (size_t)-2) { \ |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 52 | pymain->err = _Py_INIT_USER_ERR("cannot decode " NAME); \ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 53 | } \ |
| 54 | else { \ |
| 55 | pymain->err = _Py_INIT_NO_MEMORY(); \ |
| 56 | } \ |
| 57 | } while (0) |
| 58 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 59 | #ifdef MS_WINDOWS |
| 60 | #define WCSTOK wcstok_s |
| 61 | #else |
| 62 | #define WCSTOK wcstok |
| 63 | #endif |
| 64 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 65 | /* For Py_GetArgcArgv(); set by main() */ |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 66 | static wchar_t **orig_argv = NULL; |
| 67 | static int orig_argc = 0; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 69 | /* command line options */ |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 70 | #define BASE_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?" |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 72 | #define PROGRAM_OPTS BASE_OPTS |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 73 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 74 | static const _PyOS_LongOption longoptions[] = { |
| 75 | {L"check-hash-based-pycs", 1, 0}, |
| 76 | {NULL, 0, 0}, |
| 77 | }; |
| 78 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 79 | /* Short usage message (with %s for argv0) */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 80 | static const char usage_line[] = |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 81 | "usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 82 | |
| 83 | /* Long usage message, split into parts < 512 bytes */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 84 | static const char usage_1[] = "\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 85 | Options and arguments (and corresponding environment variables):\n\ |
Christian Heimes | 2ab3444 | 2008-09-03 20:31:07 +0000 | [diff] [blame] | 86 | -b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\ |
| 87 | and comparing bytes/bytearray with str. (-bb: issue errors)\n\ |
Xiang Zhang | 0710d75 | 2017-03-11 13:02:52 +0800 | [diff] [blame] | 88 | -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] | 89 | -c cmd : program passed in as string (terminates option list)\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 90 | -d : debug output from parser; also PYTHONDEBUG=x\n\ |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 91 | -E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 92 | -h : print this help message and exit (also --help)\n\ |
Guido van Rossum | 61c345f | 2001-09-04 03:26:15 +0000 | [diff] [blame] | 93 | "; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 94 | static const char usage_2[] = "\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 95 | -i : inspect interactively after running script; forces a prompt even\n\ |
| 96 | if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\ |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 97 | -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] | 98 | -m mod : run library module as a script (terminates option list)\n\ |
Cheryl Sabella | 186b606 | 2018-02-24 22:04:40 -0500 | [diff] [blame] | 99 | -O : remove assert and __debug__-dependent statements; add .opt-1 before\n\ |
| 100 | .pyc extension; also PYTHONOPTIMIZE=x\n\ |
| 101 | -OO : do -O changes and also discard docstrings; add .opt-2 before\n\ |
| 102 | .pyc extension\n\ |
Georg Brandl | 9d87119 | 2010-12-04 10:47:18 +0000 | [diff] [blame] | 103 | -q : don't print version and copyright messages on interactive startup\n\ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 104 | -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] | 105 | -S : don't imply 'import site' on initialization\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 106 | "; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 107 | static const char usage_3[] = "\ |
Berker Peksag | 7f58097 | 2017-10-13 15:16:31 +0300 | [diff] [blame] | 108 | -u : force the stdout and stderr streams to be unbuffered;\n\ |
| 109 | this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 110 | -v : verbose (trace import statements); also PYTHONVERBOSE=x\n\ |
| 111 | can be supplied multiple times to increase verbosity\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 112 | -V : print the Python version number and exit (also --version)\n\ |
INADA Naoki | 0e175a6 | 2016-11-21 20:57:14 +0900 | [diff] [blame] | 113 | when given twice, print more information about the build\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 114 | -W arg : warning control; arg is action:message:category:module:lineno\n\ |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 115 | also PYTHONWARNINGS=arg\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 116 | -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] | 117 | -X opt : set implementation-specific option\n\ |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 118 | --check-hash-based-pycs always|default|never:\n\ |
| 119 | control how Python invalidates hash-based .pyc files\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 120 | "; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 121 | static const char usage_4[] = "\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 122 | file : program read from script file\n\ |
| 123 | - : program read from stdin (default; interactive mode if a tty)\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 124 | arg ...: arguments passed to program in sys.argv[1:]\n\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 125 | Other environment variables:\n\ |
| 126 | PYTHONSTARTUP: file executed on interactive startup (no default)\n\ |
Serhiy Storchaka | 1ba0161 | 2015-12-30 09:28:19 +0200 | [diff] [blame] | 127 | PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 128 | default module search path. The result is sys.path.\n\ |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 129 | "; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 130 | static const char usage_5[] = |
Serhiy Storchaka | 1ba0161 | 2015-12-30 09:28:19 +0200 | [diff] [blame] | 131 | "PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n" |
Victor Stinner | 9802b39 | 2010-08-19 11:36:43 +0000 | [diff] [blame] | 132 | " The default module search path uses %s.\n" |
| 133 | "PYTHONCASEOK : ignore case in 'import' statements (Windows).\n" |
| 134 | "PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n" |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 135 | "PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n"; |
| 136 | static const char usage_6[] = |
| 137 | "PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n" |
| 138 | " to seed the hashes of str, bytes and datetime objects. It can also be\n" |
| 139 | " set to an integer in the range [0,4294967295] to get hash values with a\n" |
| 140 | " predictable seed.\n" |
| 141 | "PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n" |
| 142 | " on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n" |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 143 | " hooks.\n" |
Stéphane Wirtel | 7d1017d | 2017-06-12 13:30:33 +0200 | [diff] [blame] | 144 | "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] | 145 | " coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n" |
Victor Stinner | 5e3806f | 2017-11-30 11:40:24 +0100 | [diff] [blame] | 146 | " locale coercion and locale compatibility warnings on stderr.\n" |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 147 | "PYTHONDEVMODE: enable the development mode.\n" |
| 148 | "PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 149 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 150 | static void |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 151 | pymain_usage(int error, const wchar_t* program) |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 152 | { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 153 | FILE *f = error ? stderr : stdout; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | fprintf(f, usage_line, program); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 156 | if (error) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | fprintf(f, "Try `python -h' for more information.\n"); |
| 158 | else { |
| 159 | fputs(usage_1, f); |
| 160 | fputs(usage_2, f); |
| 161 | fputs(usage_3, f); |
Serhiy Storchaka | 1ba0161 | 2015-12-30 09:28:19 +0200 | [diff] [blame] | 162 | fprintf(f, usage_4, (wint_t)DELIM); |
| 163 | fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 164 | fputs(usage_6, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 168 | |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 169 | static const char* |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 170 | config_get_env_var(const _PyCoreConfig *config, const char *name) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 171 | { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 172 | assert(config->ignore_environment >= 0); |
| 173 | |
| 174 | if (config->ignore_environment) { |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | const char *var = getenv(name); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 179 | if (var && var[0] != '\0') { |
| 180 | return var; |
| 181 | } |
| 182 | else { |
| 183 | return NULL; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 188 | static int |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 189 | config_get_env_var_dup(const _PyCoreConfig *config, wchar_t **dest, wchar_t *wname, char *name) |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 190 | { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 191 | assert(config->ignore_environment >= 0); |
| 192 | |
| 193 | if (config->ignore_environment) { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 194 | *dest = NULL; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | #ifdef MS_WINDOWS |
| 199 | const wchar_t *var = _wgetenv(wname); |
| 200 | if (!var || var[0] == '\0') { |
| 201 | *dest = NULL; |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | wchar_t *copy = _PyMem_RawWcsdup(var); |
| 206 | if (copy == NULL) { |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | *dest = copy; |
| 211 | #else |
| 212 | const char *var = getenv(name); |
| 213 | if (!var || var[0] == '\0') { |
| 214 | *dest = NULL; |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | size_t len; |
| 219 | wchar_t *wvar = Py_DecodeLocale(var, &len); |
| 220 | if (!wvar) { |
| 221 | if (len == (size_t)-2) { |
| 222 | return -2; |
| 223 | } |
| 224 | else { |
| 225 | return -1; |
| 226 | } |
| 227 | } |
| 228 | *dest = wvar; |
| 229 | #endif |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 234 | static void |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 235 | pymain_run_interactive_hook(void) |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 236 | { |
| 237 | PyObject *sys, *hook, *result; |
| 238 | sys = PyImport_ImportModule("sys"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 239 | if (sys == NULL) { |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 240 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 243 | hook = PyObject_GetAttrString(sys, "__interactivehook__"); |
| 244 | Py_DECREF(sys); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 245 | if (hook == NULL) { |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 246 | PyErr_Clear(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 247 | return; |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 248 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 249 | |
| 250 | result = _PyObject_CallNoArg(hook); |
| 251 | Py_DECREF(hook); |
| 252 | if (result == NULL) { |
| 253 | goto error; |
| 254 | } |
| 255 | Py_DECREF(result); |
| 256 | |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 257 | return; |
| 258 | |
| 259 | error: |
| 260 | PySys_WriteStderr("Failed calling sys.__interactivehook__\n"); |
| 261 | PyErr_Print(); |
| 262 | PyErr_Clear(); |
| 263 | } |
| 264 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 265 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 266 | static int |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 267 | pymain_run_module(const wchar_t *modname, int set_argv0) |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | PyObject *module, *runpy, *runmodule, *runargs, *result; |
| 270 | runpy = PyImport_ImportModule("runpy"); |
| 271 | if (runpy == NULL) { |
| 272 | fprintf(stderr, "Could not import runpy module\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame] | 273 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main"); |
| 277 | if (runmodule == NULL) { |
| 278 | fprintf(stderr, "Could not access runpy._run_module_as_main\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame] | 279 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | Py_DECREF(runpy); |
| 281 | return -1; |
| 282 | } |
| 283 | module = PyUnicode_FromWideChar(modname, wcslen(modname)); |
| 284 | if (module == NULL) { |
| 285 | fprintf(stderr, "Could not convert module name to unicode\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame] | 286 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | Py_DECREF(runpy); |
| 288 | Py_DECREF(runmodule); |
| 289 | return -1; |
| 290 | } |
| 291 | runargs = Py_BuildValue("(Oi)", module, set_argv0); |
| 292 | if (runargs == NULL) { |
| 293 | fprintf(stderr, |
| 294 | "Could not create arguments for runpy._run_module_as_main\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame] | 295 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | Py_DECREF(runpy); |
| 297 | Py_DECREF(runmodule); |
| 298 | Py_DECREF(module); |
| 299 | return -1; |
| 300 | } |
| 301 | result = PyObject_Call(runmodule, runargs, NULL); |
| 302 | if (result == NULL) { |
| 303 | PyErr_Print(); |
| 304 | } |
| 305 | Py_DECREF(runpy); |
| 306 | Py_DECREF(runmodule); |
| 307 | Py_DECREF(module); |
| 308 | Py_DECREF(runargs); |
| 309 | if (result == NULL) { |
| 310 | return -1; |
| 311 | } |
| 312 | Py_DECREF(result); |
| 313 | return 0; |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 314 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 315 | |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 316 | static PyObject * |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 317 | pymain_get_importer(const wchar_t *filename) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 318 | { |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 319 | PyObject *sys_path0 = NULL, *importer; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 320 | |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 321 | sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename)); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 322 | if (sys_path0 == NULL) { |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 323 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 324 | } |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 325 | |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 326 | importer = PyImport_GetImporter(sys_path0); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 327 | if (importer == NULL) { |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 328 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 329 | } |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 330 | |
Brett Cannon | aa93642 | 2012-04-27 15:30:58 -0400 | [diff] [blame] | 331 | if (importer == Py_None) { |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 332 | Py_DECREF(sys_path0); |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 333 | Py_DECREF(importer); |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 334 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 336 | |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 337 | Py_DECREF(importer); |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 338 | return sys_path0; |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 339 | |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 340 | error: |
| 341 | Py_XDECREF(sys_path0); |
| 342 | PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n"); |
| 343 | PyErr_Print(); |
| 344 | PyErr_Clear(); |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | |
| 349 | static int |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 350 | pymain_run_command(wchar_t *command, PyCompilerFlags *cf) |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 351 | { |
| 352 | PyObject *unicode, *bytes; |
| 353 | int ret; |
| 354 | |
| 355 | unicode = PyUnicode_FromWideChar(command, -1); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 356 | if (unicode == NULL) { |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 357 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 360 | bytes = PyUnicode_AsUTF8String(unicode); |
| 361 | Py_DECREF(unicode); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 362 | if (bytes == NULL) { |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 363 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 366 | ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf); |
| 367 | Py_DECREF(bytes); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 368 | return (ret != 0); |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 369 | |
| 370 | error: |
Victor Stinner | 398356b | 2010-08-18 22:23:22 +0000 | [diff] [blame] | 371 | PySys_WriteStderr("Unable to decode the command from the command line:\n"); |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 372 | PyErr_Print(); |
| 373 | return 1; |
| 374 | } |
| 375 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 376 | |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 377 | static int |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 378 | pymain_run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf) |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 379 | { |
| 380 | PyObject *unicode, *bytes = NULL; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 381 | const char *filename_str; |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 382 | int run; |
| 383 | |
| 384 | /* call pending calls like signal handlers (SIGINT) */ |
| 385 | if (Py_MakePendingCalls() == -1) { |
| 386 | PyErr_Print(); |
| 387 | return 1; |
| 388 | } |
| 389 | |
| 390 | if (filename) { |
| 391 | unicode = PyUnicode_FromWideChar(filename, wcslen(filename)); |
| 392 | if (unicode != NULL) { |
Victor Stinner | e0f3268 | 2010-10-17 19:34:51 +0000 | [diff] [blame] | 393 | bytes = PyUnicode_EncodeFSDefault(unicode); |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 394 | Py_DECREF(unicode); |
| 395 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 396 | if (bytes != NULL) { |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 397 | filename_str = PyBytes_AsString(bytes); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 398 | } |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 399 | else { |
| 400 | PyErr_Clear(); |
Victor Stinner | e0f3268 | 2010-10-17 19:34:51 +0000 | [diff] [blame] | 401 | filename_str = "<encoding error>"; |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 404 | else { |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 405 | filename_str = "<stdin>"; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 406 | } |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 407 | |
| 408 | run = PyRun_AnyFileExFlags(fp, filename_str, filename != NULL, p_cf); |
| 409 | Py_XDECREF(bytes); |
| 410 | return run != 0; |
| 411 | } |
| 412 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 414 | /* Main program */ |
| 415 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 416 | typedef struct { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 417 | wchar_t **argv; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 418 | int nwarnoption; /* Number of -W options */ |
| 419 | wchar_t **warnoptions; /* -W options */ |
| 420 | int nenv_warnoption; /* Number of PYTHONWARNINGS options */ |
| 421 | wchar_t **env_warnoptions; /* PYTHONWARNINGS options */ |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 422 | int print_help; /* -h, -? options */ |
| 423 | int print_version; /* -V option */ |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 424 | int bytes_warning; /* Py_BytesWarningFlag, -b */ |
| 425 | int debug; /* Py_DebugFlag, -b, PYTHONDEBUG */ |
| 426 | int inspect; /* Py_InspectFlag, -i, PYTHONINSPECT */ |
| 427 | int interactive; /* Py_InteractiveFlag, -i */ |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 428 | int optimization_level; /* Py_OptimizeFlag, -O, PYTHONOPTIMIZE */ |
| 429 | int dont_write_bytecode; /* Py_DontWriteBytecodeFlag, -B, PYTHONDONTWRITEBYTECODE */ |
| 430 | int no_user_site_directory; /* Py_NoUserSiteDirectory, -I, -s, PYTHONNOUSERSITE */ |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 431 | int use_unbuffered_io; /* Py_UnbufferedStdioFlag, -u, PYTHONUNBUFFERED */ |
| 432 | int verbosity; /* Py_VerboseFlag, -v, PYTHONVERBOSE */ |
| 433 | int quiet_flag; /* Py_QuietFlag, -q */ |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 434 | const char *check_hash_pycs_mode; /* --check-hash-based-pycs */ |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 435 | #ifdef MS_WINDOWS |
| 436 | int legacy_windows_fs_encoding; /* Py_LegacyWindowsFSEncodingFlag, |
| 437 | PYTHONLEGACYWINDOWSFSENCODING */ |
| 438 | int legacy_windows_stdio; /* Py_LegacyWindowsStdioFlag, |
| 439 | PYTHONLEGACYWINDOWSSTDIO */ |
| 440 | #endif |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 441 | } _Py_CommandLineDetails; |
| 442 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 443 | /* Structure used by Py_Main() to pass data to subfunctions */ |
| 444 | typedef struct { |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 445 | /* Input arguments */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 446 | int argc; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 447 | int use_bytes_argv; |
| 448 | char **bytes_argv; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 449 | wchar_t **wchar_argv; |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 450 | |
| 451 | /* Exit status or "exit code": result of pymain_main() */ |
| 452 | int status; |
| 453 | /* Error message if a function failed */ |
| 454 | _PyInitError err; |
| 455 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 456 | /* non-zero is stdin is a TTY or if -i option is used */ |
| 457 | int stdin_is_interactive; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 458 | int skip_first_line; /* -x option */ |
| 459 | wchar_t *filename; /* Trailing arg without -c or -m */ |
| 460 | wchar_t *command; /* -c argument */ |
| 461 | wchar_t *module; /* -m argument */ |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 462 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 463 | _PyCoreConfig config; |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 464 | |
| 465 | PyObject *main_importer_path; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 466 | } _PyMain; |
| 467 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 468 | #define _PyMain_INIT \ |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 469 | {.config = _PyCoreConfig_INIT, \ |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 470 | .err = _Py_INIT_OK()} |
| 471 | /* Note: _PyMain_INIT sets other fields to 0/NULL */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 472 | |
| 473 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 474 | /* Non-zero if filename, command (-c) or module (-m) is set |
| 475 | on the command line */ |
| 476 | #define RUN_CODE(pymain) \ |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 477 | (pymain->command != NULL || pymain->filename != NULL \ |
| 478 | || pymain->module != NULL) |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 479 | |
| 480 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 481 | static void |
| 482 | clear_wstrlist(int len, wchar_t **list) |
| 483 | { |
| 484 | for (int i=0; i < len; i++) { |
| 485 | PyMem_RawFree(list[i]); |
| 486 | } |
| 487 | PyMem_RawFree(list); |
| 488 | } |
| 489 | |
| 490 | |
| 491 | static wchar_t** |
| 492 | copy_wstrlist(int len, wchar_t **list) |
| 493 | { |
| 494 | assert((len > 0 && list != NULL) || len == 0); |
| 495 | size_t size = len * sizeof(list[0]); |
| 496 | wchar_t **list_copy = PyMem_RawMalloc(size); |
| 497 | for (int i=0; i < len; i++) { |
| 498 | wchar_t* arg = _PyMem_RawWcsdup(list[i]); |
| 499 | if (arg == NULL) { |
| 500 | clear_wstrlist(i, list); |
| 501 | return NULL; |
| 502 | } |
| 503 | list_copy[i] = arg; |
| 504 | } |
| 505 | return list_copy; |
| 506 | } |
| 507 | |
| 508 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 509 | static wchar_t* |
| 510 | pymain_wstrdup(_PyMain *pymain, const wchar_t *str) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 511 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 512 | wchar_t *str2 = _PyMem_RawWcsdup(str); |
| 513 | if (str2 == NULL) { |
| 514 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 515 | return NULL; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 516 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 517 | return str2; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Victor Stinner | 92a3c6f | 2017-12-06 18:12:59 +0100 | [diff] [blame] | 520 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 521 | static int |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 522 | pymain_init_cmdline_argv(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 523 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 524 | assert(cmdline->argv == NULL); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 525 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 526 | if (pymain->use_bytes_argv) { |
| 527 | /* +1 for a the NULL terminator */ |
| 528 | size_t size = sizeof(wchar_t*) * (pymain->argc + 1); |
| 529 | wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size); |
| 530 | if (argv == NULL) { |
| 531 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 532 | return -1; |
| 533 | } |
| 534 | |
| 535 | for (int i = 0; i < pymain->argc; i++) { |
| 536 | size_t len; |
| 537 | wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len); |
| 538 | if (arg == NULL) { |
| 539 | clear_wstrlist(i, argv); |
| 540 | pymain->err = DECODE_LOCALE_ERR("command line arguments", |
| 541 | (Py_ssize_t)len); |
| 542 | return -1; |
| 543 | } |
| 544 | argv[i] = arg; |
| 545 | } |
| 546 | argv[pymain->argc] = NULL; |
| 547 | |
| 548 | cmdline->argv = argv; |
| 549 | } |
| 550 | else { |
| 551 | cmdline->argv = pymain->wchar_argv; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 554 | wchar_t *program; |
| 555 | if (pymain->argc >= 1 && cmdline->argv != NULL) { |
| 556 | program = cmdline->argv[0]; |
| 557 | } |
| 558 | else { |
| 559 | program = L""; |
| 560 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 561 | pymain->config.program = pymain_wstrdup(pymain, program); |
| 562 | if (pymain->config.program == NULL) { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 563 | return -1; |
| 564 | } |
| 565 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | |
| 570 | static void |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 571 | _PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config) |
| 572 | { |
| 573 | if (config->ignore_environment == -1) { |
| 574 | config->ignore_environment = Py_IgnoreEnvironmentFlag; |
| 575 | } |
| 576 | if (config->utf8_mode == -1) { |
| 577 | config->utf8_mode = Py_UTF8Mode; |
| 578 | } |
| 579 | if (config->isolated == -1) { |
| 580 | config->isolated = Py_IsolatedFlag; |
| 581 | } |
| 582 | if (config->site_import == -1) { |
| 583 | config->site_import = !Py_NoSiteFlag; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | |
| 588 | /* Get Py_xxx global configuration variables */ |
| 589 | static void |
| 590 | pymain_get_global_config(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
| 591 | { |
| 592 | _PyCoreConfig_GetGlobalConfig(&pymain->config); |
| 593 | |
| 594 | cmdline->bytes_warning = Py_BytesWarningFlag; |
| 595 | cmdline->debug = Py_DebugFlag; |
| 596 | cmdline->inspect = Py_InspectFlag; |
| 597 | cmdline->interactive = Py_InteractiveFlag; |
| 598 | cmdline->optimization_level = Py_OptimizeFlag; |
| 599 | cmdline->dont_write_bytecode = Py_DontWriteBytecodeFlag; |
| 600 | cmdline->no_user_site_directory = Py_NoUserSiteDirectory; |
| 601 | cmdline->use_unbuffered_io = Py_UnbufferedStdioFlag; |
| 602 | cmdline->verbosity = Py_VerboseFlag; |
| 603 | cmdline->quiet_flag = Py_QuietFlag; |
| 604 | #ifdef MS_WINDOWS |
| 605 | cmdline->legacy_windows_fs_encoding = Py_LegacyWindowsFSEncodingFlag; |
| 606 | cmdline->legacy_windows_stdio = Py_LegacyWindowsStdioFlag; |
| 607 | #endif |
| 608 | cmdline->check_hash_pycs_mode = _Py_CheckHashBasedPycsMode ; |
| 609 | } |
| 610 | |
| 611 | |
| 612 | /* Set Py_xxx global configuration variables from 'config' configuration. */ |
| 613 | void |
| 614 | _PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config) |
| 615 | { |
| 616 | if (config->ignore_environment != -1) { |
| 617 | Py_IgnoreEnvironmentFlag = config->ignore_environment; |
| 618 | } |
| 619 | if (config->utf8_mode != -1) { |
| 620 | Py_UTF8Mode = config->utf8_mode; |
| 621 | } |
| 622 | if (config->isolated != -1) { |
| 623 | Py_IsolatedFlag = config->isolated; |
| 624 | } |
| 625 | if (config->site_import != -1) { |
| 626 | Py_NoSiteFlag = !config->site_import; |
| 627 | } |
| 628 | |
| 629 | /* Random or non-zero hash seed */ |
| 630 | Py_HashRandomizationFlag = (config->use_hash_seed == 0 || |
| 631 | config->hash_seed != 0); |
| 632 | } |
| 633 | |
| 634 | |
| 635 | /* Set Py_xxx global configuration variables */ |
| 636 | static void |
| 637 | pymain_set_global_config(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
| 638 | { |
| 639 | _PyCoreConfig_SetGlobalConfig(&pymain->config); |
| 640 | |
| 641 | Py_BytesWarningFlag = cmdline->bytes_warning; |
| 642 | Py_DebugFlag = cmdline->debug; |
| 643 | Py_InspectFlag = cmdline->inspect; |
| 644 | Py_InteractiveFlag = cmdline->interactive; |
| 645 | Py_OptimizeFlag = cmdline->optimization_level; |
| 646 | Py_DontWriteBytecodeFlag = cmdline->dont_write_bytecode; |
| 647 | Py_NoUserSiteDirectory = cmdline->no_user_site_directory; |
| 648 | Py_UnbufferedStdioFlag = cmdline->use_unbuffered_io; |
| 649 | Py_VerboseFlag = cmdline->verbosity; |
| 650 | Py_QuietFlag = cmdline->quiet_flag; |
| 651 | _Py_CheckHashBasedPycsMode = cmdline->check_hash_pycs_mode; |
| 652 | #ifdef MS_WINDOWS |
| 653 | Py_LegacyWindowsFSEncodingFlag = cmdline->legacy_windows_fs_encoding; |
| 654 | Py_LegacyWindowsStdioFlag = cmdline->legacy_windows_stdio; |
| 655 | #endif |
| 656 | } |
| 657 | |
| 658 | |
| 659 | /* Free memory allocated in config, but don't clear all attributes */ |
| 660 | void |
| 661 | _PyCoreConfig_Clear(_PyCoreConfig *config) |
| 662 | { |
| 663 | #define CLEAR(ATTR) \ |
| 664 | do { \ |
| 665 | PyMem_RawFree(ATTR); \ |
| 666 | ATTR = NULL; \ |
| 667 | } while (0) |
| 668 | #define CLEAR_WSTRLIST(LEN, LIST) \ |
| 669 | do { \ |
| 670 | clear_wstrlist(LEN, LIST); \ |
| 671 | LEN = 0; \ |
| 672 | LIST = NULL; \ |
| 673 | } while (0) |
| 674 | |
| 675 | CLEAR(config->pycache_prefix); |
| 676 | CLEAR(config->module_search_path_env); |
| 677 | CLEAR(config->home); |
| 678 | CLEAR(config->program_name); |
| 679 | CLEAR(config->program); |
| 680 | |
| 681 | CLEAR_WSTRLIST(config->argc, config->argv); |
| 682 | config->argc = -1; |
| 683 | |
| 684 | CLEAR_WSTRLIST(config->nwarnoption, config->warnoptions); |
| 685 | CLEAR_WSTRLIST(config->nxoption, config->xoptions); |
| 686 | CLEAR_WSTRLIST(config->nmodule_search_path, config->module_search_paths); |
| 687 | config->nmodule_search_path = -1; |
| 688 | |
| 689 | CLEAR(config->executable); |
| 690 | CLEAR(config->prefix); |
| 691 | CLEAR(config->base_prefix); |
| 692 | CLEAR(config->exec_prefix); |
| 693 | #ifdef MS_WINDOWS |
| 694 | CLEAR(config->dll_path); |
| 695 | #endif |
| 696 | CLEAR(config->base_exec_prefix); |
| 697 | #undef CLEAR |
| 698 | #undef CLEAR_WSTRLIST |
| 699 | } |
| 700 | |
| 701 | |
| 702 | int |
| 703 | _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2) |
| 704 | { |
| 705 | _PyCoreConfig_Clear(config); |
| 706 | |
| 707 | #define COPY_ATTR(ATTR) config->ATTR = config2->ATTR |
| 708 | #define COPY_STR_ATTR(ATTR) \ |
| 709 | do { \ |
| 710 | if (config2->ATTR != NULL) { \ |
| 711 | config->ATTR = _PyMem_RawWcsdup(config2->ATTR); \ |
| 712 | if (config->ATTR == NULL) { \ |
| 713 | return -1; \ |
| 714 | } \ |
| 715 | } \ |
| 716 | } while (0) |
| 717 | #define COPY_WSTRLIST(LEN, LIST) \ |
| 718 | do { \ |
| 719 | if (config2->LIST != NULL) { \ |
| 720 | config->LIST = copy_wstrlist(config2->LEN, config2->LIST); \ |
| 721 | if (config->LIST == NULL) { \ |
| 722 | return -1; \ |
| 723 | } \ |
| 724 | } \ |
| 725 | config->LEN = config2->LEN; \ |
| 726 | } while (0) |
| 727 | |
| 728 | COPY_ATTR(ignore_environment); |
| 729 | COPY_ATTR(use_hash_seed); |
| 730 | COPY_ATTR(hash_seed); |
| 731 | COPY_ATTR(_install_importlib); |
| 732 | COPY_ATTR(allocator); |
| 733 | COPY_ATTR(dev_mode); |
| 734 | COPY_ATTR(faulthandler); |
| 735 | COPY_ATTR(tracemalloc); |
| 736 | COPY_ATTR(import_time); |
| 737 | COPY_ATTR(show_ref_count); |
| 738 | COPY_ATTR(show_alloc_count); |
| 739 | COPY_ATTR(dump_refs); |
| 740 | COPY_ATTR(malloc_stats); |
| 741 | COPY_ATTR(utf8_mode); |
| 742 | |
| 743 | COPY_STR_ATTR(pycache_prefix); |
| 744 | COPY_STR_ATTR(module_search_path_env); |
| 745 | COPY_STR_ATTR(home); |
| 746 | COPY_STR_ATTR(program_name); |
| 747 | COPY_STR_ATTR(program); |
| 748 | |
| 749 | COPY_WSTRLIST(argc, argv); |
| 750 | COPY_WSTRLIST(nwarnoption, warnoptions); |
| 751 | COPY_WSTRLIST(nxoption, xoptions); |
| 752 | COPY_WSTRLIST(nmodule_search_path, module_search_paths); |
| 753 | |
| 754 | COPY_STR_ATTR(executable); |
| 755 | COPY_STR_ATTR(prefix); |
| 756 | COPY_STR_ATTR(base_prefix); |
| 757 | COPY_STR_ATTR(exec_prefix); |
| 758 | #ifdef MS_WINDOWS |
| 759 | COPY_STR_ATTR(dll_path); |
| 760 | #endif |
| 761 | COPY_STR_ATTR(base_exec_prefix); |
| 762 | |
| 763 | COPY_ATTR(isolated); |
| 764 | COPY_ATTR(site_import); |
| 765 | |
| 766 | #undef COPY_ATTR |
| 767 | #undef COPY_STR_ATTR |
| 768 | #undef COPY_WSTRLIST |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | |
| 773 | static void |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 774 | pymain_clear_cmdline(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 775 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 776 | PyMemAllocatorEx old_alloc; |
| 777 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 778 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 779 | clear_wstrlist(cmdline->nwarnoption, cmdline->warnoptions); |
| 780 | cmdline->nwarnoption = 0; |
| 781 | cmdline->warnoptions = NULL; |
| 782 | |
| 783 | clear_wstrlist(cmdline->nenv_warnoption, cmdline->env_warnoptions); |
| 784 | cmdline->nenv_warnoption = 0; |
| 785 | cmdline->env_warnoptions = NULL; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 786 | |
| 787 | if (pymain->use_bytes_argv && cmdline->argv != NULL) { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 788 | clear_wstrlist(pymain->argc, cmdline->argv); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 789 | } |
| 790 | cmdline->argv = NULL; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 791 | |
| 792 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 793 | } |
| 794 | |
| 795 | |
| 796 | static void |
| 797 | pymain_clear_pymain(_PyMain *pymain) |
| 798 | { |
| 799 | #define CLEAR(ATTR) \ |
| 800 | do { \ |
| 801 | PyMem_RawFree(ATTR); \ |
| 802 | ATTR = NULL; \ |
| 803 | } while (0) |
| 804 | |
| 805 | CLEAR(pymain->filename); |
| 806 | CLEAR(pymain->command); |
| 807 | CLEAR(pymain->module); |
| 808 | #undef CLEAR |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 811 | static void |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 812 | pymain_clear_config(_PyMain *pymain) |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 813 | { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 814 | /* Clear core config with the memory allocator |
| 815 | used by pymain_read_conf() */ |
| 816 | PyMemAllocatorEx old_alloc; |
| 817 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 818 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 819 | _PyCoreConfig_Clear(&pymain->config); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 820 | |
| 821 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 822 | } |
| 823 | |
| 824 | |
| 825 | static void |
| 826 | pymain_free_python(_PyMain *pymain) |
| 827 | { |
| 828 | Py_CLEAR(pymain->main_importer_path); |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 829 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 830 | #ifdef __INSURE__ |
| 831 | /* Insure++ is a memory analysis tool that aids in discovering |
| 832 | * memory leaks and other memory problems. On Python exit, the |
| 833 | * interned string dictionaries are flagged as being in use at exit |
| 834 | * (which it is). Under normal circumstances, this is fine because |
| 835 | * the memory will be automatically reclaimed by the system. Under |
| 836 | * memory debugging, it's a huge source of useless noise, so we |
| 837 | * trade off slower shutdown for less distraction in the memory |
| 838 | * reports. -baw |
| 839 | */ |
| 840 | _Py_ReleaseInternedUnicodeStrings(); |
| 841 | #endif /* __INSURE__ */ |
| 842 | } |
| 843 | |
Victor Stinner | 92a3c6f | 2017-12-06 18:12:59 +0100 | [diff] [blame] | 844 | |
| 845 | static void |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 846 | pymain_free_raw(_PyMain *pymain) |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 847 | { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 848 | _PyImport_Fini2(); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 849 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 850 | /* Free global variables which cannot be freed in Py_Finalize(): |
| 851 | configuration options set before Py_Initialize() which should |
| 852 | remain valid after Py_Finalize(), since |
| 853 | Py_Initialize()-Py_Finalize() can be called multiple times. */ |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 854 | _PyPathConfig_ClearGlobal(); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 855 | |
Victor Stinner | 31e9908 | 2017-12-20 23:41:38 +0100 | [diff] [blame] | 856 | pymain_clear_config(pymain); |
| 857 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 858 | /* Force the allocator used by pymain_read_conf() */ |
| 859 | PyMemAllocatorEx old_alloc; |
| 860 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 861 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 862 | pymain_clear_pymain(pymain); |
| 863 | |
| 864 | clear_wstrlist(orig_argc, orig_argv); |
| 865 | orig_argc = 0; |
| 866 | orig_argv = NULL; |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 867 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 868 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | 92a3c6f | 2017-12-06 18:12:59 +0100 | [diff] [blame] | 869 | } |
| 870 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 871 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 872 | static void |
| 873 | pymain_free(_PyMain *pymain) |
| 874 | { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 875 | pymain_free_python(pymain); |
| 876 | pymain_free_raw(pymain); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 879 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 880 | static int |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 881 | pymain_run_main_from_importer(_PyMain *pymain) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 882 | { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 883 | /* Assume sys_path0 has already been checked by pymain_get_importer(), |
| 884 | * so put it in sys.path[0] and import __main__ */ |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 885 | PyObject *sys_path = PySys_GetObject("path"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 886 | if (sys_path == NULL) { |
| 887 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path"); |
| 888 | goto error; |
| 889 | } |
| 890 | |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 891 | if (PyList_Insert(sys_path, 0, pymain->main_importer_path)) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 892 | goto error; |
| 893 | } |
| 894 | |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 895 | int sts = pymain_run_module(L"__main__", 0); |
| 896 | return (sts != 0); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 897 | |
| 898 | error: |
| 899 | Py_CLEAR(pymain->main_importer_path); |
| 900 | PyErr_Print(); |
| 901 | return 1; |
| 902 | } |
| 903 | |
| 904 | |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 905 | _PyInitError |
| 906 | _Py_wstrlist_append(int *len, wchar_t ***list, const wchar_t *str) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 907 | { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 908 | if (*len == INT_MAX) { |
| 909 | /* len+1 would overflow */ |
| 910 | return _Py_INIT_NO_MEMORY(); |
| 911 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 912 | wchar_t *str2 = _PyMem_RawWcsdup(str); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 913 | if (str2 == NULL) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 914 | return _Py_INIT_NO_MEMORY(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 915 | } |
| 916 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 917 | size_t size = (*len + 1) * sizeof(list[0]); |
| 918 | wchar_t **list2 = (wchar_t **)PyMem_RawRealloc(*list, size); |
| 919 | if (list2 == NULL) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 920 | PyMem_RawFree(str2); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 921 | return _Py_INIT_NO_MEMORY(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 922 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 923 | list2[*len] = str2; |
| 924 | *list = list2; |
| 925 | (*len)++; |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 926 | return _Py_INIT_OK(); |
| 927 | } |
| 928 | |
| 929 | |
| 930 | static int |
| 931 | pymain_wstrlist_append(_PyMain *pymain, int *len, wchar_t ***list, const wchar_t *str) |
| 932 | { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 933 | _PyInitError err = _Py_wstrlist_append(len, list, str); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 934 | if (_Py_INIT_FAILED(err)) { |
| 935 | pymain->err = err; |
| 936 | return -1; |
| 937 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | |
| 942 | /* Parse the command line arguments |
| 943 | Return 0 on success. |
| 944 | Return 1 if parsing failed. |
| 945 | Set pymain->err and return -1 on other errors. */ |
| 946 | static int |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 947 | pymain_parse_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 948 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 949 | _PyCoreConfig *config = &pymain->config; |
| 950 | |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 951 | _PyOS_ResetGetOpt(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 952 | do { |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 953 | int longindex = -1; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 954 | int c = _PyOS_GetOpt(pymain->argc, cmdline->argv, PROGRAM_OPTS, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 955 | longoptions, &longindex); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 956 | if (c == EOF) { |
| 957 | break; |
| 958 | } |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | if (c == 'c') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | /* -c is the last option; following arguments |
| 962 | that look like options are left for the |
| 963 | command to interpret. */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 964 | size_t len = wcslen(_PyOS_optarg) + 1 + 1; |
| 965 | wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len); |
| 966 | if (command == NULL) { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 967 | pymain->err = _Py_INIT_NO_MEMORY(); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 968 | return -1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 969 | } |
Victor Stinner | 58d1683 | 2018-05-31 15:09:28 +0200 | [diff] [blame] | 970 | memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | command[len - 2] = '\n'; |
| 972 | command[len - 1] = 0; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 973 | pymain->command = command; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | break; |
| 975 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | if (c == 'm') { |
| 978 | /* -m is the last option; following arguments |
| 979 | that look like options are left for the |
| 980 | module to interpret. */ |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 981 | pymain->module = pymain_wstrdup(pymain, _PyOS_optarg); |
| 982 | if (pymain->module == NULL) { |
| 983 | return -1; |
| 984 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | break; |
| 986 | } |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | switch (c) { |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 989 | case 0: |
| 990 | // Handle long option. |
| 991 | assert(longindex == 0); // Only one long option now. |
| 992 | if (!wcscmp(_PyOS_optarg, L"always")) { |
| 993 | cmdline->check_hash_pycs_mode = "always"; |
| 994 | } else if (!wcscmp(_PyOS_optarg, L"never")) { |
| 995 | cmdline->check_hash_pycs_mode = "never"; |
| 996 | } else if (!wcscmp(_PyOS_optarg, L"default")) { |
| 997 | cmdline->check_hash_pycs_mode = "default"; |
| 998 | } else { |
| 999 | fprintf(stderr, "--check-hash-based-pycs must be one of " |
| 1000 | "'default', 'always', or 'never'\n"); |
| 1001 | return 1; |
| 1002 | } |
| 1003 | break; |
| 1004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | case 'b': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1006 | cmdline->bytes_warning++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1007 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | case 'd': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1010 | cmdline->debug++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | case 'i': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1014 | cmdline->inspect++; |
| 1015 | cmdline->interactive++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1017 | |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 1018 | case 'I': |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1019 | config->ignore_environment++; |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1020 | config->isolated++; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1021 | cmdline->no_user_site_directory++; |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 1022 | break; |
| 1023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | /* case 'J': reserved for Jython */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1026 | case 'O': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1027 | cmdline->optimization_level++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | break; |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 1029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | case 'B': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1031 | cmdline->dont_write_bytecode++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | break; |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | case 's': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1035 | cmdline->no_user_site_directory++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | break; |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 1037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | case 'S': |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1039 | config->site_import = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | break; |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | case 'E': |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1043 | config->ignore_environment++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | break; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | case 't': |
| 1047 | /* ignored for backwards compatibility */ |
| 1048 | break; |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 1049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | case 'u': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1051 | cmdline->use_unbuffered_io = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | case 'v': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1055 | cmdline->verbosity++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | case 'x': |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1059 | pymain->skip_first_line = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | break; |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | case 'h': |
| 1063 | case '?': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1064 | cmdline->print_help++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | break; |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | case 'V': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1068 | cmdline->print_version++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | break; |
Guido van Rossum | c15a9a1 | 2000-05-01 17:54:33 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | case 'W': |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1072 | if (pymain_wstrlist_append(pymain, |
| 1073 | &cmdline->nwarnoption, |
| 1074 | &cmdline->warnoptions, |
| 1075 | _PyOS_optarg) < 0) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1076 | return -1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1077 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | break; |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 1079 | |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1080 | case 'X': |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1081 | if (pymain_wstrlist_append(pymain, |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1082 | &config->nxoption, |
| 1083 | &config->xoptions, |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1084 | _PyOS_optarg) < 0) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1085 | return -1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1086 | } |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1087 | break; |
| 1088 | |
Georg Brandl | 9d87119 | 2010-12-04 10:47:18 +0000 | [diff] [blame] | 1089 | case 'q': |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1090 | cmdline->quiet_flag++; |
Georg Brandl | 9d87119 | 2010-12-04 10:47:18 +0000 | [diff] [blame] | 1091 | break; |
| 1092 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 1093 | case 'R': |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1094 | config->use_hash_seed = 0; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 1095 | break; |
| 1096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | /* This space reserved for other options */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | default: |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1100 | /* unknown argument: parsing failed */ |
| 1101 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1102 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1103 | } while (1); |
| 1104 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1105 | if (pymain->command == NULL && pymain->module == NULL |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1106 | && _PyOS_optind < pymain->argc |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1107 | && wcscmp(cmdline->argv[_PyOS_optind], L"-") != 0) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1108 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1109 | pymain->filename = pymain_wstrdup(pymain, cmdline->argv[_PyOS_optind]); |
| 1110 | if (pymain->filename == NULL) { |
| 1111 | return -1; |
| 1112 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1114 | |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 1115 | /* -c and -m options are exclusive */ |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1116 | assert(!(pymain->command != NULL && pymain->module != NULL)); |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 1117 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 1120 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1121 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1122 | static int |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1123 | add_xoption(PyObject *opts, const wchar_t *s) |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 1124 | { |
| 1125 | PyObject *name, *value; |
| 1126 | |
| 1127 | const wchar_t *name_end = wcschr(s, L'='); |
| 1128 | if (!name_end) { |
| 1129 | name = PyUnicode_FromWideChar(s, -1); |
| 1130 | value = Py_True; |
| 1131 | Py_INCREF(value); |
| 1132 | } |
| 1133 | else { |
| 1134 | name = PyUnicode_FromWideChar(s, name_end - s); |
| 1135 | value = PyUnicode_FromWideChar(name_end + 1, -1); |
| 1136 | } |
| 1137 | if (name == NULL || value == NULL) { |
| 1138 | goto error; |
| 1139 | } |
| 1140 | if (PyDict_SetItem(opts, name, value) < 0) { |
| 1141 | goto error; |
| 1142 | } |
| 1143 | Py_DECREF(name); |
| 1144 | Py_DECREF(value); |
| 1145 | return 0; |
| 1146 | |
| 1147 | error: |
| 1148 | Py_XDECREF(name); |
| 1149 | Py_XDECREF(value); |
| 1150 | return -1; |
| 1151 | } |
| 1152 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1153 | |
| 1154 | static PyObject* |
| 1155 | config_create_xoptions_dict(const _PyCoreConfig *config) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1156 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1157 | int nxoption = config->nxoption; |
| 1158 | wchar_t **xoptions = config->xoptions; |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 1159 | PyObject *dict = PyDict_New(); |
| 1160 | if (dict == NULL) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1161 | return NULL; |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1164 | for (int i=0; i < nxoption; i++) { |
| 1165 | wchar_t *option = xoptions[i]; |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1166 | if (add_xoption(dict, option) < 0) { |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 1167 | Py_DECREF(dict); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1168 | return NULL; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1169 | } |
| 1170 | } |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 1171 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1172 | return dict; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1175 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1176 | static _PyInitError |
| 1177 | config_add_warnings_optlist(_PyCoreConfig *config, int len, wchar_t **options) |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1178 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1179 | for (int i = 0; i < len; i++) { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 1180 | _PyInitError err = _Py_wstrlist_append(&config->nwarnoption, |
| 1181 | &config->warnoptions, |
| 1182 | options[i]); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1183 | if (_Py_INIT_FAILED(err)) { |
| 1184 | return err; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1185 | } |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1186 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1187 | return _Py_INIT_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1188 | } |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1189 | |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1190 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1191 | static _PyInitError |
| 1192 | config_init_warnoptions(_PyCoreConfig *config, _Py_CommandLineDetails *cmdline) |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1193 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1194 | _PyInitError err; |
| 1195 | |
| 1196 | assert(config->nwarnoption == 0); |
| 1197 | |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1198 | /* The priority order for warnings configuration is (highest precedence |
| 1199 | * first): |
| 1200 | * |
| 1201 | * - the BytesWarning filter, if needed ('-b', '-bb') |
| 1202 | * - any '-W' command line options; then |
| 1203 | * - the 'PYTHONWARNINGS' environment variable; then |
| 1204 | * - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then |
| 1205 | * - any implicit filters added by _warnings.c/warnings.py |
| 1206 | * |
| 1207 | * All settings except the last are passed to the warnings module via |
| 1208 | * the `sys.warnoptions` list. Since the warnings module works on the basis |
| 1209 | * of "the most recently added filter will be checked first", we add |
| 1210 | * the lowest precedence entries first so that later entries override them. |
| 1211 | */ |
| 1212 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1213 | if (config->dev_mode) { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 1214 | err = _Py_wstrlist_append(&config->nwarnoption, |
| 1215 | &config->warnoptions, |
| 1216 | L"default"); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1217 | if (_Py_INIT_FAILED(err)) { |
| 1218 | return err; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1219 | } |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1220 | } |
Victor Stinner | 374c6e1 | 2017-12-14 12:05:26 +0100 | [diff] [blame] | 1221 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1222 | err = config_add_warnings_optlist(config, |
| 1223 | cmdline->nenv_warnoption, |
| 1224 | cmdline->env_warnoptions); |
| 1225 | if (_Py_INIT_FAILED(err)) { |
| 1226 | return err; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1227 | } |
| 1228 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1229 | err = config_add_warnings_optlist(config, |
| 1230 | cmdline->nwarnoption, |
| 1231 | cmdline->warnoptions); |
| 1232 | if (_Py_INIT_FAILED(err)) { |
| 1233 | return err; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c |
| 1237 | * don't even try to emit a warning, so we skip setting the filter in that |
| 1238 | * case. |
| 1239 | */ |
| 1240 | if (cmdline->bytes_warning) { |
| 1241 | wchar_t *filter; |
| 1242 | if (cmdline->bytes_warning> 1) { |
| 1243 | filter = L"error::BytesWarning"; |
| 1244 | } |
| 1245 | else { |
| 1246 | filter = L"default::BytesWarning"; |
| 1247 | } |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 1248 | err = _Py_wstrlist_append(&config->nwarnoption, |
| 1249 | &config->warnoptions, |
| 1250 | filter); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1251 | if (_Py_INIT_FAILED(err)) { |
| 1252 | return err; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1253 | } |
| 1254 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1255 | return _Py_INIT_OK(); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1259 | /* Get warning options from PYTHONWARNINGS environment variable. |
| 1260 | Return 0 on success. |
| 1261 | Set pymain->err and return -1 on error. */ |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1262 | static _PyInitError |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1263 | cmdline_init_env_warnoptions(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1264 | { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1265 | if (pymain->config.ignore_environment) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1266 | return _Py_INIT_OK(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 1268 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1269 | wchar_t *env; |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1270 | int res = config_get_env_var_dup(&pymain->config, &env, |
| 1271 | L"PYTHONWARNINGS", "PYTHONWARNINGS"); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1272 | if (res < 0) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1273 | return DECODE_LOCALE_ERR("PYTHONWARNINGS", res); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1274 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1275 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1276 | if (env == NULL) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1277 | return _Py_INIT_OK(); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1278 | } |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 1279 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1280 | |
| 1281 | wchar_t *warning, *context = NULL; |
| 1282 | for (warning = WCSTOK(env, L",", &context); |
| 1283 | warning != NULL; |
| 1284 | warning = WCSTOK(NULL, L",", &context)) |
| 1285 | { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 1286 | _PyInitError err = _Py_wstrlist_append(&cmdline->nenv_warnoption, |
| 1287 | &cmdline->env_warnoptions, |
| 1288 | warning); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1289 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1290 | PyMem_RawFree(env); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1291 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1292 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1294 | PyMem_RawFree(env); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1295 | return _Py_INIT_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | |
| 1299 | static void |
| 1300 | pymain_init_stdio(_PyMain *pymain) |
| 1301 | { |
| 1302 | pymain->stdin_is_interactive = (isatty(fileno(stdin)) |
| 1303 | || Py_InteractiveFlag); |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 1304 | |
Sjoerd Mullender | 9cf424b | 2002-08-09 13:35:18 +0000 | [diff] [blame] | 1305 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
Victor Stinner | 89e3436 | 2011-01-07 18:47:22 +0000 | [diff] [blame] | 1306 | /* don't translate newlines (\r\n <=> \n) */ |
| 1307 | _setmode(fileno(stdin), O_BINARY); |
| 1308 | _setmode(fileno(stdout), O_BINARY); |
| 1309 | _setmode(fileno(stderr), O_BINARY); |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 1310 | #endif |
Victor Stinner | 89e3436 | 2011-01-07 18:47:22 +0000 | [diff] [blame] | 1311 | |
| 1312 | if (Py_UnbufferedStdioFlag) { |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 1313 | #ifdef HAVE_SETVBUF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
| 1315 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
| 1316 | setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 1317 | #else /* !HAVE_SETVBUF */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | setbuf(stdin, (char *)NULL); |
| 1319 | setbuf(stdout, (char *)NULL); |
| 1320 | setbuf(stderr, (char *)NULL); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 1321 | #endif /* !HAVE_SETVBUF */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | } |
| 1323 | else if (Py_InteractiveFlag) { |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 1324 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | /* Doesn't have to have line-buffered -- use unbuffered */ |
| 1326 | /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */ |
| 1327 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 1328 | #else /* !MS_WINDOWS */ |
| 1329 | #ifdef HAVE_SETVBUF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
| 1331 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 1332 | #endif /* HAVE_SETVBUF */ |
| 1333 | #endif /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1334 | /* Leave stderr alone - it should be unbuffered anyway. */ |
| 1335 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1336 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1337 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1338 | |
| 1339 | /* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__ |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1340 | environment variables on macOS if available. */ |
| 1341 | static _PyInitError |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1342 | config_init_program_name(_PyCoreConfig *config) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1343 | { |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1344 | assert(config->program_name == NULL); |
| 1345 | |
| 1346 | /* If Py_SetProgramName() was called, use its value */ |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 1347 | const wchar_t *program_name = _Py_path_config.program_name; |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1348 | if (program_name != NULL) { |
| 1349 | config->program_name = _PyMem_RawWcsdup(program_name); |
| 1350 | if (config->program_name == NULL) { |
| 1351 | return _Py_INIT_NO_MEMORY(); |
| 1352 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1353 | return _Py_INIT_OK(); |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
Just van Rossum | 2ac79ef | 2003-03-05 15:46:54 +0000 | [diff] [blame] | 1356 | #ifdef __APPLE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1357 | /* On MacOS X, when the Python interpreter is embedded in an |
| 1358 | application bundle, it gets executed by a bootstrapping script |
| 1359 | that does os.execve() with an argv[0] that's different from the |
| 1360 | actual Python executable. This is needed to keep the Finder happy, |
| 1361 | or rather, to work around Apple's overly strict requirements of |
| 1362 | the process name. However, we still need a usable sys.executable, |
| 1363 | so the actual executable path is passed in an environment variable. |
| 1364 | See Lib/plat-mac/bundlebuiler.py for details about the bootstrap |
| 1365 | script. */ |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1366 | const char *p = config_get_env_var(config, "PYTHONEXECUTABLE"); |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1367 | if (p != NULL) { |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1368 | size_t len; |
| 1369 | wchar_t* program_name = Py_DecodeLocale(p, &len); |
| 1370 | if (program_name == NULL) { |
| 1371 | return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment " |
| 1372 | "variable", (Py_ssize_t)len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | } |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1374 | config->program_name = program_name; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1375 | return _Py_INIT_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1376 | } |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 1377 | #ifdef WITH_NEXT_FRAMEWORK |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1378 | else { |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 1379 | const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__"); |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 1380 | if (pyvenv_launcher && *pyvenv_launcher) { |
| 1381 | /* Used by Mac/Tools/pythonw.c to forward |
| 1382 | * the argv0 of the stub executable |
| 1383 | */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1384 | size_t len; |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1385 | wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len); |
| 1386 | if (program_name == NULL) { |
| 1387 | return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment " |
| 1388 | "variable", (Py_ssize_t)len); |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 1389 | } |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1390 | config->program_name = program_name; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1391 | return _Py_INIT_OK(); |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 1392 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1394 | #endif /* WITH_NEXT_FRAMEWORK */ |
| 1395 | #endif /* __APPLE__ */ |
Victor Stinner | af5a895 | 2017-12-02 10:11:32 +0100 | [diff] [blame] | 1396 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1397 | /* Use argv[0] by default, if available */ |
| 1398 | if (config->program != NULL) { |
| 1399 | config->program_name = _PyMem_RawWcsdup(config->program); |
| 1400 | if (config->program_name == NULL) { |
| 1401 | return _Py_INIT_NO_MEMORY(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1402 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1403 | return _Py_INIT_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1404 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1405 | |
| 1406 | /* Last fall back: hardcoded string */ |
| 1407 | #ifdef MS_WINDOWS |
| 1408 | const wchar_t *default_program_name = L"python"; |
| 1409 | #else |
| 1410 | const wchar_t *default_program_name = L"python3"; |
| 1411 | #endif |
| 1412 | config->program_name = _PyMem_RawWcsdup(default_program_name); |
| 1413 | if (config->program_name == NULL) { |
| 1414 | return _Py_INIT_NO_MEMORY(); |
| 1415 | } |
| 1416 | return _Py_INIT_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1420 | static void |
| 1421 | pymain_header(_PyMain *pymain) |
| 1422 | { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1423 | if (Py_QuietFlag) { |
| 1424 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 1426 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1427 | if (!Py_VerboseFlag && (RUN_CODE(pymain) || !pymain->stdin_is_interactive)) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1428 | return; |
| 1429 | } |
| 1430 | |
| 1431 | fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform()); |
| 1432 | if (!Py_NoSiteFlag) { |
| 1433 | fprintf(stderr, "%s\n", COPYRIGHT); |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1438 | static int |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1439 | pymain_init_core_argv(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1440 | { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1441 | /* Copy argv to be able to modify it (to force -c/-m) */ |
| 1442 | int argc = pymain->argc - _PyOS_optind; |
| 1443 | wchar_t **argv; |
| 1444 | |
| 1445 | if (argc <= 0 || cmdline->argv == NULL) { |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1446 | /* Ensure at least one (empty) argument is seen */ |
| 1447 | static wchar_t *empty_argv[1] = {L""}; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1448 | argc = 1; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1449 | argv = copy_wstrlist(1, empty_argv); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1450 | } |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1451 | else { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1452 | argv = copy_wstrlist(argc, &cmdline->argv[_PyOS_optind]); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | if (argv == NULL) { |
| 1456 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 1457 | return -1; |
| 1458 | } |
| 1459 | |
| 1460 | wchar_t *arg0 = NULL; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1461 | if (pymain->command != NULL) { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1462 | /* Force sys.argv[0] = '-c' */ |
| 1463 | arg0 = L"-c"; |
| 1464 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1465 | else if (pymain->module != NULL) { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1466 | /* Force sys.argv[0] = '-m'*/ |
| 1467 | arg0 = L"-m"; |
| 1468 | } |
| 1469 | if (arg0 != NULL) { |
| 1470 | arg0 = _PyMem_RawWcsdup(arg0); |
| 1471 | if (arg0 == NULL) { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1472 | clear_wstrlist(argc, argv); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1473 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 1474 | return -1; |
| 1475 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1476 | |
| 1477 | assert(argc >= 1); |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1478 | PyMem_RawFree(argv[0]); |
| 1479 | argv[0] = arg0; |
| 1480 | } |
| 1481 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1482 | pymain->config.argc = argc; |
| 1483 | pymain->config.argv = argv; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1488 | static PyObject* |
| 1489 | wstrlist_as_pylist(int len, wchar_t **list) |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1490 | { |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1491 | assert(list != NULL || len < 1); |
| 1492 | |
| 1493 | PyObject *pylist = PyList_New(len); |
| 1494 | if (pylist == NULL) { |
| 1495 | return NULL; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1496 | } |
| 1497 | |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1498 | for (int i = 0; i < len; i++) { |
| 1499 | PyObject *v = PyUnicode_FromWideChar(list[i], -1); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1500 | if (v == NULL) { |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1501 | Py_DECREF(pylist); |
| 1502 | return NULL; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1503 | } |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1504 | PyList_SET_ITEM(pylist, i, v); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1505 | } |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 1506 | return pylist; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1507 | } |
| 1508 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1509 | |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1510 | static int |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1511 | pymain_compute_path0(_PyMain *pymain, PyObject **path0) |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 1512 | { |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1513 | if (pymain->main_importer_path != NULL) { |
| 1514 | /* Let pymain_run_main_from_importer() adjust sys.path[0] later */ |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1515 | *path0 = NULL; |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | if (Py_IsolatedFlag) { |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1520 | *path0 = NULL; |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1521 | return 0; |
| 1522 | } |
| 1523 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1524 | *path0 = _PyPathConfig_ComputeArgv0(pymain->config.argc, |
| 1525 | pymain->config.argv); |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1526 | if (*path0 == NULL) { |
| 1527 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 1528 | return -1; |
| 1529 | } |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | static int |
| 1535 | pymain_update_sys_path(_PyMain *pymain, PyObject *path0) |
| 1536 | { |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1537 | /* Prepend argv[0] to sys.path. |
| 1538 | If argv[0] is a symlink, use the real path. */ |
| 1539 | PyObject *sys_path = PySys_GetObject("path"); |
| 1540 | if (sys_path == NULL) { |
| 1541 | pymain->err = _Py_INIT_ERR("can't get sys.path"); |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 1542 | return -1; |
| 1543 | } |
| 1544 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1545 | /* Prepend path0 to sys.path */ |
| 1546 | if (PyList_Insert(sys_path, 0, path0) < 0) { |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1547 | pymain->err = _Py_INIT_ERR("sys.path.insert(0, path0) failed"); |
| 1548 | return -1; |
| 1549 | } |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 1550 | return 0; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1554 | static void |
| 1555 | pymain_import_readline(_PyMain *pymain) |
| 1556 | { |
| 1557 | if (Py_IsolatedFlag) { |
| 1558 | return; |
| 1559 | } |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1560 | if (!Py_InspectFlag && RUN_CODE(pymain)) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1561 | return; |
| 1562 | } |
| 1563 | if (!isatty(fileno(stdin))) { |
| 1564 | return; |
Nick Coghlan | d2977a3 | 2017-03-12 20:38:32 +1000 | [diff] [blame] | 1565 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1566 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1567 | PyObject *mod = PyImport_ImportModule("readline"); |
| 1568 | if (mod == NULL) { |
| 1569 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | } |
| 1571 | else { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1572 | Py_DECREF(mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | |
| 1577 | static FILE* |
| 1578 | pymain_open_filename(_PyMain *pymain) |
| 1579 | { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1580 | FILE* fp; |
| 1581 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1582 | fp = _Py_wfopen(pymain->filename, L"r"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1583 | if (fp == NULL) { |
| 1584 | char *cfilename_buffer; |
| 1585 | const char *cfilename; |
| 1586 | int err = errno; |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 1587 | cfilename_buffer = _Py_EncodeLocaleRaw(pymain->filename, NULL); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1588 | if (cfilename_buffer != NULL) |
| 1589 | cfilename = cfilename_buffer; |
| 1590 | else |
| 1591 | cfilename = "<unprintable file name>"; |
| 1592 | fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n", |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1593 | pymain->config.program, cfilename, err, strerror(err)); |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 1594 | PyMem_RawFree(cfilename_buffer); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1595 | pymain->status = 2; |
| 1596 | return NULL; |
| 1597 | } |
| 1598 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1599 | if (pymain->skip_first_line) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1600 | int ch; |
| 1601 | /* Push back first newline so line numbers |
| 1602 | remain the same */ |
| 1603 | while ((ch = getc(fp)) != EOF) { |
| 1604 | if (ch == '\n') { |
| 1605 | (void)ungetc(ch, fp); |
| 1606 | break; |
| 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | struct _Py_stat_struct sb; |
| 1612 | if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && |
| 1613 | S_ISDIR(sb.st_mode)) { |
| 1614 | fprintf(stderr, |
| 1615 | "%ls: '%ls' is a directory, cannot continue\n", |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1616 | pymain->config.program, pymain->filename); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1617 | fclose(fp); |
| 1618 | pymain->status = 1; |
| 1619 | return NULL; |
| 1620 | } |
| 1621 | |
| 1622 | return fp; |
| 1623 | } |
| 1624 | |
| 1625 | |
| 1626 | static void |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1627 | pymain_run_startup(_PyMain *pymain, PyCompilerFlags *cf) |
| 1628 | { |
| 1629 | const char *startup = config_get_env_var(&pymain->config, "PYTHONSTARTUP"); |
| 1630 | if (startup == NULL) { |
| 1631 | return; |
| 1632 | } |
| 1633 | |
| 1634 | FILE *fp = _Py_fopen(startup, "r"); |
| 1635 | if (fp == NULL) { |
| 1636 | int save_errno = errno; |
| 1637 | PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); |
| 1638 | errno = save_errno; |
| 1639 | |
| 1640 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
| 1641 | startup); |
| 1642 | PyErr_Print(); |
| 1643 | PyErr_Clear(); |
| 1644 | return; |
| 1645 | } |
| 1646 | |
| 1647 | (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); |
| 1648 | PyErr_Clear(); |
| 1649 | fclose(fp); |
| 1650 | } |
| 1651 | |
| 1652 | |
| 1653 | static void |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1654 | pymain_run_filename(_PyMain *pymain, PyCompilerFlags *cf) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1655 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1656 | if (pymain->filename == NULL && pymain->stdin_is_interactive) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1657 | Py_InspectFlag = 0; /* do exit on SystemExit */ |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1658 | pymain_run_startup(pymain, cf); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1659 | pymain_run_interactive_hook(); |
| 1660 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1661 | |
| 1662 | if (pymain->main_importer_path != NULL) { |
| 1663 | pymain->status = pymain_run_main_from_importer(pymain); |
| 1664 | return; |
| 1665 | } |
| 1666 | |
| 1667 | FILE *fp; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1668 | if (pymain->filename != NULL) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1669 | fp = pymain_open_filename(pymain); |
| 1670 | if (fp == NULL) { |
| 1671 | return; |
| 1672 | } |
| 1673 | } |
| 1674 | else { |
| 1675 | fp = stdin; |
| 1676 | } |
| 1677 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1678 | pymain->status = pymain_run_file(fp, pymain->filename, cf); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | |
| 1682 | static void |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1683 | pymain_repl(_PyMain *pymain, PyCompilerFlags *cf) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1684 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | /* Check this environment variable at the end, to give programs the |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1686 | opportunity to set it from Python. */ |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1687 | if (!Py_InspectFlag && config_get_env_var(&pymain->config, "PYTHONINSPECT")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | Py_InspectFlag = 1; |
| 1689 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1690 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1691 | if (!(Py_InspectFlag && pymain->stdin_is_interactive && RUN_CODE(pymain))) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1692 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1694 | |
| 1695 | Py_InspectFlag = 0; |
| 1696 | pymain_run_interactive_hook(); |
Victor Stinner | 33c377e | 2017-12-05 15:12:41 +0100 | [diff] [blame] | 1697 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 1698 | int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1699 | pymain->status = (res != 0); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | /* Parse the command line. |
| 1704 | Handle --version and --help options directly. |
| 1705 | |
| 1706 | Return 1 if Python must exit. |
| 1707 | Return 0 on success. |
| 1708 | Set pymain->err and return -1 on failure. */ |
| 1709 | static int |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1710 | pymain_parse_cmdline(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1711 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1712 | int res = pymain_parse_cmdline_impl(pymain, cmdline); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1713 | if (res < 0) { |
| 1714 | return -1; |
| 1715 | } |
| 1716 | if (res) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1717 | pymain_usage(1, pymain->config.program); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1718 | pymain->status = 2; |
| 1719 | return 1; |
| 1720 | } |
| 1721 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1722 | if (pymain->command != NULL || pymain->module != NULL) { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 1723 | /* Backup _PyOS_optind */ |
| 1724 | _PyOS_optind--; |
| 1725 | } |
| 1726 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1727 | return 0; |
| 1728 | } |
| 1729 | |
| 1730 | |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 1731 | static const wchar_t* |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1732 | config_get_xoption(_PyCoreConfig *config, wchar_t *name) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1733 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1734 | int nxoption = config->nxoption; |
| 1735 | wchar_t **xoptions = config->xoptions; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 1736 | for (int i=0; i < nxoption; i++) { |
| 1737 | wchar_t *option = xoptions[i]; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1738 | size_t len; |
| 1739 | wchar_t *sep = wcschr(option, L'='); |
| 1740 | if (sep != NULL) { |
| 1741 | len = (sep - option); |
| 1742 | } |
| 1743 | else { |
| 1744 | len = wcslen(option); |
| 1745 | } |
| 1746 | if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') { |
| 1747 | return option; |
| 1748 | } |
| 1749 | } |
| 1750 | return NULL; |
| 1751 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1752 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1753 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1754 | static int |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 1755 | pymain_str_to_int(const char *str, int *result) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1756 | { |
| 1757 | errno = 0; |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 1758 | const char *endptr = str; |
| 1759 | long value = strtol(str, (char **)&endptr, 10); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1760 | if (*endptr != '\0' || errno == ERANGE) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1761 | return -1; |
| 1762 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1763 | if (value < INT_MIN || value > INT_MAX) { |
| 1764 | return -1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1765 | } |
| 1766 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1767 | *result = (int)value; |
| 1768 | return 0; |
| 1769 | } |
| 1770 | |
| 1771 | |
| 1772 | static int |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 1773 | pymain_wstr_to_int(const wchar_t *wstr, int *result) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1774 | { |
| 1775 | errno = 0; |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 1776 | const wchar_t *endptr = wstr; |
| 1777 | long value = wcstol(wstr, (wchar_t **)&endptr, 10); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1778 | if (*endptr != '\0' || errno == ERANGE) { |
| 1779 | return -1; |
| 1780 | } |
| 1781 | if (value < INT_MIN || value > INT_MAX) { |
| 1782 | return -1; |
| 1783 | } |
| 1784 | |
| 1785 | *result = (int)value; |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1790 | static _PyInitError |
| 1791 | pymain_init_tracemalloc(_PyCoreConfig *config) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1792 | { |
| 1793 | int nframe; |
| 1794 | int valid; |
| 1795 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1796 | const char *env = config_get_env_var(config, "PYTHONTRACEMALLOC"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1797 | if (env) { |
| 1798 | if (!pymain_str_to_int(env, &nframe)) { |
| 1799 | valid = (nframe >= 1); |
| 1800 | } |
| 1801 | else { |
| 1802 | valid = 0; |
| 1803 | } |
| 1804 | if (!valid) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1805 | return _Py_INIT_USER_ERR("PYTHONTRACEMALLOC: invalid number " |
| 1806 | "of frames"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1807 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1808 | config->tracemalloc = nframe; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1811 | const wchar_t *xoption = config_get_xoption(config, L"tracemalloc"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1812 | if (xoption) { |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 1813 | const wchar_t *sep = wcschr(xoption, L'='); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1814 | if (sep) { |
| 1815 | if (!pymain_wstr_to_int(sep + 1, &nframe)) { |
| 1816 | valid = (nframe >= 1); |
| 1817 | } |
| 1818 | else { |
| 1819 | valid = 0; |
| 1820 | } |
| 1821 | if (!valid) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1822 | return _Py_INIT_USER_ERR("-X tracemalloc=NFRAME: " |
| 1823 | "invalid number of frames"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | else { |
| 1827 | /* -X tracemalloc behaves as -X tracemalloc=1 */ |
| 1828 | nframe = 1; |
| 1829 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1830 | config->tracemalloc = nframe; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1831 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1832 | return _Py_INIT_OK(); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 1836 | static _PyInitError |
| 1837 | pymain_init_pycache_prefix(_PyCoreConfig *config) |
| 1838 | { |
| 1839 | wchar_t *env; |
| 1840 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1841 | int res = config_get_env_var_dup(config, &env, |
| 1842 | L"PYTHONPYCACHEPREFIX", "PYTHONPYCACHEPREFIX"); |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 1843 | if (res < 0) { |
| 1844 | return DECODE_LOCALE_ERR("PYTHONPYCACHEPREFIX", res); |
| 1845 | } else if (env) { |
| 1846 | config->pycache_prefix = env; |
| 1847 | } |
| 1848 | |
| 1849 | const wchar_t *xoption = config_get_xoption(config, L"pycache_prefix"); |
| 1850 | if (xoption) { |
| 1851 | const wchar_t *sep = wcschr(xoption, L'='); |
| 1852 | if (sep && wcslen(sep) > 1) { |
| 1853 | config->pycache_prefix = _PyMem_RawWcsdup(sep + 1); |
| 1854 | if (config->pycache_prefix == NULL) { |
| 1855 | return _Py_INIT_NO_MEMORY(); |
| 1856 | } |
| 1857 | } else { |
| 1858 | // -X pycache_prefix= can cancel the env var |
| 1859 | config->pycache_prefix = NULL; |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | return _Py_INIT_OK(); |
| 1864 | } |
| 1865 | |
| 1866 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1867 | static void |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1868 | get_env_flag(_PyCoreConfig *config, int *flag, const char *name) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1869 | { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1870 | const char *var = config_get_env_var(config, name); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1871 | if (!var) { |
| 1872 | return; |
| 1873 | } |
| 1874 | int value; |
| 1875 | if (pymain_str_to_int(var, &value) < 0 || value < 0) { |
| 1876 | /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */ |
| 1877 | value = 1; |
| 1878 | } |
| 1879 | if (*flag < value) { |
| 1880 | *flag = value; |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | |
| 1885 | static void |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1886 | cmdline_get_env_flags(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1887 | { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1888 | _PyCoreConfig *config = &pymain->config; |
| 1889 | get_env_flag(config, &cmdline->debug, "PYTHONDEBUG"); |
| 1890 | get_env_flag(config, &cmdline->verbosity, "PYTHONVERBOSE"); |
| 1891 | get_env_flag(config, &cmdline->optimization_level, "PYTHONOPTIMIZE"); |
| 1892 | get_env_flag(config, &cmdline->inspect, "PYTHONINSPECT"); |
| 1893 | get_env_flag(config, &cmdline->dont_write_bytecode, "PYTHONDONTWRITEBYTECODE"); |
| 1894 | get_env_flag(config, &cmdline->no_user_site_directory, "PYTHONNOUSERSITE"); |
| 1895 | get_env_flag(config, &cmdline->use_unbuffered_io, "PYTHONUNBUFFERED"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1896 | #ifdef MS_WINDOWS |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1897 | get_env_flag(config, &cmdline->legacy_windows_fs_encoding, |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1898 | "PYTHONLEGACYWINDOWSFSENCODING"); |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1899 | get_env_flag(config, &cmdline->legacy_windows_stdio, |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1900 | "PYTHONLEGACYWINDOWSSTDIO"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1901 | #endif |
| 1902 | } |
| 1903 | |
| 1904 | |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1905 | static _PyInitError |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1906 | config_init_home(_PyCoreConfig *config) |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 1907 | { |
| 1908 | wchar_t *home; |
| 1909 | |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1910 | /* If Py_SetPythonHome() was called, use its value */ |
| 1911 | home = _Py_path_config.home; |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 1912 | if (home) { |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1913 | config->home = _PyMem_RawWcsdup(home); |
| 1914 | if (config->home == NULL) { |
| 1915 | return _Py_INIT_NO_MEMORY(); |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 1916 | } |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1917 | return _Py_INIT_OK(); |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 1918 | } |
| 1919 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1920 | int res = config_get_env_var_dup(config, &home, L"PYTHONHOME", "PYTHONHOME"); |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 1921 | if (res < 0) { |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1922 | return DECODE_LOCALE_ERR("PYTHONHOME", res); |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 1923 | } |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1924 | config->home = home; |
| 1925 | return _Py_INIT_OK(); |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | |
Victor Stinner | 358e5e1 | 2017-12-15 00:51:22 +0100 | [diff] [blame] | 1929 | static _PyInitError |
| 1930 | config_init_hash_seed(_PyCoreConfig *config) |
| 1931 | { |
| 1932 | if (config->use_hash_seed < 0) { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1933 | const char *seed_text = config_get_env_var(config, "PYTHONHASHSEED"); |
Victor Stinner | 358e5e1 | 2017-12-15 00:51:22 +0100 | [diff] [blame] | 1934 | int use_hash_seed; |
| 1935 | unsigned long hash_seed; |
| 1936 | if (_Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) { |
| 1937 | return _Py_INIT_USER_ERR("PYTHONHASHSEED must be \"random\" " |
| 1938 | "or an integer in range [0; 4294967295]"); |
| 1939 | } |
| 1940 | config->use_hash_seed = use_hash_seed; |
| 1941 | config->hash_seed = hash_seed; |
| 1942 | } |
| 1943 | return _Py_INIT_OK(); |
| 1944 | } |
| 1945 | |
| 1946 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1947 | static _PyInitError |
| 1948 | config_init_utf8_mode(_PyCoreConfig *config) |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1949 | { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1950 | /* The option was already set in config.Py_UTF8Mode, |
| 1951 | by Py_LegacyWindowsFSEncodingFlag or PYTHONLEGACYWINDOWSFSENCODING. */ |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1952 | if (config->utf8_mode >= 0) { |
| 1953 | return _Py_INIT_OK(); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1954 | } |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1955 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1956 | const wchar_t *xopt = config_get_xoption(config, L"utf8"); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1957 | if (xopt) { |
| 1958 | wchar_t *sep = wcschr(xopt, L'='); |
| 1959 | if (sep) { |
| 1960 | xopt = sep + 1; |
| 1961 | if (wcscmp(xopt, L"1") == 0) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1962 | config->utf8_mode = 1; |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1963 | } |
| 1964 | else if (wcscmp(xopt, L"0") == 0) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1965 | config->utf8_mode = 0; |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1966 | } |
| 1967 | else { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1968 | return _Py_INIT_USER_ERR("invalid -X utf8 option value"); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1969 | } |
| 1970 | } |
| 1971 | else { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1972 | config->utf8_mode = 1; |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1973 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1974 | return _Py_INIT_OK(); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1975 | } |
| 1976 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1977 | const char *opt = config_get_env_var(config, "PYTHONUTF8"); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1978 | if (opt) { |
| 1979 | if (strcmp(opt, "1") == 0) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1980 | config->utf8_mode = 1; |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1981 | } |
| 1982 | else if (strcmp(opt, "0") == 0) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1983 | config->utf8_mode = 0; |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1984 | } |
| 1985 | else { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1986 | return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment " |
| 1987 | "variable value"); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1988 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1989 | return _Py_INIT_OK(); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1990 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1991 | |
| 1992 | return _Py_INIT_OK(); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1993 | } |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1994 | |
| 1995 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 1996 | static _PyInitError |
| 1997 | config_read_env_vars(_PyCoreConfig *config) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1998 | { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 1999 | config->allocator = config_get_env_var(config, "PYTHONMALLOC"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2000 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2001 | if (config_get_env_var(config, "PYTHONDUMPREFS")) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2002 | config->dump_refs = 1; |
| 2003 | } |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2004 | if (config_get_env_var(config, "PYTHONMALLOCSTATS")) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2005 | config->malloc_stats = 1; |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2008 | const char *env = config_get_env_var(config, "PYTHONCOERCECLOCALE"); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2009 | if (env) { |
| 2010 | if (strcmp(env, "0") == 0) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2011 | config->coerce_c_locale = 0; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2012 | } |
| 2013 | else if (strcmp(env, "warn") == 0) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2014 | config->coerce_c_locale_warn = 1; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2015 | } |
| 2016 | else { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2017 | config->coerce_c_locale = 1; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2021 | wchar_t *path; |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2022 | int res = config_get_env_var_dup(config, &path, L"PYTHONPATH", "PYTHONPATH"); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2023 | if (res < 0) { |
Carl Meyer | 4857543 | 2018-05-19 16:48:22 -0600 | [diff] [blame] | 2024 | return DECODE_LOCALE_ERR("PYTHONPATH", res); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2025 | } |
| 2026 | config->module_search_path_env = path; |
| 2027 | |
| 2028 | _PyInitError err = config_init_hash_seed(config); |
| 2029 | if (_Py_INIT_FAILED(err)) { |
| 2030 | return err; |
| 2031 | } |
| 2032 | |
| 2033 | return _Py_INIT_OK(); |
| 2034 | } |
| 2035 | |
| 2036 | |
| 2037 | static _PyInitError |
| 2038 | config_read_complex_options(_PyCoreConfig *config) |
| 2039 | { |
| 2040 | /* More complex options configured by env var and -X option */ |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2041 | if (config_get_env_var(config, "PYTHONFAULTHANDLER") |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2042 | || config_get_xoption(config, L"faulthandler")) { |
| 2043 | config->faulthandler = 1; |
| 2044 | } |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2045 | if (config_get_env_var(config, "PYTHONPROFILEIMPORTTIME") |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2046 | || config_get_xoption(config, L"importtime")) { |
| 2047 | config->import_time = 1; |
| 2048 | } |
| 2049 | if (config_get_xoption(config, L"dev" ) || |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2050 | config_get_env_var(config, "PYTHONDEVMODE")) |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2051 | { |
| 2052 | config->dev_mode = 1; |
| 2053 | config->faulthandler = 1; |
| 2054 | config->allocator = "debug"; |
| 2055 | } |
| 2056 | |
| 2057 | _PyInitError err = pymain_init_tracemalloc(config); |
| 2058 | if (_Py_INIT_FAILED(err)) { |
| 2059 | return err; |
| 2060 | } |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 2061 | |
| 2062 | err = pymain_init_pycache_prefix(config); |
| 2063 | if (_Py_INIT_FAILED(err)) { |
| 2064 | return err; |
| 2065 | } |
| 2066 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2067 | return _Py_INIT_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2071 | /* Parse command line options and environment variables. |
| 2072 | This code must not use Python runtime apart PyMem_Raw memory allocator. |
| 2073 | |
| 2074 | Return 0 on success. |
| 2075 | Return 1 if Python is done and must exit. |
| 2076 | Set pymain->err and return -1 on error. */ |
| 2077 | static int |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2078 | pymain_read_conf_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2079 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2080 | _PyInitError err; |
| 2081 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2082 | int res = pymain_parse_cmdline(pymain, cmdline); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2083 | if (res != 0) { |
| 2084 | return res; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2085 | } |
| 2086 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2087 | /* Get environment variables */ |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2088 | cmdline_get_env_flags(pymain, cmdline); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2089 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2090 | err = cmdline_init_env_warnoptions(pymain, cmdline); |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2091 | if (_Py_INIT_FAILED(err)) { |
| 2092 | pymain->err = err; |
| 2093 | return -1; |
| 2094 | } |
| 2095 | |
| 2096 | #ifdef MS_WINDOWS |
| 2097 | if (cmdline->legacy_windows_fs_encoding) { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2098 | pymain->config.utf8_mode = 0; |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2099 | } |
| 2100 | #endif |
| 2101 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2102 | if (pymain_init_core_argv(pymain, cmdline) < 0) { |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2103 | return -1; |
| 2104 | } |
| 2105 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2106 | err = _PyCoreConfig_Read(&pymain->config); |
Victor Stinner | 2b822a0 | 2018-01-25 09:18:36 +0100 | [diff] [blame] | 2107 | |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 2108 | if (_Py_INIT_FAILED(err)) { |
| 2109 | pymain->err = err; |
| 2110 | return -1; |
| 2111 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2112 | return 0; |
| 2113 | } |
| 2114 | |
| 2115 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2116 | /* Read the configuration, but initialize also the LC_CTYPE locale: |
| 2117 | enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */ |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2118 | static int |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2119 | pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2120 | { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2121 | _PyCoreConfig *config = &pymain->config; |
| 2122 | _PyCoreConfig save_config = _PyCoreConfig_INIT; |
| 2123 | char *oldloc = NULL; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2124 | int res = -1; |
| 2125 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2126 | oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL)); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2127 | if (oldloc == NULL) { |
| 2128 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 2129 | goto done; |
| 2130 | } |
| 2131 | |
| 2132 | /* Reconfigure the locale to the default for this process */ |
| 2133 | _Py_SetLocaleFromEnv(LC_ALL); |
| 2134 | |
| 2135 | int locale_coerced = 0; |
| 2136 | int loops = 0; |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2137 | |
| 2138 | if (_PyCoreConfig_Copy(&save_config, config) < 0) { |
| 2139 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 2140 | goto done; |
| 2141 | } |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2142 | |
| 2143 | while (1) { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2144 | int utf8_mode = config->utf8_mode; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2145 | int encoding_changed = 0; |
| 2146 | |
| 2147 | /* Watchdog to prevent an infinite loop */ |
| 2148 | loops++; |
| 2149 | if (loops == 3) { |
| 2150 | pymain->err = _Py_INIT_ERR("Encoding changed twice while " |
| 2151 | "reading the configuration"); |
| 2152 | goto done; |
| 2153 | } |
| 2154 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2155 | if (pymain_init_cmdline_argv(pymain, cmdline) < 0) { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 2156 | goto done; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2157 | } |
| 2158 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2159 | int conf_res = pymain_read_conf_impl(pymain, cmdline); |
| 2160 | if (conf_res != 0) { |
| 2161 | res = conf_res; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2162 | goto done; |
| 2163 | } |
| 2164 | |
| 2165 | /* The legacy C locale assumes ASCII as the default text encoding, which |
| 2166 | * causes problems not only for the CPython runtime, but also other |
| 2167 | * components like GNU readline. |
| 2168 | * |
| 2169 | * Accordingly, when the CLI detects it, it attempts to coerce it to a |
| 2170 | * more capable UTF-8 based alternative. |
| 2171 | * |
| 2172 | * See the documentation of the PYTHONCOERCECLOCALE setting for more |
| 2173 | * details. |
| 2174 | */ |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2175 | if (config->coerce_c_locale == 1 && !locale_coerced) { |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2176 | locale_coerced = 1; |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2177 | _Py_CoerceLegacyLocale(config); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2178 | encoding_changed = 1; |
| 2179 | } |
| 2180 | |
| 2181 | if (utf8_mode == -1) { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2182 | if (config->utf8_mode == 1) { |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2183 | /* UTF-8 Mode enabled */ |
| 2184 | encoding_changed = 1; |
| 2185 | } |
| 2186 | } |
| 2187 | else { |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2188 | if (config->utf8_mode != utf8_mode) { |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2189 | encoding_changed = 1; |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | if (!encoding_changed) { |
| 2194 | break; |
| 2195 | } |
| 2196 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2197 | /* Reset the configuration before reading the configuration, |
| 2198 | except UTF-8 Mode. */ |
| 2199 | int new_utf8_mode = config->utf8_mode; |
| 2200 | if (_PyCoreConfig_Copy(config, &save_config) < 0) { |
| 2201 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 2202 | goto done; |
| 2203 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2204 | pymain_clear_cmdline(pymain, cmdline); |
Victor Stinner | 6c5a4b3 | 2018-06-16 00:06:28 +0200 | [diff] [blame] | 2205 | memset(cmdline, 0, sizeof(*cmdline)); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2206 | pymain_get_global_config(pymain, cmdline); |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2207 | pymain->config.utf8_mode = new_utf8_mode; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2208 | |
| 2209 | /* The encoding changed: read again the configuration |
| 2210 | with the new encoding */ |
| 2211 | } |
| 2212 | res = 0; |
| 2213 | |
| 2214 | done: |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2215 | _PyCoreConfig_Clear(&save_config); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2216 | if (oldloc != NULL) { |
| 2217 | setlocale(LC_ALL, oldloc); |
| 2218 | PyMem_RawFree(oldloc); |
| 2219 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2220 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2221 | return res; |
| 2222 | } |
| 2223 | |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 2224 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2225 | static void |
| 2226 | config_init_locale(_PyCoreConfig *config) |
| 2227 | { |
| 2228 | if (config->utf8_mode >= 0 && config->coerce_c_locale >= 0) { |
| 2229 | return; |
| 2230 | } |
| 2231 | |
| 2232 | if (_Py_LegacyLocaleDetected()) { |
| 2233 | /* POSIX locale: enable C locale coercion and UTF-8 Mode */ |
| 2234 | if (config->utf8_mode < 0) { |
| 2235 | config->utf8_mode = 1; |
| 2236 | } |
| 2237 | if (config->coerce_c_locale < 0) { |
| 2238 | config->coerce_c_locale = 1; |
| 2239 | } |
| 2240 | return; |
| 2241 | } |
| 2242 | |
| 2243 | /* By default, C locale coercion and UTF-8 Mode are disabled */ |
| 2244 | if (config->coerce_c_locale < 0) { |
| 2245 | config->coerce_c_locale = 0; |
| 2246 | } |
| 2247 | if (config->utf8_mode < 0) { |
| 2248 | config->utf8_mode = 0; |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 2253 | /* Read configuration settings from standard locations |
| 2254 | * |
| 2255 | * This function doesn't make any changes to the interpreter state - it |
| 2256 | * merely populates any missing configuration settings. This allows an |
| 2257 | * embedding application to completely override a config option by |
| 2258 | * setting it before calling this function, or else modify the default |
| 2259 | * setting before passing the fully populated config to Py_EndInitialization. |
| 2260 | * |
| 2261 | * More advanced selective initialization tricks are possible by calling |
| 2262 | * this function multiple times with various preconfigured settings. |
| 2263 | */ |
| 2264 | |
| 2265 | _PyInitError |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2266 | _PyCoreConfig_Read(_PyCoreConfig *config) |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 2267 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2268 | _PyInitError err; |
| 2269 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2270 | _PyCoreConfig_GetGlobalConfig(config); |
| 2271 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2272 | err = config_read_env_vars(config); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2273 | if (_Py_INIT_FAILED(err)) { |
| 2274 | return err; |
| 2275 | } |
| 2276 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2277 | /* -X options */ |
| 2278 | if (config_get_xoption(config, L"showrefcount")) { |
| 2279 | config->show_ref_count = 1; |
| 2280 | } |
| 2281 | if (config_get_xoption(config, L"showalloccount")) { |
| 2282 | config->show_alloc_count = 1; |
| 2283 | } |
| 2284 | |
| 2285 | err = config_read_complex_options(config); |
| 2286 | if (_Py_INIT_FAILED(err)) { |
| 2287 | return err; |
| 2288 | } |
| 2289 | |
| 2290 | err = config_init_utf8_mode(config); |
| 2291 | if (_Py_INIT_FAILED(err)) { |
| 2292 | return err; |
| 2293 | } |
| 2294 | |
| 2295 | err = config_init_home(config); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2296 | if (_Py_INIT_FAILED(err)) { |
| 2297 | return err; |
| 2298 | } |
| 2299 | |
| 2300 | err = config_init_program_name(config); |
| 2301 | if (_Py_INIT_FAILED(err)) { |
| 2302 | return err; |
| 2303 | } |
| 2304 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2305 | config_init_locale(config); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 2306 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2307 | /* Signal handlers are installed by default */ |
| 2308 | if (config->install_signal_handlers < 0) { |
| 2309 | config->install_signal_handlers = 1; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2312 | if (config->_install_importlib) { |
| 2313 | err = _PyCoreConfig_InitPathConfig(config); |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 2314 | if (_Py_INIT_FAILED(err)) { |
| 2315 | return err; |
| 2316 | } |
| 2317 | } |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 2318 | return _Py_INIT_OK(); |
| 2319 | } |
| 2320 | |
| 2321 | |
| 2322 | void |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 2323 | _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config) |
| 2324 | { |
| 2325 | Py_CLEAR(config->argv); |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2326 | Py_CLEAR(config->executable); |
| 2327 | Py_CLEAR(config->prefix); |
| 2328 | Py_CLEAR(config->base_prefix); |
| 2329 | Py_CLEAR(config->exec_prefix); |
| 2330 | Py_CLEAR(config->base_exec_prefix); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 2331 | Py_CLEAR(config->warnoptions); |
| 2332 | Py_CLEAR(config->xoptions); |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2333 | Py_CLEAR(config->module_search_path); |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 2334 | Py_CLEAR(config->pycache_prefix); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | |
| 2338 | static PyObject* |
| 2339 | config_copy_attr(PyObject *obj) |
| 2340 | { |
| 2341 | if (PyUnicode_Check(obj)) { |
| 2342 | Py_INCREF(obj); |
| 2343 | return obj; |
| 2344 | } |
| 2345 | else if (PyList_Check(obj)) { |
| 2346 | return PyList_GetSlice(obj, 0, Py_SIZE(obj)); |
| 2347 | } |
| 2348 | else if (PyDict_Check(obj)) { |
| 2349 | /* The dict type is used for xoptions. Make the assumption that keys |
| 2350 | and values are immutables */ |
| 2351 | return PyDict_Copy(obj); |
| 2352 | } |
| 2353 | else { |
| 2354 | PyErr_Format(PyExc_TypeError, |
| 2355 | "cannot copy config attribute of type %.200s", |
| 2356 | Py_TYPE(obj)->tp_name); |
| 2357 | return NULL; |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | |
| 2362 | int |
| 2363 | _PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config, |
| 2364 | const _PyMainInterpreterConfig *config2) |
| 2365 | { |
| 2366 | _PyMainInterpreterConfig_Clear(config); |
| 2367 | |
| 2368 | #define COPY_ATTR(ATTR) \ |
| 2369 | do { \ |
| 2370 | if (config2->ATTR != NULL) { \ |
| 2371 | config->ATTR = config_copy_attr(config2->ATTR); \ |
| 2372 | if (config->ATTR == NULL) { \ |
| 2373 | return -1; \ |
| 2374 | } \ |
| 2375 | } \ |
| 2376 | } while (0) |
| 2377 | |
| 2378 | COPY_ATTR(argv); |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2379 | COPY_ATTR(executable); |
| 2380 | COPY_ATTR(prefix); |
| 2381 | COPY_ATTR(base_prefix); |
| 2382 | COPY_ATTR(exec_prefix); |
| 2383 | COPY_ATTR(base_exec_prefix); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 2384 | COPY_ATTR(warnoptions); |
| 2385 | COPY_ATTR(xoptions); |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2386 | COPY_ATTR(module_search_path); |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 2387 | COPY_ATTR(pycache_prefix); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 2388 | #undef COPY_ATTR |
| 2389 | return 0; |
| 2390 | } |
| 2391 | |
| 2392 | |
| 2393 | |
| 2394 | |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2395 | _PyInitError |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2396 | _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config, |
| 2397 | const _PyCoreConfig *config) |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 2398 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2399 | if (main_config->install_signal_handlers < 0) { |
| 2400 | main_config->install_signal_handlers = config->install_signal_handlers; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 2401 | } |
| 2402 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2403 | if (main_config->xoptions == NULL) { |
| 2404 | main_config->xoptions = config_create_xoptions_dict(config); |
| 2405 | if (main_config->xoptions == NULL) { |
| 2406 | return _Py_INIT_NO_MEMORY(); |
| 2407 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2408 | } |
| 2409 | |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 2410 | #define COPY_WSTR(ATTR) \ |
| 2411 | do { \ |
| 2412 | if (main_config->ATTR == NULL) { \ |
| 2413 | main_config->ATTR = PyUnicode_FromWideChar(config->ATTR, -1); \ |
| 2414 | if (main_config->ATTR == NULL) { \ |
| 2415 | return _Py_INIT_NO_MEMORY(); \ |
| 2416 | } \ |
| 2417 | } \ |
| 2418 | } while (0) |
| 2419 | #define COPY_WSTRLIST(ATTR, LEN, LIST) \ |
| 2420 | do { \ |
| 2421 | if (ATTR == NULL) { \ |
| 2422 | ATTR = wstrlist_as_pylist(LEN, LIST); \ |
| 2423 | if (ATTR == NULL) { \ |
| 2424 | return _Py_INIT_NO_MEMORY(); \ |
| 2425 | } \ |
| 2426 | } \ |
| 2427 | } while (0) |
| 2428 | |
| 2429 | COPY_WSTRLIST(main_config->warnoptions, |
| 2430 | config->nwarnoption, config->warnoptions); |
| 2431 | if (config->argc >= 0) { |
| 2432 | COPY_WSTRLIST(main_config->argv, |
| 2433 | config->argc, config->argv); |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 2434 | } |
| 2435 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame^] | 2436 | if (config->_install_importlib) { |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 2437 | COPY_WSTR(executable); |
| 2438 | COPY_WSTR(prefix); |
| 2439 | COPY_WSTR(base_prefix); |
| 2440 | COPY_WSTR(exec_prefix); |
| 2441 | COPY_WSTR(base_exec_prefix); |
| 2442 | |
| 2443 | COPY_WSTRLIST(main_config->module_search_path, |
| 2444 | config->nmodule_search_path, config->module_search_paths); |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 2445 | |
| 2446 | if (config->pycache_prefix != NULL) { |
| 2447 | COPY_WSTR(pycache_prefix); |
| 2448 | } else { |
| 2449 | main_config->pycache_prefix = NULL; |
| 2450 | } |
| 2451 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2452 | } |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2453 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 2454 | return _Py_INIT_OK(); |
Victor Stinner | 8ded5b8 | 2018-01-24 17:03:28 +0100 | [diff] [blame] | 2455 | #undef COPY_WSTR |
| 2456 | #undef COPY_WSTRLIST |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | |
| 2460 | static int |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2461 | pymain_init_python_main(_PyMain *pymain) |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 2462 | { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2463 | _PyInitError err; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2464 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2465 | _PyMainInterpreterConfig main_config = _PyMainInterpreterConfig_INIT; |
| 2466 | err = _PyMainInterpreterConfig_Read(&main_config, &pymain->config); |
| 2467 | if (!_Py_INIT_FAILED(err)) { |
| 2468 | err = _Py_InitializeMainInterpreter(&main_config); |
| 2469 | } |
| 2470 | _PyMainInterpreterConfig_Clear(&main_config); |
| 2471 | |
| 2472 | if (_Py_INIT_FAILED(err)) { |
| 2473 | pymain->err = err; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2474 | return -1; |
| 2475 | } |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 2476 | return 0; |
| 2477 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2478 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 2479 | |
| 2480 | static int |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2481 | pymain_init_sys_path(_PyMain *pymain) |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 2482 | { |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2483 | if (pymain->filename != NULL) { |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 2484 | /* If filename is a package (ex: directory or ZIP file) which contains |
| 2485 | __main__.py, main_importer_path is set to filename and will be |
| 2486 | prepended to sys.path by pymain_run_main_from_importer(). Otherwise, |
| 2487 | main_importer_path is set to NULL. */ |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2488 | pymain->main_importer_path = pymain_get_importer(pymain->filename); |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 2489 | } |
| 2490 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2491 | PyObject *path0; |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2492 | if (pymain_compute_path0(pymain, &path0) < 0) { |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 2493 | return -1; |
| 2494 | } |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2495 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2496 | pymain_clear_config(pymain); |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2497 | |
| 2498 | if (path0 != NULL) { |
| 2499 | if (pymain_update_sys_path(pymain, path0) < 0) { |
| 2500 | Py_DECREF(path0); |
| 2501 | return -1; |
| 2502 | } |
| 2503 | Py_DECREF(path0); |
| 2504 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2505 | return 0; |
| 2506 | } |
| 2507 | |
| 2508 | |
| 2509 | static void |
| 2510 | pymain_run_python(_PyMain *pymain) |
| 2511 | { |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2512 | PyCompilerFlags cf = {.cf_flags = 0}; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2513 | |
| 2514 | pymain_header(pymain); |
| 2515 | pymain_import_readline(pymain); |
| 2516 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2517 | if (pymain->command) { |
| 2518 | pymain->status = pymain_run_command(pymain->command, &cf); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2519 | } |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2520 | else if (pymain->module) { |
| 2521 | pymain->status = (pymain_run_module(pymain->module, 1) != 0); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2522 | } |
| 2523 | else { |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2524 | pymain_run_filename(pymain, &cf); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2525 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2526 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2527 | pymain_repl(pymain, &cf); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 2531 | static void |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2532 | pymain_init(_PyMain *pymain) |
| 2533 | { |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2534 | /* 754 requires that FP exceptions run in "no stop" mode by default, |
| 2535 | * and until C vendors implement C99's ways to control FP exceptions, |
| 2536 | * Python requires non-stop mode. Alas, some platforms enable FP |
| 2537 | * exceptions by default. Here we disable them. |
| 2538 | */ |
| 2539 | #ifdef __FreeBSD__ |
| 2540 | fedisableexcept(FE_OVERFLOW); |
| 2541 | #endif |
| 2542 | |
Victor Stinner | e32e79f | 2017-11-23 01:49:45 +0100 | [diff] [blame] | 2543 | pymain->config.install_signal_handlers = 1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2544 | } |
| 2545 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2546 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2547 | static int |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2548 | pymain_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2549 | { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 2550 | pymain->err = _PyRuntime_Initialize(); |
| 2551 | if (_Py_INIT_FAILED(pymain->err)) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2552 | return -1; |
| 2553 | } |
| 2554 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2555 | int res = pymain_read_conf(pymain, cmdline); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2556 | if (res < 0) { |
| 2557 | return -1; |
| 2558 | } |
| 2559 | if (res > 0) { |
| 2560 | /* --help or --version command: we are done */ |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2561 | return 1; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2562 | } |
| 2563 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2564 | if (cmdline->print_help) { |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2565 | pymain_usage(0, pymain->config.program); |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2566 | return 1; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | if (cmdline->print_version) { |
| 2570 | printf("Python %s\n", |
| 2571 | (cmdline->print_version >= 2) ? Py_GetVersion() : PY_VERSION); |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2572 | return 1; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2573 | } |
| 2574 | |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 2575 | /* For Py_GetArgcArgv(). Cleared by pymain_free(). */ |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2576 | orig_argv = copy_wstrlist(pymain->argc, cmdline->argv); |
| 2577 | if (orig_argv == NULL) { |
| 2578 | pymain->err = _Py_INIT_NO_MEMORY(); |
| 2579 | return -1; |
| 2580 | } |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 2581 | orig_argc = pymain->argc; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2582 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2583 | _PyInitError err = config_init_warnoptions(&pymain->config, cmdline); |
| 2584 | if (_Py_INIT_FAILED(err)) { |
| 2585 | pymain->err = err; |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2586 | return -1; |
| 2587 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2588 | return 0; |
| 2589 | } |
Barry Warsaw | 3e13b1e | 2001-02-23 16:46:39 +0000 | [diff] [blame] | 2590 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2591 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2592 | /* Read the configuration into _PyCoreConfig and _PyMain, initialize the |
| 2593 | LC_CTYPE locale and Py_DecodeLocale(). |
| 2594 | |
| 2595 | Configuration: |
| 2596 | |
| 2597 | * Command line arguments |
| 2598 | * Environment variables |
| 2599 | * Py_xxx global configuration variables |
| 2600 | |
| 2601 | _Py_CommandLineDetails is a temporary structure used to prioritize these |
| 2602 | variables. */ |
| 2603 | static int |
| 2604 | pymain_cmdline(_PyMain *pymain) |
| 2605 | { |
Victor Stinner | 31e9908 | 2017-12-20 23:41:38 +0100 | [diff] [blame] | 2606 | /* Force default allocator, since pymain_free() and pymain_clear_config() |
| 2607 | must use the same allocator than this function. */ |
| 2608 | PyMemAllocatorEx old_alloc; |
| 2609 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 2610 | #ifdef Py_DEBUG |
| 2611 | PyMemAllocatorEx default_alloc; |
| 2612 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc); |
| 2613 | #endif |
| 2614 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2615 | _Py_CommandLineDetails cmdline; |
| 2616 | memset(&cmdline, 0, sizeof(cmdline)); |
| 2617 | |
| 2618 | pymain_get_global_config(pymain, &cmdline); |
| 2619 | |
| 2620 | int res = pymain_cmdline_impl(pymain, &cmdline); |
| 2621 | |
| 2622 | pymain_set_global_config(pymain, &cmdline); |
| 2623 | |
| 2624 | pymain_clear_cmdline(pymain, &cmdline); |
Victor Stinner | 31e9908 | 2017-12-20 23:41:38 +0100 | [diff] [blame] | 2625 | |
| 2626 | #ifdef Py_DEBUG |
| 2627 | /* Make sure that PYMEM_DOMAIN_RAW has not been modified */ |
| 2628 | PyMemAllocatorEx cur_alloc; |
| 2629 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc); |
| 2630 | assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0); |
| 2631 | #endif |
| 2632 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2633 | return res; |
| 2634 | } |
| 2635 | |
| 2636 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2637 | static int |
| 2638 | pymain_main(_PyMain *pymain) |
| 2639 | { |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 2640 | pymain_init(pymain); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2641 | |
Victor Stinner | ca719ac | 2017-12-20 18:00:19 +0100 | [diff] [blame] | 2642 | int res = pymain_cmdline(pymain); |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2643 | if (res < 0) { |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2644 | _Py_FatalInitError(pymain->err); |
| 2645 | } |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2646 | if (res == 1) { |
| 2647 | goto done; |
| 2648 | } |
| 2649 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2650 | pymain_init_stdio(pymain); |
| 2651 | |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 2652 | /* bpo-34008: For backward compatibility reasons, calling Py_Main() after |
| 2653 | Py_Initialize() ignores the new configuration. */ |
| 2654 | if (!_PyRuntime.initialized) { |
| 2655 | pymain->err = _Py_InitializeCore(&pymain->config); |
| 2656 | if (_Py_INIT_FAILED(pymain->err)) { |
| 2657 | _Py_FatalInitError(pymain->err); |
| 2658 | } |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | if (pymain_init_python_main(pymain) < 0) { |
| 2662 | _Py_FatalInitError(pymain->err); |
| 2663 | } |
| 2664 | |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 2665 | if (pymain_init_sys_path(pymain) < 0) { |
| 2666 | _Py_FatalInitError(pymain->err); |
| 2667 | } |
| 2668 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2669 | pymain_run_python(pymain); |
| 2670 | |
| 2671 | if (Py_FinalizeEx() < 0) { |
| 2672 | /* Value unlikely to be confused with a non-error exit status or |
| 2673 | other special meaning */ |
| 2674 | pymain->status = 120; |
| 2675 | } |
| 2676 | |
| 2677 | done: |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2678 | pymain_free(pymain); |
| 2679 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2680 | return pymain->status; |
| 2681 | } |
| 2682 | |
| 2683 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2684 | int |
| 2685 | Py_Main(int argc, wchar_t **argv) |
| 2686 | { |
| 2687 | _PyMain pymain = _PyMain_INIT; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 2688 | pymain.use_bytes_argv = 0; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2689 | pymain.argc = argc; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 2690 | pymain.wchar_argv = argv; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2691 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2692 | return pymain_main(&pymain); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2695 | |
| 2696 | int |
| 2697 | _Py_UnixMain(int argc, char **argv) |
| 2698 | { |
| 2699 | _PyMain pymain = _PyMain_INIT; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2700 | pymain.use_bytes_argv = 1; |
Victor Stinner | c4bca95 | 2017-12-19 23:48:17 +0100 | [diff] [blame] | 2701 | pymain.argc = argc; |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 2702 | pymain.bytes_argv = argv; |
| 2703 | |
| 2704 | return pymain_main(&pymain); |
| 2705 | } |
| 2706 | |
| 2707 | |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 2708 | /* this is gonna seem *real weird*, but if you put some other code between |
| 2709 | Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the |
| 2710 | while statement in Misc/gdbinit:ppystack */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 2711 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 2712 | /* Make the *original* argc/argv available to other modules. |
| 2713 | This is rare, but it is needed by the secureware extension. */ |
| 2714 | |
| 2715 | void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2716 | Py_GetArgcArgv(int *argc, wchar_t ***argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 2717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | *argc = orig_argc; |
| 2719 | *argv = orig_argv; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 2720 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2721 | |
| 2722 | #ifdef __cplusplus |
| 2723 | } |
| 2724 | #endif |