Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-93103: Py_DecodeLocale() uses _PyRuntime.preconfig #93187

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,9 @@ _Py_DecodeLocaleEx(const char* arg, wchar_t **wstr, size_t *wlen,
return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
errors);
#else
int use_utf8 = (Py_UTF8Mode == 1);
int use_utf8 = (_PyRuntime.preconfig.utf8_mode >= 1);
#ifdef MS_WINDOWS
use_utf8 |= !Py_LegacyWindowsFSEncodingFlag;
use_utf8 |= (_PyRuntime.preconfig.legacy_windows_fs_encoding == 0);
#endif
if (use_utf8) {
return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
Expand Down Expand Up @@ -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,
raw_malloc, errors);
#else
int use_utf8 = (Py_UTF8Mode == 1);
int use_utf8 = (_PyRuntime.preconfig.utf8_mode >= 1);
#ifdef MS_WINDOWS
use_utf8 |= !Py_LegacyWindowsFSEncodingFlag;
use_utf8 |= (_PyRuntime.preconfig.legacy_windows_fs_encoding == 0);
#endif
if (use_utf8) {
return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
Expand Down
28 changes: 10 additions & 18 deletions Python/preconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,12 +826,10 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
_Py_SetLocaleFromEnv(LC_CTYPE);
}

_PyPreCmdline cmdline = _PyPreCmdline_INIT;
int init_utf8_mode = Py_UTF8Mode;
#ifdef MS_WINDOWS
int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
#endif
PyPreConfig save_runtime_config;
preconfig_copy(&save_runtime_config, &_PyRuntime.preconfig);

_PyPreCmdline cmdline = _PyPreCmdline_INIT;
int locale_coerced = 0;
int loops = 0;

Expand All @@ -847,11 +845,9 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
}

/* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
Py_UTF8Mode = config->utf8_mode;
#ifdef MS_WINDOWS
Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
#endif
on the utf8_mode and legacy_windows_fs_encoding members
of _PyRuntime.preconfig. */
preconfig_copy(&_PyRuntime.preconfig, config);

if (args) {
// Set command line arguments at each iteration. If they are bytes
Expand Down Expand Up @@ -914,14 +910,10 @@ _PyPreConfig_Read(PyPreConfig *config, const _PyArgv *args)
status = _PyStatus_OK();

done:
if (init_ctype_locale != NULL) {
setlocale(LC_CTYPE, init_ctype_locale);
PyMem_RawFree(init_ctype_locale);
}
Py_UTF8Mode = init_utf8_mode ;
#ifdef MS_WINDOWS
Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;
#endif
// Revert side effects
setlocale(LC_CTYPE, init_ctype_locale);
PyMem_RawFree(init_ctype_locale);
preconfig_copy(&_PyRuntime.preconfig, &save_runtime_config);
_PyPreCmdline_Clear(&cmdline);
return status;
}
Expand Down