osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Block Ctrl+S while running Python script at Windows console?


On 3/19/19, Grant Edwards <grant.b.edwards at gmail.com> wrote:
>
> This has nothing to do with Python does it?
>
> Isn't Python is just writing to stdout and those write calls are
> blocking due because the terminal emulator has stopped reading

It turns out the original poster had quick-edit mode enabled and the
pause was due to accidentally selecting text. A Python script can
disable this mode via GetConsoleMode / SetConsoleMode [1], called via
PyWin32 or ctypes. The original mode should be restored using an
atexit function. With quick-edit mode disabled, selecting text
requires enabling mark mode via the edit menu or Ctrl+M.

If it had been a Ctrl+S pause, the only way to programmatically
disable it for the current console is to disable line-input mode,
which is what msvcrt.getwch() does temporarily. However, disabling
line-input mode in general would break the REPL and input().

[1]: https://docs.microsoft.com/en-us/windows/console/getconsolemode

> the other end of the pipe/pty/queue/buffer/whatever-it's-called-
> in-windows?

In Windows 8+, a console client has handles for files on the ConDrv
device, which by default includes "Reference" (inherited reference to
a console), "Connect" (generic functions, such as setting the title),
"Input" (stdin), and "Output" (stdout, stderr). The host process
(conhost.exe) has a handle for ConDrv as well, from which it receives
IOCTL requests from its registered clients.

Windows 10 (release 1809) also supports pseudoconsoles [2], which are
roughly equivalent to Unix PTYs. The pseudoconsole interface is
restricted to virtual-terminal streams over a pair of pipes, so the
console's function-based API still has to be implemented for clients
by an instance of conhost.exe. It basically looks like the following:
Pseudoconsole Host <-- Pipe I/O --> conhost.exe <-- ConDrv I/O -->
Clients.

[2]: https://docs.microsoft.com/en-us/windows/console/pseudoconsoles