Skip to content

Commit e8382e5

Browse files
authored
gh-74857, PEP 538: Coerce POSIX locale to UTF-8 based locale (#139238)
1 parent a79ce35 commit e8382e5

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

Lib/test/test_c_locale_coercion.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# Set the list of ways we expect to be able to ask for the "C" locale.
1616
# 'invalid.ascii' is an invalid LOCALE name and so should get turned in to the
1717
# default locale, which is traditionally C.
18-
EXPECTED_C_LOCALE_EQUIVALENTS = ["C", "invalid.ascii"]
18+
EXPECTED_C_LOCALE_EQUIVALENTS = ["C", "POSIX", "invalid.ascii"]
1919

2020
# Set our expectation for the default encoding used in the C locale
2121
# for the filesystem encoding and the standard streams
@@ -55,11 +55,6 @@
5555
# VxWorks defaults to using UTF-8 for all system interfaces
5656
EXPECTED_C_LOCALE_STREAM_ENCODING = "utf-8"
5757
EXPECTED_C_LOCALE_FS_ENCODING = "utf-8"
58-
if sys.platform.startswith("linux"):
59-
# Linux recognizes POSIX as a synonym for C. Python will always coerce
60-
# if the locale is set to POSIX, but not all platforms will use the
61-
# C locale encodings if POSIX is set, so we'll only test it on linux.
62-
EXPECTED_C_LOCALE_EQUIVALENTS.append("POSIX")
6358

6459
# Note that the above expectations are still wrong in some cases, such as:
6560
# * Windows when PYTHONLEGACYWINDOWSFSENCODING is set
@@ -467,8 +462,9 @@ def test_PYTHONCOERCECLOCALE_set_to_one(self):
467462
loc = locale.setlocale(locale.LC_CTYPE, "")
468463
except locale.Error as e:
469464
self.skipTest(str(e))
470-
if loc == "C":
471-
self.skipTest("test requires LC_CTYPE locale different than C")
465+
if loc in ("C", "POSIX"):
466+
self.skipTest("test requires LC_CTYPE locale different "
467+
"than C and POSIX")
472468
if loc in TARGET_LOCALES :
473469
self.skipTest("coerced LC_CTYPE locale: %s" % loc)
474470

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:pep:`538`: Coerce the POSIX locale to a UTF-8 based locale. Patch by Victor
2+
Stinner.

Python/pylifecycle.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ _Py_LegacyLocaleDetected(int warn)
209209
* we may also want to check for that explicitly.
210210
*/
211211
const char *ctype_loc = setlocale(LC_CTYPE, NULL);
212-
return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0;
212+
if (ctype_loc == NULL) {
213+
return 0;
214+
}
215+
return (strcmp(ctype_loc, "C") == 0 || strcmp(ctype_loc, "POSIX") == 0);
213216
#else
214217
/* Windows uses code pages instead of locales, so no locale is legacy */
215218
return 0;

0 commit comments

Comments
 (0)