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