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) \ |
| 10 | ? _Py_INIT_USER_ERR("cannot decode " NAME) \ |
| 11 | : _Py_INIT_NO_MEMORY()) |
| 12 | |
| 13 | |
| 14 | /* --- File system encoding/errors -------------------------------- */ |
| 15 | |
| 16 | /* The filesystem encoding is chosen by config_init_fs_encoding(), |
| 17 | see also initfsencoding(). */ |
| 18 | const char *Py_FileSystemDefaultEncoding = NULL; |
| 19 | int Py_HasFileSystemDefaultEncoding = 0; |
| 20 | const char *Py_FileSystemDefaultEncodeErrors = NULL; |
| 21 | int _Py_HasFileSystemDefaultEncodeErrors = 0; |
| 22 | |
| 23 | void |
| 24 | _Py_ClearFileSystemEncoding(void) |
| 25 | { |
| 26 | if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) { |
| 27 | PyMem_RawFree((char*)Py_FileSystemDefaultEncoding); |
| 28 | Py_FileSystemDefaultEncoding = NULL; |
| 29 | } |
| 30 | if (!_Py_HasFileSystemDefaultEncodeErrors && Py_FileSystemDefaultEncodeErrors) { |
| 31 | PyMem_RawFree((char*)Py_FileSystemDefaultEncodeErrors); |
| 32 | Py_FileSystemDefaultEncodeErrors = NULL; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors |
| 38 | global configuration variables. */ |
| 39 | int |
| 40 | _Py_SetFileSystemEncoding(const char *encoding, const char *errors) |
| 41 | { |
| 42 | char *encoding2 = _PyMem_RawStrdup(encoding); |
| 43 | if (encoding2 == NULL) { |
| 44 | return -1; |
| 45 | } |
| 46 | |
| 47 | char *errors2 = _PyMem_RawStrdup(errors); |
| 48 | if (errors2 == NULL) { |
| 49 | PyMem_RawFree(encoding2); |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | _Py_ClearFileSystemEncoding(); |
| 54 | |
| 55 | Py_FileSystemDefaultEncoding = encoding2; |
| 56 | Py_HasFileSystemDefaultEncoding = 0; |
| 57 | |
| 58 | Py_FileSystemDefaultEncodeErrors = errors2; |
| 59 | _Py_HasFileSystemDefaultEncodeErrors = 0; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /* --- _PyArgv ---------------------------------------------------- */ |
| 65 | |
| 66 | _PyInitError |
| 67 | _PyArgv_Decode(const _PyArgv *args, wchar_t*** argv_p) |
| 68 | { |
| 69 | wchar_t** argv; |
| 70 | if (args->use_bytes_argv) { |
| 71 | /* +1 for a the NULL terminator */ |
| 72 | size_t size = sizeof(wchar_t*) * (args->argc + 1); |
| 73 | argv = (wchar_t **)PyMem_RawMalloc(size); |
| 74 | if (argv == NULL) { |
| 75 | return _Py_INIT_NO_MEMORY(); |
| 76 | } |
| 77 | |
| 78 | for (int i = 0; i < args->argc; i++) { |
| 79 | size_t len; |
| 80 | wchar_t *arg = Py_DecodeLocale(args->bytes_argv[i], &len); |
| 81 | if (arg == NULL) { |
| 82 | _Py_wstrlist_clear(i, argv); |
| 83 | return DECODE_LOCALE_ERR("command line arguments", |
| 84 | (Py_ssize_t)len); |
| 85 | } |
| 86 | argv[i] = arg; |
| 87 | } |
| 88 | argv[args->argc] = NULL; |
| 89 | } |
| 90 | else { |
| 91 | argv = args->wchar_argv; |
| 92 | } |
| 93 | *argv_p = argv; |
| 94 | return _Py_INIT_OK(); |
| 95 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 96 | |
| 97 | |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 98 | /* --- _PyPreCmdline ------------------------------------------------- */ |
| 99 | |
| 100 | typedef struct { |
| 101 | const _PyArgv *args; |
| 102 | int argc; |
| 103 | wchar_t **argv; |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 104 | int nxoption; /* Number of -X options */ |
| 105 | wchar_t **xoptions; /* -X options */ |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 106 | } _PyPreCmdline; |
| 107 | |
| 108 | |
| 109 | static void |
| 110 | precmdline_clear(_PyPreCmdline *cmdline) |
| 111 | { |
| 112 | if (cmdline->args->use_bytes_argv && cmdline->argv != NULL) { |
| 113 | _Py_wstrlist_clear(cmdline->args->argc, cmdline->argv); |
| 114 | } |
| 115 | cmdline->argv = NULL; |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 116 | |
| 117 | _Py_wstrlist_clear(cmdline->nxoption, cmdline->xoptions); |
| 118 | cmdline->nxoption = 0; |
| 119 | cmdline->xoptions = NULL; |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 123 | /* --- _PyPreConfig ----------------------------------------------- */ |
| 124 | |
| 125 | void |
| 126 | _PyPreConfig_Clear(_PyPreConfig *config) |
| 127 | { |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 128 | #define CLEAR(ATTR) \ |
| 129 | do { \ |
| 130 | PyMem_RawFree(ATTR); \ |
| 131 | ATTR = NULL; \ |
| 132 | } while (0) |
| 133 | |
| 134 | CLEAR(config->allocator); |
| 135 | |
| 136 | #undef CLEAR |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | int |
| 141 | _PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2) |
| 142 | { |
| 143 | _PyPreConfig_Clear(config); |
| 144 | |
| 145 | #define COPY_ATTR(ATTR) config->ATTR = config2->ATTR |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 146 | #define COPY_STR_ATTR(ATTR) \ |
| 147 | do { \ |
| 148 | if (config2->ATTR != NULL) { \ |
| 149 | config->ATTR = _PyMem_RawStrdup(config2->ATTR); \ |
| 150 | if (config->ATTR == NULL) { \ |
| 151 | return -1; \ |
| 152 | } \ |
| 153 | } \ |
| 154 | } while (0) |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 155 | |
| 156 | COPY_ATTR(isolated); |
| 157 | COPY_ATTR(use_environment); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 158 | COPY_ATTR(coerce_c_locale); |
| 159 | COPY_ATTR(coerce_c_locale_warn); |
| 160 | #ifdef MS_WINDOWS |
| 161 | COPY_ATTR(legacy_windows_fs_encoding); |
| 162 | #endif |
| 163 | COPY_ATTR(utf8_mode); |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 164 | COPY_ATTR(dev_mode); |
| 165 | COPY_STR_ATTR(allocator); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 166 | |
| 167 | #undef COPY_ATTR |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 168 | #undef COPY_STR_ATTR |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | void |
| 174 | _PyPreConfig_GetGlobalConfig(_PyPreConfig *config) |
| 175 | { |
| 176 | #define COPY_FLAG(ATTR, VALUE) \ |
| 177 | if (config->ATTR == -1) { \ |
| 178 | config->ATTR = VALUE; \ |
| 179 | } |
| 180 | #define COPY_NOT_FLAG(ATTR, VALUE) \ |
| 181 | if (config->ATTR == -1) { \ |
| 182 | config->ATTR = !(VALUE); \ |
| 183 | } |
| 184 | |
| 185 | COPY_FLAG(isolated, Py_IsolatedFlag); |
| 186 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 187 | #ifdef MS_WINDOWS |
| 188 | COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag); |
| 189 | #endif |
| 190 | COPY_FLAG(utf8_mode, Py_UTF8Mode); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 191 | |
| 192 | #undef COPY_FLAG |
| 193 | #undef COPY_NOT_FLAG |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void |
| 198 | _PyPreConfig_SetGlobalConfig(const _PyPreConfig *config) |
| 199 | { |
| 200 | #define COPY_FLAG(ATTR, VAR) \ |
| 201 | if (config->ATTR != -1) { \ |
| 202 | VAR = config->ATTR; \ |
| 203 | } |
| 204 | #define COPY_NOT_FLAG(ATTR, VAR) \ |
| 205 | if (config->ATTR != -1) { \ |
| 206 | VAR = !config->ATTR; \ |
| 207 | } |
| 208 | |
| 209 | COPY_FLAG(isolated, Py_IsolatedFlag); |
| 210 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 211 | #ifdef MS_WINDOWS |
| 212 | COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag); |
| 213 | #endif |
| 214 | COPY_FLAG(utf8_mode, Py_UTF8Mode); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 215 | |
| 216 | #undef COPY_FLAG |
| 217 | #undef COPY_NOT_FLAG |
| 218 | } |
| 219 | |
| 220 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 221 | const char* |
| 222 | _PyPreConfig_GetEnv(const _PyPreConfig *config, const char *name) |
| 223 | { |
| 224 | assert(config->use_environment >= 0); |
| 225 | |
| 226 | if (!config->use_environment) { |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | const char *var = getenv(name); |
| 231 | if (var && var[0] != '\0') { |
| 232 | return var; |
| 233 | } |
| 234 | else { |
| 235 | return NULL; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | |
| 240 | int |
| 241 | _Py_str_to_int(const char *str, int *result) |
| 242 | { |
| 243 | const char *endptr = str; |
| 244 | errno = 0; |
| 245 | long value = strtol(str, (char **)&endptr, 10); |
| 246 | if (*endptr != '\0' || errno == ERANGE) { |
| 247 | return -1; |
| 248 | } |
| 249 | if (value < INT_MIN || value > INT_MAX) { |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | *result = (int)value; |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | void |
| 259 | _Py_get_env_flag(_PyPreConfig *config, int *flag, const char *name) |
| 260 | { |
| 261 | const char *var = _PyPreConfig_GetEnv(config, name); |
| 262 | if (!var) { |
| 263 | return; |
| 264 | } |
| 265 | int value; |
| 266 | if (_Py_str_to_int(var, &value) < 0 || value < 0) { |
| 267 | /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */ |
| 268 | value = 1; |
| 269 | } |
| 270 | if (*flag < value) { |
| 271 | *flag = value; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | |
| 276 | const wchar_t* |
| 277 | _Py_get_xoption(int nxoption, wchar_t * const *xoptions, const wchar_t *name) |
| 278 | { |
| 279 | for (int i=0; i < nxoption; i++) { |
| 280 | const wchar_t *option = xoptions[i]; |
| 281 | size_t len; |
| 282 | wchar_t *sep = wcschr(option, L'='); |
| 283 | if (sep != NULL) { |
| 284 | len = (sep - option); |
| 285 | } |
| 286 | else { |
| 287 | len = wcslen(option); |
| 288 | } |
| 289 | if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') { |
| 290 | return option; |
| 291 | } |
| 292 | } |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | static _PyInitError |
| 298 | preconfig_init_utf8_mode(_PyPreConfig *config, const _PyPreCmdline *cmdline) |
| 299 | { |
| 300 | const wchar_t *xopt; |
| 301 | if (cmdline) { |
| 302 | xopt = _Py_get_xoption(cmdline->nxoption, cmdline->xoptions, L"utf8"); |
| 303 | } |
| 304 | else { |
| 305 | xopt = NULL; |
| 306 | } |
| 307 | if (xopt) { |
| 308 | wchar_t *sep = wcschr(xopt, L'='); |
| 309 | if (sep) { |
| 310 | xopt = sep + 1; |
| 311 | if (wcscmp(xopt, L"1") == 0) { |
| 312 | config->utf8_mode = 1; |
| 313 | } |
| 314 | else if (wcscmp(xopt, L"0") == 0) { |
| 315 | config->utf8_mode = 0; |
| 316 | } |
| 317 | else { |
| 318 | return _Py_INIT_USER_ERR("invalid -X utf8 option value"); |
| 319 | } |
| 320 | } |
| 321 | else { |
| 322 | config->utf8_mode = 1; |
| 323 | } |
| 324 | return _Py_INIT_OK(); |
| 325 | } |
| 326 | |
| 327 | const char *opt = _PyPreConfig_GetEnv(config, "PYTHONUTF8"); |
| 328 | if (opt) { |
| 329 | if (strcmp(opt, "1") == 0) { |
| 330 | config->utf8_mode = 1; |
| 331 | } |
| 332 | else if (strcmp(opt, "0") == 0) { |
| 333 | config->utf8_mode = 0; |
| 334 | } |
| 335 | else { |
| 336 | return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment " |
| 337 | "variable value"); |
| 338 | } |
| 339 | return _Py_INIT_OK(); |
| 340 | } |
| 341 | |
| 342 | return _Py_INIT_OK(); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | static void |
| 347 | preconfig_init_locale(_PyPreConfig *config) |
| 348 | { |
| 349 | /* Test also if coerce_c_locale equals 1: PYTHONCOERCECLOCALE=1 doesn't |
| 350 | imply that the C locale is always coerced. It is only coerced if |
| 351 | if the LC_CTYPE locale is "C". */ |
| 352 | if (config->coerce_c_locale != 0) { |
| 353 | /* The C locale enables the C locale coercion (PEP 538) */ |
| 354 | if (_Py_LegacyLocaleDetected()) { |
| 355 | config->coerce_c_locale = 1; |
| 356 | } |
| 357 | else { |
| 358 | config->coerce_c_locale = 0; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | |
| 364 | static _PyInitError |
| 365 | preconfig_read(_PyPreConfig *config, const _PyPreCmdline *cmdline) |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 366 | { |
| 367 | _PyPreConfig_GetGlobalConfig(config); |
| 368 | |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 369 | /* isolated and use_environment */ |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 370 | if (config->isolated > 0) { |
| 371 | config->use_environment = 0; |
| 372 | } |
| 373 | |
| 374 | /* Default values */ |
| 375 | if (config->use_environment < 0) { |
| 376 | config->use_environment = 0; |
| 377 | } |
| 378 | |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 379 | /* legacy_windows_fs_encoding, utf8_mode, coerce_c_locale */ |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 380 | if (config->use_environment) { |
| 381 | #ifdef MS_WINDOWS |
| 382 | _Py_get_env_flag(config, &config->legacy_windows_fs_encoding, |
| 383 | "PYTHONLEGACYWINDOWSFSENCODING"); |
| 384 | #endif |
| 385 | |
| 386 | const char *env = _PyPreConfig_GetEnv(config, "PYTHONCOERCECLOCALE"); |
| 387 | if (env) { |
| 388 | if (strcmp(env, "0") == 0) { |
| 389 | if (config->coerce_c_locale < 0) { |
| 390 | config->coerce_c_locale = 0; |
| 391 | } |
| 392 | } |
| 393 | else if (strcmp(env, "warn") == 0) { |
| 394 | config->coerce_c_locale_warn = 1; |
| 395 | } |
| 396 | else { |
| 397 | if (config->coerce_c_locale < 0) { |
| 398 | config->coerce_c_locale = 1; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | #ifdef MS_WINDOWS |
| 405 | if (config->legacy_windows_fs_encoding) { |
| 406 | config->utf8_mode = 0; |
| 407 | } |
| 408 | #endif |
| 409 | |
| 410 | if (config->utf8_mode < 0) { |
| 411 | _PyInitError err = preconfig_init_utf8_mode(config, cmdline); |
| 412 | if (_Py_INIT_FAILED(err)) { |
| 413 | return err; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (config->coerce_c_locale != 0) { |
| 418 | preconfig_init_locale(config); |
| 419 | } |
| 420 | |
| 421 | #ifndef MS_WINDOWS |
| 422 | if (config->utf8_mode < 0) { |
| 423 | /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */ |
| 424 | const char *ctype_loc = setlocale(LC_CTYPE, NULL); |
| 425 | if (ctype_loc != NULL |
| 426 | && (strcmp(ctype_loc, "C") == 0 |
| 427 | || strcmp(ctype_loc, "POSIX") == 0)) |
| 428 | { |
| 429 | config->utf8_mode = 1; |
| 430 | } |
| 431 | } |
| 432 | #endif |
| 433 | |
| 434 | if (config->coerce_c_locale < 0) { |
| 435 | config->coerce_c_locale = 0; |
| 436 | } |
| 437 | if (config->utf8_mode < 0) { |
| 438 | config->utf8_mode = 0; |
| 439 | } |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 440 | if (config->coerce_c_locale < 0) { |
| 441 | config->coerce_c_locale = 0; |
| 442 | } |
| 443 | |
| 444 | /* dev_mode */ |
| 445 | if ((cmdline && _Py_get_xoption(cmdline->nxoption, cmdline->xoptions, L"dev")) |
| 446 | || _PyPreConfig_GetEnv(config, "PYTHONDEVMODE")) |
| 447 | { |
| 448 | config->dev_mode = 1; |
| 449 | } |
| 450 | if (config->dev_mode < 0) { |
| 451 | config->dev_mode = 0; |
| 452 | } |
| 453 | |
| 454 | /* allocator */ |
| 455 | if (config->dev_mode && config->allocator == NULL) { |
| 456 | config->allocator = _PyMem_RawStrdup("debug"); |
| 457 | if (config->allocator == NULL) { |
| 458 | return _Py_INIT_NO_MEMORY(); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (config->allocator == NULL) { |
| 463 | const char *allocator = _PyPreConfig_GetEnv(config, "PYTHONMALLOC"); |
| 464 | if (allocator) { |
| 465 | config->allocator = _PyMem_RawStrdup(allocator); |
| 466 | if (config->allocator == NULL) { |
| 467 | return _Py_INIT_NO_MEMORY(); |
| 468 | } |
| 469 | } |
| 470 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 471 | |
| 472 | assert(config->coerce_c_locale >= 0); |
| 473 | assert(config->utf8_mode >= 0); |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 474 | assert(config->isolated >= 0); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 475 | assert(config->use_environment >= 0); |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 476 | assert(config->dev_mode >= 0); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 477 | |
| 478 | return _Py_INIT_OK(); |
| 479 | } |
| 480 | |
| 481 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 482 | _PyInitError |
| 483 | _PyPreConfig_Read(_PyPreConfig *config) |
| 484 | { |
| 485 | return preconfig_read(config, NULL); |
| 486 | } |
| 487 | |
| 488 | |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 489 | int |
| 490 | _PyPreConfig_AsDict(const _PyPreConfig *config, PyObject *dict) |
| 491 | { |
| 492 | #define SET_ITEM(KEY, EXPR) \ |
| 493 | do { \ |
| 494 | PyObject *obj = (EXPR); \ |
| 495 | if (obj == NULL) { \ |
| 496 | goto fail; \ |
| 497 | } \ |
| 498 | int res = PyDict_SetItemString(dict, (KEY), obj); \ |
| 499 | Py_DECREF(obj); \ |
| 500 | if (res < 0) { \ |
| 501 | goto fail; \ |
| 502 | } \ |
| 503 | } while (0) |
| 504 | #define SET_ITEM_INT(ATTR) \ |
| 505 | SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR)) |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 506 | #define FROM_STRING(STR) \ |
| 507 | ((STR != NULL) ? \ |
| 508 | PyUnicode_FromString(STR) \ |
| 509 | : (Py_INCREF(Py_None), Py_None)) |
| 510 | #define SET_ITEM_STR(ATTR) \ |
| 511 | SET_ITEM(#ATTR, FROM_STRING(config->ATTR)) |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 512 | |
| 513 | SET_ITEM_INT(isolated); |
| 514 | SET_ITEM_INT(use_environment); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 515 | SET_ITEM_INT(coerce_c_locale); |
| 516 | SET_ITEM_INT(coerce_c_locale_warn); |
| 517 | SET_ITEM_INT(utf8_mode); |
| 518 | #ifdef MS_WINDOWS |
| 519 | SET_ITEM_INT(legacy_windows_fs_encoding); |
| 520 | #endif |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 521 | SET_ITEM_INT(dev_mode); |
| 522 | SET_ITEM_STR(allocator); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 523 | return 0; |
| 524 | |
| 525 | fail: |
| 526 | return -1; |
| 527 | |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 528 | #undef FROM_STRING |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 529 | #undef SET_ITEM |
| 530 | #undef SET_ITEM_INT |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame^] | 531 | #undef SET_ITEM_STR |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 532 | } |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 533 | |
| 534 | |
| 535 | /* Parse the command line arguments */ |
| 536 | static _PyInitError |
| 537 | preconfig_parse_cmdline(_PyPreConfig *config, _PyPreCmdline *cmdline) |
| 538 | { |
| 539 | _PyOS_ResetGetOpt(); |
| 540 | /* Don't log parsing errors into stderr here: _PyCoreConfig_ReadFromArgv() |
| 541 | is responsible for that */ |
| 542 | _PyOS_opterr = 0; |
| 543 | do { |
| 544 | int longindex = -1; |
| 545 | int c = _PyOS_GetOpt(cmdline->args->argc, cmdline->argv, &longindex); |
| 546 | |
| 547 | if (c == EOF || c == 'c' || c == 'm') { |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | switch (c) { |
| 552 | case 'E': |
| 553 | config->use_environment = 0; |
| 554 | break; |
| 555 | |
| 556 | case 'I': |
| 557 | config->isolated++; |
| 558 | break; |
| 559 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 560 | case 'X': |
| 561 | { |
| 562 | _PyInitError err; |
| 563 | err = _Py_wstrlist_append(&cmdline->nxoption, |
| 564 | &cmdline->xoptions, |
| 565 | _PyOS_optarg); |
| 566 | if (_Py_INIT_FAILED(err)) { |
| 567 | return err; |
| 568 | } |
| 569 | break; |
| 570 | } |
| 571 | |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 572 | default: |
| 573 | /* ignore other argument: |
| 574 | handled by _PyCoreConfig_ReadFromArgv() */ |
| 575 | break; |
| 576 | } |
| 577 | } while (1); |
| 578 | |
| 579 | return _Py_INIT_OK(); |
| 580 | } |
| 581 | |
| 582 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 583 | static _PyInitError |
| 584 | preconfig_from_argv(_PyPreConfig *config, const _PyArgv *args) |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 585 | { |
| 586 | _PyInitError err; |
| 587 | |
| 588 | _PyPreCmdline cmdline; |
| 589 | memset(&cmdline, 0, sizeof(cmdline)); |
| 590 | cmdline.args = args; |
| 591 | |
| 592 | err = _PyArgv_Decode(cmdline.args, &cmdline.argv); |
| 593 | if (_Py_INIT_FAILED(err)) { |
| 594 | goto done; |
| 595 | } |
| 596 | |
| 597 | err = preconfig_parse_cmdline(config, &cmdline); |
| 598 | if (_Py_INIT_FAILED(err)) { |
| 599 | goto done; |
| 600 | } |
| 601 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 602 | err = preconfig_read(config, &cmdline); |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 603 | if (_Py_INIT_FAILED(err)) { |
| 604 | goto done; |
| 605 | } |
| 606 | err = _Py_INIT_OK(); |
| 607 | |
| 608 | done: |
| 609 | precmdline_clear(&cmdline); |
| 610 | return err; |
| 611 | } |
| 612 | |
| 613 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 614 | /* Read the preconfiguration. */ |
| 615 | _PyInitError |
| 616 | _PyPreConfig_ReadFromArgv(_PyPreConfig *config, const _PyArgv *args) |
| 617 | { |
| 618 | _PyInitError err; |
| 619 | |
| 620 | err = _PyRuntime_Initialize(); |
| 621 | if (_Py_INIT_FAILED(err)) { |
| 622 | return err; |
| 623 | } |
| 624 | |
| 625 | char *init_ctype_locale = NULL; |
| 626 | int init_utf8_mode = Py_UTF8Mode; |
| 627 | #ifdef MS_WINDOWS |
| 628 | int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag; |
| 629 | #endif |
| 630 | _PyPreConfig save_config = _PyPreConfig_INIT; |
| 631 | int locale_coerced = 0; |
| 632 | int loops = 0; |
| 633 | |
| 634 | /* copy LC_CTYPE locale */ |
| 635 | const char *loc = setlocale(LC_CTYPE, NULL); |
| 636 | if (loc == NULL) { |
| 637 | err = _Py_INIT_ERR("failed to LC_CTYPE locale"); |
| 638 | goto done; |
| 639 | } |
| 640 | init_ctype_locale = _PyMem_RawStrdup(loc); |
| 641 | if (init_ctype_locale == NULL) { |
| 642 | err = _Py_INIT_NO_MEMORY(); |
| 643 | goto done; |
| 644 | } |
| 645 | |
| 646 | if (_PyPreConfig_Copy(&save_config, config) < 0) { |
| 647 | err = _Py_INIT_NO_MEMORY(); |
| 648 | goto done; |
| 649 | } |
| 650 | |
| 651 | /* Set LC_CTYPE to the user preferred locale */ |
| 652 | _Py_SetLocaleFromEnv(LC_CTYPE); |
| 653 | |
| 654 | while (1) { |
| 655 | int utf8_mode = config->utf8_mode; |
| 656 | |
| 657 | /* Watchdog to prevent an infinite loop */ |
| 658 | loops++; |
| 659 | if (loops == 3) { |
| 660 | err = _Py_INIT_ERR("Encoding changed twice while " |
| 661 | "reading the configuration"); |
| 662 | goto done; |
| 663 | } |
| 664 | |
| 665 | /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend |
| 666 | on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */ |
| 667 | Py_UTF8Mode = config->utf8_mode; |
| 668 | #ifdef MS_WINDOWS |
| 669 | Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding; |
| 670 | #endif |
| 671 | |
| 672 | err = preconfig_from_argv(config, args); |
| 673 | if (_Py_INIT_FAILED(err)) { |
| 674 | goto done; |
| 675 | } |
| 676 | |
| 677 | if (locale_coerced) { |
| 678 | config->coerce_c_locale = 1; |
| 679 | } |
| 680 | |
| 681 | /* The legacy C locale assumes ASCII as the default text encoding, which |
| 682 | * causes problems not only for the CPython runtime, but also other |
| 683 | * components like GNU readline. |
| 684 | * |
| 685 | * Accordingly, when the CLI detects it, it attempts to coerce it to a |
| 686 | * more capable UTF-8 based alternative. |
| 687 | * |
| 688 | * See the documentation of the PYTHONCOERCECLOCALE setting for more |
| 689 | * details. |
| 690 | */ |
| 691 | int encoding_changed = 0; |
| 692 | if (config->coerce_c_locale && !locale_coerced) { |
| 693 | locale_coerced = 1; |
| 694 | _Py_CoerceLegacyLocale(0); |
| 695 | encoding_changed = 1; |
| 696 | } |
| 697 | |
| 698 | if (utf8_mode == -1) { |
| 699 | if (config->utf8_mode == 1) { |
| 700 | /* UTF-8 Mode enabled */ |
| 701 | encoding_changed = 1; |
| 702 | } |
| 703 | } |
| 704 | else { |
| 705 | if (config->utf8_mode != utf8_mode) { |
| 706 | encoding_changed = 1; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | if (!encoding_changed) { |
| 711 | break; |
| 712 | } |
| 713 | |
| 714 | /* Reset the configuration before reading again the configuration, |
| 715 | just keep UTF-8 Mode value. */ |
| 716 | int new_utf8_mode = config->utf8_mode; |
| 717 | int new_coerce_c_locale = config->coerce_c_locale; |
| 718 | if (_PyPreConfig_Copy(config, &save_config) < 0) { |
| 719 | err = _Py_INIT_NO_MEMORY(); |
| 720 | goto done; |
| 721 | } |
| 722 | config->utf8_mode = new_utf8_mode; |
| 723 | config->coerce_c_locale = new_coerce_c_locale; |
| 724 | |
| 725 | /* The encoding changed: read again the configuration |
| 726 | with the new encoding */ |
| 727 | } |
| 728 | err = _Py_INIT_OK(); |
| 729 | |
| 730 | done: |
| 731 | if (init_ctype_locale != NULL) { |
| 732 | setlocale(LC_CTYPE, init_ctype_locale); |
| 733 | } |
| 734 | _PyPreConfig_Clear(&save_config); |
| 735 | Py_UTF8Mode = init_utf8_mode ; |
| 736 | #ifdef MS_WINDOWS |
| 737 | Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding; |
| 738 | #endif |
| 739 | return err; |
| 740 | } |
| 741 | |
| 742 | |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 743 | void |
| 744 | _PyPreConfig_Write(const _PyPreConfig *config) |
| 745 | { |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 746 | _PyPreConfig_SetGlobalConfig(config); |
| 747 | |
| 748 | if (config->coerce_c_locale) { |
| 749 | _Py_CoerceLegacyLocale(config->coerce_c_locale_warn); |
| 750 | } |
| 751 | |
| 752 | /* Set LC_CTYPE to the user preferred locale */ |
| 753 | _Py_SetLocaleFromEnv(LC_CTYPE); |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 754 | } |