xonsh.shells.ptk_shell.input_hook

Serve the built-in input() from a prompt_toolkit prompt.

CPython’s input() only gets line editing when the readline module has been imported: importing it installs PyOS_ReadlineFunctionPointer, which input() calls whenever stdin and stdout are interactive. The prompt_toolkit shell never imports readline, so input() falls back to a plain read from the terminal in canonical mode. There the kernel assembles the line and caps it at MAX_CANON — 1024 bytes on macOS and the BSDs, 4096 on Linux. Everything past the cap is discarded, including the terminating newline, so the read never completes and the call hangs until the user interrupts it. getpass.getpass() has the same problem: it clears ECHO but leaves canonical mode on.

Routing input() through prompt_toolkit puts the terminal in raw mode for the duration of the read, which removes the limit and adds the usual line editing, kill/yank and history keys.

The hook is installed only while the interactive prompt_toolkit shell is running its command loop (see PromptToolkitShell.cmdloop), and it hands the call back to the original input() whenever a prompt_toolkit prompt cannot be driven safely — off the main thread, without a terminal, or while another prompt_toolkit application already owns the screen.

class xonsh.shells.ptk_shell.input_hook.PTKInputHook(shell=None)[source]

Callable replacement for builtins.input.

Parameters:
shellPromptToolkitShell

The shell that owns this hook. Only used to read the editing mode, so the input() prompt matches the shell’s key bindings.

install()[source]

Replace builtins.input with this hook.

prompt(message)[source]

Read one line through a dedicated prompt_toolkit session.

restore()[source]

Put the original builtins.input back.

usable()[source]

Whether a prompt_toolkit prompt can be driven right now.