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