Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 1 | #include "Python.h" |
Victor Stinner | 4f98f46 | 2020-04-15 04:01:58 +0200 | [diff] [blame] | 2 | #include "pycore_getopt.h" // _PyOS_GetOpt() |
| 3 | #include "pycore_initconfig.h" // _PyArgv |
Victor Stinner | d9ea5ca | 2020-04-15 02:57:50 +0200 | [diff] [blame] | 4 | #include "pycore_pymem.h" // _PyMem_GetAllocatorName() |
| 5 | #include "pycore_runtime.h" // _PyRuntime_Initialize() |
| 6 | #include <locale.h> // setlocale() |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 7 | |
| 8 | |
| 9 | #define DECODE_LOCALE_ERR(NAME, LEN) \ |
| 10 | (((LEN) == -2) \ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 11 | ? _PyStatus_ERR("cannot decode " NAME) \ |
| 12 | : _PyStatus_NO_MEMORY()) |
| 13 | |
| 14 | |
| 15 | /* Forward declarations */ |
| 16 | static void |
| 17 | preconfig_copy(PyPreConfig *config, const PyPreConfig *config2); |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 18 | |
| 19 | |
| 20 | /* --- File system encoding/errors -------------------------------- */ |
| 21 | |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 22 | const char *Py_FileSystemDefaultEncoding = NULL; |
| 23 | int Py_HasFileSystemDefaultEncoding = 0; |
| 24 | const char *Py_FileSystemDefaultEncodeErrors = NULL; |
| 25 | int _Py_HasFileSystemDefaultEncodeErrors = 0; |
| 26 | |
| 27 | void |
| 28 | _Py_ClearFileSystemEncoding(void) |
| 29 | { |
| 30 | if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) { |
| 31 | PyMem_RawFree((char*)Py_FileSystemDefaultEncoding); |
| 32 | Py_FileSystemDefaultEncoding = NULL; |
| 33 | } |
| 34 | if (!_Py_HasFileSystemDefaultEncodeErrors && Py_FileSystemDefaultEncodeErrors) { |
| 35 | PyMem_RawFree((char*)Py_FileSystemDefaultEncodeErrors); |
| 36 | Py_FileSystemDefaultEncodeErrors = NULL; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | |
| 41 | /* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame] | 42 | global configuration variables to PyConfig.filesystem_encoding and |
| 43 | PyConfig.filesystem_errors (encoded to UTF-8). |
| 44 | |
| 45 | Function called by _PyUnicode_InitEncodings(). */ |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 46 | int |
| 47 | _Py_SetFileSystemEncoding(const char *encoding, const char *errors) |
| 48 | { |
| 49 | char *encoding2 = _PyMem_RawStrdup(encoding); |
| 50 | if (encoding2 == NULL) { |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | char *errors2 = _PyMem_RawStrdup(errors); |
| 55 | if (errors2 == NULL) { |
| 56 | PyMem_RawFree(encoding2); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | _Py_ClearFileSystemEncoding(); |
| 61 | |
| 62 | Py_FileSystemDefaultEncoding = encoding2; |
| 63 | Py_HasFileSystemDefaultEncoding = 0; |
| 64 | |
| 65 | Py_FileSystemDefaultEncodeErrors = errors2; |
| 66 | _Py_HasFileSystemDefaultEncodeErrors = 0; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* --- _PyArgv ---------------------------------------------------- */ |
| 72 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 73 | /* Decode bytes_argv using Py_DecodeLocale() */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 74 | PyStatus |
| 75 | _PyArgv_AsWstrList(const _PyArgv *args, PyWideStringList *list) |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 76 | { |
Victor Stinner | fb4ae15 | 2019-09-30 01:40:17 +0200 | [diff] [blame] | 77 | PyWideStringList wargv = _PyWideStringList_INIT; |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 78 | if (args->use_bytes_argv) { |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 79 | size_t size = sizeof(wchar_t*) * args->argc; |
| 80 | wargv.items = (wchar_t **)PyMem_RawMalloc(size); |
| 81 | if (wargv.items == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 82 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 85 | for (Py_ssize_t i = 0; i < args->argc; i++) { |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 86 | size_t len; |
| 87 | wchar_t *arg = Py_DecodeLocale(args->bytes_argv[i], &len); |
| 88 | if (arg == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 89 | _PyWideStringList_Clear(&wargv); |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 90 | return DECODE_LOCALE_ERR("command line arguments", |
| 91 | (Py_ssize_t)len); |
| 92 | } |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 93 | wargv.items[i] = arg; |
| 94 | wargv.length++; |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 95 | } |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 96 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 97 | _PyWideStringList_Clear(list); |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 98 | *list = wargv; |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 99 | } |
| 100 | else { |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 101 | wargv.length = args->argc; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 102 | wargv.items = (wchar_t **)args->wchar_argv; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 103 | if (_PyWideStringList_Copy(list, &wargv) < 0) { |
| 104 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 105 | } |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 106 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 107 | return _PyStatus_OK(); |
Victor Stinner | 91b9ecf | 2019-03-01 17:52:56 +0100 | [diff] [blame] | 108 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 109 | |
| 110 | |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 111 | /* --- _PyPreCmdline ------------------------------------------------- */ |
| 112 | |
Victor Stinner | fa15376 | 2019-03-20 04:25:38 +0100 | [diff] [blame] | 113 | void |
| 114 | _PyPreCmdline_Clear(_PyPreCmdline *cmdline) |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 115 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 116 | _PyWideStringList_Clear(&cmdline->argv); |
| 117 | _PyWideStringList_Clear(&cmdline->xoptions); |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 121 | PyStatus |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 122 | _PyPreCmdline_SetArgv(_PyPreCmdline *cmdline, const _PyArgv *args) |
Victor Stinner | fa15376 | 2019-03-20 04:25:38 +0100 | [diff] [blame] | 123 | { |
| 124 | return _PyArgv_AsWstrList(args, &cmdline->argv); |
| 125 | } |
| 126 | |
| 127 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 128 | static void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 129 | precmdline_get_preconfig(_PyPreCmdline *cmdline, const PyPreConfig *config) |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 130 | { |
| 131 | #define COPY_ATTR(ATTR) \ |
| 132 | if (config->ATTR != -1) { \ |
| 133 | cmdline->ATTR = config->ATTR; \ |
| 134 | } |
| 135 | |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 136 | COPY_ATTR(isolated); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 137 | COPY_ATTR(use_environment); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 138 | COPY_ATTR(dev_mode); |
| 139 | |
| 140 | #undef COPY_ATTR |
| 141 | } |
| 142 | |
| 143 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 144 | static void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 145 | precmdline_set_preconfig(const _PyPreCmdline *cmdline, PyPreConfig *config) |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 146 | { |
| 147 | #define COPY_ATTR(ATTR) \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 148 | config->ATTR = cmdline->ATTR |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 149 | |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 150 | COPY_ATTR(isolated); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 151 | COPY_ATTR(use_environment); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 152 | COPY_ATTR(dev_mode); |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 153 | |
| 154 | #undef COPY_ATTR |
| 155 | } |
| 156 | |
| 157 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 158 | PyStatus |
| 159 | _PyPreCmdline_SetConfig(const _PyPreCmdline *cmdline, PyConfig *config) |
Victor Stinner | a6fbc4e | 2019-03-25 18:37:10 +0100 | [diff] [blame] | 160 | { |
| 161 | #define COPY_ATTR(ATTR) \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 162 | config->ATTR = cmdline->ATTR |
| 163 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 164 | PyStatus status = _PyWideStringList_Extend(&config->xoptions, &cmdline->xoptions); |
| 165 | if (_PyStatus_EXCEPTION(status)) { |
| 166 | return status; |
Victor Stinner | a6fbc4e | 2019-03-25 18:37:10 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Victor Stinner | a6fbc4e | 2019-03-25 18:37:10 +0100 | [diff] [blame] | 169 | COPY_ATTR(isolated); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 170 | COPY_ATTR(use_environment); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 171 | COPY_ATTR(dev_mode); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 172 | return _PyStatus_OK(); |
Victor Stinner | a6fbc4e | 2019-03-25 18:37:10 +0100 | [diff] [blame] | 173 | |
| 174 | #undef COPY_ATTR |
| 175 | } |
| 176 | |
| 177 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 178 | /* Parse the command line arguments */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 179 | static PyStatus |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 180 | precmdline_parse_cmdline(_PyPreCmdline *cmdline) |
| 181 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 182 | const PyWideStringList *argv = &cmdline->argv; |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 183 | |
| 184 | _PyOS_ResetGetOpt(); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 185 | /* Don't log parsing errors into stderr here: PyConfig_Read() |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 186 | is responsible for that */ |
| 187 | _PyOS_opterr = 0; |
| 188 | do { |
| 189 | int longindex = -1; |
| 190 | int c = _PyOS_GetOpt(argv->length, argv->items, &longindex); |
| 191 | |
| 192 | if (c == EOF || c == 'c' || c == 'm') { |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | switch (c) { |
| 197 | case 'E': |
| 198 | cmdline->use_environment = 0; |
| 199 | break; |
| 200 | |
| 201 | case 'I': |
| 202 | cmdline->isolated = 1; |
| 203 | break; |
| 204 | |
| 205 | case 'X': |
| 206 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 207 | PyStatus status = PyWideStringList_Append(&cmdline->xoptions, |
| 208 | _PyOS_optarg); |
| 209 | if (_PyStatus_EXCEPTION(status)) { |
| 210 | return status; |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 211 | } |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | default: |
| 216 | /* ignore other argument: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 217 | handled by PyConfig_Read() */ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | } while (1); |
| 221 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 222 | return _PyStatus_OK(); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 226 | PyStatus |
| 227 | _PyPreCmdline_Read(_PyPreCmdline *cmdline, const PyPreConfig *preconfig) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 228 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 229 | precmdline_get_preconfig(cmdline, preconfig); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 230 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 231 | if (preconfig->parse_argv) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 232 | PyStatus status = precmdline_parse_cmdline(cmdline); |
| 233 | if (_PyStatus_EXCEPTION(status)) { |
| 234 | return status; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 235 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* isolated, use_environment */ |
| 239 | if (cmdline->isolated < 0) { |
| 240 | cmdline->isolated = 0; |
| 241 | } |
| 242 | if (cmdline->isolated > 0) { |
| 243 | cmdline->use_environment = 0; |
| 244 | } |
| 245 | if (cmdline->use_environment < 0) { |
| 246 | cmdline->use_environment = 0; |
| 247 | } |
| 248 | |
| 249 | /* dev_mode */ |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 250 | if ((cmdline->dev_mode < 0) |
| 251 | && (_Py_get_xoption(&cmdline->xoptions, L"dev") |
| 252 | || _Py_GetEnv(cmdline->use_environment, "PYTHONDEVMODE"))) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 253 | { |
| 254 | cmdline->dev_mode = 1; |
| 255 | } |
| 256 | if (cmdline->dev_mode < 0) { |
| 257 | cmdline->dev_mode = 0; |
| 258 | } |
| 259 | |
| 260 | assert(cmdline->use_environment >= 0); |
| 261 | assert(cmdline->isolated >= 0); |
| 262 | assert(cmdline->dev_mode >= 0); |
| 263 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 264 | return _PyStatus_OK(); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 268 | /* --- PyPreConfig ----------------------------------------------- */ |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 269 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 270 | |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 271 | void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 272 | _PyPreConfig_InitCompatConfig(PyPreConfig *config) |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 273 | { |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 274 | memset(config, 0, sizeof(*config)); |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 275 | |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 276 | config->_config_init = (int)_PyConfig_INIT_COMPAT; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 277 | config->parse_argv = 0; |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 278 | config->isolated = -1; |
| 279 | config->use_environment = -1; |
| 280 | config->configure_locale = 1; |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 281 | |
| 282 | /* bpo-36443: C locale coercion (PEP 538) and UTF-8 Mode (PEP 540) |
| 283 | are disabled by default using the Compat configuration. |
| 284 | |
| 285 | Py_UTF8Mode=1 enables the UTF-8 mode. PYTHONUTF8 environment variable |
| 286 | is ignored (even if use_environment=1). */ |
| 287 | config->utf8_mode = 0; |
| 288 | config->coerce_c_locale = 0; |
| 289 | config->coerce_c_locale_warn = 0; |
| 290 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 291 | config->dev_mode = -1; |
Victor Stinner | c5fa364 | 2020-05-05 16:41:11 +0200 | [diff] [blame] | 292 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 293 | /* bpo-40512: pymalloc is not compatible with subinterpreters, |
| 294 | force usage of libc malloc() which is thread-safe. */ |
| 295 | #ifdef Py_DEBUG |
| 296 | config->allocator = PYMEM_ALLOCATOR_MALLOC_DEBUG; |
| 297 | #else |
| 298 | config->allocator = PYMEM_ALLOCATOR_MALLOC; |
| 299 | #endif |
| 300 | #else |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 301 | config->allocator = PYMEM_ALLOCATOR_NOT_SET; |
Victor Stinner | c5fa364 | 2020-05-05 16:41:11 +0200 | [diff] [blame] | 302 | #endif |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 303 | #ifdef MS_WINDOWS |
| 304 | config->legacy_windows_fs_encoding = -1; |
| 305 | #endif |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 309 | void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 310 | PyPreConfig_InitPythonConfig(PyPreConfig *config) |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 311 | { |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 312 | _PyPreConfig_InitCompatConfig(config); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 313 | |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 314 | config->_config_init = (int)_PyConfig_INIT_PYTHON; |
| 315 | config->isolated = 0; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 316 | config->parse_argv = 1; |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 317 | config->use_environment = 1; |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 318 | /* Set to -1 to enable C locale coercion (PEP 538) and UTF-8 Mode (PEP 540) |
| 319 | depending on the LC_CTYPE locale, PYTHONUTF8 and PYTHONCOERCECLOCALE |
| 320 | environment variables. */ |
| 321 | config->coerce_c_locale = -1; |
| 322 | config->coerce_c_locale_warn = -1; |
| 323 | config->utf8_mode = -1; |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 324 | #ifdef MS_WINDOWS |
| 325 | config->legacy_windows_fs_encoding = 0; |
| 326 | #endif |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 330 | void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 331 | PyPreConfig_InitIsolatedConfig(PyPreConfig *config) |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 332 | { |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 333 | _PyPreConfig_InitCompatConfig(config); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 334 | |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 335 | config->_config_init = (int)_PyConfig_INIT_ISOLATED; |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 336 | config->configure_locale = 0; |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 337 | config->isolated = 1; |
| 338 | config->use_environment = 0; |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 339 | config->utf8_mode = 0; |
| 340 | config->dev_mode = 0; |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 341 | #ifdef MS_WINDOWS |
| 342 | config->legacy_windows_fs_encoding = 0; |
| 343 | #endif |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 347 | PyStatus |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 348 | _PyPreConfig_InitFromPreConfig(PyPreConfig *config, |
| 349 | const PyPreConfig *config2) |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 350 | { |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 351 | PyPreConfig_InitPythonConfig(config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 352 | preconfig_copy(config, config2); |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 353 | return _PyStatus_OK(); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 357 | void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 358 | _PyPreConfig_InitFromConfig(PyPreConfig *preconfig, const PyConfig *config) |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 359 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 360 | _PyConfigInitEnum config_init = (_PyConfigInitEnum)config->_config_init; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 361 | switch (config_init) { |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 362 | case _PyConfig_INIT_PYTHON: |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 363 | PyPreConfig_InitPythonConfig(preconfig); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 364 | break; |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 365 | case _PyConfig_INIT_ISOLATED: |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 366 | PyPreConfig_InitIsolatedConfig(preconfig); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 367 | break; |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 368 | case _PyConfig_INIT_COMPAT: |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 369 | default: |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 370 | _PyPreConfig_InitCompatConfig(preconfig); |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 373 | _PyPreConfig_GetConfig(preconfig, config); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 377 | static void |
| 378 | preconfig_copy(PyPreConfig *config, const PyPreConfig *config2) |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 379 | { |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 380 | #define COPY_ATTR(ATTR) config->ATTR = config2->ATTR |
| 381 | |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 382 | COPY_ATTR(_config_init); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 383 | COPY_ATTR(parse_argv); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 384 | COPY_ATTR(isolated); |
| 385 | COPY_ATTR(use_environment); |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 386 | COPY_ATTR(configure_locale); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 387 | COPY_ATTR(dev_mode); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 388 | COPY_ATTR(coerce_c_locale); |
| 389 | COPY_ATTR(coerce_c_locale_warn); |
Victor Stinner | b594784 | 2019-05-18 00:38:16 +0200 | [diff] [blame] | 390 | COPY_ATTR(utf8_mode); |
| 391 | COPY_ATTR(allocator); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 392 | #ifdef MS_WINDOWS |
| 393 | COPY_ATTR(legacy_windows_fs_encoding); |
| 394 | #endif |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 395 | |
| 396 | #undef COPY_ATTR |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 400 | PyObject* |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 401 | _PyPreConfig_AsDict(const PyPreConfig *config) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 402 | { |
| 403 | PyObject *dict; |
| 404 | |
| 405 | dict = PyDict_New(); |
| 406 | if (dict == NULL) { |
| 407 | return NULL; |
| 408 | } |
| 409 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 410 | #define SET_ITEM_INT(ATTR) \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 411 | do { \ |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 412 | PyObject *obj = PyLong_FromLong(config->ATTR); \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 413 | if (obj == NULL) { \ |
| 414 | goto fail; \ |
| 415 | } \ |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 416 | int res = PyDict_SetItemString(dict, #ATTR, obj); \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 417 | Py_DECREF(obj); \ |
| 418 | if (res < 0) { \ |
| 419 | goto fail; \ |
| 420 | } \ |
| 421 | } while (0) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 422 | |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 423 | SET_ITEM_INT(_config_init); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 424 | SET_ITEM_INT(parse_argv); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 425 | SET_ITEM_INT(isolated); |
| 426 | SET_ITEM_INT(use_environment); |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 427 | SET_ITEM_INT(configure_locale); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 428 | SET_ITEM_INT(coerce_c_locale); |
| 429 | SET_ITEM_INT(coerce_c_locale_warn); |
| 430 | SET_ITEM_INT(utf8_mode); |
| 431 | #ifdef MS_WINDOWS |
| 432 | SET_ITEM_INT(legacy_windows_fs_encoding); |
| 433 | #endif |
| 434 | SET_ITEM_INT(dev_mode); |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 435 | SET_ITEM_INT(allocator); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 436 | return dict; |
| 437 | |
| 438 | fail: |
| 439 | Py_DECREF(dict); |
| 440 | return NULL; |
| 441 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 442 | #undef SET_ITEM_INT |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 446 | void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 447 | _PyPreConfig_GetConfig(PyPreConfig *preconfig, const PyConfig *config) |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 448 | { |
| 449 | #define COPY_ATTR(ATTR) \ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 450 | if (config->ATTR != -1) { \ |
| 451 | preconfig->ATTR = config->ATTR; \ |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 454 | COPY_ATTR(parse_argv); |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 455 | COPY_ATTR(isolated); |
| 456 | COPY_ATTR(use_environment); |
| 457 | COPY_ATTR(dev_mode); |
| 458 | |
| 459 | #undef COPY_ATTR |
| 460 | } |
| 461 | |
| 462 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 463 | static void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 464 | preconfig_get_global_vars(PyPreConfig *config) |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 465 | { |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 466 | if (config->_config_init != _PyConfig_INIT_COMPAT) { |
| 467 | /* Python and Isolated configuration ignore global variables */ |
| 468 | return; |
| 469 | } |
| 470 | |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 471 | #define COPY_FLAG(ATTR, VALUE) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 472 | if (config->ATTR < 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 473 | config->ATTR = VALUE; \ |
| 474 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 475 | #define COPY_NOT_FLAG(ATTR, VALUE) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 476 | if (config->ATTR < 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 477 | config->ATTR = !(VALUE); \ |
| 478 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 479 | |
| 480 | COPY_FLAG(isolated, Py_IsolatedFlag); |
| 481 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 482 | if (Py_UTF8Mode > 0) { |
| 483 | config->utf8_mode = Py_UTF8Mode; |
| 484 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 485 | #ifdef MS_WINDOWS |
| 486 | COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag); |
| 487 | #endif |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 488 | |
| 489 | #undef COPY_FLAG |
| 490 | #undef COPY_NOT_FLAG |
| 491 | } |
| 492 | |
| 493 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 494 | static void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 495 | preconfig_set_global_vars(const PyPreConfig *config) |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 496 | { |
| 497 | #define COPY_FLAG(ATTR, VAR) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 498 | if (config->ATTR >= 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 499 | VAR = config->ATTR; \ |
| 500 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 501 | #define COPY_NOT_FLAG(ATTR, VAR) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 502 | if (config->ATTR >= 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 503 | VAR = !config->ATTR; \ |
| 504 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 505 | |
| 506 | COPY_FLAG(isolated, Py_IsolatedFlag); |
| 507 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 508 | #ifdef MS_WINDOWS |
| 509 | COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag); |
| 510 | #endif |
| 511 | COPY_FLAG(utf8_mode, Py_UTF8Mode); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 512 | |
| 513 | #undef COPY_FLAG |
| 514 | #undef COPY_NOT_FLAG |
| 515 | } |
| 516 | |
| 517 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 518 | const char* |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 519 | _Py_GetEnv(int use_environment, const char *name) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 520 | { |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 521 | assert(use_environment >= 0); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 522 | |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 523 | if (!use_environment) { |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | const char *var = getenv(name); |
| 528 | if (var && var[0] != '\0') { |
| 529 | return var; |
| 530 | } |
| 531 | else { |
| 532 | return NULL; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | |
| 537 | int |
| 538 | _Py_str_to_int(const char *str, int *result) |
| 539 | { |
| 540 | const char *endptr = str; |
| 541 | errno = 0; |
| 542 | long value = strtol(str, (char **)&endptr, 10); |
| 543 | if (*endptr != '\0' || errno == ERANGE) { |
| 544 | return -1; |
| 545 | } |
| 546 | if (value < INT_MIN || value > INT_MAX) { |
| 547 | return -1; |
| 548 | } |
| 549 | |
| 550 | *result = (int)value; |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | |
| 555 | void |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 556 | _Py_get_env_flag(int use_environment, int *flag, const char *name) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 557 | { |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 558 | const char *var = _Py_GetEnv(use_environment, name); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 559 | if (!var) { |
| 560 | return; |
| 561 | } |
| 562 | int value; |
| 563 | if (_Py_str_to_int(var, &value) < 0 || value < 0) { |
| 564 | /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */ |
| 565 | value = 1; |
| 566 | } |
| 567 | if (*flag < value) { |
| 568 | *flag = value; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | |
| 573 | const wchar_t* |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 574 | _Py_get_xoption(const PyWideStringList *xoptions, const wchar_t *name) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 575 | { |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 576 | for (Py_ssize_t i=0; i < xoptions->length; i++) { |
| 577 | const wchar_t *option = xoptions->items[i]; |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 578 | size_t len; |
| 579 | wchar_t *sep = wcschr(option, L'='); |
| 580 | if (sep != NULL) { |
| 581 | len = (sep - option); |
| 582 | } |
| 583 | else { |
| 584 | len = wcslen(option); |
| 585 | } |
| 586 | if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') { |
| 587 | return option; |
| 588 | } |
| 589 | } |
| 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 594 | static PyStatus |
| 595 | preconfig_init_utf8_mode(PyPreConfig *config, const _PyPreCmdline *cmdline) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 596 | { |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 597 | #ifdef MS_WINDOWS |
| 598 | if (config->legacy_windows_fs_encoding) { |
| 599 | config->utf8_mode = 0; |
| 600 | } |
| 601 | #endif |
| 602 | |
| 603 | if (config->utf8_mode >= 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 604 | return _PyStatus_OK(); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 605 | } |
| 606 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 607 | const wchar_t *xopt; |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 608 | xopt = _Py_get_xoption(&cmdline->xoptions, L"utf8"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 609 | if (xopt) { |
| 610 | wchar_t *sep = wcschr(xopt, L'='); |
| 611 | if (sep) { |
| 612 | xopt = sep + 1; |
| 613 | if (wcscmp(xopt, L"1") == 0) { |
| 614 | config->utf8_mode = 1; |
| 615 | } |
| 616 | else if (wcscmp(xopt, L"0") == 0) { |
| 617 | config->utf8_mode = 0; |
| 618 | } |
| 619 | else { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 620 | return _PyStatus_ERR("invalid -X utf8 option value"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | else { |
| 624 | config->utf8_mode = 1; |
| 625 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 626 | return _PyStatus_OK(); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 629 | const char *opt = _Py_GetEnv(config->use_environment, "PYTHONUTF8"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 630 | if (opt) { |
| 631 | if (strcmp(opt, "1") == 0) { |
| 632 | config->utf8_mode = 1; |
| 633 | } |
| 634 | else if (strcmp(opt, "0") == 0) { |
| 635 | config->utf8_mode = 0; |
| 636 | } |
| 637 | else { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 638 | return _PyStatus_ERR("invalid PYTHONUTF8 environment " |
Victor Stinner | db71975 | 2019-05-01 05:35:33 +0200 | [diff] [blame] | 639 | "variable value"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 640 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 641 | return _PyStatus_OK(); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 644 | |
| 645 | #ifndef MS_WINDOWS |
| 646 | if (config->utf8_mode < 0) { |
| 647 | /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */ |
| 648 | const char *ctype_loc = setlocale(LC_CTYPE, NULL); |
| 649 | if (ctype_loc != NULL |
| 650 | && (strcmp(ctype_loc, "C") == 0 |
| 651 | || strcmp(ctype_loc, "POSIX") == 0)) |
| 652 | { |
| 653 | config->utf8_mode = 1; |
| 654 | } |
| 655 | } |
| 656 | #endif |
| 657 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 658 | if (config->utf8_mode < 0) { |
| 659 | config->utf8_mode = 0; |
| 660 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 661 | return _PyStatus_OK(); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | |
| 665 | static void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 666 | preconfig_init_coerce_c_locale(PyPreConfig *config) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 667 | { |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 668 | if (!config->configure_locale) { |
| 669 | config->coerce_c_locale = 0; |
| 670 | config->coerce_c_locale_warn = 0; |
| 671 | return; |
| 672 | } |
| 673 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 674 | const char *env = _Py_GetEnv(config->use_environment, "PYTHONCOERCECLOCALE"); |
| 675 | if (env) { |
| 676 | if (strcmp(env, "0") == 0) { |
| 677 | if (config->coerce_c_locale < 0) { |
| 678 | config->coerce_c_locale = 0; |
| 679 | } |
| 680 | } |
| 681 | else if (strcmp(env, "warn") == 0) { |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 682 | if (config->coerce_c_locale_warn < 0) { |
| 683 | config->coerce_c_locale_warn = 1; |
| 684 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 685 | } |
| 686 | else { |
| 687 | if (config->coerce_c_locale < 0) { |
| 688 | config->coerce_c_locale = 1; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /* Test if coerce_c_locale equals to -1 or equals to 1: |
| 694 | PYTHONCOERCECLOCALE=1 doesn't imply that the C locale is always coerced. |
| 695 | It is only coerced if if the LC_CTYPE locale is "C". */ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 696 | if (config->coerce_c_locale < 0 || config->coerce_c_locale == 1) { |
| 697 | /* The C locale enables the C locale coercion (PEP 538) */ |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 698 | if (_Py_LegacyLocaleDetected(0)) { |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 699 | config->coerce_c_locale = 2; |
| 700 | } |
| 701 | else { |
| 702 | config->coerce_c_locale = 0; |
| 703 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 706 | if (config->coerce_c_locale_warn < 0) { |
| 707 | config->coerce_c_locale_warn = 0; |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 708 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 709 | } |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 710 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 711 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 712 | static PyStatus |
| 713 | preconfig_init_allocator(PyPreConfig *config) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 714 | { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 715 | if (config->allocator == PYMEM_ALLOCATOR_NOT_SET) { |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 716 | /* bpo-34247. The PYTHONMALLOC environment variable has the priority |
| 717 | over PYTHONDEV env var and "-X dev" command line option. |
| 718 | For example, PYTHONMALLOC=malloc PYTHONDEVMODE=1 sets the memory |
| 719 | allocators to "malloc" (and not to "debug"). */ |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 720 | const char *envvar = _Py_GetEnv(config->use_environment, "PYTHONMALLOC"); |
| 721 | if (envvar) { |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 722 | PyMemAllocatorName name; |
| 723 | if (_PyMem_GetAllocatorName(envvar, &name) < 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 724 | return _PyStatus_ERR("PYTHONMALLOC: unknown allocator"); |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 725 | } |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 726 | config->allocator = (int)name; |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 727 | } |
| 728 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 729 | |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 730 | if (config->dev_mode && config->allocator == PYMEM_ALLOCATOR_NOT_SET) { |
| 731 | config->allocator = PYMEM_ALLOCATOR_DEBUG; |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 732 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 733 | return _PyStatus_OK(); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 737 | static PyStatus |
| 738 | preconfig_read(PyPreConfig *config, _PyPreCmdline *cmdline) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 739 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 740 | PyStatus status; |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 741 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 742 | status = _PyPreCmdline_Read(cmdline, config); |
| 743 | if (_PyStatus_EXCEPTION(status)) { |
| 744 | return status; |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 745 | } |
| 746 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 747 | precmdline_set_preconfig(cmdline, config); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 748 | |
| 749 | /* legacy_windows_fs_encoding, coerce_c_locale, utf8_mode */ |
| 750 | #ifdef MS_WINDOWS |
| 751 | _Py_get_env_flag(config->use_environment, |
| 752 | &config->legacy_windows_fs_encoding, |
| 753 | "PYTHONLEGACYWINDOWSFSENCODING"); |
| 754 | #endif |
| 755 | |
| 756 | preconfig_init_coerce_c_locale(config); |
| 757 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 758 | status = preconfig_init_utf8_mode(config, cmdline); |
| 759 | if (_PyStatus_EXCEPTION(status)) { |
| 760 | return status; |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /* allocator */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 764 | status = preconfig_init_allocator(config); |
| 765 | if (_PyStatus_EXCEPTION(status)) { |
| 766 | return status; |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 767 | } |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 768 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 769 | assert(config->coerce_c_locale >= 0); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 770 | assert(config->coerce_c_locale_warn >= 0); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 771 | #ifdef MS_WINDOWS |
| 772 | assert(config->legacy_windows_fs_encoding >= 0); |
| 773 | #endif |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 774 | assert(config->utf8_mode >= 0); |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 775 | assert(config->isolated >= 0); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 776 | assert(config->use_environment >= 0); |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 777 | assert(config->dev_mode >= 0); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 778 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 779 | return _PyStatus_OK(); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 783 | /* Read the configuration from: |
| 784 | |
| 785 | - command line arguments |
| 786 | - environment variables |
| 787 | - Py_xxx global configuration variables |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 788 | - the LC_CTYPE locale */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 789 | PyStatus |
| 790 | _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 791 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 792 | PyStatus status; |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 793 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 794 | status = _PyRuntime_Initialize(); |
| 795 | if (_PyStatus_EXCEPTION(status)) { |
| 796 | return status; |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 799 | preconfig_get_global_vars(config); |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 800 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 801 | /* Copy LC_CTYPE locale, since it's modified later */ |
| 802 | const char *loc = setlocale(LC_CTYPE, NULL); |
| 803 | if (loc == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 804 | return _PyStatus_ERR("failed to LC_CTYPE locale"); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 805 | } |
| 806 | char *init_ctype_locale = _PyMem_RawStrdup(loc); |
| 807 | if (init_ctype_locale == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 808 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /* Save the config to be able to restore it if encodings change */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 812 | PyPreConfig save_config; |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 813 | |
| 814 | status = _PyPreConfig_InitFromPreConfig(&save_config, config); |
| 815 | if (_PyStatus_EXCEPTION(status)) { |
| 816 | return status; |
| 817 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 818 | |
| 819 | /* Set LC_CTYPE to the user preferred locale */ |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 820 | if (config->configure_locale) { |
| 821 | _Py_SetLocaleFromEnv(LC_CTYPE); |
| 822 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 823 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 824 | _PyPreCmdline cmdline = _PyPreCmdline_INIT; |
Victor Stinner | 6a8c313 | 2019-04-05 11:44:04 +0200 | [diff] [blame] | 825 | int init_utf8_mode = Py_UTF8Mode; |
| 826 | #ifdef MS_WINDOWS |
| 827 | int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag; |
| 828 | #endif |
| 829 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 830 | int locale_coerced = 0; |
| 831 | int loops = 0; |
| 832 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 833 | while (1) { |
| 834 | int utf8_mode = config->utf8_mode; |
| 835 | |
| 836 | /* Watchdog to prevent an infinite loop */ |
| 837 | loops++; |
| 838 | if (loops == 3) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 839 | status = _PyStatus_ERR("Encoding changed twice while " |
Victor Stinner | 8eb4aea | 2020-06-10 19:33:11 +0200 | [diff] [blame] | 840 | "reading the configuration"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 841 | goto done; |
| 842 | } |
| 843 | |
| 844 | /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend |
| 845 | on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */ |
| 846 | Py_UTF8Mode = config->utf8_mode; |
| 847 | #ifdef MS_WINDOWS |
| 848 | Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding; |
| 849 | #endif |
| 850 | |
Victor Stinner | 8eb4aea | 2020-06-10 19:33:11 +0200 | [diff] [blame] | 851 | if (args) { |
| 852 | // Set command line arguments at each iteration. If they are bytes |
| 853 | // strings, they are decoded from the new encoding. |
| 854 | status = _PyPreCmdline_SetArgv(&cmdline, args); |
| 855 | if (_PyStatus_EXCEPTION(status)) { |
| 856 | goto done; |
| 857 | } |
| 858 | } |
| 859 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 860 | status = preconfig_read(config, &cmdline); |
| 861 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 862 | goto done; |
| 863 | } |
| 864 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 865 | /* The legacy C locale assumes ASCII as the default text encoding, which |
| 866 | * causes problems not only for the CPython runtime, but also other |
| 867 | * components like GNU readline. |
| 868 | * |
| 869 | * Accordingly, when the CLI detects it, it attempts to coerce it to a |
| 870 | * more capable UTF-8 based alternative. |
| 871 | * |
| 872 | * See the documentation of the PYTHONCOERCECLOCALE setting for more |
| 873 | * details. |
| 874 | */ |
| 875 | int encoding_changed = 0; |
| 876 | if (config->coerce_c_locale && !locale_coerced) { |
| 877 | locale_coerced = 1; |
| 878 | _Py_CoerceLegacyLocale(0); |
| 879 | encoding_changed = 1; |
| 880 | } |
| 881 | |
| 882 | if (utf8_mode == -1) { |
| 883 | if (config->utf8_mode == 1) { |
| 884 | /* UTF-8 Mode enabled */ |
| 885 | encoding_changed = 1; |
| 886 | } |
| 887 | } |
| 888 | else { |
| 889 | if (config->utf8_mode != utf8_mode) { |
| 890 | encoding_changed = 1; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | if (!encoding_changed) { |
| 895 | break; |
| 896 | } |
| 897 | |
| 898 | /* Reset the configuration before reading again the configuration, |
Victor Stinner | 8eb4aea | 2020-06-10 19:33:11 +0200 | [diff] [blame] | 899 | just keep UTF-8 Mode and coerce C locale value. */ |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 900 | int new_utf8_mode = config->utf8_mode; |
| 901 | int new_coerce_c_locale = config->coerce_c_locale; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 902 | preconfig_copy(config, &save_config); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 903 | config->utf8_mode = new_utf8_mode; |
| 904 | config->coerce_c_locale = new_coerce_c_locale; |
| 905 | |
| 906 | /* The encoding changed: read again the configuration |
| 907 | with the new encoding */ |
| 908 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 909 | status = _PyStatus_OK(); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 910 | |
| 911 | done: |
| 912 | if (init_ctype_locale != NULL) { |
| 913 | setlocale(LC_CTYPE, init_ctype_locale); |
Victor Stinner | c183444 | 2019-03-18 22:24:28 +0100 | [diff] [blame] | 914 | PyMem_RawFree(init_ctype_locale); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 915 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 916 | Py_UTF8Mode = init_utf8_mode ; |
| 917 | #ifdef MS_WINDOWS |
| 918 | Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding; |
| 919 | #endif |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 920 | _PyPreCmdline_Clear(&cmdline); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 921 | return status; |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 925 | /* Write the pre-configuration: |
| 926 | |
| 927 | - set the memory allocators |
| 928 | - set Py_xxx global configuration variables |
| 929 | - set the LC_CTYPE locale (coerce C locale, PEP 538) and set the UTF-8 mode |
| 930 | (PEP 540) |
Victor Stinner | c656e25 | 2019-03-06 01:13:43 +0100 | [diff] [blame] | 931 | |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 932 | The applied configuration is written into _PyRuntime.preconfig. |
| 933 | If the C locale cannot be coerced, set coerce_c_locale to 0. |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 934 | |
| 935 | Do nothing if called after Py_Initialize(): ignore the new |
| 936 | pre-configuration. */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 937 | PyStatus |
| 938 | _PyPreConfig_Write(const PyPreConfig *src_config) |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 939 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 940 | PyPreConfig config; |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 941 | |
| 942 | PyStatus status = _PyPreConfig_InitFromPreConfig(&config, src_config); |
| 943 | if (_PyStatus_EXCEPTION(status)) { |
| 944 | return status; |
| 945 | } |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 946 | |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 947 | if (_PyRuntime.core_initialized) { |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 948 | /* bpo-34008: Calling this functions after Py_Initialize() ignores |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 949 | the new configuration. */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 950 | return _PyStatus_OK(); |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 951 | } |
| 952 | |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 953 | PyMemAllocatorName name = (PyMemAllocatorName)config.allocator; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 954 | if (name != PYMEM_ALLOCATOR_NOT_SET) { |
| 955 | if (_PyMem_SetupAllocators(name) < 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 956 | return _PyStatus_ERR("Unknown PYTHONMALLOC allocator"); |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 960 | preconfig_set_global_vars(&config); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 961 | |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 962 | if (config.configure_locale) { |
| 963 | if (config.coerce_c_locale) { |
| 964 | if (!_Py_CoerceLegacyLocale(config.coerce_c_locale_warn)) { |
| 965 | /* C locale not coerced */ |
| 966 | config.coerce_c_locale = 0; |
| 967 | } |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 968 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 969 | |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 970 | /* Set LC_CTYPE to the user preferred locale */ |
| 971 | _Py_SetLocaleFromEnv(LC_CTYPE); |
| 972 | } |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 973 | |
Victor Stinner | 6d5ee97 | 2019-03-23 12:05:43 +0100 | [diff] [blame] | 974 | /* Write the new pre-configuration into _PyRuntime */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 975 | preconfig_copy(&_PyRuntime.preconfig, &config); |
Victor Stinner | 6d5ee97 | 2019-03-23 12:05:43 +0100 | [diff] [blame] | 976 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 977 | return _PyStatus_OK(); |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 978 | } |