blob: 71a6ee6c072ef0fc7b6dfbc79388e779b12251a0 [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) \
Victor Stinnerdb719752019-05-01 05:35:33 +020010 ? _Py_INIT_ERR("cannot decode " NAME) \
Victor Stinner91b9ecf2019-03-01 17:52:56 +010011 : _Py_INIT_NO_MEMORY())
12
13
14/* --- File system encoding/errors -------------------------------- */
15
16/* The filesystem encoding is chosen by config_init_fs_encoding(),
Victor Stinner709d23d2019-05-02 14:56:30 -040017 see also initfsencoding().
18
19 Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors
20 are encoded to UTF-8. */
Victor Stinner91b9ecf2019-03-01 17:52:56 +010021const char *Py_FileSystemDefaultEncoding = NULL;
22int Py_HasFileSystemDefaultEncoding = 0;
23const char *Py_FileSystemDefaultEncodeErrors = NULL;
24int _Py_HasFileSystemDefaultEncodeErrors = 0;
25
26void
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. */
42int
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 Stinnerf8ba6f52019-03-26 16:58:50 +010069/* Decode bytes_argv using Py_DecodeLocale() */
Victor Stinner91b9ecf2019-03-01 17:52:56 +010070_PyInitError
Victor Stinner74f65682019-03-15 15:08:05 +010071_PyArgv_AsWstrList(const _PyArgv *args, _PyWstrList *list)
Victor Stinner91b9ecf2019-03-01 17:52:56 +010072{
Victor Stinner74f65682019-03-15 15:08:05 +010073 _PyWstrList wargv = _PyWstrList_INIT;
Victor Stinner91b9ecf2019-03-01 17:52:56 +010074 if (args->use_bytes_argv) {
Victor Stinner74f65682019-03-15 15:08:05 +010075 size_t size = sizeof(wchar_t*) * args->argc;
76 wargv.items = (wchar_t **)PyMem_RawMalloc(size);
77 if (wargv.items == NULL) {
Victor Stinner91b9ecf2019-03-01 17:52:56 +010078 return _Py_INIT_NO_MEMORY();
79 }
80
Victor Stinner74f65682019-03-15 15:08:05 +010081 for (Py_ssize_t i = 0; i < args->argc; i++) {
Victor Stinner91b9ecf2019-03-01 17:52:56 +010082 size_t len;
83 wchar_t *arg = Py_DecodeLocale(args->bytes_argv[i], &len);
84 if (arg == NULL) {
Victor Stinner74f65682019-03-15 15:08:05 +010085 _PyWstrList_Clear(&wargv);
Victor Stinner91b9ecf2019-03-01 17:52:56 +010086 return DECODE_LOCALE_ERR("command line arguments",
87 (Py_ssize_t)len);
88 }
Victor Stinner74f65682019-03-15 15:08:05 +010089 wargv.items[i] = arg;
90 wargv.length++;
Victor Stinner91b9ecf2019-03-01 17:52:56 +010091 }
Victor Stinner74f65682019-03-15 15:08:05 +010092
93 _PyWstrList_Clear(list);
94 *list = wargv;
Victor Stinner91b9ecf2019-03-01 17:52:56 +010095 }
96 else {
Victor Stinner74f65682019-03-15 15:08:05 +010097 wargv.length = args->argc;
Victor Stinner6d1c4672019-05-20 11:02:00 +020098 wargv.items = (wchar_t **)args->wchar_argv;
Victor Stinner74f65682019-03-15 15:08:05 +010099 if (_PyWstrList_Copy(list, &wargv) < 0) {
100 return _Py_INIT_NO_MEMORY();
101 }
Victor Stinner91b9ecf2019-03-01 17:52:56 +0100102 }
Victor Stinner91b9ecf2019-03-01 17:52:56 +0100103 return _Py_INIT_OK();
104}
Victor Stinnercad1f742019-03-05 02:01:27 +0100105
106
Victor Stinner6dcb5422019-03-05 02:44:12 +0100107/* --- _PyPreCmdline ------------------------------------------------- */
108
Victor Stinnerfa153762019-03-20 04:25:38 +0100109void
110_PyPreCmdline_Clear(_PyPreCmdline *cmdline)
Victor Stinner6dcb5422019-03-05 02:44:12 +0100111{
Victor Stinner74f65682019-03-15 15:08:05 +0100112 _PyWstrList_Clear(&cmdline->argv);
113 _PyWstrList_Clear(&cmdline->xoptions);
Victor Stinner6dcb5422019-03-05 02:44:12 +0100114}
115
116
Victor Stinnerfa153762019-03-20 04:25:38 +0100117_PyInitError
Victor Stinnerf72346c2019-03-25 17:54:58 +0100118_PyPreCmdline_SetArgv(_PyPreCmdline *cmdline, const _PyArgv *args)
Victor Stinnerfa153762019-03-20 04:25:38 +0100119{
120 return _PyArgv_AsWstrList(args, &cmdline->argv);
121}
122
123
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100124static void
Victor Stinnerf72346c2019-03-25 17:54:58 +0100125_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 Stinnerf72346c2019-03-25 17:54:58 +0100132 COPY_ATTR(isolated);
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100133 COPY_ATTR(use_environment);
Victor Stinner20004952019-03-26 02:31:11 +0100134 COPY_ATTR(dev_mode);
135
136#undef COPY_ATTR
137}
138
139
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100140static void
Victor Stinner20004952019-03-26 02:31:11 +0100141_PyPreCmdline_SetPreConfig(const _PyPreCmdline *cmdline, _PyPreConfig *config)
142{
143#define COPY_ATTR(ATTR) \
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100144 config->ATTR = cmdline->ATTR
Victor Stinner20004952019-03-26 02:31:11 +0100145
Victor Stinner20004952019-03-26 02:31:11 +0100146 COPY_ATTR(isolated);
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100147 COPY_ATTR(use_environment);
Victor Stinner20004952019-03-26 02:31:11 +0100148 COPY_ATTR(dev_mode);
Victor Stinnerf72346c2019-03-25 17:54:58 +0100149
150#undef COPY_ATTR
151}
152
153
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100154int
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100155_PyPreCmdline_SetCoreConfig(const _PyPreCmdline *cmdline, _PyCoreConfig *config)
156{
157#define COPY_ATTR(ATTR) \
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100158 config->ATTR = cmdline->ATTR
159
160 if (_PyWstrList_Extend(&config->xoptions, &cmdline->xoptions) < 0) {
161 return -1;
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100162 }
163
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100164 COPY_ATTR(isolated);
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100165 COPY_ATTR(use_environment);
Victor Stinner20004952019-03-26 02:31:11 +0100166 COPY_ATTR(dev_mode);
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100167 return 0;
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100168
169#undef COPY_ATTR
170}
171
172
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100173/* Parse the command line arguments */
174static _PyInitError
175precmdline_parse_cmdline(_PyPreCmdline *cmdline)
176{
Victor Stinner870b0352019-05-17 03:15:12 +0200177 const _PyWstrList *argv = &cmdline->argv;
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100178
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 Stinner6d1c4672019-05-20 11:02:00 +0200220_PyPreCmdline_Read(_PyPreCmdline *cmdline, const _PyPreConfig *preconfig)
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100221{
Victor Stinner6d1c4672019-05-20 11:02:00 +0200222 _PyPreCmdline_GetPreConfig(cmdline, preconfig);
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100223
Victor Stinner6d1c4672019-05-20 11:02:00 +0200224 if (preconfig->parse_argv) {
225 _PyInitError err = precmdline_parse_cmdline(cmdline);
226 if (_Py_INIT_FAILED(err)) {
227 return err;
228 }
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100229 }
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 Stinnerbab0db62019-05-18 03:21:27 +0200243 if ((cmdline->dev_mode < 0)
244 && (_Py_get_xoption(&cmdline->xoptions, L"dev")
245 || _Py_GetEnv(cmdline->use_environment, "PYTHONDEVMODE")))
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100246 {
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 Stinnercad1f742019-03-05 02:01:27 +0100261/* --- _PyPreConfig ----------------------------------------------- */
262
Victor Stinnerbab0db62019-05-18 03:21:27 +0200263
Victor Stinnercab5d072019-05-17 19:01:14 +0200264void
265_PyPreConfig_Init(_PyPreConfig *config)
266{
Victor Stinnerbab0db62019-05-18 03:21:27 +0200267 memset(config, 0, sizeof(*config));
268
269 config->_config_version = _Py_CONFIG_VERSION;
Victor Stinner6d1c4672019-05-20 11:02:00 +0200270 config->parse_argv = 0;
Victor Stinnerbab0db62019-05-18 03:21:27 +0200271 config->isolated = -1;
272 config->use_environment = -1;
273 config->configure_locale = 1;
274 config->utf8_mode = -2;
275 config->dev_mode = -1;
276 config->allocator = PYMEM_ALLOCATOR_NOT_SET;
277#ifdef MS_WINDOWS
278 config->legacy_windows_fs_encoding = -1;
279#endif
Victor Stinnercab5d072019-05-17 19:01:14 +0200280}
281
282
283void
284_PyPreConfig_InitPythonConfig(_PyPreConfig *config)
285{
286 _PyPreConfig_Init(config);
287
Victor Stinner6d1c4672019-05-20 11:02:00 +0200288 config->parse_argv = 1;
Victor Stinnercab5d072019-05-17 19:01:14 +0200289 /* Set to -1 to enable C locale coercion (PEP 538) and UTF-8 Mode (PEP 540)
290 depending on the LC_CTYPE locale, PYTHONUTF8 and PYTHONCOERCECLOCALE
291 environment variables. */
292 config->coerce_c_locale = -1;
293 config->coerce_c_locale_warn = -1;
294 config->utf8_mode = -1;
295}
296
297
298void
299_PyPreConfig_InitIsolatedConfig(_PyPreConfig *config)
300{
301 _PyPreConfig_Init(config);
302
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200303 config->configure_locale = 0;
Victor Stinnercab5d072019-05-17 19:01:14 +0200304 config->isolated = 1;
305 config->use_environment = 0;
Victor Stinnerbab0db62019-05-18 03:21:27 +0200306 config->utf8_mode = 0;
307 config->dev_mode = 0;
Victor Stinnercab5d072019-05-17 19:01:14 +0200308#ifdef MS_WINDOWS
309 config->legacy_windows_fs_encoding = 0;
310#endif
Victor Stinnercab5d072019-05-17 19:01:14 +0200311}
312
313
Victor Stinnerb5947842019-05-18 00:38:16 +0200314void
Victor Stinner6d1c4672019-05-20 11:02:00 +0200315_PyPreConfig_InitFromPreConfig(_PyPreConfig *config,
316 const _PyPreConfig *config2)
317{
318 _PyPreConfig_Init(config);
319 _PyPreConfig_Copy(config, config2);
320}
321
322
323void
324_PyPreConfig_InitFromCoreConfig(_PyPreConfig *config,
325 const _PyCoreConfig *coreconfig)
326{
327 _PyCoreConfigInitEnum config_init = (_PyCoreConfigInitEnum)coreconfig->_config_init;
328 switch (config_init) {
329 case _PyCoreConfig_INIT_PYTHON:
330 _PyPreConfig_InitPythonConfig(config);
331 break;
332 case _PyCoreConfig_INIT_ISOLATED:
333 _PyPreConfig_InitIsolatedConfig(config);
334 break;
335 case _PyCoreConfig_INIT:
336 default:
337 _PyPreConfig_Init(config);
338 }
339 _PyPreConfig_GetCoreConfig(config, coreconfig);
340}
341
342
343void
Victor Stinnercad1f742019-03-05 02:01:27 +0100344_PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2)
345{
Victor Stinnercad1f742019-03-05 02:01:27 +0100346#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
347
Victor Stinner6d1c4672019-05-20 11:02:00 +0200348 COPY_ATTR(parse_argv);
Victor Stinnercad1f742019-03-05 02:01:27 +0100349 COPY_ATTR(isolated);
350 COPY_ATTR(use_environment);
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200351 COPY_ATTR(configure_locale);
Victor Stinner20004952019-03-26 02:31:11 +0100352 COPY_ATTR(dev_mode);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100353 COPY_ATTR(coerce_c_locale);
354 COPY_ATTR(coerce_c_locale_warn);
Victor Stinnerb5947842019-05-18 00:38:16 +0200355 COPY_ATTR(utf8_mode);
356 COPY_ATTR(allocator);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100357#ifdef MS_WINDOWS
358 COPY_ATTR(legacy_windows_fs_encoding);
359#endif
Victor Stinnercad1f742019-03-05 02:01:27 +0100360
361#undef COPY_ATTR
Victor Stinnercad1f742019-03-05 02:01:27 +0100362}
363
364
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100365PyObject*
366_PyPreConfig_AsDict(const _PyPreConfig *config)
367{
368 PyObject *dict;
369
370 dict = PyDict_New();
371 if (dict == NULL) {
372 return NULL;
373 }
374
Victor Stinner6d1c4672019-05-20 11:02:00 +0200375#define SET_ITEM_INT(ATTR) \
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100376 do { \
Victor Stinner6d1c4672019-05-20 11:02:00 +0200377 PyObject *obj = PyLong_FromLong(config->ATTR); \
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100378 if (obj == NULL) { \
379 goto fail; \
380 } \
Victor Stinner6d1c4672019-05-20 11:02:00 +0200381 int res = PyDict_SetItemString(dict, #ATTR, obj); \
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100382 Py_DECREF(obj); \
383 if (res < 0) { \
384 goto fail; \
385 } \
386 } while (0)
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100387
Victor Stinner6d1c4672019-05-20 11:02:00 +0200388 SET_ITEM_INT(parse_argv);
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100389 SET_ITEM_INT(isolated);
390 SET_ITEM_INT(use_environment);
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200391 SET_ITEM_INT(configure_locale);
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100392 SET_ITEM_INT(coerce_c_locale);
393 SET_ITEM_INT(coerce_c_locale_warn);
394 SET_ITEM_INT(utf8_mode);
395#ifdef MS_WINDOWS
396 SET_ITEM_INT(legacy_windows_fs_encoding);
397#endif
398 SET_ITEM_INT(dev_mode);
Victor Stinnerb16b4e42019-05-17 15:20:52 +0200399 SET_ITEM_INT(allocator);
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100400 return dict;
401
402fail:
403 Py_DECREF(dict);
404 return NULL;
405
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100406#undef SET_ITEM_INT
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100407}
408
409
Victor Stinner5ac27a52019-03-27 13:40:14 +0100410void
Victor Stinnercab5d072019-05-17 19:01:14 +0200411_PyPreConfig_GetCoreConfig(_PyPreConfig *config,
Victor Stinner5ac27a52019-03-27 13:40:14 +0100412 const _PyCoreConfig *core_config)
413{
414#define COPY_ATTR(ATTR) \
415 if (core_config->ATTR != -1) { \
416 config->ATTR = core_config->ATTR; \
417 }
418
Victor Stinner6d1c4672019-05-20 11:02:00 +0200419 COPY_ATTR(parse_argv);
Victor Stinner5ac27a52019-03-27 13:40:14 +0100420 COPY_ATTR(isolated);
421 COPY_ATTR(use_environment);
422 COPY_ATTR(dev_mode);
423
424#undef COPY_ATTR
425}
426
427
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100428static void
Victor Stinnercad1f742019-03-05 02:01:27 +0100429_PyPreConfig_GetGlobalConfig(_PyPreConfig *config)
430{
431#define COPY_FLAG(ATTR, VALUE) \
Victor Stinnercab5d072019-05-17 19:01:14 +0200432 if (config->ATTR < 0) { \
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100433 config->ATTR = VALUE; \
434 }
Victor Stinnercad1f742019-03-05 02:01:27 +0100435#define COPY_NOT_FLAG(ATTR, VALUE) \
Victor Stinnercab5d072019-05-17 19:01:14 +0200436 if (config->ATTR < 0) { \
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100437 config->ATTR = !(VALUE); \
438 }
Victor Stinnercad1f742019-03-05 02:01:27 +0100439
440 COPY_FLAG(isolated, Py_IsolatedFlag);
441 COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100442#ifdef MS_WINDOWS
443 COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
444#endif
Victor Stinnercab5d072019-05-17 19:01:14 +0200445 if (config->utf8_mode == -2) {
446 config->utf8_mode = Py_UTF8Mode;
Victor Stinnerd929f182019-03-27 18:28:46 +0100447 }
Victor Stinnercad1f742019-03-05 02:01:27 +0100448
449#undef COPY_FLAG
450#undef COPY_NOT_FLAG
451}
452
453
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100454static void
Victor Stinnercad1f742019-03-05 02:01:27 +0100455_PyPreConfig_SetGlobalConfig(const _PyPreConfig *config)
456{
457#define COPY_FLAG(ATTR, VAR) \
Victor Stinnercab5d072019-05-17 19:01:14 +0200458 if (config->ATTR >= 0) { \
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100459 VAR = config->ATTR; \
460 }
Victor Stinnercad1f742019-03-05 02:01:27 +0100461#define COPY_NOT_FLAG(ATTR, VAR) \
Victor Stinnercab5d072019-05-17 19:01:14 +0200462 if (config->ATTR >= 0) { \
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100463 VAR = !config->ATTR; \
464 }
Victor Stinnercad1f742019-03-05 02:01:27 +0100465
466 COPY_FLAG(isolated, Py_IsolatedFlag);
467 COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100468#ifdef MS_WINDOWS
469 COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
470#endif
471 COPY_FLAG(utf8_mode, Py_UTF8Mode);
Victor Stinnercad1f742019-03-05 02:01:27 +0100472
473#undef COPY_FLAG
474#undef COPY_NOT_FLAG
475}
476
477
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100478const char*
Victor Stinnerf78a5e92019-03-26 00:03:15 +0100479_Py_GetEnv(int use_environment, const char *name)
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100480{
Victor Stinnerf78a5e92019-03-26 00:03:15 +0100481 assert(use_environment >= 0);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100482
Victor Stinnerf78a5e92019-03-26 00:03:15 +0100483 if (!use_environment) {
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100484 return NULL;
485 }
486
487 const char *var = getenv(name);
488 if (var && var[0] != '\0') {
489 return var;
490 }
491 else {
492 return NULL;
493 }
494}
495
496
497int
498_Py_str_to_int(const char *str, int *result)
499{
500 const char *endptr = str;
501 errno = 0;
502 long value = strtol(str, (char **)&endptr, 10);
503 if (*endptr != '\0' || errno == ERANGE) {
504 return -1;
505 }
506 if (value < INT_MIN || value > INT_MAX) {
507 return -1;
508 }
509
510 *result = (int)value;
511 return 0;
512}
513
514
515void
Victor Stinnerf78a5e92019-03-26 00:03:15 +0100516_Py_get_env_flag(int use_environment, int *flag, const char *name)
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100517{
Victor Stinnerf78a5e92019-03-26 00:03:15 +0100518 const char *var = _Py_GetEnv(use_environment, name);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100519 if (!var) {
520 return;
521 }
522 int value;
523 if (_Py_str_to_int(var, &value) < 0 || value < 0) {
524 /* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
525 value = 1;
526 }
527 if (*flag < value) {
528 *flag = value;
529 }
530}
531
532
533const wchar_t*
Victor Stinner74f65682019-03-15 15:08:05 +0100534_Py_get_xoption(const _PyWstrList *xoptions, const wchar_t *name)
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100535{
Victor Stinner74f65682019-03-15 15:08:05 +0100536 for (Py_ssize_t i=0; i < xoptions->length; i++) {
537 const wchar_t *option = xoptions->items[i];
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100538 size_t len;
539 wchar_t *sep = wcschr(option, L'=');
540 if (sep != NULL) {
541 len = (sep - option);
542 }
543 else {
544 len = wcslen(option);
545 }
546 if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
547 return option;
548 }
549 }
550 return NULL;
551}
552
553
554static _PyInitError
555preconfig_init_utf8_mode(_PyPreConfig *config, const _PyPreCmdline *cmdline)
556{
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100557#ifdef MS_WINDOWS
558 if (config->legacy_windows_fs_encoding) {
559 config->utf8_mode = 0;
560 }
561#endif
562
563 if (config->utf8_mode >= 0) {
564 return _Py_INIT_OK();
565 }
566
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100567 const wchar_t *xopt;
568 if (cmdline) {
Victor Stinner74f65682019-03-15 15:08:05 +0100569 xopt = _Py_get_xoption(&cmdline->xoptions, L"utf8");
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100570 }
571 else {
572 xopt = NULL;
573 }
574 if (xopt) {
575 wchar_t *sep = wcschr(xopt, L'=');
576 if (sep) {
577 xopt = sep + 1;
578 if (wcscmp(xopt, L"1") == 0) {
579 config->utf8_mode = 1;
580 }
581 else if (wcscmp(xopt, L"0") == 0) {
582 config->utf8_mode = 0;
583 }
584 else {
Victor Stinnerdb719752019-05-01 05:35:33 +0200585 return _Py_INIT_ERR("invalid -X utf8 option value");
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100586 }
587 }
588 else {
589 config->utf8_mode = 1;
590 }
591 return _Py_INIT_OK();
592 }
593
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100594 const char *opt = _Py_GetEnv(config->use_environment, "PYTHONUTF8");
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100595 if (opt) {
596 if (strcmp(opt, "1") == 0) {
597 config->utf8_mode = 1;
598 }
599 else if (strcmp(opt, "0") == 0) {
600 config->utf8_mode = 0;
601 }
602 else {
Victor Stinnerdb719752019-05-01 05:35:33 +0200603 return _Py_INIT_ERR("invalid PYTHONUTF8 environment "
604 "variable value");
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100605 }
606 return _Py_INIT_OK();
607 }
608
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100609
610#ifndef MS_WINDOWS
611 if (config->utf8_mode < 0) {
612 /* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */
613 const char *ctype_loc = setlocale(LC_CTYPE, NULL);
614 if (ctype_loc != NULL
615 && (strcmp(ctype_loc, "C") == 0
616 || strcmp(ctype_loc, "POSIX") == 0))
617 {
618 config->utf8_mode = 1;
619 }
620 }
621#endif
622
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100623 if (config->utf8_mode < 0) {
624 config->utf8_mode = 0;
625 }
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100626 return _Py_INIT_OK();
627}
628
629
630static void
631preconfig_init_coerce_c_locale(_PyPreConfig *config)
632{
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200633 if (!config->configure_locale) {
634 config->coerce_c_locale = 0;
635 config->coerce_c_locale_warn = 0;
636 return;
637 }
638
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100639 const char *env = _Py_GetEnv(config->use_environment, "PYTHONCOERCECLOCALE");
640 if (env) {
641 if (strcmp(env, "0") == 0) {
642 if (config->coerce_c_locale < 0) {
643 config->coerce_c_locale = 0;
644 }
645 }
646 else if (strcmp(env, "warn") == 0) {
Victor Stinnercab5d072019-05-17 19:01:14 +0200647 if (config->coerce_c_locale_warn < 0) {
648 config->coerce_c_locale_warn = 1;
649 }
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100650 }
651 else {
652 if (config->coerce_c_locale < 0) {
653 config->coerce_c_locale = 1;
654 }
655 }
656 }
657
658 /* Test if coerce_c_locale equals to -1 or equals to 1:
659 PYTHONCOERCECLOCALE=1 doesn't imply that the C locale is always coerced.
660 It is only coerced if if the LC_CTYPE locale is "C". */
Victor Stinnercab5d072019-05-17 19:01:14 +0200661 if (config->coerce_c_locale < 0 || config->coerce_c_locale == 1) {
662 /* The C locale enables the C locale coercion (PEP 538) */
663 if (_Py_LegacyLocaleDetected()) {
664 config->coerce_c_locale = 2;
665 }
666 else {
667 config->coerce_c_locale = 0;
668 }
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100669 }
670
Victor Stinnercab5d072019-05-17 19:01:14 +0200671 if (config->coerce_c_locale_warn < 0) {
672 config->coerce_c_locale_warn = 0;
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100673 }
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100674}
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100675
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100676
677static _PyInitError
678preconfig_init_allocator(_PyPreConfig *config)
679{
Victor Stinnerb16b4e42019-05-17 15:20:52 +0200680 if (config->allocator == PYMEM_ALLOCATOR_NOT_SET) {
Victor Stinner25d13f32019-03-06 12:51:53 +0100681 /* bpo-34247. The PYTHONMALLOC environment variable has the priority
682 over PYTHONDEV env var and "-X dev" command line option.
683 For example, PYTHONMALLOC=malloc PYTHONDEVMODE=1 sets the memory
684 allocators to "malloc" (and not to "debug"). */
Victor Stinnerb16b4e42019-05-17 15:20:52 +0200685 const char *envvar = _Py_GetEnv(config->use_environment, "PYTHONMALLOC");
686 if (envvar) {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200687 PyMemAllocatorName name;
688 if (_PyMem_GetAllocatorName(envvar, &name) < 0) {
Victor Stinnerb16b4e42019-05-17 15:20:52 +0200689 return _Py_INIT_ERR("PYTHONMALLOC: unknown allocator");
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100690 }
Victor Stinner6d1c4672019-05-20 11:02:00 +0200691 config->allocator = (int)name;
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100692 }
693 }
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100694
Victor Stinnerb16b4e42019-05-17 15:20:52 +0200695 if (config->dev_mode && config->allocator == PYMEM_ALLOCATOR_NOT_SET) {
696 config->allocator = PYMEM_ALLOCATOR_DEBUG;
Victor Stinner25d13f32019-03-06 12:51:53 +0100697 }
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100698 return _Py_INIT_OK();
699}
700
701
702static _PyInitError
Victor Stinner5ac27a52019-03-27 13:40:14 +0100703preconfig_read(_PyPreConfig *config, _PyPreCmdline *cmdline)
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100704{
705 _PyInitError err;
706
Victor Stinner5ac27a52019-03-27 13:40:14 +0100707 err = _PyPreCmdline_Read(cmdline, config);
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100708 if (_Py_INIT_FAILED(err)) {
709 return err;
710 }
711
712 _PyPreCmdline_SetPreConfig(cmdline, config);
713
714 /* legacy_windows_fs_encoding, coerce_c_locale, utf8_mode */
715#ifdef MS_WINDOWS
716 _Py_get_env_flag(config->use_environment,
717 &config->legacy_windows_fs_encoding,
718 "PYTHONLEGACYWINDOWSFSENCODING");
719#endif
720
721 preconfig_init_coerce_c_locale(config);
722
723 err = preconfig_init_utf8_mode(config, cmdline);
724 if (_Py_INIT_FAILED(err)) {
725 return err;
726 }
727
728 /* allocator */
729 err = preconfig_init_allocator(config);
730 if (_Py_INIT_FAILED(err)) {
731 return err;
732 }
Victor Stinner25d13f32019-03-06 12:51:53 +0100733
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100734 assert(config->coerce_c_locale >= 0);
Victor Stinnercab5d072019-05-17 19:01:14 +0200735 assert(config->coerce_c_locale_warn >= 0);
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100736#ifdef MS_WINDOWS
737 assert(config->legacy_windows_fs_encoding >= 0);
738#endif
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100739 assert(config->utf8_mode >= 0);
Victor Stinner6dcb5422019-03-05 02:44:12 +0100740 assert(config->isolated >= 0);
Victor Stinnercad1f742019-03-05 02:01:27 +0100741 assert(config->use_environment >= 0);
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100742 assert(config->dev_mode >= 0);
Victor Stinnercad1f742019-03-05 02:01:27 +0100743
744 return _Py_INIT_OK();
745}
746
747
Victor Stinner4fffd382019-03-06 01:44:31 +0100748/* Read the configuration from:
749
750 - command line arguments
751 - environment variables
752 - Py_xxx global configuration variables
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100753 - the LC_CTYPE locale */
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100754_PyInitError
Victor Stinner5ac27a52019-03-27 13:40:14 +0100755_PyPreConfig_Read(_PyPreConfig *config, const _PyArgv *args)
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100756{
757 _PyInitError err;
758
759 err = _PyRuntime_Initialize();
760 if (_Py_INIT_FAILED(err)) {
761 return err;
762 }
763
Victor Stinnerf29084d2019-03-20 02:20:13 +0100764 _PyPreConfig_GetGlobalConfig(config);
765
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100766 /* Copy LC_CTYPE locale, since it's modified later */
767 const char *loc = setlocale(LC_CTYPE, NULL);
768 if (loc == NULL) {
769 return _Py_INIT_ERR("failed to LC_CTYPE locale");
770 }
771 char *init_ctype_locale = _PyMem_RawStrdup(loc);
772 if (init_ctype_locale == NULL) {
773 return _Py_INIT_NO_MEMORY();
774 }
775
776 /* Save the config to be able to restore it if encodings change */
Victor Stinnercab5d072019-05-17 19:01:14 +0200777 _PyPreConfig save_config;
Victor Stinner6d1c4672019-05-20 11:02:00 +0200778 _PyPreConfig_InitFromPreConfig(&save_config, config);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100779
780 /* Set LC_CTYPE to the user preferred locale */
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200781 if (config->configure_locale) {
782 _Py_SetLocaleFromEnv(LC_CTYPE);
783 }
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100784
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100785 _PyPreCmdline cmdline = _PyPreCmdline_INIT;
Victor Stinner6a8c3132019-04-05 11:44:04 +0200786 int init_utf8_mode = Py_UTF8Mode;
787#ifdef MS_WINDOWS
788 int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
789#endif
790
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100791 if (args) {
792 err = _PyPreCmdline_SetArgv(&cmdline, args);
793 if (_Py_INIT_FAILED(err)) {
794 goto done;
795 }
796 }
797
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100798 int locale_coerced = 0;
799 int loops = 0;
800
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100801 while (1) {
802 int utf8_mode = config->utf8_mode;
803
804 /* Watchdog to prevent an infinite loop */
805 loops++;
806 if (loops == 3) {
807 err = _Py_INIT_ERR("Encoding changed twice while "
808 "reading the configuration");
809 goto done;
810 }
811
812 /* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
813 on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
814 Py_UTF8Mode = config->utf8_mode;
815#ifdef MS_WINDOWS
816 Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
817#endif
818
Victor Stinner5ac27a52019-03-27 13:40:14 +0100819 err = preconfig_read(config, &cmdline);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100820 if (_Py_INIT_FAILED(err)) {
821 goto done;
822 }
823
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100824 /* The legacy C locale assumes ASCII as the default text encoding, which
825 * causes problems not only for the CPython runtime, but also other
826 * components like GNU readline.
827 *
828 * Accordingly, when the CLI detects it, it attempts to coerce it to a
829 * more capable UTF-8 based alternative.
830 *
831 * See the documentation of the PYTHONCOERCECLOCALE setting for more
832 * details.
833 */
834 int encoding_changed = 0;
835 if (config->coerce_c_locale && !locale_coerced) {
836 locale_coerced = 1;
837 _Py_CoerceLegacyLocale(0);
838 encoding_changed = 1;
839 }
840
841 if (utf8_mode == -1) {
842 if (config->utf8_mode == 1) {
843 /* UTF-8 Mode enabled */
844 encoding_changed = 1;
845 }
846 }
847 else {
848 if (config->utf8_mode != utf8_mode) {
849 encoding_changed = 1;
850 }
851 }
852
853 if (!encoding_changed) {
854 break;
855 }
856
857 /* Reset the configuration before reading again the configuration,
858 just keep UTF-8 Mode value. */
859 int new_utf8_mode = config->utf8_mode;
860 int new_coerce_c_locale = config->coerce_c_locale;
Victor Stinnerb5947842019-05-18 00:38:16 +0200861 _PyPreConfig_Copy(config, &save_config);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100862 config->utf8_mode = new_utf8_mode;
863 config->coerce_c_locale = new_coerce_c_locale;
864
865 /* The encoding changed: read again the configuration
866 with the new encoding */
867 }
868 err = _Py_INIT_OK();
869
870done:
871 if (init_ctype_locale != NULL) {
872 setlocale(LC_CTYPE, init_ctype_locale);
Victor Stinnerc1834442019-03-18 22:24:28 +0100873 PyMem_RawFree(init_ctype_locale);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100874 }
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100875 Py_UTF8Mode = init_utf8_mode ;
876#ifdef MS_WINDOWS
877 Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;
878#endif
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100879 _PyPreCmdline_Clear(&cmdline);
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100880 return err;
881}
882
883
Victor Stinner4fffd382019-03-06 01:44:31 +0100884/* Write the pre-configuration:
885
886 - set the memory allocators
887 - set Py_xxx global configuration variables
888 - set the LC_CTYPE locale (coerce C locale, PEP 538) and set the UTF-8 mode
889 (PEP 540)
Victor Stinnerc656e252019-03-06 01:13:43 +0100890
891 If the memory allocator is changed, config is re-allocated with new
Victor Stinner4fffd382019-03-06 01:44:31 +0100892 allocator. So calling _PyPreConfig_Clear(config) is safe after this call.
893
894 Do nothing if called after Py_Initialize(): ignore the new
895 pre-configuration. */
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100896_PyInitError
Victor Stinnerb16b4e42019-05-17 15:20:52 +0200897_PyPreConfig_Write(const _PyPreConfig *config)
Victor Stinner6dcb5422019-03-05 02:44:12 +0100898{
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100899 if (_PyRuntime.core_initialized) {
Victor Stinner4fffd382019-03-06 01:44:31 +0100900 /* bpo-34008: Calling this functions after Py_Initialize() ignores
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100901 the new configuration. */
Victor Stinnerc656e252019-03-06 01:13:43 +0100902 return _Py_INIT_OK();
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100903 }
904
Victor Stinner6d1c4672019-05-20 11:02:00 +0200905 PyMemAllocatorName name = (PyMemAllocatorName)config->allocator;
906 if (name != PYMEM_ALLOCATOR_NOT_SET) {
907 if (_PyMem_SetupAllocators(name) < 0) {
Victor Stinnerb16b4e42019-05-17 15:20:52 +0200908 return _Py_INIT_ERR("Unknown PYTHONMALLOC allocator");
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100909 }
910 }
911
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100912 _PyPreConfig_SetGlobalConfig(config);
913
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200914 if (config->configure_locale) {
915 if (config->coerce_c_locale) {
916 _Py_CoerceLegacyLocale(config->coerce_c_locale_warn);
917 }
Victor Stinner5a02e0d2019-03-05 12:32:09 +0100918
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200919 /* Set LC_CTYPE to the user preferred locale */
920 _Py_SetLocaleFromEnv(LC_CTYPE);
921 }
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100922
Victor Stinner6d5ee972019-03-23 12:05:43 +0100923 /* Write the new pre-configuration into _PyRuntime */
Victor Stinnerb5947842019-05-18 00:38:16 +0200924 _PyPreConfig_Copy(&_PyRuntime.preconfig, config);
Victor Stinner6d5ee972019-03-23 12:05:43 +0100925
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100926 return _Py_INIT_OK();
Victor Stinner6dcb5422019-03-05 02:44:12 +0100927}