Skip to content

Commit 188ebfa

Browse files
authored
bpo-34589: Make _PyCoreConfig.coerce_c_locale private (GH-9371)
_PyCoreConfig: * Rename coerce_c_locale to _coerce_c_locale * Rename coerce_c_locale_warn to _coerce_c_locale_warn These fields are now private (name prefixed by "_").
1 parent c62ab28 commit 188ebfa

File tree

7 files changed

+46
-32
lines changed

7 files changed

+46
-32
lines changed

Include/coreconfig.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ typedef struct {
6363
int show_alloc_count; /* -X showalloccount */
6464
int dump_refs; /* PYTHONDUMPREFS */
6565
int malloc_stats; /* PYTHONMALLOCSTATS */
66-
int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
67-
int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
6866

6967
/* Python filesystem encoding and error handler:
7068
sys.getfilesystemencoding() and sys.getfilesystemencodeerrors().
@@ -297,6 +295,22 @@ typedef struct {
297295
If set to -1 (default), inherit Py_FrozenFlag value. */
298296
int _frozen;
299297

298+
/* C locale coercion (PEP 538).
299+
300+
The option is enabled by the PYTHONCOERCECLOCALE environment
301+
variable. The option is also enabled if the LC_CTYPE locale is "C"
302+
and a target locale (ex: "C.UTF-8") is supported by the platform.
303+
304+
See also the _coerce_c_locale_warn option. */
305+
int _coerce_c_locale;
306+
307+
/* C locale coercion warning (PEP 538).
308+
309+
Enabled by the PYTHONCOERCECLOCALE=warn environment variable.
310+
311+
See also the _coerce_c_locale option. */
312+
int _coerce_c_locale_warn;
313+
300314
} _PyCoreConfig;
301315

302316
#ifdef MS_WINDOWS
@@ -314,7 +328,7 @@ typedef struct {
314328
.use_hash_seed = -1, \
315329
.faulthandler = -1, \
316330
.tracemalloc = -1, \
317-
.coerce_c_locale = -1, \
331+
._coerce_c_locale = -1, \
318332
.utf8_mode = -1, \
319333
.argc = -1, \
320334
.nmodule_search_path = -1, \

Lib/test/test_embed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
277277
'filesystem_errors': None,
278278

279279
'utf8_mode': 0,
280-
'coerce_c_locale': 0,
281-
'coerce_c_locale_warn': 0,
282280

283281
'pycache_prefix': NULL_STR,
284282
'program_name': './_testembed',
@@ -306,6 +304,8 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
306304
'_install_importlib': 1,
307305
'_check_hash_pycs_mode': 'default',
308306
'_frozen': 0,
307+
'_coerce_c_locale': 0,
308+
'_coerce_c_locale_warn': 0,
309309
}
310310

311311
def get_stdio_encoding(self, env):

Modules/_testcapimodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4701,10 +4701,6 @@ get_coreconfig(PyObject *self, PyObject *Py_UNUSED(args))
47014701
PyLong_FromLong(config->dump_refs));
47024702
SET_ITEM("malloc_stats",
47034703
PyLong_FromLong(config->malloc_stats));
4704-
SET_ITEM("coerce_c_locale",
4705-
PyLong_FromLong(config->coerce_c_locale));
4706-
SET_ITEM("coerce_c_locale_warn",
4707-
PyLong_FromLong(config->coerce_c_locale_warn));
47084704
SET_ITEM("filesystem_encoding",
47094705
FROM_STRING(config->filesystem_encoding));
47104706
SET_ITEM("filesystem_errors",
@@ -4783,6 +4779,10 @@ get_coreconfig(PyObject *self, PyObject *Py_UNUSED(args))
47834779
FROM_STRING(config->_check_hash_pycs_mode));
47844780
SET_ITEM("_frozen",
47854781
PyLong_FromLong(config->_frozen));
4782+
SET_ITEM("_coerce_c_locale",
4783+
PyLong_FromLong(config->_coerce_c_locale));
4784+
SET_ITEM("_coerce_c_locale_warn",
4785+
PyLong_FromLong(config->_coerce_c_locale_warn));
47864786

