Skip to content

Commit 95cc3ee

Browse files
authored
Revert "[3.7] bpo-34589: Add -X coerce_c_locale option; C locale coercion off by default (GH-9379)" (GH-9416)
This reverts commit 144f1e2.
1 parent 73c0006 commit 95cc3ee

File tree

15 files changed

+67
-224
lines changed

15 files changed

+67
-224
lines changed

Doc/using/cmdline.rst

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -438,19 +438,10 @@ Miscellaneous options
438438
* Set the :attr:`~sys.flags.dev_mode` attribute of :attr:`sys.flags` to
439439
``True``
440440

441-
* ``-X utf8`` enables UTF-8 mode (:pep:`540`) for operating system interfaces, overriding
441+
* ``-X utf8`` enables UTF-8 mode for operating system interfaces, overriding
442442
the default locale-aware mode. ``-X utf8=0`` explicitly disables UTF-8
443443
mode (even when it would otherwise activate automatically).
444444
See :envvar:`PYTHONUTF8` for more details.
445-
* ``-X coerce_c_locale`` or ``-X coerce_c_locale=1`` tries to coerce the C
446-
locale (:pep:`538`).
447-
``-X coerce_c_locale=0`` skips coercing the legacy ASCII-based C and POSIX
448-
locales to a more capable UTF-8 based alternative.
449-
``-X coerce_c_locale=warn`` will cause Python to emit warning messages on
450-
``stderr`` if either the locale coercion activates, or else if a locale
451-
that *would* have triggered coercion is still active when the Python
452-
runtime is initialized.
453-
See :envvar:`PYTHONCOERCECLOCALE` for more details.
454445

455446
It also allows passing arbitrary values and retrieving them through the
456447
:data:`sys._xoptions` dictionary.
@@ -470,9 +461,6 @@ Miscellaneous options
470461
.. versionadded:: 3.7
471462
The ``-X importtime``, ``-X dev`` and ``-X utf8`` options.
472463

473-
.. versionadded:: 3.7.1
474-
The ``-X coerce_c_locale`` option.
475-
476464

477465
Options you shouldn't use
478466
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -846,8 +834,6 @@ conflict.
846834
order to force the interpreter to use ``ASCII`` instead of ``UTF-8`` for
847835
system interfaces.
848836

849-
Also available as the :option:`-X` ``coerce_c_locale`` option.
850-
851837
Availability: \*nix
852838

853839
.. versionadded:: 3.7

