Skip to content

Commit 501d704

Browse files
committed
Update checks rules
1 parent bcdf95a commit 501d704

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

Doc/library/getpass.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ The :mod:`getpass` module provides two functions:
2626
argument is ignored on Windows).
2727

2828
The *echochar* argument controls how user input is displayed while typing.
29-
If *echochar* is ``None`` (default), input remains hidden. If *echochar* is
30-
a string, each typed character is replaced with the given string. But this
31-
string must be ASCII character. For example, ``echochar='*'`` will display
29+
If *echochar* is ``None`` (default), input remains hidden. Otherwise,
30+
*echochar* must be a printable ASCII string and each typed character
31+
is replaced by the former. For example, ``echochar='*'`` will display
3232
asterisks instead of the actual input.
3333

3434
If echo free input is unavailable getpass() falls back to printing

Lib/getpass.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ def unix_getpass(prompt='Password: ', stream=None, *, echochar=None):
4343
4444
Always restores terminal settings before returning.
4545
"""
46-
if not _is_ascii(echochar):
47-
return ValueError(f"'echochar' must be ASCII, got: {echochar!r}")
46+
_check_echochar(echochar)
4847

4948
passwd = None
5049
with contextlib.ExitStack() as stack:
@@ -109,8 +108,7 @@ def win_getpass(prompt='Password: ', stream=None, *, echochar=None):
109108
"""Prompt for password with echo off, using Windows getwch()."""
110109
if sys.stdin is not sys.__stdin__:
111110
return fallback_getpass(prompt, stream)
112-
if not _is_ascii(echochar):
113-
return ValueError(f"'echochar' must be ASCII, got: {echochar!r}")
111+
_check_echochar(echochar)
114112

115113
for c in prompt:
116114
msvcrt.putwch(c)
@@ -146,11 +144,12 @@ def fallback_getpass(prompt='Password: ', stream=None):
146144
return _raw_input(prompt, stream)
147145

148146

149-
def _is_ascii(echochar):
147+
def _check_echochar(echochar):
150148
# ASCII excluding control characters
151-
if echochar and (32 <= ord(echochar) <= 127):
152-
return True
153-
return False
149+
if echochar and not (len(echochar) == 1 and
150+
echochar.isprintable() and
151+
echochar.isascii()):
152+
raise ValueError(f"'echochar' must be ASCII, got: {echochar!r}")
154153

155154

156155
def _raw_input(prompt="", stream=None, input=None):

0 commit comments

Comments
 (0)