Skip to content

Commit 90da605

Browse files
Address reviews
1 parent 3e05fa3 commit 90da605

File tree

3 files changed

+6
-13
lines changed

3 files changed

+6
-13
lines changed

Doc/library/getpass.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The :mod:`!getpass` module provides two functions:
5555
.. versionchanged:: 3.14
5656
Added the *echo_char* parameter for keyboard feedback.
5757

58-
.. versionchanged:: 3.15
58+
.. versionchanged:: next
5959
When using non-empty *echo_char* on Unix, keyboard shortcuts (including cursor
6060
movement and line editing) are now properly handled using the terminal's
6161
control character configuration.

Lib/getpass.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ def _handle_erase_word(self):
326326

327327
def _handle(self, char):
328328
"""Handle a single character input. Returns True if handled."""
329-
self.eof_pressed = False
330329
handler = self._dispatch.get(char)
331330
if handler:
332331
handler()
@@ -345,18 +344,15 @@ def readline(self, input):
345344
elif self.literal_next:
346345
self._insert_char(char)
347346
self.literal_next = False
348-
self.eof_pressed = False
349347
# Check if it's the LNEXT character
350348
elif char == self.ctrl['LNEXT']:
351349
self.literal_next = True
352-
self.eof_pressed = False
353350
# Check for special control characters
354351
elif char == self.ctrl['INTR']:
355352
raise KeyboardInterrupt
356353
elif char == self.ctrl['EOF']:
357354
if self.eof_pressed:
358355
break
359-
self.eof_pressed = True
360356
elif char == '\x00':
361357
pass
362358
elif self._handle(char):
@@ -365,7 +361,8 @@ def readline(self, input):
365361
else:
366362
# Insert as normal character
367363
self._insert_char(char)
368-
self.eof_pressed = False
364+
365+
self.eof_pressed = (char == self.ctrl['EOF'])
369366

370367
return ''.join(self.password)
371368

Lib/test/test_getpass.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,14 @@ def test_ctrl_w_display_preserves_prompt(self):
127127
output = self.check_raw_input('hello world\x17\n', 'hello ')
128128
# The final visible state should be "Password: ******"
129129
# Verify prompt is rewritten during refresh, not overwritten by stars
130-
self.assertTrue(output.endswith('Password: ******'),
131-
f'Prompt corrupted in display: {output!r}')
130+
self.assertEndsWith(output, 'Password: ******')
132131

133132
def test_ctrl_a_insert_display_preserves_prompt(self):
134133
# Reproducer from gh-138577: type "abc", Ctrl+A, type "x"
135134
# Display must show "Password: ****" not "****word: ***"
136135
output = self.check_raw_input('abc\x01x\n', 'xabc')
137136
# The final visible state should be "Password: ****"
138-
self.assertTrue(output.endswith('Password: ****\x08\x08\x08'),
139-
f'Prompt corrupted in display: {output!r}')
137+
self.assertEndsWith(output, 'Password: ****\x08\x08\x08')
140138

141139
def test_lnext_ctrl_v_with_echo_char(self):
142140
# Ctrl+V (LNEXT) should insert the next character literally
@@ -156,10 +154,8 @@ def test_ctrl_k_kill_forward_with_echo_char(self):
156154

157155
def test_ctrl_c_interrupt_with_echo_char(self):
158156
# Ctrl+C should raise KeyboardInterrupt
159-
mock_input = StringIO('test\x03more')
160-
mock_output = StringIO()
161157
with self.assertRaises(KeyboardInterrupt):
162-
getpass._raw_input('Password: ', mock_output, mock_input, '*')
158+
self.check_raw_input('test\x03more', '')
163159

164160
def test_ctrl_d_eof_with_echo_char(self):
165161
# Ctrl+D twice should cause EOF

0 commit comments

Comments
 (0)