Handle Ctrl-C in masked input

This commit is contained in:
thePR0M3TH3AN committed 2025-08-22 21:32:18 -04:00
1 parent d9f76ee668
commit c0a6187478
2 files changed
+38

No files matched your search

+6
View File
@@ -58,6 +58,8 @@ def _masked_input_windows(prompt: str) -> str:
buffer: list[str] = []
while True:
ch = msvcrt.getwch()
if ch == "\x03":
raise KeyboardInterrupt
if ch in ("\r", "\n"):
sys.stdout.write("\n")
return "".join(buffer)
@@ -85,6 +87,8 @@ def _masked_input_posix(prompt: str) -> str:
tty.setraw(fd)
while True:
ch = sys.stdin.read(1)
if ch == "\x03":
raise KeyboardInterrupt
if ch in ("\r", "\n"):
sys.stdout.write("\n")
return "".join(buffer)
@@ -105,6 +109,8 @@ def masked_input(prompt: str) -> str:
func = _masked_input_windows if sys.platform == "win32" else _masked_input_posix
try:
return func(prompt)
except KeyboardInterrupt:
raise
except Exception: # pragma: no cover - fallback when TTY operations fail
return input(prompt)