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