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; |
| 98 | wargv.items = args->wchar_argv; |
| 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 |
| 220 | _PyPreCmdline_Read(_PyPreCmdline *cmdline, |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 221 | const _PyPreConfig *preconfig) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 222 | { |
| 223 | if (preconfig) { |
| 224 | _PyPreCmdline_GetPreConfig(cmdline, preconfig); |
| 225 | } |
| 226 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 227 | _PyInitError err = precmdline_parse_cmdline(cmdline); |
| 228 | if (_Py_INIT_FAILED(err)) { |
| 229 | return err; |
| 230 | } |
| 231 | |
| 232 | /* isolated, use_environment */ |
| 233 | if (cmdline->isolated < 0) { |
| 234 | cmdline->isolated = 0; |
| 235 | } |
| 236 | if (cmdline->isolated > 0) { |
| 237 | cmdline->use_environment = 0; |
| 238 | } |
| 239 | if (cmdline->use_environment < 0) { |
| 240 | cmdline->use_environment = 0; |
| 241 | } |
| 242 | |
| 243 | /* dev_mode */ |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 244 | if ((cmdline->dev_mode < 0) |
| 245 | && (_Py_get_xoption(&cmdline->xoptions, L"dev") |
| 246 | || _Py_GetEnv(cmdline->use_environment, "PYTHONDEVMODE"))) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 247 | { |
| 248 | cmdline->dev_mode = 1; |
| 249 | } |
| 250 | if (cmdline->dev_mode < 0) { |
| 251 | cmdline->dev_mode = 0; |
| 252 | } |
| 253 | |
| 254 | assert(cmdline->use_environment >= 0); |
| 255 | assert(cmdline->isolated >= 0); |
| 256 | assert(cmdline->dev_mode >= 0); |
| 257 | |
| 258 | return _Py_INIT_OK(); |
| 259 | } |
| 260 | |
| 261 | |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 262 | /* --- _PyPreConfig ----------------------------------------------- */ |
| 263 | |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 264 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 265 | void |
| 266 | _PyPreConfig_Init(_PyPreConfig *config) |
| 267 | { |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 268 | memset(config, 0, sizeof(*config)); |
| 269 | |
| 270 | config->_config_version = _Py_CONFIG_VERSION; |
| 271 | config->isolated = -1; |
| 272 | config->use_environment = -1; |
| 273 | config->configure_locale = 1; |
| 274 | config->utf8_mode = -2; |
| 275 | config->dev_mode = -1; |
| 276 | config->allocator = PYMEM_ALLOCATOR_NOT_SET; |
| 277 | #ifdef MS_WINDOWS |
| 278 | config->legacy_windows_fs_encoding = -1; |
| 279 | #endif |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | |
| 283 | void |
| 284 | _PyPreConfig_InitPythonConfig(_PyPreConfig *config) |
| 285 | { |
| 286 | _PyPreConfig_Init(config); |
| 287 | |
| 288 | /* Set to -1 to enable C locale coercion (PEP 538) and UTF-8 Mode (PEP 540) |
| 289 | depending on the LC_CTYPE locale, PYTHONUTF8 and PYTHONCOERCECLOCALE |
| 290 | environment variables. */ |
| 291 | config->coerce_c_locale = -1; |
| 292 | config->coerce_c_locale_warn = -1; |
| 293 | config->utf8_mode = -1; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | void |
| 298 | _PyPreConfig_InitIsolatedConfig(_PyPreConfig *config) |
| 299 | { |
| 300 | _PyPreConfig_Init(config); |
| 301 | |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 302 | config->configure_locale = 0; |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 303 | config->isolated = 1; |
| 304 | config->use_environment = 0; |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 305 | config->utf8_mode = 0; |
| 306 | config->dev_mode = 0; |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 307 | #ifdef MS_WINDOWS |
| 308 | config->legacy_windows_fs_encoding = 0; |
| 309 | #endif |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
Victor Stinner | b594784 | 2019-05-18 00:38:16 +0200 | [diff] [blame] | 313 | void |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 314 | _PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2) |
| 315 | { |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 316 | #define COPY_ATTR(ATTR) config->ATTR = config2->ATTR |
| 317 | |
| 318 | COPY_ATTR(isolated); |
| 319 | COPY_ATTR(use_environment); |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 320 | COPY_ATTR(configure_locale); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 321 | COPY_ATTR(dev_mode); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 322 | COPY_ATTR(coerce_c_locale); |
| 323 | COPY_ATTR(coerce_c_locale_warn); |
Victor Stinner | b594784 | 2019-05-18 00:38:16 +0200 | [diff] [blame] | 324 | COPY_ATTR(utf8_mode); |
| 325 | COPY_ATTR(allocator); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 326 | #ifdef MS_WINDOWS |
| 327 | COPY_ATTR(legacy_windows_fs_encoding); |
| 328 | #endif |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 329 | |
| 330 | #undef COPY_ATTR |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 334 | PyObject* |
| 335 | _PyPreConfig_AsDict(const _PyPreConfig *config) |
| 336 | { |
| 337 | PyObject *dict; |
| 338 | |
| 339 | dict = PyDict_New(); |
| 340 | if (dict == NULL) { |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | #define SET_ITEM(KEY, EXPR) \ |
| 345 | do { \ |
| 346 | PyObject *obj = (EXPR); \ |
| 347 | if (obj == NULL) { \ |
| 348 | goto fail; \ |
| 349 | } \ |
| 350 | int res = PyDict_SetItemString(dict, (KEY), obj); \ |
| 351 | Py_DECREF(obj); \ |
| 352 | if (res < 0) { \ |
| 353 | goto fail; \ |
| 354 | } \ |
| 355 | } while (0) |
| 356 | #define SET_ITEM_INT(ATTR) \ |
| 357 | SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR)) |
| 358 | #define FROM_STRING(STR) \ |
| 359 | ((STR != NULL) ? \ |
| 360 | PyUnicode_FromString(STR) \ |
| 361 | : (Py_INCREF(Py_None), Py_None)) |
| 362 | #define SET_ITEM_STR(ATTR) \ |
| 363 | SET_ITEM(#ATTR, FROM_STRING(config->ATTR)) |
| 364 | |
| 365 | SET_ITEM_INT(isolated); |
| 366 | SET_ITEM_INT(use_environment); |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 367 | SET_ITEM_INT(configure_locale); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 368 | SET_ITEM_INT(coerce_c_locale); |
| 369 | SET_ITEM_INT(coerce_c_locale_warn); |
| 370 | SET_ITEM_INT(utf8_mode); |
| 371 | #ifdef MS_WINDOWS |
| 372 | SET_ITEM_INT(legacy_windows_fs_encoding); |
| 373 | #endif |
| 374 | SET_ITEM_INT(dev_mode); |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 375 | SET_ITEM_INT(allocator); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 376 | return dict; |
| 377 | |
| 378 | fail: |
| 379 | Py_DECREF(dict); |
| 380 | return NULL; |
| 381 | |
| 382 | #undef FROM_STRING |
| 383 | #undef SET_ITEM |
| 384 | #undef SET_ITEM_INT |
| 385 | #undef SET_ITEM_STR |
| 386 | } |
| 387 | |
| 388 | |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 389 | void |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 390 | _PyPreConfig_GetCoreConfig(_PyPreConfig *config, |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 391 | const _PyCoreConfig *core_config) |
| 392 | { |
| 393 | #define COPY_ATTR(ATTR) \ |
| 394 | if (core_config->ATTR != -1) { \ |
| 395 | config->ATTR = core_config->ATTR; \ |
| 396 | } |
| 397 | |
| 398 | COPY_ATTR(isolated); |
| 399 | COPY_ATTR(use_environment); |
| 400 | COPY_ATTR(dev_mode); |
| 401 | |
| 402 | #undef COPY_ATTR |
| 403 | } |
| 404 | |
| 405 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 406 | static void |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 407 | _PyPreConfig_GetGlobalConfig(_PyPreConfig *config) |
| 408 | { |
| 409 | #define COPY_FLAG(ATTR, VALUE) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 410 | if (config->ATTR < 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 411 | config->ATTR = VALUE; \ |
| 412 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 413 | #define COPY_NOT_FLAG(ATTR, VALUE) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 414 | if (config->ATTR < 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 415 | config->ATTR = !(VALUE); \ |
| 416 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 417 | |
| 418 | COPY_FLAG(isolated, Py_IsolatedFlag); |
| 419 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 420 | #ifdef MS_WINDOWS |
| 421 | COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag); |
| 422 | #endif |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 423 | if (config->utf8_mode == -2) { |
| 424 | config->utf8_mode = Py_UTF8Mode; |
Victor Stinner | d929f18 | 2019-03-27 18:28:46 +0100 | [diff] [blame] | 425 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 426 | |
| 427 | #undef COPY_FLAG |
| 428 | #undef COPY_NOT_FLAG |
| 429 | } |
| 430 | |
| 431 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 432 | static void |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 433 | _PyPreConfig_SetGlobalConfig(const _PyPreConfig *config) |
| 434 | { |
| 435 | #define COPY_FLAG(ATTR, VAR) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 436 | if (config->ATTR >= 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 437 | VAR = config->ATTR; \ |
| 438 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 439 | #define COPY_NOT_FLAG(ATTR, VAR) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 440 | if (config->ATTR >= 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 441 | VAR = !config->ATTR; \ |
| 442 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 443 | |
| 444 | COPY_FLAG(isolated, Py_IsolatedFlag); |
| 445 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 446 | #ifdef MS_WINDOWS |
| 447 | COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag); |
| 448 | #endif |
| 449 | COPY_FLAG(utf8_mode, Py_UTF8Mode); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 450 | |
| 451 | #undef COPY_FLAG |
| 452 | #undef COPY_NOT_FLAG |
| 453 | } |
| 454 | |
| 455 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 456 | const char* |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 457 | _Py_GetEnv(int use_environment, const char *name) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 458 | { |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 459 | assert(use_environment >= 0); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 460 | |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 461 | if (!use_environment) { |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | const char *var = getenv(name); |
| 466 | if (var && var[0] != '\0') { |
| 467 | return var; |
| 468 | } |
| 469 | else { |
| 470 | return NULL; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | |
| 475 | int |
| 476 | _Py_str_to_int(const char *str, int *result) |
| 477 | { |
| 478 | const char *endptr = str; |
| 479 | errno = 0; |
| 480 | long value = strtol(str, (char **)&endptr, 10); |
| 481 | if (*endptr != '\0' || errno == ERANGE) { |
| 482 | return -1; |
| 483 | } |
| 484 | if (value < INT_MIN || value > INT_MAX) { |
| 485 | return -1; |
| 486 | } |
| 487 | |
| 488 | *result = (int)value; |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | void |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 494 | _Py_get_env_flag(int use_environment, int *flag, const char *name) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 495 | { |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 496 | const char *var = _Py_GetEnv(use_environment, name); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 497 | if (!var) { |
| 498 | return; |
| 499 | } |
| 500 | int value; |
| 501 | if (_Py_str_to_int(var, &value) < 0 || value < 0) { |
| 502 | /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */ |
| 503 | value = 1; |
| 504 | } |
| 505 | if (*flag < value) { |
| 506 | *flag = value; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | |
| 511 | const wchar_t* |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 512 | _Py_get_xoption(const _PyWstrList *xoptions, const wchar_t *name) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 513 | { |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 514 | for (Py_ssize_t i=0; i < xoptions->length; i++) { |
| 515 | const wchar_t *option = xoptions->items[i]; |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 516 | size_t len; |
| 517 | wchar_t *sep = wcschr(option, L'='); |
| 518 | if (sep != NULL) { |
| 519 | len = (sep - option); |
| 520 | } |
| 521 | else { |
| 522 | len = wcslen(option); |
| 523 | } |
| 524 | if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') { |
| 525 | return option; |
| 526 | } |
| 527 | } |
| 528 | return NULL; |
| 529 | } |
| 530 | |
| 531 | |
| 532 | static _PyInitError |
| 533 | preconfig_init_utf8_mode(_PyPreConfig *config, const _PyPreCmdline *cmdline) |
| 534 | { |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 535 | #ifdef MS_WINDOWS |
| 536 | if (config->legacy_windows_fs_encoding) { |
| 537 | config->utf8_mode = 0; |
| 538 | } |
| 539 | #endif |
| 540 | |
| 541 | if (config->utf8_mode >= 0) { |
| 542 | return _Py_INIT_OK(); |
| 543 | } |
| 544 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 545 | const wchar_t *xopt; |
| 546 | if (cmdline) { |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 547 | xopt = _Py_get_xoption(&cmdline->xoptions, L"utf8"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 548 | } |
| 549 | else { |
| 550 | xopt = NULL; |
| 551 | } |
| 552 | if (xopt) { |
| 553 | wchar_t *sep = wcschr(xopt, L'='); |
| 554 | if (sep) { |
| 555 | xopt = sep + 1; |
| 556 | if (wcscmp(xopt, L"1") == 0) { |
| 557 | config->utf8_mode = 1; |
| 558 | } |
| 559 | else if (wcscmp(xopt, L"0") == 0) { |
| 560 | config->utf8_mode = 0; |
| 561 | } |
| 562 | else { |
Victor Stinner | db71975 | 2019-05-01 05:35:33 +0200 | [diff] [blame] | 563 | return _Py_INIT_ERR("invalid -X utf8 option value"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | else { |
| 567 | config->utf8_mode = 1; |
| 568 | } |
| 569 | return _Py_INIT_OK(); |
| 570 | } |
| 571 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 572 | const char *opt = _Py_GetEnv(config->use_environment, "PYTHONUTF8"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 573 | if (opt) { |
| 574 | if (strcmp(opt, "1") == 0) { |
| 575 | config->utf8_mode = 1; |
| 576 | } |
| 577 | else if (strcmp(opt, "0") == 0) { |
| 578 | config->utf8_mode = 0; |
| 579 | } |
| 580 | else { |
Victor Stinner | db71975 | 2019-05-01 05:35:33 +0200 | [diff] [blame] | 581 | return _Py_INIT_ERR("invalid PYTHONUTF8 environment " |
| 582 | "variable value"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 583 | } |
| 584 | return _Py_INIT_OK(); |
| 585 | } |
| 586 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 587 | |
| 588 | #ifndef MS_WINDOWS |
| 589 | if (config->utf8_mode < 0) { |
| 590 | /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */ |
| 591 | const char *ctype_loc = setlocale(LC_CTYPE, NULL); |
| 592 | if (ctype_loc != NULL |
| 593 | && (strcmp(ctype_loc, "C") == 0 |
| 594 | || strcmp(ctype_loc, "POSIX") == 0)) |
| 595 | { |
| 596 | config->utf8_mode = 1; |
| 597 | } |
| 598 | } |
| 599 | #endif |
| 600 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 601 | if (config->utf8_mode < 0) { |
| 602 | config->utf8_mode = 0; |
| 603 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 604 | return _Py_INIT_OK(); |
| 605 | } |
| 606 | |
| 607 | |
| 608 | static void |
| 609 | preconfig_init_coerce_c_locale(_PyPreConfig *config) |
| 610 | { |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 611 | if (!config->configure_locale) { |
| 612 | config->coerce_c_locale = 0; |
| 613 | config->coerce_c_locale_warn = 0; |
| 614 | return; |
| 615 | } |
| 616 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 617 | const char *env = _Py_GetEnv(config->use_environment, "PYTHONCOERCECLOCALE"); |
| 618 | if (env) { |
| 619 | if (strcmp(env, "0") == 0) { |
| 620 | if (config->coerce_c_locale < 0) { |
| 621 | config->coerce_c_locale = 0; |
| 622 | } |
| 623 | } |
| 624 | else if (strcmp(env, "warn") == 0) { |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 625 | if (config->coerce_c_locale_warn < 0) { |
| 626 | config->coerce_c_locale_warn = 1; |
| 627 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 628 | } |
| 629 | else { |
| 630 | if (config->coerce_c_locale < 0) { |
| 631 | config->coerce_c_locale = 1; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /* Test if coerce_c_locale equals to -1 or equals to 1: |
| 637 | PYTHONCOERCECLOCALE=1 doesn't imply that the C locale is always coerced. |
| 638 | It is only coerced if if the LC_CTYPE locale is "C". */ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 639 | if (config->coerce_c_locale < 0 || config->coerce_c_locale == 1) { |
| 640 | /* The C locale enables the C locale coercion (PEP 538) */ |
| 641 | if (_Py_LegacyLocaleDetected()) { |
| 642 | config->coerce_c_locale = 2; |
| 643 | } |
| 644 | else { |
| 645 | config->coerce_c_locale = 0; |
| 646 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 647 | } |
| 648 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 649 | if (config->coerce_c_locale_warn < 0) { |
| 650 | config->coerce_c_locale_warn = 0; |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 651 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 652 | } |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 653 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 654 | |
| 655 | static _PyInitError |
| 656 | preconfig_init_allocator(_PyPreConfig *config) |
| 657 | { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 658 | if (config->allocator == PYMEM_ALLOCATOR_NOT_SET) { |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 659 | /* bpo-34247. The PYTHONMALLOC environment variable has the priority |
| 660 | over PYTHONDEV env var and "-X dev" command line option. |
| 661 | For example, PYTHONMALLOC=malloc PYTHONDEVMODE=1 sets the memory |
| 662 | allocators to "malloc" (and not to "debug"). */ |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 663 | const char *envvar = _Py_GetEnv(config->use_environment, "PYTHONMALLOC"); |
| 664 | if (envvar) { |
| 665 | if (_PyMem_GetAllocatorName(envvar, &config->allocator) < 0) { |
| 666 | return _Py_INIT_ERR("PYTHONMALLOC: unknown allocator"); |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 670 | |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 671 | if (config->dev_mode && config->allocator == PYMEM_ALLOCATOR_NOT_SET) { |
| 672 | config->allocator = PYMEM_ALLOCATOR_DEBUG; |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 673 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 674 | return _Py_INIT_OK(); |
| 675 | } |
| 676 | |
| 677 | |
| 678 | static _PyInitError |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 679 | preconfig_read(_PyPreConfig *config, _PyPreCmdline *cmdline) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 680 | { |
| 681 | _PyInitError err; |
| 682 | |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 683 | err = _PyPreCmdline_Read(cmdline, config); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 684 | if (_Py_INIT_FAILED(err)) { |
| 685 | return err; |
| 686 | } |
| 687 | |
| 688 | _PyPreCmdline_SetPreConfig(cmdline, config); |
| 689 | |
| 690 | /* legacy_windows_fs_encoding, coerce_c_locale, utf8_mode */ |
| 691 | #ifdef MS_WINDOWS |
| 692 | _Py_get_env_flag(config->use_environment, |
| 693 | &config->legacy_windows_fs_encoding, |
| 694 | "PYTHONLEGACYWINDOWSFSENCODING"); |
| 695 | #endif |
| 696 | |
| 697 | preconfig_init_coerce_c_locale(config); |
| 698 | |
| 699 | err = preconfig_init_utf8_mode(config, cmdline); |
| 700 | if (_Py_INIT_FAILED(err)) { |
| 701 | return err; |
| 702 | } |
| 703 | |
| 704 | /* allocator */ |
| 705 | err = preconfig_init_allocator(config); |
| 706 | if (_Py_INIT_FAILED(err)) { |
| 707 | return err; |
| 708 | } |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 709 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 710 | assert(config->coerce_c_locale >= 0); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 711 | assert(config->coerce_c_locale_warn >= 0); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 712 | #ifdef MS_WINDOWS |
| 713 | assert(config->legacy_windows_fs_encoding >= 0); |
| 714 | #endif |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 715 | assert(config->utf8_mode >= 0); |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 716 | assert(config->isolated >= 0); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 717 | assert(config->use_environment >= 0); |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 718 | assert(config->dev_mode >= 0); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 719 | |
| 720 | return _Py_INIT_OK(); |
| 721 | } |
| 722 | |
| 723 | |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 724 | /* Read the configuration from: |
| 725 | |
| 726 | - command line arguments |
| 727 | - environment variables |
| 728 | - Py_xxx global configuration variables |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 729 | - the LC_CTYPE locale */ |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 730 | _PyInitError |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 731 | _PyPreConfig_Read(_PyPreConfig *config, const _PyArgv *args) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 732 | { |
| 733 | _PyInitError err; |
| 734 | |
| 735 | err = _PyRuntime_Initialize(); |
| 736 | if (_Py_INIT_FAILED(err)) { |
| 737 | return err; |
| 738 | } |
| 739 | |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 740 | _PyPreConfig_GetGlobalConfig(config); |
| 741 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 742 | /* Copy LC_CTYPE locale, since it's modified later */ |
| 743 | const char *loc = setlocale(LC_CTYPE, NULL); |
| 744 | if (loc == NULL) { |
| 745 | return _Py_INIT_ERR("failed to LC_CTYPE locale"); |
| 746 | } |
| 747 | char *init_ctype_locale = _PyMem_RawStrdup(loc); |
| 748 | if (init_ctype_locale == NULL) { |
| 749 | return _Py_INIT_NO_MEMORY(); |
| 750 | } |
| 751 | |
| 752 | /* Save the config to be able to restore it if encodings change */ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 753 | _PyPreConfig save_config; |
| 754 | _PyPreConfig_Init(&save_config); |
Victor Stinner | b594784 | 2019-05-18 00:38:16 +0200 | [diff] [blame] | 755 | _PyPreConfig_Copy(&save_config, config); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 756 | |
| 757 | /* Set LC_CTYPE to the user preferred locale */ |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 758 | if (config->configure_locale) { |
| 759 | _Py_SetLocaleFromEnv(LC_CTYPE); |
| 760 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 761 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 762 | _PyPreCmdline cmdline = _PyPreCmdline_INIT; |
Victor Stinner | 6a8c313 | 2019-04-05 11:44:04 +0200 | [diff] [blame] | 763 | int init_utf8_mode = Py_UTF8Mode; |
| 764 | #ifdef MS_WINDOWS |
| 765 | int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag; |
| 766 | #endif |
| 767 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 768 | if (args) { |
| 769 | err = _PyPreCmdline_SetArgv(&cmdline, args); |
| 770 | if (_Py_INIT_FAILED(err)) { |
| 771 | goto done; |
| 772 | } |
| 773 | } |
| 774 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 775 | int locale_coerced = 0; |
| 776 | int loops = 0; |
| 777 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 778 | while (1) { |
| 779 | int utf8_mode = config->utf8_mode; |
| 780 | |
| 781 | /* Watchdog to prevent an infinite loop */ |
| 782 | loops++; |
| 783 | if (loops == 3) { |
| 784 | err = _Py_INIT_ERR("Encoding changed twice while " |
| 785 | "reading the configuration"); |
| 786 | goto done; |
| 787 | } |
| 788 | |
| 789 | /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend |
| 790 | on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */ |
| 791 | Py_UTF8Mode = config->utf8_mode; |
| 792 | #ifdef MS_WINDOWS |
| 793 | Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding; |
| 794 | #endif |
| 795 | |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 796 | err = preconfig_read(config, &cmdline); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 797 | if (_Py_INIT_FAILED(err)) { |
| 798 | goto done; |
| 799 | } |
| 800 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 801 | /* The legacy C locale assumes ASCII as the default text encoding, which |
| 802 | * causes problems not only for the CPython runtime, but also other |
| 803 | * components like GNU readline. |
| 804 | * |
| 805 | * Accordingly, when the CLI detects it, it attempts to coerce it to a |
| 806 | * more capable UTF-8 based alternative. |
| 807 | * |
| 808 | * See the documentation of the PYTHONCOERCECLOCALE setting for more |
| 809 | * details. |
| 810 | */ |
| 811 | int encoding_changed = 0; |
| 812 | if (config->coerce_c_locale && !locale_coerced) { |
| 813 | locale_coerced = 1; |
| 814 | _Py_CoerceLegacyLocale(0); |
| 815 | encoding_changed = 1; |
| 816 | } |
| 817 | |
| 818 | if (utf8_mode == -1) { |
| 819 | if (config->utf8_mode == 1) { |
| 820 | /* UTF-8 Mode enabled */ |
| 821 | encoding_changed = 1; |
| 822 | } |
| 823 | } |
| 824 | else { |
| 825 | if (config->utf8_mode != utf8_mode) { |
| 826 | encoding_changed = 1; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | if (!encoding_changed) { |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | /* Reset the configuration before reading again the configuration, |
| 835 | just keep UTF-8 Mode value. */ |
| 836 | int new_utf8_mode = config->utf8_mode; |
| 837 | int new_coerce_c_locale = config->coerce_c_locale; |
Victor Stinner | b594784 | 2019-05-18 00:38:16 +0200 | [diff] [blame] | 838 | _PyPreConfig_Copy(config, &save_config); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 839 | config->utf8_mode = new_utf8_mode; |
| 840 | config->coerce_c_locale = new_coerce_c_locale; |
| 841 | |
| 842 | /* The encoding changed: read again the configuration |
| 843 | with the new encoding */ |
| 844 | } |
| 845 | err = _Py_INIT_OK(); |
| 846 | |
| 847 | done: |
| 848 | if (init_ctype_locale != NULL) { |
| 849 | setlocale(LC_CTYPE, init_ctype_locale); |
Victor Stinner | c183444 | 2019-03-18 22:24:28 +0100 | [diff] [blame] | 850 | PyMem_RawFree(init_ctype_locale); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 851 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 852 | Py_UTF8Mode = init_utf8_mode ; |
| 853 | #ifdef MS_WINDOWS |
| 854 | Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding; |
| 855 | #endif |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 856 | _PyPreCmdline_Clear(&cmdline); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 857 | return err; |
| 858 | } |
| 859 | |
| 860 | |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 861 | /* Write the pre-configuration: |
| 862 | |
| 863 | - set the memory allocators |
| 864 | - set Py_xxx global configuration variables |
| 865 | - set the LC_CTYPE locale (coerce C locale, PEP 538) and set the UTF-8 mode |
| 866 | (PEP 540) |
Victor Stinner | c656e25 | 2019-03-06 01:13:43 +0100 | [diff] [blame] | 867 | |
| 868 | If the memory allocator is changed, config is re-allocated with new |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 869 | allocator. So calling _PyPreConfig_Clear(config) is safe after this call. |
| 870 | |
| 871 | Do nothing if called after Py_Initialize(): ignore the new |
| 872 | pre-configuration. */ |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 873 | _PyInitError |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 874 | _PyPreConfig_Write(const _PyPreConfig *config) |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 875 | { |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 876 | if (_PyRuntime.core_initialized) { |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 877 | /* bpo-34008: Calling this functions after Py_Initialize() ignores |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 878 | the new configuration. */ |
Victor Stinner | c656e25 | 2019-03-06 01:13:43 +0100 | [diff] [blame] | 879 | return _Py_INIT_OK(); |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 880 | } |
| 881 | |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 882 | if (config->allocator != PYMEM_ALLOCATOR_NOT_SET) { |
| 883 | if (_PyMem_SetupAllocators(config->allocator) < 0) { |
| 884 | return _Py_INIT_ERR("Unknown PYTHONMALLOC allocator"); |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 888 | _PyPreConfig_SetGlobalConfig(config); |
| 889 | |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 890 | if (config->configure_locale) { |
| 891 | if (config->coerce_c_locale) { |
| 892 | _Py_CoerceLegacyLocale(config->coerce_c_locale_warn); |
| 893 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 894 | |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 895 | /* Set LC_CTYPE to the user preferred locale */ |
| 896 | _Py_SetLocaleFromEnv(LC_CTYPE); |
| 897 | } |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 898 | |
Victor Stinner | 6d5ee97 | 2019-03-23 12:05:43 +0100 | [diff] [blame] | 899 | /* Write the new pre-configuration into _PyRuntime */ |
Victor Stinner | b594784 | 2019-05-18 00:38:16 +0200 | [diff] [blame] | 900 | _PyPreConfig_Copy(&_PyRuntime.preconfig, config); |
Victor Stinner | 6d5ee97 | 2019-03-23 12:05:43 +0100 | [diff] [blame] | 901 | |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 902 | return _Py_INIT_OK(); |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 903 | } |