[issue44762] getpass.getpass on Windows fallback detection is bad

Eryk Sun report at bugs.python.org
Wed Jul 28 11:52:59 EDT 2021


Eryk Sun <eryksun at gmail.com> added the comment:

> When the check incorrectly infers that it can use `msvcrt` while 
> its stdin is a pipe, the calls to `putwch` and `getwch` are going 
> into the void and the program effectively freezes waiting for 
> input that never comes.

The C runtime's getwch() and putwch() functions use the console I/O files "CONIN$" and "CONOUT$". If "CONIN$" can't be opened because the process has no console, then getwch() fails and returns U+FFFF. But mintty does have a console, albeit with a hidden window, which gets inherited by bash and child processes. So getwch() ends up waiting for a key to be pressed in an inaccessible, hidden window.

It should suffice to check that sys.stdin isn't None and that sys.stdin.isatty() is True, since it's reasonable to assume that the console has a visible window in this case. Currently this isn't a bulletproof check, however, because the Windows C runtime doesn't implement isatty() correctly. It returns true for any character device, which includes the common case of redirecting standard I/O to the "NUL" device. io.FileIO.isatty() could be updated to return True only if both C isatty() is true and GetConsoleMode() succeeds. This would filter out false positives for non-console character devices, particularly "NUL".

----------
nosy: +eryksun

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44762>
_______________________________________


More information about the Python-bugs-list mailing list