Doc/whatsnew/3.7.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,10 +2494,3 @@ versions, it respected an ill-defined subset of those environment variables,
24942494
while in Python 3.7.0 it didn't read any of them due to :issue:`34247`). If
24952495
this behavior is unwanted, set :c:data:`Py_IgnoreEnvironmentFlag` to 1 before
24962496
calling :c:func:`Py_Initialize`.
2497-
2498-
:c:func:`Py_Initialize` and :c:func:`Py_Main` cannot enable the C locale
2499-
coercion (:pep:`538`) anymore: it is always disabled. It can now only be
2500-
enabled by the Python program ("python3).
2501-
2502-
New :option:`-X` ``coerce_c_locale`` command line option to control C locale
2503-
coercion (:pep:`538`).

Include/pylifecycle.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,7 @@ PyAPI_FUNC(int) Py_FdIsInteractive(FILE *, const char *);
119119
/* Bootstrap __main__ (defined in Modules/main.c) */
120120
PyAPI_FUNC(int) Py_Main(int argc, wchar_t **argv);
121121
#ifdef Py_BUILD_CORE
122-
# ifdef MS_WINDOWS
123-
PyAPI_FUNC(int) _Py_WindowsMain(int argc, wchar_t **argv);
124-
# else
125122
PyAPI_FUNC(int) _Py_UnixMain(int argc, char **argv);
126-
# endif
127123
#endif
128124

129125
/* In getpath.c */

Include/pystate.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ typedef struct {
4141
int show_alloc_count; /* -X showalloccount */
4242
int dump_refs; /* PYTHONDUMPREFS */
4343
int malloc_stats; /* PYTHONMALLOCSTATS */
44+
int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
45+
int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
4446
int utf8_mode; /* PYTHONUTF8, -X utf8; -1 means unknown */
4547

4648
wchar_t *program_name; /* Program name, see also Py_GetProgramName() */
@@ -72,17 +74,14 @@ typedef struct {
7274

7375
/* Private fields */
7476
int _disable_importlib; /* Needed by freeze_importlib */
75-
int _coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
76-
int _coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
7777
} _PyCoreConfig;
7878

7979
#define _PyCoreConfig_INIT \
8080
(_PyCoreConfig){ \
8181
.install_signal_handlers = -1, \
8282
.ignore_environment = -1, \
8383
.use_hash_seed = -1, \
84-
._coerce_c_locale = 0, \
85-
._coerce_c_locale_warn = 0, \
84+
.coerce_c_locale = -1, \
8685
.faulthandler = -1, \
8786
.tracemalloc = -1, \
8887
.utf8_mode = -1, \

Lib/test/test_c_locale_coercion.py

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def _handle_output_variations(data):
139139
return data
140140

141141
@classmethod
142-
def get_child_details(cls, env_vars, xoption=None):
142+
def get_child_details(cls, env_vars):
143143
"""Retrieves fsencoding and standard stream details from a child process
144144
145145
Returns (encoding_details, stderr_lines):
@@ -150,11 +150,10 @@ def get_child_details(cls, env_vars, xoption=None):
150150
The child is run in isolated mode if the current interpreter supports
151151
that.
152152
"""
153-
args = []
154-
if xoption:
155-
args.extend(("-X", f"coerce_c_locale={xoption}"))
156-
args.extend(("-X", "utf8=0", "-c", cls.CHILD_PROCESS_SCRIPT))
157-
result, py_cmd = run_python_until_end(*args, **env_vars)
153+
result, py_cmd = run_python_until_end(
154+
"-X", "utf8=0", "-c", cls.CHILD_PROCESS_SCRIPT,
155+
**env_vars
156+
)
158157
if not result.rc == 0:
159158
result.fail(py_cmd)
160159
# All subprocess outputs in this test case should be pure ASCII
@@ -213,16 +212,15 @@ def _check_child_encoding_details(self,
213212
expected_fs_encoding,
214213
expected_stream_encoding,
215214
expected_warnings,
216-
coercion_expected,
217-
xoption=None):
215+
coercion_expected):
218216
"""Check the C locale handling for the given process environment
219217
220218
Parameters:
221219
expected_fs_encoding: expected sys.getfilesystemencoding() result
222220
expected_stream_encoding: expected encoding for standard streams
223221
expected_warning: stderr output to expect (if any)
224222
"""
225-
result = EncodingDetails.get_child_details(env_vars, xoption)
223+
result = EncodingDetails.get_child_details(env_vars)
226224
encoding_details, stderr_lines = result
227225
expected_details = EncodingDetails.get_expected_details(
228226
coercion_expected,
@@ -292,7 +290,6 @@ def _check_c_locale_coercion(self,
292290
coerce_c_locale,
293291
expected_warnings=None,
294292
coercion_expected=True,
295-
use_xoption=False,
296293
**extra_vars):
297294
"""Check the C locale handling for various configurations
298295
@@ -322,12 +319,8 @@ def _check_c_locale_coercion(self,
322319
"PYTHONCOERCECLOCALE": "",
323320
}
324321
base_var_dict.update(extra_vars)
325-
xoption = None
326322
if coerce_c_locale is not None:
327-
if use_xoption:
328-
xoption = coerce_c_locale
329-
else:
330-
base_var_dict["PYTHONCOERCECLOCALE"] = coerce_c_locale
323+
base_var_dict["PYTHONCOERCECLOCALE"] = coerce_c_locale
331324

332325
# Check behaviour for the default locale
333326
with self.subTest(default_locale=True,
@@ -349,8 +342,7 @@ def _check_c_locale_coercion(self,
349342
fs_encoding,
350343
stream_encoding,
351344
_expected_warnings,
352-
_coercion_expected,
353-
xoption=xoption)
345+
_coercion_expected)
354346

355347
# Check behaviour for explicitly configured locales
356348
for locale_to_set in EXPECTED_C_LOCALE_EQUIVALENTS:
@@ -365,8 +357,7 @@ def _check_c_locale_coercion(self,
365357
fs_encoding,
366358
stream_encoding,
367359
expected_warnings,
368-
coercion_expected,
369-
xoption=xoption)
360+
coercion_expected)
370361

371362
def test_PYTHONCOERCECLOCALE_not_set(self):
372363
# This should coerce to the first available target locale by default
@@ -413,32 +404,6 @@ def test_LC_ALL_set_to_C(self):
413404
expected_warnings=[LEGACY_LOCALE_WARNING],
414405
coercion_expected=False)
415406

416-
def test_xoption_set_to_1(self):
417-
self._check_c_locale_coercion("utf-8", "utf-8", coerce_c_locale="1",
418-
use_xoption=True)
419-
420-
def test_xoption_set_to_zero(self):
421-
# The setting "0" should result in the locale coercion being disabled
422-
self._check_c_locale_coercion(EXPECTED_C_LOCALE_FS_ENCODING,
423-
EXPECTED_C_LOCALE_STREAM_ENCODING,
424-
coerce_c_locale="0",
425-
coercion_expected=False,
426-
use_xoption=True)
427-
# Setting LC_ALL=C shouldn't make any difference to the behaviour
428-
self._check_c_locale_coercion(EXPECTED_C_LOCALE_FS_ENCODING,
429-
EXPECTED_C_LOCALE_STREAM_ENCODING,
430-
coerce_c_locale="0",
431-
LC_ALL="C",
432-
coercion_expected=False,
433-
use_xoption=True)
434-
435-
def test_xoption_set_to_warn(self):
436-
# -X coerce_c_locale=warn enables runtime warnings for legacy locales
437-
self._check_c_locale_coercion("utf-8", "utf-8",
438-
coerce_c_locale="warn",
439-
expected_warnings=[CLI_COERCION_WARNING],
440-
use_xoption=True)
441-
442407
def test_main():
443408
test.support.run_unittest(
444409
LocaleConfigurationTests,

Lib/test/test_cmd_line.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,13 @@ def test_undecodable_code(self):
159159
env = os.environ.copy()
160160
# Use C locale to get ascii for the locale encoding
161161
env['LC_ALL'] = 'C'
162+
env['PYTHONCOERCECLOCALE'] = '0'
162163
code = (
163164
b'import locale; '
164165
b'print(ascii("' + undecodable + b'"), '
165166
b'locale.getpreferredencoding())')
166167
p = subprocess.Popen(
167-
[sys.executable,
168-
# Disable C locale coercion and UTF-8 Mode to not use UTF-8
169-
"-X", "coerce_c_locale=0",
170-
"-X", "utf8=0",
171-
"-c", code],
168+
[sys.executable, "-c", code],
172169
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
173170
env=env)
174171
stdout, stderr = p.communicate()

Lib/test/test_embed.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
267267
'malloc_stats': 0,
268268
'utf8_mode': 0,
269269

270+
'coerce_c_locale': 0,
271+
'coerce_c_locale_warn': 0,
272+
270273
'program_name': './_testembed',
271274
'argc': 0,
272275
'argv': '[]',
@@ -287,8 +290,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
287290

288291
'_disable_importlib': 0,
289292
'Py_FrozenFlag': 0,
290-
'_coerce_c_locale': 0,
291-
'_coerce_c_locale_warn': 0,
292293
}
293294

294295
def check_config(self, testname, expected):

Lib/test/test_sys.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,9 @@ def test_getfilesystemencoding(self):
656656

657657
def c_locale_get_error_handler(self, locale, isolated=False, encoding=None):
658658
# Force the POSIX locale
659-
env = dict(os.environ)
659+
env = os.environ.copy()
660660
env["LC_ALL"] = locale
661+
env["PYTHONCOERCECLOCALE"] = "0"
661662
code = '\n'.join((
662663
'import sys',
663664
'def dump(name):',
@@ -667,10 +668,7 @@ def c_locale_get_error_handler(self, locale, isolated=False, encoding=None):
667668
'dump("stdout")',
668669
'dump("stderr")',
669670
))
670-
args = [sys.executable,
671-
"-X", "utf8=0",
672-
"-X", "coerce_c_locale=0",
673-
"-c", code]
671+
args = [sys.executable, "-c", code]
674672
if isolated:
675673
args.append("-I")
676674
if encoding is not None:

Lib/test/test_utf8_mode.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ def posix_locale(self):
2727
return (loc in POSIX_LOCALES)
2828

2929
def get_output(self, *args, failure=False, **kw):
30-
# Always disable the C locale coercion (PEP 538)
31-
args = ('-X', 'coerce_c_locale=0', *args)
3230
kw = dict(self.DEFAULT_ENV, **kw)
3331
if failure:
3432
out = assert_python_failure(*args, **kw)
@@ -118,6 +116,7 @@ def test_filesystemencoding(self):
118116
# PYTHONLEGACYWINDOWSFSENCODING disables the UTF-8 mode
119117
# and has the priority over -X utf8 and PYTHONUTF8
120118
out = self.get_output('-X', 'utf8', '-c', code,
119+
PYTHONUTF8='strict',
121120
PYTHONLEGACYWINDOWSFSENCODING='1')
122121
self.assertEqual(out, 'mbcs/replace')
123122

Misc/NEWS.d/next/C API/2018-09-18-00-09-31.bpo-34589.C7bUpq.rst

Lines changed: 0 additions & 3 deletions
This file was deleted.

Misc/NEWS.d/next/Core and Builtins/2018-09-18-01-41-33.bpo-34589.lLVTYc.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)