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