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

bpo-30544: _io._WindowsConsoleIO.write raises the wrong error when WriteConsoleW fails #1912

Merged
merged 2 commits into from
Jun 2, 2017
Merged
Changes from 1 commit
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
Prev Previous commit
bpo-30544: _io._WindowsConsoleIO.write raises the wrong error when Wr…
…iteConsoleW fails
  • Loading branch information
segevfiner committed Jun 2, 2017
commit 3b5a295487e891b48999e64de9d5023a60bfc857
7 changes: 2 additions & 5 deletions Modules/_io/winconsoleio.c
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,6 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b)
BOOL res = TRUE;
wchar_t *wbuf;
DWORD len, wlen, n = 0;
DWORD err;

if (self->handle == INVALID_HANDLE_VALUE)
return err_closed();
Expand Down Expand Up @@ -1001,10 +1000,7 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b)
wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, wbuf, wlen);
if (wlen) {
res = WriteConsoleW(self->handle, wbuf, wlen, &n, NULL);
if (!res)
err = GetLastError();

if (n < wlen) {
if (res && n < wlen) {
/* Wrote fewer characters than expected, which means our
* len value may be wrong. So recalculate it from the
* characters that were written. As this could potentially
Expand All @@ -1023,6 +1019,7 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b)
Py_END_ALLOW_THREADS

if (!res) {
DWORD err = GetLastError();
PyMem_Free(wbuf);
return PyErr_SetFromWindowsErr(err);
}
Expand Down