gh-132888: Fix Windows API error checking in pyrepl.windows_console#144248
gh-132888: Fix Windows API error checking in pyrepl.windows_console#144248vstinner merged 6 commits intopython:mainfrom
Conversation
| WaitForSingleObject.argtypes = [HANDLE, DWORD] | ||
| WaitForSingleObject.restype = DWORD | ||
|
|
||
| OutHandle = GetStdHandle(STD_OUTPUT_HANDLE) |
There was a problem hiding this comment.
GetStdHandle can return INVALID_HANDLE_VALUE and should be checked, too?
There was a problem hiding this comment.
This is at the top level of the file, so it will be executed when importing this module. Therefore, we can't raise an exception here. What we can do is check if the result is INVALID_HANDLE_VALUE and set it to None or zero.
We will pass the OutHandle to other Windows APIs, like ReadConsoleInput. There, we will check the result and raise an exception using get_last_error().
However, if we don't check the result and just keep it as INVALID_HANDLE_VALUE, the whole process won't change. Keeping INVALID_HANDLE_VALUE might actually be a better choice, since 0 may be valid in some situations. That's why MS introduced INVALID_HANDLE_VALUE instead of using zero.
Therefore, I think it's better to just keep the current implementation.
There was a problem hiding this comment.
I agree to all your points 👍
|
In general this lgtm. I've built locally and did a few very basic manual tests like redirecting stdout / stderr. anymore (#132962 (comment)). But turning the silent swallowing into raising exceptions might bite us (although most probably the right thing to do), like you've seen in the failing tests in headless mode. Most probably the swallowing just let another error surface slightly later, anyway, so I think it is ok to fail early and loud? |
Hi, thank you for your review. I'm quite unsure whether we should throw the error early during initialization, but since this is a private module and its main usage is in an environment with a console, and the error is just raised when there is no console, I think it's acceptable to raise the error early |
|
Merged, thanks for the fix. I prefer to not backport the change to stable branches (3.13, 3.14), since it makes _pyrepl stricter and it may raise new exceptions. I prefer to wait to see how it goes in the main branch first. Maybe we need to catch some exceptions in some places. |
|
This change fixed the issue gh-132962 "windows console: -i parameter and redirected output gives error in 3.13". |
|
|
I'm not familiar with the failed test, but I think it's not related to this change. |
|
Correct, the test_launcher failures are unrelated to this pyrepl change. |
ctypes.GetLastError()in windows_console.py #132888