From 2e422e5ba8dd0835772f16ba03e68e961e3a8913 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 11 Aug 2022 05:20:05 +0530 Subject: [PATCH] Use getpass() to read the password from STDIN when it is a tty --- kitty/remote_control.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/kitty/remote_control.py b/kitty/remote_control.py index 9150ada06..18604f262 100644 --- a/kitty/remote_control.py +++ b/kitty/remote_control.py @@ -402,18 +402,22 @@ def get_password(opts: RCOptions) -> str: ans = opts.password if not ans and opts.password_file: if opts.password_file == '-': - ans = sys.stdin.read().strip() - try: - tty_fd = os.open(os.ctermid(), os.O_RDONLY | os.O_CLOEXEC) - except OSError: - pass + if sys.stdin.isatty(): + from getpass import getpass + ans = getpass() else: - with open(tty_fd, closefd=True): - os.dup2(tty_fd, sys.stdin.fileno()) + ans = sys.stdin.read().rstrip() + try: + tty_fd = os.open(os.ctermid(), os.O_RDONLY | os.O_CLOEXEC) + except OSError: + pass + else: + with open(tty_fd, closefd=True): + os.dup2(tty_fd, sys.stdin.fileno()) else: try: with open(resolve_custom_file(opts.password_file)) as f: - ans = f.read().strip() + ans = f.read().rstrip() except OSError: pass if not ans and opts.password_env: