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; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 98 | wargv.items = (wchar_t **)args->wchar_argv; |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 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 |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 220 | _PyPreCmdline_Read(_PyPreCmdline *cmdline, const _PyPreConfig *preconfig) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 221 | { |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 222 | _PyPreCmdline_GetPreConfig(cmdline, preconfig); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 223 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 224 | if (preconfig->parse_argv) { |
| 225 | _PyInitError err = precmdline_parse_cmdline(cmdline); |
| 226 | if (_Py_INIT_FAILED(err)) { |
| 227 | return err; |
| 228 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* isolated, use_environment */ |
| 232 | if (cmdline->isolated < 0) { |
| 233 | cmdline->isolated = 0; |
| 234 | } |
| 235 | if (cmdline->isolated > 0) { |
| 236 | cmdline->use_environment = 0; |
| 237 | } |
| 238 | if (cmdline->use_environment < 0) { |
| 239 | cmdline->use_environment = 0; |
| 240 | } |
| 241 | |
| 242 | /* dev_mode */ |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 243 | if ((cmdline->dev_mode < 0) |
| 244 | && (_Py_get_xoption(&cmdline->xoptions, L"dev") |
| 245 | || _Py_GetEnv(cmdline->use_environment, "PYTHONDEVMODE"))) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 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 | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 263 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 264 | void |
| 265 | _PyPreConfig_Init(_PyPreConfig *config) |
| 266 | { |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 267 | memset(config, 0, sizeof(*config)); |
| 268 | |
| 269 | config->_config_version = _Py_CONFIG_VERSION; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 270 | config->parse_argv = 0; |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 271 | config->isolated = -1; |
| 272 | config->use_environment = -1; |
| 273 | config->configure_locale = 1; |
| 274 | config->utf8_mode = -2; |
| 275 | config->dev_mode = -1; |
| 276 | config->allocator = PYMEM_ALLOCATOR_NOT_SET; |
| 277 | #ifdef MS_WINDOWS |
| 278 | config->legacy_windows_fs_encoding = -1; |
| 279 | #endif |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | |
| 283 | void |
| 284 | _PyPreConfig_InitPythonConfig(_PyPreConfig *config) |
| 285 | { |
| 286 | _PyPreConfig_Init(config); |
| 287 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 288 | config->parse_argv = 1; |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 289 | /* Set to -1 to enable C locale coercion (PEP 538) and UTF-8 Mode (PEP 540) |
| 290 | depending on the LC_CTYPE locale, PYTHONUTF8 and PYTHONCOERCECLOCALE |
| 291 | environment variables. */ |
| 292 | config->coerce_c_locale = -1; |
| 293 | config->coerce_c_locale_warn = -1; |
| 294 | config->utf8_mode = -1; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | void |
| 299 | _PyPreConfig_InitIsolatedConfig(_PyPreConfig *config) |
| 300 | { |
| 301 | _PyPreConfig_Init(config); |
| 302 | |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 303 | config->configure_locale = 0; |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 304 | config->isolated = 1; |
| 305 | config->use_environment = 0; |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 306 | config->utf8_mode = 0; |
| 307 | config->dev_mode = 0; |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 308 | #ifdef MS_WINDOWS |
| 309 | config->legacy_windows_fs_encoding = 0; |
| 310 | #endif |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | |
Victor Stinner | b594784 | 2019-05-18 00:38:16 +0200 | [diff] [blame] | 314 | void |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 315 | _PyPreConfig_InitFromPreConfig(_PyPreConfig *config, |
| 316 | const _PyPreConfig *config2) |
| 317 | { |
| 318 | _PyPreConfig_Init(config); |
| 319 | _PyPreConfig_Copy(config, config2); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | void |
| 324 | _PyPreConfig_InitFromCoreConfig(_PyPreConfig *config, |
| 325 | const _PyCoreConfig *coreconfig) |
| 326 | { |
| 327 | _PyCoreConfigInitEnum config_init = (_PyCoreConfigInitEnum)coreconfig->_config_init; |
| 328 | switch (config_init) { |
| 329 | case _PyCoreConfig_INIT_PYTHON: |
| 330 | _PyPreConfig_InitPythonConfig(config); |
| 331 | break; |
| 332 | case _PyCoreConfig_INIT_ISOLATED: |
| 333 | _PyPreConfig_InitIsolatedConfig(config); |
| 334 | break; |
| 335 | case _PyCoreConfig_INIT: |
| 336 | default: |
| 337 | _PyPreConfig_Init(config); |
| 338 | } |
| 339 | _PyPreConfig_GetCoreConfig(config, coreconfig); |
| 340 | } |
| 341 | |
| 342 | |
| 343 | void |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 344 | _PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2) |
| 345 | { |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 346 | #define COPY_ATTR(ATTR) config->ATTR = config2->ATTR |
| 347 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 348 | COPY_ATTR(parse_argv); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 349 | COPY_ATTR(isolated); |
| 350 | COPY_ATTR(use_environment); |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 351 | COPY_ATTR(configure_locale); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 352 | COPY_ATTR(dev_mode); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 353 | COPY_ATTR(coerce_c_locale); |
| 354 | COPY_ATTR(coerce_c_locale_warn); |
Victor Stinner | b594784 | 2019-05-18 00:38:16 +0200 | [diff] [blame] | 355 | COPY_ATTR(utf8_mode); |
| 356 | COPY_ATTR(allocator); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 357 | #ifdef MS_WINDOWS |
| 358 | COPY_ATTR(legacy_windows_fs_encoding); |
| 359 | #endif |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 360 | |
| 361 | #undef COPY_ATTR |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 365 | PyObject* |
| 366 | _PyPreConfig_AsDict(const _PyPreConfig *config) |
| 367 | { |
| 368 | PyObject *dict; |
| 369 | |
| 370 | dict = PyDict_New(); |
| 371 | if (dict == NULL) { |
| 372 | return NULL; |
| 373 | } |
| 374 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 375 | #define SET_ITEM_INT(ATTR) \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 376 | do { \ |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 377 | PyObject *obj = PyLong_FromLong(config->ATTR); \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 378 | if (obj == NULL) { \ |
| 379 | goto fail; \ |
| 380 | } \ |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 381 | int res = PyDict_SetItemString(dict, #ATTR, obj); \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 382 | Py_DECREF(obj); \ |
| 383 | if (res < 0) { \ |
| 384 | goto fail; \ |
| 385 | } \ |
| 386 | } while (0) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 387 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 388 | SET_ITEM_INT(parse_argv); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 389 | SET_ITEM_INT(isolated); |
| 390 | SET_ITEM_INT(use_environment); |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 391 | SET_ITEM_INT(configure_locale); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 392 | SET_ITEM_INT(coerce_c_locale); |
| 393 | SET_ITEM_INT(coerce_c_locale_warn); |
| 394 | SET_ITEM_INT(utf8_mode); |
| 395 | #ifdef MS_WINDOWS |
| 396 | SET_ITEM_INT(legacy_windows_fs_encoding); |
| 397 | #endif |
| 398 | SET_ITEM_INT(dev_mode); |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 399 | SET_ITEM_INT(allocator); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 400 | return dict; |
| 401 | |
| 402 | fail: |
| 403 | Py_DECREF(dict); |
| 404 | return NULL; |
| 405 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 406 | #undef SET_ITEM_INT |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 410 | void |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 411 | _PyPreConfig_GetCoreConfig(_PyPreConfig *config, |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 412 | const _PyCoreConfig *core_config) |
| 413 | { |
| 414 | #define COPY_ATTR(ATTR) \ |
| 415 | if (core_config->ATTR != -1) { \ |
| 416 | config->ATTR = core_config->ATTR; \ |
| 417 | } |
| 418 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 419 | COPY_ATTR(parse_argv); |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 420 | COPY_ATTR(isolated); |
| 421 | COPY_ATTR(use_environment); |
| 422 | COPY_ATTR(dev_mode); |
| 423 | |
| 424 | #undef COPY_ATTR |
| 425 | } |
| 426 | |
| 427 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 428 | static void |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 429 | _PyPreConfig_GetGlobalConfig(_PyPreConfig *config) |
| 430 | { |
| 431 | #define COPY_FLAG(ATTR, VALUE) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 432 | if (config->ATTR < 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 433 | config->ATTR = VALUE; \ |
| 434 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 435 | #define COPY_NOT_FLAG(ATTR, VALUE) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 436 | if (config->ATTR < 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 437 | config->ATTR = !(VALUE); \ |
| 438 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 439 | |
| 440 | COPY_FLAG(isolated, Py_IsolatedFlag); |
| 441 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 442 | #ifdef MS_WINDOWS |
| 443 | COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag); |
| 444 | #endif |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 445 | if (config->utf8_mode == -2) { |
| 446 | config->utf8_mode = Py_UTF8Mode; |
Victor Stinner | d929f18 | 2019-03-27 18:28:46 +0100 | [diff] [blame] | 447 | } |
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 | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 454 | static void |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 455 | _PyPreConfig_SetGlobalConfig(const _PyPreConfig *config) |
| 456 | { |
| 457 | #define COPY_FLAG(ATTR, VAR) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 458 | if (config->ATTR >= 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 459 | VAR = config->ATTR; \ |
| 460 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 461 | #define COPY_NOT_FLAG(ATTR, VAR) \ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 462 | if (config->ATTR >= 0) { \ |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 463 | VAR = !config->ATTR; \ |
| 464 | } |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 465 | |
| 466 | COPY_FLAG(isolated, Py_IsolatedFlag); |
| 467 | COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 468 | #ifdef MS_WINDOWS |
| 469 | COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag); |
| 470 | #endif |
| 471 | COPY_FLAG(utf8_mode, Py_UTF8Mode); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 472 | |
| 473 | #undef COPY_FLAG |
| 474 | #undef COPY_NOT_FLAG |
| 475 | } |
| 476 | |
| 477 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 478 | const char* |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 479 | _Py_GetEnv(int use_environment, const char *name) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 480 | { |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 481 | assert(use_environment >= 0); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 482 | |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 483 | if (!use_environment) { |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 484 | return NULL; |
| 485 | } |
| 486 | |
| 487 | const char *var = getenv(name); |
| 488 | if (var && var[0] != '\0') { |
| 489 | return var; |
| 490 | } |
| 491 | else { |
| 492 | return NULL; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | |
| 497 | int |
| 498 | _Py_str_to_int(const char *str, int *result) |
| 499 | { |
| 500 | const char *endptr = str; |
| 501 | errno = 0; |
| 502 | long value = strtol(str, (char **)&endptr, 10); |
| 503 | if (*endptr != '\0' || errno == ERANGE) { |
| 504 | return -1; |
| 505 | } |
| 506 | if (value < INT_MIN || value > INT_MAX) { |
| 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | *result = (int)value; |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | void |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 516 | _Py_get_env_flag(int use_environment, int *flag, const char *name) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 517 | { |
Victor Stinner | f78a5e9 | 2019-03-26 00:03:15 +0100 | [diff] [blame] | 518 | const char *var = _Py_GetEnv(use_environment, name); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 519 | if (!var) { |
| 520 | return; |
| 521 | } |
| 522 | int value; |
| 523 | if (_Py_str_to_int(var, &value) < 0 || value < 0) { |
| 524 | /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */ |
| 525 | value = 1; |
| 526 | } |
| 527 | if (*flag < value) { |
| 528 | *flag = value; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | |
| 533 | const wchar_t* |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 534 | _Py_get_xoption(const _PyWstrList *xoptions, const wchar_t *name) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 535 | { |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 536 | for (Py_ssize_t i=0; i < xoptions->length; i++) { |
| 537 | const wchar_t *option = xoptions->items[i]; |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 538 | size_t len; |
| 539 | wchar_t *sep = wcschr(option, L'='); |
| 540 | if (sep != NULL) { |
| 541 | len = (sep - option); |
| 542 | } |
| 543 | else { |
| 544 | len = wcslen(option); |
| 545 | } |
| 546 | if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') { |
| 547 | return option; |
| 548 | } |
| 549 | } |
| 550 | return NULL; |
| 551 | } |
| 552 | |
| 553 | |
| 554 | static _PyInitError |
| 555 | preconfig_init_utf8_mode(_PyPreConfig *config, const _PyPreCmdline *cmdline) |
| 556 | { |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 557 | #ifdef MS_WINDOWS |
| 558 | if (config->legacy_windows_fs_encoding) { |
| 559 | config->utf8_mode = 0; |
| 560 | } |
| 561 | #endif |
| 562 | |
| 563 | if (config->utf8_mode >= 0) { |
| 564 | return _Py_INIT_OK(); |
| 565 | } |
| 566 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 567 | const wchar_t *xopt; |
| 568 | if (cmdline) { |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 569 | xopt = _Py_get_xoption(&cmdline->xoptions, L"utf8"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 570 | } |
| 571 | else { |
| 572 | xopt = NULL; |
| 573 | } |
| 574 | if (xopt) { |
| 575 | wchar_t *sep = wcschr(xopt, L'='); |
| 576 | if (sep) { |
| 577 | xopt = sep + 1; |
| 578 | if (wcscmp(xopt, L"1") == 0) { |
| 579 | config->utf8_mode = 1; |
| 580 | } |
| 581 | else if (wcscmp(xopt, L"0") == 0) { |
| 582 | config->utf8_mode = 0; |
| 583 | } |
| 584 | else { |
Victor Stinner | db71975 | 2019-05-01 05:35:33 +0200 | [diff] [blame] | 585 | return _Py_INIT_ERR("invalid -X utf8 option value"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | else { |
| 589 | config->utf8_mode = 1; |
| 590 | } |
| 591 | return _Py_INIT_OK(); |
| 592 | } |
| 593 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 594 | const char *opt = _Py_GetEnv(config->use_environment, "PYTHONUTF8"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 595 | if (opt) { |
| 596 | if (strcmp(opt, "1") == 0) { |
| 597 | config->utf8_mode = 1; |
| 598 | } |
| 599 | else if (strcmp(opt, "0") == 0) { |
| 600 | config->utf8_mode = 0; |
| 601 | } |
| 602 | else { |
Victor Stinner | db71975 | 2019-05-01 05:35:33 +0200 | [diff] [blame] | 603 | return _Py_INIT_ERR("invalid PYTHONUTF8 environment " |
| 604 | "variable value"); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 605 | } |
| 606 | return _Py_INIT_OK(); |
| 607 | } |
| 608 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 609 | |
| 610 | #ifndef MS_WINDOWS |
| 611 | if (config->utf8_mode < 0) { |
| 612 | /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */ |
| 613 | const char *ctype_loc = setlocale(LC_CTYPE, NULL); |
| 614 | if (ctype_loc != NULL |
| 615 | && (strcmp(ctype_loc, "C") == 0 |
| 616 | || strcmp(ctype_loc, "POSIX") == 0)) |
| 617 | { |
| 618 | config->utf8_mode = 1; |
| 619 | } |
| 620 | } |
| 621 | #endif |
| 622 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 623 | if (config->utf8_mode < 0) { |
| 624 | config->utf8_mode = 0; |
| 625 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 626 | return _Py_INIT_OK(); |
| 627 | } |
| 628 | |
| 629 | |
| 630 | static void |
| 631 | preconfig_init_coerce_c_locale(_PyPreConfig *config) |
| 632 | { |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 633 | if (!config->configure_locale) { |
| 634 | config->coerce_c_locale = 0; |
| 635 | config->coerce_c_locale_warn = 0; |
| 636 | return; |
| 637 | } |
| 638 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 639 | const char *env = _Py_GetEnv(config->use_environment, "PYTHONCOERCECLOCALE"); |
| 640 | if (env) { |
| 641 | if (strcmp(env, "0") == 0) { |
| 642 | if (config->coerce_c_locale < 0) { |
| 643 | config->coerce_c_locale = 0; |
| 644 | } |
| 645 | } |
| 646 | else if (strcmp(env, "warn") == 0) { |
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 = 1; |
| 649 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 650 | } |
| 651 | else { |
| 652 | if (config->coerce_c_locale < 0) { |
| 653 | config->coerce_c_locale = 1; |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | /* Test if coerce_c_locale equals to -1 or equals to 1: |
| 659 | PYTHONCOERCECLOCALE=1 doesn't imply that the C locale is always coerced. |
| 660 | It is only coerced if if the LC_CTYPE locale is "C". */ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 661 | if (config->coerce_c_locale < 0 || config->coerce_c_locale == 1) { |
| 662 | /* The C locale enables the C locale coercion (PEP 538) */ |
| 663 | if (_Py_LegacyLocaleDetected()) { |
| 664 | config->coerce_c_locale = 2; |
| 665 | } |
| 666 | else { |
| 667 | config->coerce_c_locale = 0; |
| 668 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 669 | } |
| 670 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 671 | if (config->coerce_c_locale_warn < 0) { |
| 672 | config->coerce_c_locale_warn = 0; |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 673 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 674 | } |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 675 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 676 | |
| 677 | static _PyInitError |
| 678 | preconfig_init_allocator(_PyPreConfig *config) |
| 679 | { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 680 | if (config->allocator == PYMEM_ALLOCATOR_NOT_SET) { |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 681 | /* bpo-34247. The PYTHONMALLOC environment variable has the priority |
| 682 | over PYTHONDEV env var and "-X dev" command line option. |
| 683 | For example, PYTHONMALLOC=malloc PYTHONDEVMODE=1 sets the memory |
| 684 | allocators to "malloc" (and not to "debug"). */ |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 685 | const char *envvar = _Py_GetEnv(config->use_environment, "PYTHONMALLOC"); |
| 686 | if (envvar) { |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 687 | PyMemAllocatorName name; |
| 688 | if (_PyMem_GetAllocatorName(envvar, &name) < 0) { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 689 | return _Py_INIT_ERR("PYTHONMALLOC: unknown allocator"); |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 690 | } |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 691 | config->allocator = (int)name; |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 692 | } |
| 693 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 694 | |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 695 | if (config->dev_mode && config->allocator == PYMEM_ALLOCATOR_NOT_SET) { |
| 696 | config->allocator = PYMEM_ALLOCATOR_DEBUG; |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 697 | } |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 698 | return _Py_INIT_OK(); |
| 699 | } |
| 700 | |
| 701 | |
| 702 | static _PyInitError |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 703 | preconfig_read(_PyPreConfig *config, _PyPreCmdline *cmdline) |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 704 | { |
| 705 | _PyInitError err; |
| 706 | |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 707 | err = _PyPreCmdline_Read(cmdline, config); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 708 | if (_Py_INIT_FAILED(err)) { |
| 709 | return err; |
| 710 | } |
| 711 | |
| 712 | _PyPreCmdline_SetPreConfig(cmdline, config); |
| 713 | |
| 714 | /* legacy_windows_fs_encoding, coerce_c_locale, utf8_mode */ |
| 715 | #ifdef MS_WINDOWS |
| 716 | _Py_get_env_flag(config->use_environment, |
| 717 | &config->legacy_windows_fs_encoding, |
| 718 | "PYTHONLEGACYWINDOWSFSENCODING"); |
| 719 | #endif |
| 720 | |
| 721 | preconfig_init_coerce_c_locale(config); |
| 722 | |
| 723 | err = preconfig_init_utf8_mode(config, cmdline); |
| 724 | if (_Py_INIT_FAILED(err)) { |
| 725 | return err; |
| 726 | } |
| 727 | |
| 728 | /* allocator */ |
| 729 | err = preconfig_init_allocator(config); |
| 730 | if (_Py_INIT_FAILED(err)) { |
| 731 | return err; |
| 732 | } |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 733 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 734 | assert(config->coerce_c_locale >= 0); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 735 | assert(config->coerce_c_locale_warn >= 0); |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 736 | #ifdef MS_WINDOWS |
| 737 | assert(config->legacy_windows_fs_encoding >= 0); |
| 738 | #endif |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 739 | assert(config->utf8_mode >= 0); |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 740 | assert(config->isolated >= 0); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 741 | assert(config->use_environment >= 0); |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 742 | assert(config->dev_mode >= 0); |
Victor Stinner | cad1f74 | 2019-03-05 02:01:27 +0100 | [diff] [blame] | 743 | |
| 744 | return _Py_INIT_OK(); |
| 745 | } |
| 746 | |
| 747 | |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 748 | /* Read the configuration from: |
| 749 | |
| 750 | - command line arguments |
| 751 | - environment variables |
| 752 | - Py_xxx global configuration variables |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 753 | - the LC_CTYPE locale */ |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 754 | _PyInitError |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 755 | _PyPreConfig_Read(_PyPreConfig *config, const _PyArgv *args) |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 756 | { |
| 757 | _PyInitError err; |
| 758 | |
| 759 | err = _PyRuntime_Initialize(); |
| 760 | if (_Py_INIT_FAILED(err)) { |
| 761 | return err; |
| 762 | } |
| 763 | |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 764 | _PyPreConfig_GetGlobalConfig(config); |
| 765 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 766 | /* Copy LC_CTYPE locale, since it's modified later */ |
| 767 | const char *loc = setlocale(LC_CTYPE, NULL); |
| 768 | if (loc == NULL) { |
| 769 | return _Py_INIT_ERR("failed to LC_CTYPE locale"); |
| 770 | } |
| 771 | char *init_ctype_locale = _PyMem_RawStrdup(loc); |
| 772 | if (init_ctype_locale == NULL) { |
| 773 | return _Py_INIT_NO_MEMORY(); |
| 774 | } |
| 775 | |
| 776 | /* Save the config to be able to restore it if encodings change */ |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 777 | _PyPreConfig save_config; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 778 | _PyPreConfig_InitFromPreConfig(&save_config, config); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 779 | |
| 780 | /* Set LC_CTYPE to the user preferred locale */ |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 781 | if (config->configure_locale) { |
| 782 | _Py_SetLocaleFromEnv(LC_CTYPE); |
| 783 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 784 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 785 | _PyPreCmdline cmdline = _PyPreCmdline_INIT; |
Victor Stinner | 6a8c313 | 2019-04-05 11:44:04 +0200 | [diff] [blame] | 786 | int init_utf8_mode = Py_UTF8Mode; |
| 787 | #ifdef MS_WINDOWS |
| 788 | int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag; |
| 789 | #endif |
| 790 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 791 | if (args) { |
| 792 | err = _PyPreCmdline_SetArgv(&cmdline, args); |
| 793 | if (_Py_INIT_FAILED(err)) { |
| 794 | goto done; |
| 795 | } |
| 796 | } |
| 797 | |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 798 | int locale_coerced = 0; |
| 799 | int loops = 0; |
| 800 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 801 | while (1) { |
| 802 | int utf8_mode = config->utf8_mode; |
| 803 | |
| 804 | /* Watchdog to prevent an infinite loop */ |
| 805 | loops++; |
| 806 | if (loops == 3) { |
| 807 | err = _Py_INIT_ERR("Encoding changed twice while " |
| 808 | "reading the configuration"); |
| 809 | goto done; |
| 810 | } |
| 811 | |
| 812 | /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend |
| 813 | on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */ |
| 814 | Py_UTF8Mode = config->utf8_mode; |
| 815 | #ifdef MS_WINDOWS |
| 816 | Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding; |
| 817 | #endif |
| 818 | |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 819 | err = preconfig_read(config, &cmdline); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 820 | if (_Py_INIT_FAILED(err)) { |
| 821 | goto done; |
| 822 | } |
| 823 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 824 | /* The legacy C locale assumes ASCII as the default text encoding, which |
| 825 | * causes problems not only for the CPython runtime, but also other |
| 826 | * components like GNU readline. |
| 827 | * |
| 828 | * Accordingly, when the CLI detects it, it attempts to coerce it to a |
| 829 | * more capable UTF-8 based alternative. |
| 830 | * |
| 831 | * See the documentation of the PYTHONCOERCECLOCALE setting for more |
| 832 | * details. |
| 833 | */ |
| 834 | int encoding_changed = 0; |
| 835 | if (config->coerce_c_locale && !locale_coerced) { |
| 836 | locale_coerced = 1; |
| 837 | _Py_CoerceLegacyLocale(0); |
| 838 | encoding_changed = 1; |
| 839 | } |
| 840 | |
| 841 | if (utf8_mode == -1) { |
| 842 | if (config->utf8_mode == 1) { |
| 843 | /* UTF-8 Mode enabled */ |
| 844 | encoding_changed = 1; |
| 845 | } |
| 846 | } |
| 847 | else { |
| 848 | if (config->utf8_mode != utf8_mode) { |
| 849 | encoding_changed = 1; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | if (!encoding_changed) { |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | /* Reset the configuration before reading again the configuration, |
| 858 | just keep UTF-8 Mode value. */ |
| 859 | int new_utf8_mode = config->utf8_mode; |
| 860 | int new_coerce_c_locale = config->coerce_c_locale; |
Victor Stinner | b594784 | 2019-05-18 00:38:16 +0200 | [diff] [blame] | 861 | _PyPreConfig_Copy(config, &save_config); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 862 | config->utf8_mode = new_utf8_mode; |
| 863 | config->coerce_c_locale = new_coerce_c_locale; |
| 864 | |
| 865 | /* The encoding changed: read again the configuration |
| 866 | with the new encoding */ |
| 867 | } |
| 868 | err = _Py_INIT_OK(); |
| 869 | |
| 870 | done: |
| 871 | if (init_ctype_locale != NULL) { |
| 872 | setlocale(LC_CTYPE, init_ctype_locale); |
Victor Stinner | c183444 | 2019-03-18 22:24:28 +0100 | [diff] [blame] | 873 | PyMem_RawFree(init_ctype_locale); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 874 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 875 | Py_UTF8Mode = init_utf8_mode ; |
| 876 | #ifdef MS_WINDOWS |
| 877 | Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding; |
| 878 | #endif |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 879 | _PyPreCmdline_Clear(&cmdline); |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 880 | return err; |
| 881 | } |
| 882 | |
| 883 | |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 884 | /* Write the pre-configuration: |
| 885 | |
| 886 | - set the memory allocators |
| 887 | - set Py_xxx global configuration variables |
| 888 | - set the LC_CTYPE locale (coerce C locale, PEP 538) and set the UTF-8 mode |
| 889 | (PEP 540) |
Victor Stinner | c656e25 | 2019-03-06 01:13:43 +0100 | [diff] [blame] | 890 | |
| 891 | If the memory allocator is changed, config is re-allocated with new |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 892 | allocator. So calling _PyPreConfig_Clear(config) is safe after this call. |
| 893 | |
| 894 | Do nothing if called after Py_Initialize(): ignore the new |
| 895 | pre-configuration. */ |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 896 | _PyInitError |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 897 | _PyPreConfig_Write(const _PyPreConfig *config) |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 898 | { |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 899 | if (_PyRuntime.core_initialized) { |
Victor Stinner | 4fffd38 | 2019-03-06 01:44:31 +0100 | [diff] [blame] | 900 | /* bpo-34008: Calling this functions after Py_Initialize() ignores |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 901 | the new configuration. */ |
Victor Stinner | c656e25 | 2019-03-06 01:13:43 +0100 | [diff] [blame] | 902 | return _Py_INIT_OK(); |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame^] | 905 | PyMemAllocatorName name = (PyMemAllocatorName)config->allocator; |
| 906 | if (name != PYMEM_ALLOCATOR_NOT_SET) { |
| 907 | if (_PyMem_SetupAllocators(name) < 0) { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 908 | return _Py_INIT_ERR("Unknown PYTHONMALLOC allocator"); |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 912 | _PyPreConfig_SetGlobalConfig(config); |
| 913 | |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 914 | if (config->configure_locale) { |
| 915 | if (config->coerce_c_locale) { |
| 916 | _Py_CoerceLegacyLocale(config->coerce_c_locale_warn); |
| 917 | } |
Victor Stinner | 5a02e0d | 2019-03-05 12:32:09 +0100 | [diff] [blame] | 918 | |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 919 | /* Set LC_CTYPE to the user preferred locale */ |
| 920 | _Py_SetLocaleFromEnv(LC_CTYPE); |
| 921 | } |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 922 | |
Victor Stinner | 6d5ee97 | 2019-03-23 12:05:43 +0100 | [diff] [blame] | 923 | /* Write the new pre-configuration into _PyRuntime */ |
Victor Stinner | b594784 | 2019-05-18 00:38:16 +0200 | [diff] [blame] | 924 | _PyPreConfig_Copy(&_PyRuntime.preconfig, config); |
Victor Stinner | 6d5ee97 | 2019-03-23 12:05:43 +0100 | [diff] [blame] | 925 | |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 926 | return _Py_INIT_OK(); |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 927 | } |