Skip to content

Commit 905f1ac

Browse files
authored
bpo-34523: Fix config_init_fs_encoding() for ASCII (GH-10232)
* bpo-34523, bpo-34403: Fix config_init_fs_encoding(): it now uses ASCII if _Py_GetForceASCII() is true. * Fix a regression of commit b2457ef. * Fix also a memory leak: get_locale_encoding() already allocates memory, no need to duplicate the string.
1 parent b232df9 commit 905f1ac

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

Include/coreconfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ typedef struct {
7575
highest priority;
7676
* PYTHONIOENCODING environment variable;
7777
* The UTF-8 Mode uses UTF-8/surrogateescape;
78+
* If Python forces the usage of the ASCII encoding (ex: C locale
79+
or POSIX locale on FreeBSD or HP-UX), use ASCII/surrogateescape;
7880
* locale encoding: ANSI code page on Windows, UTF-8 on Android,
7981
LC_CTYPE locale encoding on other platforms;
8082
* On Windows, "surrogateescape" error handler;

Python/coreconfig.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,44 +1164,46 @@ config_init_fs_encoding(_PyCoreConfig *config)
11641164
}
11651165
}
11661166

1167-
/* Windows defaults to utf-8/surrogatepass (PEP 529) */
1167+
/* Windows defaults to utf-8/surrogatepass (PEP 529).
1168+
1169+
Note: UTF-8 Mode takes the same code path and the Legacy Windows FS
1170+
encoding has the priortiy over UTF-8 Mode. */
11681171
if (config->filesystem_encoding == NULL) {
11691172
config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
11701173
if (config->filesystem_encoding == NULL) {
11711174
return _Py_INIT_NO_MEMORY();
11721175
}
11731176
}
1177+
11741178
if (config->filesystem_errors == NULL) {
11751179
config->filesystem_errors = _PyMem_RawStrdup("surrogatepass");
11761180
if (config->filesystem_errors == NULL) {
11771181
return _Py_INIT_NO_MEMORY();
11781182
}
11791183
}
11801184
#else
1181-
if (config->utf8_mode) {
1182-
/* UTF-8 Mode use: utf-8/surrogateescape */
1183-
if (config->filesystem_encoding == NULL) {
1185+
if (config->filesystem_encoding == NULL) {
1186+
if (config->utf8_mode) {
1187+
/* UTF-8 Mode use: utf-8/surrogateescape */
11841188
config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
1185-
if (config->filesystem_encoding == NULL) {
1186-
return _Py_INIT_NO_MEMORY();
1187-
}
1189+
/* errors defaults to surrogateescape above */
11881190
}
1189-
/* errors defaults to surrogateescape above */
1190-
}
1191-
1192-
if (config->filesystem_encoding == NULL) {
1193-
/* macOS and Android use UTF-8, other platforms use
1194-
the locale encoding. */
1195-
char *locale_encoding;
1191+
else if (_Py_GetForceASCII()) {
1192+
config->filesystem_encoding = _PyMem_RawStrdup("ascii");
1193+
}
1194+
else {
1195+
/* macOS and Android use UTF-8,
1196+
other platforms use the locale encoding. */
11961197
#if defined(__APPLE__) || defined(__ANDROID__)
1197-
locale_encoding = "UTF-8";
1198+
config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
11981199
#else
1199-
_PyInitError err = get_locale_encoding(&locale_encoding);
1200-
if (_Py_INIT_FAILED(err)) {
1201-
return err;
1202-
}
1200+
_PyInitError err = get_locale_encoding(&config->filesystem_encoding);
1201+
if (_Py_INIT_FAILED(err)) {
1202+
return err;
1203+
}
12031204
#endif
1204-
config->filesystem_encoding = _PyMem_RawStrdup(locale_encoding);
1205+
}
1206+
12051207
if (config->filesystem_encoding == NULL) {
12061208
return _Py_INIT_NO_MEMORY();
12071209
}

0 commit comments

Comments
 (0)