mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
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 b2457efc78
.
* Fix also a memory leak: get_locale_encoding() already allocates
memory, no need to duplicate the string.
This commit is contained in:
parent
b232df9197
commit
905f1ace5f
2 changed files with 26 additions and 22 deletions
|
@ -1164,13 +1164,17 @@ config_init_fs_encoding(_PyCoreConfig *config)
|
|||
}
|
||||
}
|
||||
|
||||
/* Windows defaults to utf-8/surrogatepass (PEP 529) */
|
||||
/* Windows defaults to utf-8/surrogatepass (PEP 529).
|
||||
|
||||
Note: UTF-8 Mode takes the same code path and the Legacy Windows FS
|
||||
encoding has the priortiy over UTF-8 Mode. */
|
||||
if (config->filesystem_encoding == NULL) {
|
||||
config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
|
||||
if (config->filesystem_encoding == NULL) {
|
||||
return _Py_INIT_NO_MEMORY();
|
||||
}
|
||||
}
|
||||
|
||||
if (config->filesystem_errors == NULL) {
|
||||
config->filesystem_errors = _PyMem_RawStrdup("surrogatepass");
|
||||
if (config->filesystem_errors == NULL) {
|
||||
|
@ -1178,30 +1182,28 @@ config_init_fs_encoding(_PyCoreConfig *config)
|
|||
}
|
||||
}
|
||||
#else
|
||||
if (config->utf8_mode) {
|
||||
/* UTF-8 Mode use: utf-8/surrogateescape */
|
||||
if (config->filesystem_encoding == NULL) {
|
||||
config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
|
||||
if (config->filesystem_encoding == NULL) {
|
||||
return _Py_INIT_NO_MEMORY();
|
||||
}
|
||||
}
|
||||
/* errors defaults to surrogateescape above */
|
||||
}
|
||||
|
||||
if (config->filesystem_encoding == NULL) {
|
||||
/* macOS and Android use UTF-8, other platforms use
|
||||
the locale encoding. */
|
||||
char *locale_encoding;
|
||||
#if defined(__APPLE__) || defined(__ANDROID__)
|
||||
locale_encoding = "UTF-8";
|
||||
#else
|
||||
_PyInitError err = get_locale_encoding(&locale_encoding);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
if (config->utf8_mode) {
|
||||
/* UTF-8 Mode use: utf-8/surrogateescape */
|
||||
config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
|
||||
/* errors defaults to surrogateescape above */
|
||||
}
|
||||
else if (_Py_GetForceASCII()) {
|
||||
config->filesystem_encoding = _PyMem_RawStrdup("ascii");
|
||||
}
|
||||
else {
|
||||
/* macOS and Android use UTF-8,
|
||||
other platforms use the locale encoding. */
|
||||
#if defined(__APPLE__) || defined(__ANDROID__)
|
||||
config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
|
||||
#else
|
||||
_PyInitError err = get_locale_encoding(&config->filesystem_encoding);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
config->filesystem_encoding = _PyMem_RawStrdup(locale_encoding);
|
||||
}
|
||||
|
||||
if (config->filesystem_encoding == NULL) {
|
||||
return _Py_INIT_NO_MEMORY();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue