blob: ee9dca4bdc924126feea57c98d56ba7feb2fd1ba [file] [log] [blame]
Victor Stinner91b9ecf2019-03-01 17:52:56 +01001#include "Python.h"
2#include "pycore_coreconfig.h"
Victor Stinner6dcb5422019-03-05 02:44:12 +01003#include "pycore_getopt.h"
Victor Stinner5a02e0d2019-03-05 12:32:09 +01004#include "pycore_pystate.h" /* _PyRuntime_Initialize() */
5#include <locale.h> /* setlocale() */
Victor Stinner91b9ecf2019-03-01 17:52:56 +01006
7
8#define DECODE_LOCALE_ERR(NAME, LEN) \
9 (((LEN) == -2) \
10 ? _Py_INIT_USER_ERR("cannot decode " NAME) \
11 : _Py_INIT_NO_MEMORY())
12
13
14/* --- File system encoding/errors -------------------------------- */
15
16/* The filesystem encoding is chosen by config_init_fs_encoding(),
17 see also initfsencoding(). */
18const char *Py_FileSystemDefaultEncoding = NULL;
19int Py_HasFileSystemDefaultEncoding = 0;
20const char *Py_FileSystemDefaultEncodeErrors = NULL;
21int _Py_HasFileSystemDefaultEncodeErrors = 0;
22
23void
24_Py_ClearFileSystemEncoding(void)
25{
26 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
27 PyMem_RawFree((char*)Py_FileSystemDefaultEncoding);
28 Py_FileSystemDefaultEncoding = NULL;
29 }
30 if (!_Py_HasFileSystemDefaultEncodeErrors && Py_FileSystemDefaultEncodeErrors) {
31 PyMem_RawFree((char*)Py_FileSystemDefaultEncodeErrors);
32 Py_FileSystemDefaultEncodeErrors = NULL;
33 }
34}
35
36
37/* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors
38 global configuration variables. */
39int
40_Py_SetFileSystemEncoding(const char *encoding, const char *errors)
41{
42 char *encoding2 = _PyMem_RawStrdup(encoding);
43 if (encoding2 == NULL) {
44 return -1;
45 }
46
47 char *errors2 = _PyMem_RawStrdup(errors);
48 if (errors2 == NULL) {
49 PyMem_RawFree(encoding2);
50 return -1;
51 }
52
53 _Py_ClearFileSystemEncoding();
54
55 Py_FileSystemDefaultEncoding = encoding2;
56 Py_HasFileSystemDefaultEncoding = 0;
57
58 Py_FileSystemDefaultEncodeErrors = errors2;
59 _Py_HasFileSystemDefaultEncodeErrors = 0;
60 return 0;
61}
62
63
64/* --- _PyArgv ---------------------------------------------------- */
65
66_PyInitError
67_PyArgv_Decode(const _PyArgv *args, wchar_t*** argv_p)
68{
69 wchar_t** argv;
70 if (args->use_bytes_argv) {
71 /* +1 for a the NULL terminator */
72 size_t size = sizeof(wchar_t*) * (args->argc + 1);
73 argv = (wchar_t **)PyMem_RawMalloc(size);
74 if (argv == NULL) {
75 return _Py_INIT_NO_MEMORY();
76 }
77
78 for (int i = 0; i < args->argc; i++) {
79 size_t len;
80 wchar_t *arg = Py_DecodeLocale(args->bytes_argv[i], &len);
81 if (arg == NULL) {
82 _Py_wstrlist_clear(i, argv);
83 return DECODE_LOCALE_ERR("command line arguments",
84 (Py_ssize_t)len);
85 }
86 argv[i] = arg;
87 }
88 argv[args->argc] = NULL;
89 }
90 else {
91 argv = args->wchar_argv;
92 }
93 *argv_p = argv;
94 return _Py_INIT_OK();
95}
Victor Stinnercad1f742019-03-05 02:01:27 +010096
97
Victor Stinner6dcb5422019-03-05 02:44:12 +010098/* --- _PyPreCmdline ------------------------------------------------- */
99
100typedef struct {
101 const _PyArgv *args;
102 int argc;
103 wchar_t **argv;
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100104 int nxoption; /* Number of -X options */
105 wchar_t **xoptions; /* -X options */
Victor Stinner6dcb5422019-03-05 02:44:12 +0100106} _PyPreCmdline;
107
108
109static void
110precmdline_clear(_PyPreCmdline *cmdline)
111{
112 if (cmdline->args->use_bytes_argv && cmdline->argv != NULL) {
113 _Py_wstrlist_clear(cmdline->args->argc, cmdline->argv);
114 }
115 cmdline->argv = NULL;
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100116
117 _Py_wstrlist_clear(cmdline->nxoption, cmdline->xoptions);
118 cmdline->nxoption = 0;
119 cmdline->xoptions = NULL;
Victor Stinner6dcb5422019-03-05 02:44:12 +0100120}
121
122
Victor Stinnercad1f742019-03-05 02:01:27 +0100123/* --- _PyPreConfig ----------------------------------------------- */
124
125void
126_PyPreConfig_Clear(_PyPreConfig *config)
127{
Victor Stinnerc656e252019-03-06 01:13:43 +0100128 PyMem_RawFree(config->allocator);
129 config->allocator = NULL;
Victor Stinnercad1f742019-03-05 02:01:27 +0100130}
131
132
133int
134_PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2)
135{
136 _PyPreConfig_Clear(config);
137
138#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100139#define COPY_STR_ATTR(ATTR) \
140 do { \
141 if (config2->ATTR != NULL) { \
142 config->ATTR = _PyMem_RawStrdup(config2->ATTR); \
143 if (config->ATTR == NULL) { \
144 return -1; \
145 } \
146 } \
147 } while (0)
Victor Stinnercad1f742019-03-05 02:01:27 +0100148
149 COPY_ATTR(isolated);
150 COPY_ATTR(use_environment);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100151 COPY_ATTR(coerce_c_locale);
152 COPY_ATTR(coerce_c_locale_warn);
153#ifdef MS_WINDOWS
154 COPY_ATTR(legacy_windows_fs_encoding);
155#endif
156 COPY_ATTR(utf8_mode);
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100157 COPY_ATTR(dev_mode);
158 COPY_STR_ATTR(allocator);
Victor Stinnercad1f742019-03-05 02:01:27 +0100159
160#undef COPY_ATTR
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100161#undef COPY_STR_ATTR
Victor Stinnercad1f742019-03-05 02:01:27 +0100162 return 0;
163}
164
165
166void
167_PyPreConfig_GetGlobalConfig(_PyPreConfig *config)
168{
169#define COPY_FLAG(ATTR, VALUE) \
170 if (config->ATTR == -1) { \
171 config->ATTR = VALUE; \
172 }
173#define COPY_NOT_FLAG(ATTR, VALUE) \
174 if (config->ATTR == -1) { \
175 config->ATTR = !(VALUE); \
176 }
177
178 COPY_FLAG(isolated, Py_IsolatedFlag);
179 COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100180#ifdef MS_WINDOWS
181 COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
182#endif
183 COPY_FLAG(utf8_mode, Py_UTF8Mode);
Victor Stinnercad1f742019-03-05 02:01:27 +0100184
185#undef COPY_FLAG
186#undef COPY_NOT_FLAG
187}
188
189
190void
191_PyPreConfig_SetGlobalConfig(const _PyPreConfig *config)
192{
193#define COPY_FLAG(ATTR, VAR) \
194 if (config->ATTR != -1) { \
195 VAR = config->ATTR; \
196 }
197#define COPY_NOT_FLAG(ATTR, VAR) \
198 if (config->ATTR != -1) { \
199 VAR = !config->ATTR; \
200 }
201
202 COPY_FLAG(isolated, Py_IsolatedFlag);
203 COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100204#ifdef MS_WINDOWS
205 COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
206#endif
207 COPY_FLAG(utf8_mode, Py_UTF8Mode);
Victor Stinnercad1f742019-03-05 02:01:27 +0100208
209#undef COPY_FLAG
210#undef COPY_NOT_FLAG
211}
212
213
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100214const char*
215_PyPreConfig_GetEnv(const _PyPreConfig *config, const char *name)
216{
217 assert(config->use_environment >= 0);
218
219 if (!config->use_environment) {
220 return NULL;
221 }
222
223 const char *var = getenv(name);
224 if (var && var[0] != '\0') {
225 return var;
226 }
227 else {
228 return NULL;
229 }
230}
231
232
233int
234_Py_str_to_int(const char *str, int *result)
235{
236 const char *endptr = str;
237 errno = 0;
238 long value = strtol(str, (char **)&endptr, 10);
239 if (*endptr != '\0' || errno == ERANGE) {
240 return -1;
241 }
242 if (value < INT_MIN || value > INT_MAX) {
243 return -1;
244 }
245
246 *result = (int)value;
247 return 0;
248}
249
250
251void
252_Py_get_env_flag(_PyPreConfig *config, int *flag, const char *name)
253{
254 const char *var = _PyPreConfig_GetEnv(config, name);
255 if (!var) {
256 return;
257 }
258 int value;
259 if (_Py_str_to_int(var, &value) < 0 || value < 0) {
260 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
261 value = 1;
262 }
263 if (*flag < value) {
264 *flag = value;
265 }
266}
267
268
269const wchar_t*
270_Py_get_xoption(int nxoption, wchar_t * const *xoptions, const wchar_t *name)
271{
272 for (int i=0; i < nxoption; i++) {
273 const wchar_t *option = xoptions[i];
274 size_t len;
275 wchar_t *sep = wcschr(option, L'=');
276 if (sep != NULL) {
277 len = (sep - option);
278 }
279 else {
280 len = wcslen(option);
281 }
282 if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
283 return option;
284 }
285 }
286 return NULL;
287}
288
289
290static _PyInitError
291preconfig_init_utf8_mode(_PyPreConfig *config, const _PyPreCmdline *cmdline)
292{
293 const wchar_t *xopt;
294 if (cmdline) {
295 xopt = _Py_get_xoption(cmdline->nxoption, cmdline->xoptions, L"utf8");
296 }
297 else {
298 xopt = NULL;
299 }
300 if (xopt) {
301 wchar_t *sep = wcschr(xopt, L'=');
302 if (sep) {
303 xopt = sep + 1;
304 if (wcscmp(xopt, L"1") == 0) {
305 config->utf8_mode = 1;
306 }
307 else if (wcscmp(xopt, L"0") == 0) {
308 config->utf8_mode = 0;
309 }
310 else {
311 return _Py_INIT_USER_ERR("invalid -X utf8 option value");
312 }
313 }
314 else {
315 config->utf8_mode = 1;
316 }
317 return _Py_INIT_OK();
318 }
319
320 const char *opt = _PyPreConfig_GetEnv(config, "PYTHONUTF8");
321 if (opt) {
322 if (strcmp(opt, "1") == 0) {
323 config->utf8_mode = 1;
324 }
325 else if (strcmp(opt, "0") == 0) {
326 config->utf8_mode = 0;
327 }
328 else {
329 return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
330 "variable value");
331 }
332 return _Py_INIT_OK();
333 }
334
335 return _Py_INIT_OK();
336}
337
338
339static void
340preconfig_init_locale(_PyPreConfig *config)
341{
342 /* Test also if coerce_c_locale equals 1: PYTHONCOERCECLOCALE=1 doesn't
343 imply that the C locale is always coerced. It is only coerced if
344 if the LC_CTYPE locale is "C". */
345 if (config->coerce_c_locale != 0) {
346 /* The C locale enables the C locale coercion (PEP 538) */
347 if (_Py_LegacyLocaleDetected()) {
348 config->coerce_c_locale = 1;
349 }
350 else {
351 config->coerce_c_locale = 0;
352 }
353 }
354}
355
356
357static _PyInitError
358preconfig_read(_PyPreConfig *config, const _PyPreCmdline *cmdline)
Victor Stinnercad1f742019-03-05 02:01:27 +0100359{
360 _PyPreConfig_GetGlobalConfig(config);
361
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100362 /* isolated and use_environment */
Victor Stinnercad1f742019-03-05 02:01:27 +0100363 if (config->isolated > 0) {
364 config->use_environment = 0;
365 }
366
367 /* Default values */
368 if (config->use_environment < 0) {
369 config->use_environment = 0;
370 }
371
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100372 /* legacy_windows_fs_encoding, utf8_mode, coerce_c_locale */
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100373 if (config->use_environment) {
374#ifdef MS_WINDOWS
375 _Py_get_env_flag(config, &config->legacy_windows_fs_encoding,
376 "PYTHONLEGACYWINDOWSFSENCODING");
377#endif
378
379 const char *env = _PyPreConfig_GetEnv(config, "PYTHONCOERCECLOCALE");
380 if (env) {
381 if (strcmp(env, "0") == 0) {
382 if (config->coerce_c_locale < 0) {
383 config->coerce_c_locale = 0;
384 }
385 }
386 else if (strcmp(env, "warn") == 0) {
387 config->coerce_c_locale_warn = 1;
388 }
389 else {
390 if (config->coerce_c_locale < 0) {
391 config->coerce_c_locale = 1;
392 }
393 }
394 }
395 }
396
397#ifdef MS_WINDOWS
398 if (config->legacy_windows_fs_encoding) {
399 config->utf8_mode = 0;
400 }
401#endif
402
403 if (config->utf8_mode < 0) {
404 _PyInitError err = preconfig_init_utf8_mode(config, cmdline);
405 if (_Py_INIT_FAILED(err)) {
406 return err;
407 }
408 }
409
410 if (config->coerce_c_locale != 0) {
411 preconfig_init_locale(config);
412 }
413
414#ifndef MS_WINDOWS
415 if (config->utf8_mode < 0) {
416 /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */
417 const char *ctype_loc = setlocale(LC_CTYPE, NULL);
418 if (ctype_loc != NULL
419 && (strcmp(ctype_loc, "C") == 0
420 || strcmp(ctype_loc, "POSIX") == 0))
421 {
422 config->utf8_mode = 1;
423 }
424 }
425#endif
426
427 if (config->coerce_c_locale < 0) {
428 config->coerce_c_locale = 0;
429 }
430 if (config->utf8_mode < 0) {
431 config->utf8_mode = 0;
432 }
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100433 if (config->coerce_c_locale < 0) {
434 config->coerce_c_locale = 0;
435 }
436
437 /* dev_mode */
438 if ((cmdline && _Py_get_xoption(cmdline->nxoption, cmdline->xoptions, L"dev"))
439 || _PyPreConfig_GetEnv(config, "PYTHONDEVMODE"))
440 {
441 config->dev_mode = 1;
442 }
443 if (config->dev_mode < 0) {
444 config->dev_mode = 0;
445 }
446
447 /* allocator */
448 if (config->dev_mode && config->allocator == NULL) {
Victor Stinnerc656e252019-03-06 01:13:43 +0100449 config->allocator = _PyMem_RawStrdup("debug");
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100450 if (config->allocator == NULL) {
451 return _Py_INIT_NO_MEMORY();
452 }
453 }
454
455 if (config->allocator == NULL) {
456 const char *allocator = _PyPreConfig_GetEnv(config, "PYTHONMALLOC");
457 if (allocator) {
458 config->allocator = _PyMem_RawStrdup(allocator);
459 if (config->allocator == NULL) {
460 return _Py_INIT_NO_MEMORY();
461 }
462 }
463 }
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100464
465 assert(config->coerce_c_locale >= 0);
466 assert(config->utf8_mode >= 0);
Victor Stinner6dcb5422019-03-05 02:44:12 +0100467 assert(config->isolated >= 0);
Victor Stinnercad1f742019-03-05 02:01:27 +0100468 assert(config->use_environment >= 0);
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100469 assert(config->dev_mode >= 0);
Victor Stinnercad1f742019-03-05 02:01:27 +0100470
471 return _Py_INIT_OK();
472}
473
474
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100475_PyInitError
476_PyPreConfig_Read(_PyPreConfig *config)
477{
478 return preconfig_read(config, NULL);
479}
480
481
Victor Stinnercad1f742019-03-05 02:01:27 +0100482int
483_PyPreConfig_AsDict(const _PyPreConfig *config, PyObject *dict)
484{
485#define SET_ITEM(KEY, EXPR) \
486 do { \
487 PyObject *obj = (EXPR); \
488 if (obj == NULL) { \
489 goto fail; \
490 } \
491 int res = PyDict_SetItemString(dict, (KEY), obj); \
492 Py_DECREF(obj); \
493 if (res < 0) { \
494 goto fail; \
495 } \
496 } while (0)
497#define SET_ITEM_INT(ATTR) \
498 SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR))
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100499#define FROM_STRING(STR) \
500 ((STR != NULL) ? \
501 PyUnicode_FromString(STR) \
502 : (Py_INCREF(Py_None), Py_None))
503#define SET_ITEM_STR(ATTR) \
504 SET_ITEM(#ATTR, FROM_STRING(config->ATTR))
Victor Stinnercad1f742019-03-05 02:01:27 +0100505
506 SET_ITEM_INT(isolated);
507 SET_ITEM_INT(use_environment);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100508 SET_ITEM_INT(coerce_c_locale);
509 SET_ITEM_INT(coerce_c_locale_warn);
510 SET_ITEM_INT(utf8_mode);
511#ifdef MS_WINDOWS
512 SET_ITEM_INT(legacy_windows_fs_encoding);
513#endif
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100514 SET_ITEM_INT(dev_mode);
515 SET_ITEM_STR(allocator);
Victor Stinnercad1f742019-03-05 02:01:27 +0100516 return 0;
517
518fail:
519 return -1;
520
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100521#undef FROM_STRING
Victor Stinnercad1f742019-03-05 02:01:27 +0100522#undef SET_ITEM
523#undef SET_ITEM_INT
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100524#undef SET_ITEM_STR
Victor Stinnercad1f742019-03-05 02:01:27 +0100525}
Victor Stinner6dcb5422019-03-05 02:44:12 +0100526
527
528/* Parse the command line arguments */
529static _PyInitError
530preconfig_parse_cmdline(_PyPreConfig *config, _PyPreCmdline *cmdline)
531{
532 _PyOS_ResetGetOpt();
533 /* Don't log parsing errors into stderr here: _PyCoreConfig_ReadFromArgv()
534 is responsible for that */
535 _PyOS_opterr = 0;
536 do {
537 int longindex = -1;
538 int c = _PyOS_GetOpt(cmdline->args->argc, cmdline->argv, &longindex);
539
540 if (c == EOF || c == 'c' || c == 'm') {
541 break;
542 }
543
544 switch (c) {
545 case 'E':
546 config->use_environment = 0;
547 break;
548
549 case 'I':
550 config->isolated++;
551 break;
552
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100553 case 'X':
554 {
555 _PyInitError err;
556 err = _Py_wstrlist_append(&cmdline->nxoption,
557 &cmdline->xoptions,
558 _PyOS_optarg);
559 if (_Py_INIT_FAILED(err)) {
560 return err;
561 }
562 break;
563 }
564
Victor Stinner6dcb5422019-03-05 02:44:12 +0100565 default:
566 /* ignore other argument:
567 handled by _PyCoreConfig_ReadFromArgv() */
568 break;
569 }
570 } while (1);
571
572 return _Py_INIT_OK();
573}
574
575
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100576static _PyInitError
577preconfig_from_argv(_PyPreConfig *config, const _PyArgv *args)
Victor Stinner6dcb5422019-03-05 02:44:12 +0100578{
579 _PyInitError err;
580
581 _PyPreCmdline cmdline;
582 memset(&cmdline, 0, sizeof(cmdline));
583 cmdline.args = args;
584
585 err = _PyArgv_Decode(cmdline.args, &cmdline.argv);
586 if (_Py_INIT_FAILED(err)) {
587 goto done;
588 }
589
590 err = preconfig_parse_cmdline(config, &cmdline);
591 if (_Py_INIT_FAILED(err)) {
592 goto done;
593 }
594
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100595 err = preconfig_read(config, &cmdline);
Victor Stinner6dcb5422019-03-05 02:44:12 +0100596 if (_Py_INIT_FAILED(err)) {
597 goto done;
598 }
599 err = _Py_INIT_OK();
600
601done:
602 precmdline_clear(&cmdline);
603 return err;
604}
605
606
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100607/* Read the preconfiguration. */
608_PyInitError
609_PyPreConfig_ReadFromArgv(_PyPreConfig *config, const _PyArgv *args)
610{
611 _PyInitError err;
612
613 err = _PyRuntime_Initialize();
614 if (_Py_INIT_FAILED(err)) {
615 return err;
616 }
617
618 char *init_ctype_locale = NULL;
619 int init_utf8_mode = Py_UTF8Mode;
620#ifdef MS_WINDOWS
621 int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
622#endif
623 _PyPreConfig save_config = _PyPreConfig_INIT;
624 int locale_coerced = 0;
625 int loops = 0;
626
627 /* copy LC_CTYPE locale */
628 const char *loc = setlocale(LC_CTYPE, NULL);
629 if (loc == NULL) {
630 err = _Py_INIT_ERR("failed to LC_CTYPE locale");
631 goto done;
632 }
633 init_ctype_locale = _PyMem_RawStrdup(loc);
634 if (init_ctype_locale == NULL) {
635 err = _Py_INIT_NO_MEMORY();
636 goto done;
637 }
638
639 if (_PyPreConfig_Copy(&save_config, config) < 0) {
640 err = _Py_INIT_NO_MEMORY();
641 goto done;
642 }
643
644 /* Set LC_CTYPE to the user preferred locale */
645 _Py_SetLocaleFromEnv(LC_CTYPE);
646
647 while (1) {
648 int utf8_mode = config->utf8_mode;
649
650 /* Watchdog to prevent an infinite loop */
651 loops++;
652 if (loops == 3) {
653 err = _Py_INIT_ERR("Encoding changed twice while "
654 "reading the configuration");
655 goto done;
656 }
657
658 /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
659 on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
660 Py_UTF8Mode = config->utf8_mode;
661#ifdef MS_WINDOWS
662 Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
663#endif
664
665 err = preconfig_from_argv(config, args);
666 if (_Py_INIT_FAILED(err)) {
667 goto done;
668 }
669
670 if (locale_coerced) {
671 config->coerce_c_locale = 1;
672 }
673
674 /* The legacy C locale assumes ASCII as the default text encoding, which
675 * causes problems not only for the CPython runtime, but also other
676 * components like GNU readline.
677 *
678 * Accordingly, when the CLI detects it, it attempts to coerce it to a
679 * more capable UTF-8 based alternative.
680 *
681 * See the documentation of the PYTHONCOERCECLOCALE setting for more
682 * details.
683 */
684 int encoding_changed = 0;
685 if (config->coerce_c_locale && !locale_coerced) {
686 locale_coerced = 1;
687 _Py_CoerceLegacyLocale(0);
688 encoding_changed = 1;
689 }
690
691 if (utf8_mode == -1) {
692 if (config->utf8_mode == 1) {
693 /* UTF-8 Mode enabled */
694 encoding_changed = 1;
695 }
696 }
697 else {
698 if (config->utf8_mode != utf8_mode) {
699 encoding_changed = 1;
700 }
701 }
702
703 if (!encoding_changed) {
704 break;
705 }
706
707 /* Reset the configuration before reading again the configuration,
708 just keep UTF-8 Mode value. */
709 int new_utf8_mode = config->utf8_mode;
710 int new_coerce_c_locale = config->coerce_c_locale;
711 if (_PyPreConfig_Copy(config, &save_config) < 0) {
712 err = _Py_INIT_NO_MEMORY();
713 goto done;
714 }
715 config->utf8_mode = new_utf8_mode;
716 config->coerce_c_locale = new_coerce_c_locale;
717
718 /* The encoding changed: read again the configuration
719 with the new encoding */
720 }
721 err = _Py_INIT_OK();
722
723done:
724 if (init_ctype_locale != NULL) {
725 setlocale(LC_CTYPE, init_ctype_locale);
726 }
727 _PyPreConfig_Clear(&save_config);
728 Py_UTF8Mode = init_utf8_mode ;
729#ifdef MS_WINDOWS
730 Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;
731#endif
732 return err;
733}
734
735
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100736static _PyInitError
Victor Stinnerc656e252019-03-06 01:13:43 +0100737_PyPreConfig_SetAllocator(_PyPreConfig *config)
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100738{
Victor Stinnerc656e252019-03-06 01:13:43 +0100739 assert(!_PyRuntime.core_initialized);
740
741 PyMemAllocatorEx old_alloc;
742 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
743
744 if (_PyMem_SetupAllocators(config->allocator) < 0) {
745 return _Py_INIT_USER_ERR("Unknown PYTHONMALLOC allocator");
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100746 }
Victor Stinnerc656e252019-03-06 01:13:43 +0100747
748 /* Copy the pre-configuration with the new allocator */
749 _PyPreConfig config2 = _PyPreConfig_INIT;
750 if (_PyPreConfig_Copy(&config2, config) < 0) {
751 _PyPreConfig_Clear(&config2);
752 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
753 return _Py_INIT_NO_MEMORY();
754 }
755
756 /* Free the old config and replace config with config2. Since config now
757 owns the data, don't free config2. */
758 PyMemAllocatorEx new_alloc;
759 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &new_alloc);
760 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
761 _PyPreConfig_Clear(config);
762 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &new_alloc);
763
764 *config = config2;
765
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100766 return _Py_INIT_OK();
767}
768
769
Victor Stinnerc656e252019-03-06 01:13:43 +0100770/* Write the pre-configuration.
771
772 If the memory allocator is changed, config is re-allocated with new
773 allocator. So calling _PyPreConfig_Clear(config) is safe after this call. */
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100774_PyInitError
Victor Stinnerc656e252019-03-06 01:13:43 +0100775_PyPreConfig_Write(_PyPreConfig *config)
Victor Stinner6dcb5422019-03-05 02:44:12 +0100776{
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100777 if (_PyRuntime.core_initialized) {
778 /* bpo-34008: Calling Py_Main() after Py_Initialize() ignores
779 the new configuration. */
Victor Stinnerc656e252019-03-06 01:13:43 +0100780 return _Py_INIT_OK();
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100781 }
782
783 if (config->allocator != NULL) {
Victor Stinnerc656e252019-03-06 01:13:43 +0100784 _PyInitError err = _PyPreConfig_SetAllocator(config);
785 if (_Py_INIT_FAILED(err)) {
786 return err;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100787 }
788 }
789
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100790 _PyPreConfig_SetGlobalConfig(config);
791
792 if (config->coerce_c_locale) {
793 _Py_CoerceLegacyLocale(config->coerce_c_locale_warn);
794 }
795
796 /* Set LC_CTYPE to the user preferred locale */
797 _Py_SetLocaleFromEnv(LC_CTYPE);
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100798
799 return _Py_INIT_OK();
Victor Stinner6dcb5422019-03-05 02:44:12 +0100800}