mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
gh-93103: Py_DecodeLocale() uses _PyRuntime.preconfig (#93187)
The Py_DecodeLocale() and Py_EncodeLocale() now use _PyRuntime.preconfig, rather than Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag global configuration varibles, to decide if the UTF-8 encoding is used or not. As documented, these functions must not be called before Python is preinitialized. The new PyConfig API should now be used, rather than using deprecated functions like Py_SetPath() or PySys_SetArgv().
This commit is contained in:
parent
c7667a2d35
commit
32b7bcffba
2 changed files with 14 additions and 22 deletions
|
@ -603,9 +603,9 @@ _Py_DecodeLocaleEx(const char* arg, wchar_t **wstr, size_t *wlen,
|
||||||
return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
|
return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
|
||||||
errors);
|
errors);
|
||||||
#else
|
#else
|
||||||
int use_utf8 = (Py_UTF8Mode == 1);
|
int use_utf8 = (_PyRuntime.preconfig.utf8_mode >= 1);
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
use_utf8 |= !Py_LegacyWindowsFSEncodingFlag;
|
use_utf8 |= (_PyRuntime.preconfig.legacy_windows_fs_encoding == 0);
|
||||||
#endif
|
#endif
|
||||||
if (use_utf8) {
|
if (use_utf8) {
|
||||||
return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
|
return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
|
||||||
|
@ -795,9 +795,9 @@ encode_locale_ex(const wchar_t *text, char **str, size_t *error_pos,
|
||||||
return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
|
return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
|
||||||
raw_malloc, errors);
|
raw_malloc, errors);
|
||||||
#else
|
#else
|
||||||
int use_utf8 = (Py_UTF8Mode == 1);
|
int use_utf8 = (_PyRuntime.preconfig.utf8_mode >= 1);
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
use_utf8 |= !Py_LegacyWindowsFSEncodingFlag;
|
use_utf8 |= (_PyRuntime.preconfig.legacy_windows_fs_encoding == 0);
|
||||||
#endif
|
#endif
|
||||||
if (use_utf8) {
|
if (use_utf8) {
|
||||||
return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
|
return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
|
||||||
|
|
|
@ -826,12 +826,10 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
|
||||||
_Py_SetLocaleFromEnv(LC_CTYPE);
|
_Py_SetLocaleFromEnv(LC_CTYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
_PyPreCmdline cmdline = _PyPreCmdline_INIT;
|
PyPreConfig save_runtime_config;
|
||||||
int init_utf8_mode = Py_UTF8Mode;
|
preconfig_copy(&save_runtime_config, &_PyRuntime.preconfig);
|
||||||
#ifdef MS_WINDOWS
|
|
||||||
int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
_PyPreCmdline cmdline = _PyPreCmdline_INIT;
|
||||||
int locale_coerced = 0;
|
int locale_coerced = 0;
|
||||||
int loops = 0;
|
int loops = 0;
|
||||||
|
|
||||||
|
@ -847,11 +845,9 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
|
/* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
|
||||||
on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
|
on the utf8_mode and legacy_windows_fs_encoding members
|
||||||
Py_UTF8Mode = config->utf8_mode;
|
of _PyRuntime.preconfig. */
|
||||||
#ifdef MS_WINDOWS
|
preconfig_copy(&_PyRuntime.preconfig, config);
|
||||||
Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (args) {
|
if (args) {
|
||||||
// Set command line arguments at each iteration. If they are bytes
|
// Set command line arguments at each iteration. If they are bytes
|
||||||
|
@ -914,14 +910,10 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
|
||||||
status = _PyStatus_OK();
|
status = _PyStatus_OK();
|
||||||
|
|
||||||
done:
|
done:
|
||||||
if (init_ctype_locale != NULL) {
|
// Revert side effects
|
||||||
setlocale(LC_CTYPE, init_ctype_locale);
|
setlocale(LC_CTYPE, init_ctype_locale);
|
||||||
PyMem_RawFree(init_ctype_locale);
|
PyMem_RawFree(init_ctype_locale);
|
||||||
}
|
preconfig_copy(&_PyRuntime.preconfig, &save_runtime_config);
|
||||||
Py_UTF8Mode = init_utf8_mode ;
|
|
||||||
#ifdef MS_WINDOWS
|
|
||||||
Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;
|
|
||||||
#endif
|
|
||||||
_PyPreCmdline_Clear(&cmdline);
|
_PyPreCmdline_Clear(&cmdline);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue