Skip to content

Commit 4eb5701

Browse files
committed
modify
1 parent fcda047 commit 4eb5701

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Lib/getpass.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ def fallback_getpass(prompt='Password: ', stream=None, *, echo_char=None):
145145

146146
def _check_echo_char(echo_char):
147147
# Single-character ASCII excluding control characters
148-
if echo_char and not (
148+
if echo_char is None:
149+
return
150+
if not isinstance(echo_char, str):
151+
raise TypeError("'echo_char' must be type 'str' or 'None', got: "
152+
f"{echo_char!r} (type: {type(echo_char).__name__})")
153+
if not (
149154
len(echo_char) == 1
150155
and echo_char.isprintable()
151156
and echo_char.isascii()

Lib/test/test_getpass.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,38 @@ def test_control_chars_with_echo_char(self):
200200
self.assertEqual(result, expect_result)
201201
self.assertEqual('Password: *******\x08 \x08', mock_output.getvalue())
202202

203+
class GetpassEchoCharValidationTest(unittest.TestCase):
204+
def test_accepts_none(self):
205+
getpass._check_echo_char(None)
206+
207+
def test_accepts_single_printable_ascii(self):
208+
for ch in ["*", "A", " "]:
209+
getpass._check_echo_char(ch)
210+
211+
def test_rejects_multi_character_strings(self):
212+
for s in ["***", "AA", "aA*!"]:
213+
with self.assertRaises(ValueError):
214+
getpass._check_echo_char(s)
215+
216+
def test_rejects_non_ascii(self):
217+
for s in ["Æ", "❤️", "🐍"]:
218+
with self.assertRaises(ValueError):
219+
getpass._check_echo_char(s)
220+
221+
def test_rejects_control_characters(self):
222+
for ch in ["\n", "\t", "\r", "\x00", "\x7f", "\x07"]:
223+
with self.assertRaises(ValueError):
224+
getpass._check_echo_char(ch)
225+
226+
def test_rejects_non_string(self):
227+
for item in [b"*", 0]:
228+
with self.assertRaises(TypeError):
229+
getpass._check_echo_char(item)
230+
231+
def test_rejects_empty_string(self):
232+
for item in [""]:
233+
with self.assertRaises(ValueError):
234+
getpass._check_echo_char(item)
203235

204236
if __name__ == "__main__":
205237
unittest.main()

0 commit comments

Comments
 (0)