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