Skip to content
Myr

Explicit cast to HANDLE for Read/WriteConsoleW calls

Before adoption: this commit came in with the history the project adopted, so no landing records it.

See [1]. The C call expects the first argument to be of type HANDLE. Most Python variants can directly and safely pass the int, but not all. This can cause click to raise OSError with errno 6 (invalid file handle). [1] https://docs.microsoft.com/en-us/windows/console/writeconsole

authored by Paul Kilgo · committed by David Lord · parent 79e4ae3 · Browse files · 2 files+4 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+4 added−3 removed

  • other:+4 added−3 removed

2 files+4 added−3 removed

modified:click/_winconsole.py+3 added-3 removed
22 unmodified lines
23 PyObject_GetBuffer = pythonapi.PyObject_GetBuffer24 PyBuffer_Release = pythonapi.PyBuffer_Release25except ImportError:26 pythonapi = None27from ctypes.wintypes import LPWSTR, LPCWSTR27from ctypes.wintypes import LPWSTR, LPCWSTR, HANDLE282930c_ssize_p = POINTER(c_ssize_t)31
90 unmodified lines
122 buffer = get_buffer(b, writable=True)123 code_units_to_be_read = bytes_to_be_read // 2124 code_units_read = c_ulong()125126 rv = ReadConsoleW(self.handle, buffer, code_units_to_be_read,126 rv = ReadConsoleW(HANDLE(self.handle), buffer, code_units_to_be_read,127 byref(code_units_read), None)128 if GetLastError() == ERROR_OPERATION_ABORTED:129 # wait for KeyboardInterrupt130 time.sleep(0.1)
24 unmodified lines
155 code_units_to_be_written = min(bytes_to_be_written,156 MAX_BYTES_WRITTEN) // 2157 code_units_written = c_ulong()158159 WriteConsoleW(self.handle, buf, code_units_to_be_written,159 WriteConsoleW(HANDLE(self.handle), buf, code_units_to_be_written,160 byref(code_units_written), None)161 bytes_written = 2 * code_units_written.value162163 if bytes_written == 0 and bytes_to_be_written > 0:
144 unmodified lines
modified:CHANGES.rst+1 added
10 unmodified lines
11- Fix link in ``unicode_literals`` error message. (`#1151`_)12- Add support for colored output on UNIX Jupyter notebooks. (`#1185`_)13- Remove unused compat shim for ``bytes``. (`#1195`_)14- Expand unit testing around termui, especially getchar (Windows). (`#1116`_)15- Fix output on Windows Python 2.7 built with MSVC 14. #13421617.. _#1167: https://github.com/pallets/click/pull/116718.. _#1151: https://github.com/pallets/click/pull/115119.. _#1185: https://github.com/pallets/click/issues/1185
634 unmodified lines