Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bb60653
gh-138577: Fix keyboard shortcuts in getpass with echo_char
CuriousLearner Nov 15, 2025
31e35e4
Address reviews on dispatcher pattern + handle other ctrl chars
CuriousLearner Nov 16, 2025
b8609bd
Address reviews
CuriousLearner Nov 30, 2025
2691ad9
Merge remote-tracking branch 'upstream/main' into fix-gh-138577
CuriousLearner Mar 21, 2026
6a59f3b
Address reviews
CuriousLearner Mar 22, 2026
d280ce9
fix: prevent prompt corruption during getpass echo_char line editing
CuriousLearner Mar 23, 2026
3f1a861
fix: disable IEXTEN in non-canonical mode to allow Ctrl+V (LNEXT) han…
CuriousLearner Mar 23, 2026
537392c
refactor: address review on getpass echo_char line editing
CuriousLearner Mar 23, 2026
e1e4aa3
fix: Add comments about ICANON and IEXTEN
CuriousLearner Mar 23, 2026
3e05fa3
Merge branch 'main' of github.com:python/cpython into fix-gh-138577
CuriousLearner Mar 23, 2026
90da605
Address reviews
CuriousLearner Mar 24, 2026
ef1efcb
Merge branch 'main' into fix-gh-138577
CuriousLearner Mar 24, 2026
a7c1de3
Remove prefix from private class methods
CuriousLearner Mar 24, 2026
78b2c6a
Merge branch 'fix-gh-138577' of github.com:CuriousLearner/cpython int…
CuriousLearner Mar 24, 2026
e1461a7
Merge branch 'main' of github.com:python/cpython into fix-gh-138577
CuriousLearner Mar 24, 2026
741a817
Apply suggestions from code review
vstinner Mar 24, 2026
cc5dc99
chore! update some documentation
picnixz Mar 29, 2026
0f5f5c8
fix! refresh screen on Ctrl+A/Ctrl-E
picnixz Mar 29, 2026
9d90dfa
refactor! use more handlers
picnixz Mar 29, 2026
3b2ae38
chore! reduce diff against `main`
picnixz Mar 29, 2026
919af5c
test: add cursor position tests for Ctrl+A/Ctrl+E in getpass echo_char
CuriousLearner Mar 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor! use more handlers
  • Loading branch information
picnixz committed Mar 29, 2026
commit 9d90dfad25b61e9aa2496fadee4a5c1b0d5f4fb8
67 changes: 45 additions & 22 deletions Lib/getpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class GetPassWarning(UserWarning): pass

# Default POSIX control character mappings
_POSIX_CTRL_CHARS = frozendict({
'ERASE': '\x7f', # DEL/Backspace
'BS': '\x08', # Backspace
'ERASE': '\x7f', # DEL
'KILL': '\x15', # Ctrl+U - kill line
'WERASE': '\x17', # Ctrl+W - erase word
'LNEXT': '\x16', # Ctrl+V - literal next
Expand Down Expand Up @@ -251,13 +252,18 @@ def __init__(self, stream, echo_char, ctrl_chars, prompt=""):
self.literal_next = False
self.ctrl = ctrl_chars
self.dispatch = {
ctrl_chars['SOH']: self.handle_move_start, # Ctrl+A
ctrl_chars['ENQ']: self.handle_move_end, # Ctrl+E
ctrl_chars['VT']: self.handle_kill_forward, # Ctrl+K
ctrl_chars['KILL']: self.handle_kill_line, # Ctrl+U
ctrl_chars['WERASE']: self.handle_erase_word, # Ctrl+W
ctrl_chars['ERASE']: self.handle_erase, # DEL
'\b': self.handle_erase, # Backspace
ctrl_chars['SOH']: self.handle_move_start, # Ctrl+A
ctrl_chars['ENQ']: self.handle_move_end, # Ctrl+E
ctrl_chars['VT']: self.handle_kill_forward, # Ctrl+K
ctrl_chars['KILL']: self.handle_kill_line, # Ctrl+U
ctrl_chars['WERASE']: self.handle_erase_word, # Ctrl+W
ctrl_chars['ERASE']: self.handle_erase, # DEL
ctrl_chars['BS']: self.handle_erase, # Backspace
# special characters
ctrl_chars['LNEXT']: self.handle_literal_next, # Ctrl+V
ctrl_chars['EOF']: self.handle_eof, # Ctrl+D
ctrl_chars['INTR']: self.handle_interrupt, # Ctrl+C
'\x00': self.handle_nop, # ignore NUL
}

def refresh_display(self, prev_len=None):
Expand Down Expand Up @@ -285,6 +291,14 @@ def insert_char(self, char):
self.stream.write(self.echo_char)
self.stream.flush()

def is_eol(self, char):
"""Check if *char* is a line terminator."""
return char in ('\r', '\n')

def is_eof(self, char):
"""Check if *char* is a file terminator."""
return char == self.ctrl['EOF']

def handle_move_start(self):
"""Move cursor to beginning (Ctrl+A)."""
self.cursor_pos = 0
Expand Down Expand Up @@ -332,6 +346,23 @@ def handle_erase_word(self):
del self.password[self.cursor_pos:old_cursor]
self.refresh_display(prev_len)

def handle_literal_next(self):
"""State transition to indicate that the next character is literal."""
assert self.literal_next is False
self.literal_next = True

def handle_eof(self):
"""State transition to indicate that the pressed character was EOF."""
assert self.eof_pressed is False
self.eof_pressed = True

def handle_interrupt(self):
"""Raise a KeyboardInterrupt after Ctrl+C has been received."""
raise KeyboardInterrupt

def handle_nop(self):
"""Handler for an ignored character."""

def handle(self, char):
"""Handle a single character input. Returns True if handled."""
handler = self.dispatch.get(char)
Expand All @@ -345,33 +376,25 @@ def readline(self, input):
while True:
assert self.cursor_pos >= 0
char = input.read(1)

# Check for line terminators
if char in ('\n', '\r'):
if self.is_eol(char):
break
# Handle literal next mode first as Ctrl+V quotes characters.
elif self.literal_next:
# Handle literal next mode first as Ctrl+V quotes characters.
self.insert_char(char)
self.literal_next = False
# Check if it's the LNEXT character
elif char == self.ctrl['LNEXT']:
self.literal_next = True
# Check for special control characters
elif char == self.ctrl['INTR']:
raise KeyboardInterrupt
elif char == self.ctrl['EOF']:
# Handle EOF now as Ctrl+D must be pressed twice
# consecutively to stop reading from the input.
elif self.is_eof(char):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For posterity, but this state transition was really the most annoying. I collasped the if but it's not equivalent!

The flow is:

  • EOF (first time) -> do nothing but record EOF pressed
  • EOF (second time) -> stop!

If I do something else:

  • EOF (first time) -> do nothing but record EOF pressed
  • Any other character that is not EOF/EOL (including Ctrl+V) -> fall through and set eof_pressed = False before reading the next character.

I considered having the state acceptance being in a function but I can't just encode three transitions ("break loop", "pass to handlers", "fall through") with just booleans so I decided against.

if self.eof_pressed:
break
elif char == '\x00':
pass
elif self.handle(char):
# Dispatched to handler.
pass
else:
# Insert as normal character.
self.insert_char(char)

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

Comment thread
vstinner marked this conversation as resolved.
return ''.join(self.password)

Expand Down