Skip to content

Commit 3cd187b

Browse files
committed
Issue python#28333: Enables Unicode for ps1/ps2 and input() prompts. (Patch by Eryk Sun)
1 parent c3215f5 commit 3cd187b

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ Library
213213
Windows
214214
-------
215215

216+
- Issue #28333: Enables Unicode for ps1/ps2 and input() prompts
217+
216218
- Issue #28251: Improvements to help manuals on Windows.
217219

218220
- Issue #28110: launcher.msi has different product codes between 32-bit and

Parser/myreadline.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,37 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
203203

204204
#ifdef MS_WINDOWS
205205
if (!Py_LegacyWindowsStdioFlag && sys_stdin == stdin) {
206-
HANDLE hStdIn;
206+
HANDLE hStdIn, hStdErr;
207207

208208
_Py_BEGIN_SUPPRESS_IPH
209209
hStdIn = (HANDLE)_get_osfhandle(fileno(sys_stdin));
210+
hStdErr = (HANDLE)_get_osfhandle(fileno(stderr));
210211
_Py_END_SUPPRESS_IPH
211212

212213
if (_get_console_type(hStdIn) == 'r') {
213214
fflush(sys_stdout);
214-
if (prompt)
215-
fprintf(stderr, "%s", prompt);
216-
fflush(stderr);
215+
if (prompt) {
216+
if (_get_console_type(hStdErr) == 'w') {
217+
wchar_t *wbuf;
218+
int wlen;
219+
wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
220+
NULL, 0);
221+
if (wlen++ &&
222+
(wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t)))) {
223+
wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
224+
wbuf, wlen);
225+
if (wlen) {
226+
DWORD n;
227+
fflush(stderr);
228+
WriteConsoleW(hStdErr, wbuf, wlen, &n, NULL);
229+
}
230+
PyMem_RawFree(wbuf);
231+
}
232+
} else {
233+
fprintf(stderr, "%s", prompt);
234+
fflush(stderr);
235+
}
236+
}
217237
clearerr(sys_stdin);
218238
return _PyOS_WindowsConsoleReadline(hStdIn);
219239
}

0 commit comments

Comments
 (0)