47874787
return dict;
47884788

Modules/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,9 +1342,9 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
13421342
* See the documentation of the PYTHONCOERCECLOCALE setting for more
13431343
* details.
13441344
*/
1345-
if (config->coerce_c_locale && !locale_coerced) {
1345+
if (config->_coerce_c_locale && !locale_coerced) {
13461346
locale_coerced = 1;
1347-
_Py_CoerceLegacyLocale(config->coerce_c_locale_warn);
1347+
_Py_CoerceLegacyLocale(config->_coerce_c_locale_warn);
13481348
encoding_changed = 1;
13491349
}
13501350

@@ -1367,15 +1367,15 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
13671367
/* Reset the configuration before reading again the configuration,
13681368
just keep UTF-8 Mode value. */
13691369
int new_utf8_mode = config->utf8_mode;
1370-
int new_coerce_c_locale = config->coerce_c_locale;
1370+
int new_coerce_c_locale = config->_coerce_c_locale;
13711371
if (_PyCoreConfig_Copy(config, &save_config) < 0) {
13721372
pymain->err = _Py_INIT_NO_MEMORY();
13731373
goto done;
13741374
}
13751375
pymain_clear_cmdline(pymain, cmdline);
13761376
memset(cmdline, 0, sizeof(*cmdline));
13771377
config->utf8_mode = new_utf8_mode;
1378-
config->coerce_c_locale = new_coerce_c_locale;
1378+
config->_coerce_c_locale = new_coerce_c_locale;
13791379

13801380
/* The encoding changed: read again the configuration
13811381
with the new encoding */

Programs/_testembed.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,6 @@ dump_config(void)
330330

331331
printf("filesystem_encoding = %s\n", config->filesystem_encoding);
332332
printf("filesystem_errors = %s\n", config->filesystem_errors);
333-
printf("coerce_c_locale = %i\n", config->coerce_c_locale);
334-
printf("coerce_c_locale_warn = %i\n", config->coerce_c_locale_warn);
335333
printf("utf8_mode = %i\n", config->utf8_mode);
336334

337335
printf("pycache_prefix = %ls\n", config->pycache_prefix);
@@ -385,6 +383,8 @@ dump_config(void)
385383
printf("_install_importlib = %i\n", config->_install_importlib);
386384
printf("_check_hash_pycs_mode = %s\n", config->_check_hash_pycs_mode);
387385
printf("_frozen = %i\n", config->_frozen);
386+
printf("_coerce_c_locale = %i\n", config->_coerce_c_locale);
387+
printf("_coerce_c_locale_warn = %i\n", config->_coerce_c_locale_warn);
388388

389389
#undef ASSERT_EQUAL
390390
#undef ASSERT_STR_EQUAL
@@ -482,7 +482,7 @@ static int test_init_from_config(void)
482482
putenv("PYTHONMALLOCSTATS=0");
483483
config.malloc_stats = 1;
484484

485-
/* FIXME: test coerce_c_locale and coerce_c_locale_warn */
485+
/* FIXME: test _coerce_c_locale and _coerce_c_locale_warn */
486486

487487
putenv("PYTHONUTF8=0");
488488
Py_UTF8Mode = 0;
@@ -606,8 +606,8 @@ static int test_init_isolated(void)
606606
/* Test _PyCoreConfig.isolated=1 */
607607
_PyCoreConfig config = _PyCoreConfig_INIT;
608608

609-
/* Set coerce_c_locale and utf8_mode to not depend on the locale */
610-
config.coerce_c_locale = 0;
609+
/* Set _coerce_c_locale and utf8_mode to not depend on the locale */
610+
config._coerce_c_locale = 0;
611611
config.utf8_mode = 0;
612612
/* Use path starting with "./" avoids a search along the PATH */
613613
config.program_name = L"./_testembed";

Python/coreconfig.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
303303
COPY_ATTR(dump_refs);
304304
COPY_ATTR(malloc_stats);
305305

306-
COPY_ATTR(coerce_c_locale);
307-
COPY_ATTR(coerce_c_locale_warn);
306+
COPY_ATTR(_coerce_c_locale);
307+
COPY_ATTR(_coerce_c_locale_warn);
308308
COPY_ATTR(utf8_mode);
309309

310310
COPY_WSTR_ATTR(pycache_prefix);
@@ -811,16 +811,16 @@ config_read_env_vars(_PyCoreConfig *config)
811811
const char *env = _PyCoreConfig_GetEnv(config, "PYTHONCOERCECLOCALE");
812812
if (env) {
813813
if (strcmp(env, "0") == 0) {
814-
if (config->coerce_c_locale < 0) {
815-
config->coerce_c_locale = 0;
814+
if (config->_coerce_c_locale < 0) {
815+
config->_coerce_c_locale = 0;
816816
}
817817
}
818818
else if (strcmp(env, "warn") == 0) {
819-
config->coerce_c_locale_warn = 1;
819+
config->_coerce_c_locale_warn = 1;
820820
}
821821
else {
822-
if (config->coerce_c_locale < 0) {
823-
config->coerce_c_locale = 1;
822+
if (config->_coerce_c_locale < 0) {
823+
config->_coerce_c_locale = 1;
824824
}
825825
}
826826
}
@@ -967,10 +967,10 @@ config_read_complex_options(_PyCoreConfig *config)
967967
static void
968968
config_init_locale(_PyCoreConfig *config)
969969
{
970-
if (config->coerce_c_locale < 0) {
970+
if (config->_coerce_c_locale < 0) {
971971
/* The C locale enables the C locale coercion (PEP 538) */
972972
if (_Py_LegacyLocaleDetected()) {
973-
config->coerce_c_locale = 1;
973+
config->_coerce_c_locale = 1;
974974
}
975975
}
976976

@@ -1291,7 +1291,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
12911291
}
12921292
}
12931293

1294-
if (config->utf8_mode < 0 || config->coerce_c_locale < 0) {
1294+
if (config->utf8_mode < 0 || config->_coerce_c_locale < 0) {
12951295
config_init_locale(config);
12961296
}
12971297

@@ -1321,8 +1321,8 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
13211321
if (config->tracemalloc < 0) {
13221322
config->tracemalloc = 0;
13231323
}
1324-
if (config->coerce_c_locale < 0) {
1325-
config->coerce_c_locale = 0;
1324+
if (config->_coerce_c_locale < 0) {
1325+
config->_coerce_c_locale = 0;
13261326
}
13271327
if (config->utf8_mode < 0) {
13281328
config->utf8_mode = 0;
@@ -1343,7 +1343,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
13431343
return err;
13441344
}
13451345

1346-
assert(config->coerce_c_locale >= 0);
1346+
assert(config->_coerce_c_locale >= 0);
13471347
assert(config->use_environment >= 0);
13481348
assert(config->filesystem_encoding != NULL);
13491349
assert(config->filesystem_errors != NULL);

Python/pylifecycle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static const char *_C_LOCALE_WARNING =
301301
static void
302302
_emit_stderr_warning_for_legacy_locale(const _PyCoreConfig *core_config)
303303
{
304-
if (core_config->coerce_c_locale_warn && _Py_LegacyLocaleDetected()) {
304+
if (core_config->_coerce_c_locale_warn && _Py_LegacyLocaleDetected()) {
305305
PySys_FormatStderr("%s", _C_LOCALE_WARNING);
306306
}
307307
}

0 commit comments

Comments
 (0)