[issue45870] There's no readline module on Windows Python (cmd.Cmd)

Eryk Sun report at bugs.python.org
Sat Nov 27 18:04:59 EST 2021


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

> AFAIK the reason command history works in cmd.py on Windows is 
> that it's built into the terminal program. Or maybe into the
> operating system.

As mentioned in msg406800, input editing, history (e.g. up/down arrows, F7 popup, F8 completion), and alias support is implemented by the Windows console host (conhost.exe or openconsole.exe) for ReadFile() and ReadConsole() calls when the input stream is in line-input mode. Currently, it's the same whether a classic console session or a pseudoconsole (headless) session is hosted. When Windows Terminal is used, the overall connection of components looks like Python<->ConDrv (kernel device)<->OpenConsole<->NamedPipe (kernel device)<->Windows Terminal. The headless console's use of Windows Terminal for the user interface doesn't matter to Python's ReadConsoleW() call.

A headless console session always starts with 4 history buffers (one for each attached process) that store up to 50 commands. For a classic console session, the initial number and size of history buffers can be configured in the session properties or defaults. It can always be set dynamically via SetConsoleHistoryInfo(). There's *undocumented* support to get the commands from a history buffer that's associated with the name of an attached process: GetConsoleCommandHistoryLengthW(executable_name) and GetConsoleCommandHistoryW(buffer, buffer_length, executable_name). However, the API provides no function to set the command history. I suppose one could loop over WriteConsoleInputW() and ReadConsoleW() to implement it as a kludge.

----------

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


More information about the Python-bugs-list mailing list