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