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