Skip to content

Commit 5a02e0d

Browse files
authored
bpo-36142: Add _PyPreConfig.utf8_mode (GH-12174)
* Move following fields from _PyCoreConfig to _PyPreConfig: * coerce_c_locale * coerce_c_locale_warn * legacy_windows_stdio * utf8_mode * _PyPreConfig_ReadFromArgv() is now responsible to choose the filesystem encoding * _PyPreConfig_Write() now sets the LC_CTYPE locale
1 parent 5b10b98 commit 5a02e0d

File tree

7 files changed

+465
-366
lines changed

7 files changed

+465
-366
lines changed

Include/cpython/coreconfig.h

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,42 @@ typedef struct {
6060
Set to 0 by -E command line option. If set to -1 (default), it is
6161
set to !Py_IgnoreEnvironmentFlag. */
6262
int use_environment;
63+
64+
int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
65+
int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
66+
67+
#ifdef MS_WINDOWS
68+
/* If greater than 1, use the "mbcs" encoding instead of the UTF-8
69+
encoding for the filesystem encoding.
70+
71+
Set to 1 if the PYTHONLEGACYWINDOWSFSENCODING environment variable is
72+
set to a non-empty string. If set to -1 (default), inherit
73+
Py_LegacyWindowsFSEncodingFlag value.
74+
75+
See PEP 529 for more details. */
76+
int legacy_windows_fs_encoding;
77+
#endif
78+
79+
/* Enable UTF-8 mode?
80+
Set by -X utf8 command line option and PYTHONUTF8 environment variable.
81+
If set to -1 (default), inherit Py_UTF8Mode value. */
82+
int utf8_mode;
6383
} _PyPreConfig;
6484

85+
#ifdef MS_WINDOWS
86+
# define _PyPreConfig_WINDOWS_INIT \
87+
.legacy_windows_fs_encoding = -1,
88+
#else
89+
# define _PyPreConfig_WINDOWS_INIT
90+
#endif
91+
6592
#define _PyPreConfig_INIT \
6693
(_PyPreConfig){ \
94+
_PyPreConfig_WINDOWS_INIT \
6795
.isolated = -1, \
68-
.use_environment = -1}
96+
.use_environment = -1, \
97+
.coerce_c_locale = -1, \
98+
.utf8_mode = -1}
6999

70100

