Skip to content
Closed
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
4 changes: 4 additions & 0 deletions Include/fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ PyAPI_FUNC(char*) _Py_EncodeLocaleRaw(
#endif

#ifdef Py_BUILD_CORE
PyAPI_FUNC(wchar_t *) _Py_DecodeUTF8(
const char *arg,
size_t *size);

PyAPI_FUNC(int) _Py_DecodeUTF8Ex(
const char *arg,
Py_ssize_t arglen,
Expand Down
8 changes: 7 additions & 1 deletion Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,13 @@ pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,

for (int i = 0; i < pymain->argc; i++) {
size_t len;
wchar_t *arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
wchar_t *arg;
if (config->utf8_mode > 0) {
arg = _Py_DecodeUTF8(pymain->bytes_argv[i], &len);
}
else {
arg = Py_DecodeLocale(pymain->bytes_argv[i], &len);
}
if (arg == NULL) {
_Py_wstrlist_clear(i, argv);
pymain->err = DECODE_LOCALE_ERR("command line arguments",
Expand Down
15 changes: 15 additions & 0 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,21 @@ Py_DecodeLocale(const char* arg, size_t *wlen)
}


wchar_t*
_Py_DecodeUTF8(const char* arg, size_t *wlen)
{
wchar_t *wstr;
int res = _Py_DecodeUTF8Ex(arg, strlen(arg), &wstr, wlen, NULL, 1);
if (res != 0) {
if (wlen != NULL) {
*wlen = (size_t)res;
}
return NULL;
}
return wstr;
}


static int
encode_current_locale(const wchar_t *text, char **str,
size_t *error_pos, const char **reason,
Expand Down