Skip to content

Commit 75e37bc

Browse files
committed
Add echochar check for ASCII
1 parent e2351ab commit 75e37bc

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Lib/getpass.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def unix_getpass(prompt='Password: ', stream=None, *, echochar=None):
4343
4444
Always restores terminal settings before returning.
4545
"""
46+
if echochar and not echochar.isascii():
47+
return ValueError(f"Invalid echochar: {echochar}. "
48+
"ASCII character expected.")
49+
4650
passwd = None
4751
with contextlib.ExitStack() as stack:
4852
try:
@@ -108,6 +112,9 @@ def win_getpass(prompt='Password: ', stream=None, *, echochar=None):
108112
"""Prompt for password with echo off, using Windows getwch()."""
109113
if sys.stdin is not sys.__stdin__:
110114
return fallback_getpass(prompt, stream)
115+
if echochar and not echochar.isascii():
116+
return ValueError(f"Invalid echochar: {echochar}. "
117+
"ASCII character expected.")
111118

112119
for c in prompt:
113120
msvcrt.putwch(c)
@@ -120,10 +127,9 @@ def win_getpass(prompt='Password: ', stream=None, *, echochar=None):
120127
raise KeyboardInterrupt
121128
if c == '\b':
122129
if echochar and pw:
123-
for _ in echochar:
124-
msvcrt.putwch('\b')
125-
msvcrt.putwch(' ')
126-
msvcrt.putwch('\b')
130+
msvcrt.putwch('\b')
131+
msvcrt.putwch(' ')
132+
msvcrt.putwch('\b')
127133
pw = pw[:-1]
128134
else:
129135
pw = pw + c
@@ -186,7 +192,7 @@ def _input_with_echochar(prompt, stream, input, echochar):
186192
raise KeyboardInterrupt
187193
if char == '\x7f' or char == '\b':
188194
if passwd:
189-
stream.write("\b \b" * len(echochar))
195+
stream.write("\b \b")
190196
stream.flush()
191197
passwd = passwd[:-1]
192198
else:

0 commit comments

Comments
 (0)