71101
/* --- _PyCoreConfig ---------------------------------------------- */
@@ -95,8 +125,6 @@ typedef struct {
95125
int show_alloc_count; /* -X showalloccount */
96126
int dump_refs; /* PYTHONDUMPREFS */
97127
int malloc_stats; /* PYTHONMALLOCSTATS */
98-
int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
99-
int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
100128

101129
/* Python filesystem encoding and error handler:
102130
sys.getfilesystemencoding() and sys.getfilesystemencodeerrors().
@@ -134,11 +162,6 @@ typedef struct {
134162
char *filesystem_encoding;
135163
char *filesystem_errors;
136164

137-
/* Enable UTF-8 mode?
138-
Set by -X utf8 command line option and PYTHONUTF8 environment variable.
139-
If set to -1 (default), inherit Py_UTF8Mode value. */
140-
int utf8_mode;
141-
142165
wchar_t *pycache_prefix; /* PYTHONPYCACHEPREFIX, -X pycache_prefix=PATH */
143166

144167
wchar_t *program_name; /* Program name, see also Py_GetProgramName() */
@@ -277,16 +300,6 @@ typedef struct {
277300
char *stdio_errors;
278301

279302
#ifdef MS_WINDOWS
280-
/* If greater than 1, use the "mbcs" encoding instead of the UTF-8
281-
encoding for the filesystem encoding.
282-
283-
Set to 1 if the PYTHONLEGACYWINDOWSFSENCODING environment variable is
284-
set to a non-empty string. If set to -1 (default), inherit
285-
Py_LegacyWindowsFSEncodingFlag value.
286-
287-
See PEP 529 for more details. */
288-
int legacy_windows_fs_encoding;
289-
290303
/* If greater than zero, use io.FileIO instead of WindowsConsoleIO for sys
291304
standard streams.
292305
@@ -340,21 +353,19 @@ typedef struct {
340353

341354
#ifdef MS_WINDOWS
342355
# define _PyCoreConfig_WINDOWS_INIT \
343-
.legacy_windows_fs_encoding = -1, \
344356
.legacy_windows_stdio = -1,
345357
#else
346358
# define _PyCoreConfig_WINDOWS_INIT
347359
#endif
348360

349361
#define _PyCoreConfig_INIT \
350362
(_PyCoreConfig){ \
363+
_PyCoreConfig_WINDOWS_INIT \
351364
.preconfig = _PyPreConfig_INIT, \
352365
.install_signal_handlers = 1, \
353366
.use_hash_seed = -1, \
354367
.faulthandler = -1, \
355368
.tracemalloc = -1, \
356-
.coerce_c_locale = -1, \
357-
.utf8_mode = -1, \
358369
.argc = -1, \
359370
.nmodule_search_path = -1, \
360371
.site_import = -1, \
@@ -368,7 +379,6 @@ typedef struct {
368379
.quiet = -1, \
369380
.user_site_directory = -1, \
370381
.buffered_stdio = -1, \
371-
_PyCoreConfig_WINDOWS_INIT \
372382
._install_importlib = 1, \
373383
._check_hash_pycs_mode = "default", \
374384
._frozen = -1}

Include/internal/pycore_coreconfig.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,24 @@ PyAPI_FUNC(int) _Py_SetArgcArgv(int argc, wchar_t * const *argv);
3636

3737
/* --- _PyPreConfig ----------------------------------------------- */
3838

39+
PyAPI_FUNC(int) _Py_str_to_int(
40+
const char *str,
41+
int *result);
42+
PyAPI_FUNC(const wchar_t*) _Py_get_xoption(
43+
int nxoption,
44+
wchar_t * const *xoptions,
45+
const wchar_t *name);
46+
3947
PyAPI_FUNC(void) _PyPreConfig_Clear(_PyPreConfig *config);
4048
PyAPI_FUNC(int) _PyPreConfig_Copy(_PyPreConfig *config,
4149
const _PyPreConfig *config2);
4250
PyAPI_FUNC(void) _PyPreConfig_GetGlobalConfig(_PyPreConfig *config);
4351
PyAPI_FUNC(void) _PyPreConfig_SetGlobalConfig(const _PyPreConfig *config);
52+
PyAPI_FUNC(const char*) _PyPreConfig_GetEnv(const _PyPreConfig *config,
53+
const char *name);
54+
PyAPI_FUNC(void) _Py_get_env_flag(_PyPreConfig *config,
55+
int *flag,
56+
const char *name);
4457
PyAPI_FUNC(_PyInitError) _PyPreConfig_Read(_PyPreConfig *config);
4558
PyAPI_FUNC(int) _PyPreConfig_AsDict(const _PyPreConfig *config,
4659
PyObject *dict);

Programs/_testembed.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static int test_init_from_config(void)
461461

462462
putenv("PYTHONUTF8=0");
463463
Py_UTF8Mode = 0;
464-
config.utf8_mode = 1;
464+
config.preconfig.utf8_mode = 1;
465465

466466
putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
467467
config.pycache_prefix = L"conf_pycache_prefix";
@@ -610,8 +610,8 @@ static int test_init_isolated(void)
610610
config.preconfig.isolated = 1;
611611

612612
/* Set coerce_c_locale and utf8_mode to not depend on the locale */
613-
config.coerce_c_locale = 0;
614-
config.utf8_mode = 0;
613+
config.preconfig.coerce_c_locale = 0;
614+
config.preconfig.utf8_mode = 0;
615615
/* Use path starting with "./" avoids a search along the PATH */
616616
config.program_name = L"./_testembed";
617617

0 commit comments

Comments
 (0)