Explicit cast to HANDLE for Read/WriteConsoleW calls#1342
Conversation
|
What is HANDLE and why does it fix this? |
|
It's a Windows API type which is just a void pointer. The docs for WriteConsoleW/ReadConsoleW say the first argument should be a HANDLE. If you step through the code As for why or if it causes the specific behavior I ran into, I'm less sure about. I could wave my hands and say "undefined behavior" but I was hoping someone in the community could say with more conviction. |
|
Does this still work for the older builds that didn't demonstrate the issue before? Sorry for the questions, I am not a Windows dev 😩 |
|
These are the builds that I tried, and they all seem to work with this patch:
|
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
We have a custom build of Python 2.7 which is built against MSVC 14. We found that Click's windows console functionality was degraded after making the switch the MSVC 14, hitting that explicit
raise OSErrorin_WindowsConsoleWriter.write()with errno 6 (invalid file handle).I run into the error using
click.echo(), just for some cases, but it seems like you hit one of the bad cases and it ruins the rest of the cases. For example, here are the results I get for individual calls:but run the two in sequence and they both fail:
My output:
I may be a very remote corner case, since the MSVC 9 build of Python 2.7.16 and the MSVC 14 build of Python 3.6.8 both work fine.
Stack for reference: