Skip to content

Commit cc5dc99

Browse files
committed
chore! update some documentation
1 parent 741a817 commit cc5dc99

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Lib/getpass.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def _get_terminal_ctrl_chars(fd):
4444
"""Extract control characters from terminal settings.
4545
4646
Returns a dict mapping control char names to their str values.
47-
Falls back to POSIX defaults if termios isn't available.
47+
48+
Falls back to POSIX defaults if termios is not available
49+
or if the control character is not supported by termios.
4850
"""
4951
ctrl = dict(_POSIX_CTRL_CHARS)
5052
try:
@@ -53,7 +55,8 @@ def _get_terminal_ctrl_chars(fd):
5355
except (termios.error, OSError):
5456
return ctrl
5557

56-
# Ctrl+A/E/K (SOH/ENQ/VT) are not in termios, use POSIX defaults
58+
# Use defaults for Backspace (BS) and Ctrl+A/E/K (SOH/ENQ/VT)
59+
# as they are not in the termios control characters array.
5760
for name in ('ERASE', 'KILL', 'WERASE', 'LNEXT', 'EOF', 'INTR'):
5861
cap = getattr(termios, f'V{name}')
5962
if cap < len(cc):
@@ -108,7 +111,7 @@ def unix_getpass(prompt='Password: ', stream=None, *, echo_char=None):
108111
old = termios.tcgetattr(fd) # a copy to save
109112
new = old[:]
110113
new[3] &= ~termios.ECHO # 3 == 'lflags'
111-
# Extract control characters before changing terminal mode
114+
# Extract control characters before changing terminal mode.
112115
term_ctrl_chars = None
113116
if echo_char:
114117
# ICANON enables canonical (line-buffered) mode where
@@ -117,7 +120,7 @@ def unix_getpass(prompt='Password: ', stream=None, *, echo_char=None):
117120
new[3] &= ~termios.ICANON
118121
# IEXTEN enables implementation-defined input processing
119122
# such as LNEXT (Ctrl+V). Disable it so the terminal
120-
# driver doesn't intercept these characters before our
123+
# driver does not intercept these characters before our
121124
# code can handle them.
122125
new[3] &= ~termios.IEXTEN
123126
term_ctrl_chars = _get_terminal_ctrl_chars(fd)
@@ -258,11 +261,13 @@ def __init__(self, stream, echo_char, ctrl_chars, prompt=""):
258261
}
259262

260263
def refresh_display(self, prev_len=None):
261-
"""Redraw the entire password line with *echo_char*."""
264+
"""Redraw the entire password line with *echo_char*.
265+
266+
If *prev_len* is not specified, the current password length is used.
267+
"""
262268
prompt_len = len(self.prompt)
263-
# Use prev_len if given, otherwise current password length
264269
clear_len = prev_len if prev_len is not None else len(self.password)
265-
# Clear the entire line (prompt + password) and rewrite
270+
# Clear the entire line (prompt + password) and rewrite.
266271
self.stream.write('\r' + ' ' * (prompt_len + clear_len) + '\r')
267272
self.stream.write(self.prompt + self.echo_char * len(self.password))
268273
if self.cursor_pos < len(self.password):
@@ -273,7 +278,7 @@ def insert_char(self, char):
273278
"""Insert *char* at cursor position."""
274279
self.password.insert(self.cursor_pos, char)
275280
self.cursor_pos += 1
276-
# Only refresh if inserting in middle
281+
# Only refresh if inserting in middle.
277282
if self.cursor_pos < len(self.password):
278283
self.refresh_display()
279284
else:
@@ -313,13 +318,13 @@ def handle_kill_forward(self):
313318
def handle_erase_word(self):
314319
"""Erase previous word (Ctrl+W)."""
315320
old_cursor = self.cursor_pos
316-
# Skip trailing spaces
321+
# Calculate the starting position of the previous word,
322+
# ignoring trailing whitespaces.
317323
while self.cursor_pos > 0 and self.password[self.cursor_pos - 1] == ' ':
318324
self.cursor_pos -= 1
319-
# Skip the word
320325
while self.cursor_pos > 0 and self.password[self.cursor_pos - 1] != ' ':
321326
self.cursor_pos -= 1
322-
# Remove the deleted portion
327+
# Delete the previous word and refresh the screen.
323328
prev_len = len(self.password)
324329
del self.password[self.cursor_pos:old_cursor]
325330
self.refresh_display(prev_len)
@@ -340,8 +345,8 @@ def readline(self, input):
340345
# Check for line terminators
341346
if char in ('\n', '\r'):
342347
break
343-
# Handle literal next mode FIRST (Ctrl+V quotes next char)
344348
elif self.literal_next:
349+
# Handle literal next mode first as Ctrl+V quotes characters.
345350
self.insert_char(char)
346351
self.literal_next = False
347352
# Check if it's the LNEXT character
@@ -356,10 +361,10 @@ def readline(self, input):
356361
elif char == '\x00':
357362
pass
358363
elif self.handle(char):
359-
# Dispatched to handler
364+
# Dispatched to handler.
360365
pass
361366
else:
362-
# Insert as normal character
367+
# Insert as normal character.
363368
self.insert_char(char)
364369

365370
self.eof_pressed = (char == self.ctrl['EOF'])

0 commit comments

Comments
 (0)