Skip to content

Commit 6a59f3b

Browse files
Address reviews
1 parent 2691ad9 commit 6a59f3b

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

Lib/getpass.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class _PasswordLineEditor:
232232
def __init__(self, stream, echo_char, ctrl_chars):
233233
self.stream = stream
234234
self.echo_char = echo_char
235-
self.passwd = ""
235+
self.passwd = []
236236
self.cursor_pos = 0
237237
self.eof_pressed = False
238238
self.literal_next = False
@@ -256,12 +256,12 @@ def _refresh_display(self):
256256
self.stream.flush()
257257

258258
def _erase_chars(self, count):
259-
"""Erase count echo characters from display."""
259+
"""Erase *count* echo characters from display."""
260260
self.stream.write("\b \b" * count)
261261

262262
def _insert_char(self, char):
263-
"""Insert character at cursor position."""
264-
self.passwd = self.passwd[:self.cursor_pos] + char + self.passwd[self.cursor_pos:]
263+
"""Insert *char* at cursor position."""
264+
self.passwd.insert(self.cursor_pos, char)
265265
self.cursor_pos += 1
266266
# Only refresh if inserting in middle
267267
if self.cursor_pos < len(self.passwd):
@@ -280,41 +280,42 @@ def _handle_move_end(self):
280280

281281
def _handle_erase(self):
282282
"""Delete character before cursor (Backspace/DEL)."""
283-
if self.cursor_pos > 0:
284-
self.passwd = self.passwd[:self.cursor_pos-1] + self.passwd[self.cursor_pos:]
285-
self.cursor_pos -= 1
286-
# Only refresh if deleting from middle
287-
if self.cursor_pos < len(self.passwd):
288-
self._refresh_display()
289-
else:
290-
self.stream.write("\b \b")
291-
self.stream.flush()
283+
if self.cursor_pos <= 0:
284+
return
285+
del self.passwd[self.cursor_pos - 1]
286+
self.cursor_pos -= 1
287+
# Only refresh if deleting from middle
288+
if self.cursor_pos < len(self.passwd):
289+
self._refresh_display()
290+
else:
291+
self.stream.write("\b \b")
292+
self.stream.flush()
292293

293294
def _handle_kill_line(self):
294295
"""Erase entire line (Ctrl+U)."""
295296
self._erase_chars(len(self.passwd))
296-
self.passwd = ""
297+
self.passwd.clear()
297298
self.cursor_pos = 0
298299
self.stream.flush()
299300

300301
def _handle_kill_forward(self):
301302
"""Kill from cursor to end (Ctrl+K)."""
302303
chars_to_delete = len(self.passwd) - self.cursor_pos
303-
self.passwd = self.passwd[:self.cursor_pos]
304+
del self.passwd[self.cursor_pos:]
304305
self._erase_chars(chars_to_delete)
305306
self.stream.flush()
306307

307308
def _handle_erase_word(self):
308309
"""Erase previous word (Ctrl+W)."""
309310
old_cursor = self.cursor_pos
310311
# Skip trailing spaces
311-
while self.cursor_pos > 0 and self.passwd[self.cursor_pos-1] == ' ':
312+
while self.cursor_pos > 0 and self.passwd[self.cursor_pos - 1] == ' ':
312313
self.cursor_pos -= 1
313-
# Delete the word
314-
while self.cursor_pos > 0 and self.passwd[self.cursor_pos-1] != ' ':
314+
# Skip the word
315+
while self.cursor_pos > 0 and self.passwd[self.cursor_pos - 1] != ' ':
315316
self.cursor_pos -= 1
316317
# Remove the deleted portion
317-
self.passwd = self.passwd[:self.cursor_pos] + self.passwd[old_cursor:]
318+
del self.passwd[self.cursor_pos:old_cursor]
318319
self._refresh_display()
319320

320321
def handle(self, char):
@@ -363,7 +364,7 @@ def _readline_with_echo_char(stream, input, echo_char, term_ctrl_chars=None):
363364
editor._insert_char(char)
364365
editor.eof_pressed = False
365366

366-
return editor.passwd
367+
return ''.join(editor.passwd)
367368

368369

369370
def getuser():

0 commit comments

Comments
 (0)