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