-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
gh-138577: Fix keyboard shortcuts in getpass with echo_char #141597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,52 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class GetPassWarning(UserWarning): pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Default POSIX control character mappings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _POSIX_CTRL_CHARS = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'ERASE': b'\x7f', # DEL/Backspace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'KILL': b'\x15', # Ctrl+U - kill line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'WERASE': b'\x17', # Ctrl+W - erase word | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'LNEXT': b'\x16', # Ctrl+V - literal next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'EOF': b'\x04', # Ctrl+D - EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'INTR': b'\x03', # Ctrl+C - interrupt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'SOH': b'\x01', # Ctrl+A - start of heading (beginning of line) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'ENQ': b'\x05', # Ctrl+E - enquiry (end of line) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'VT': b'\x0b', # Ctrl+K - vertical tab (kill forward) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _get_terminal_ctrl_chars(fd): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Extract control characters from terminal settings. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns a dict mapping control char names to their byte values. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Falls back to POSIX defaults if termios isn't available. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| old = termios.tcgetattr(fd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cc = old[6] # Index 6 is the control characters array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'ERASE': cc[termios.VERASE] if termios.VERASE < len(cc) else _POSIX_CTRL_CHARS['ERASE'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'KILL': cc[termios.VKILL] if termios.VKILL < len(cc) else _POSIX_CTRL_CHARS['KILL'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'WERASE': cc[termios.VWERASE] if termios.VWERASE < len(cc) else _POSIX_CTRL_CHARS['WERASE'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'LNEXT': cc[termios.VLNEXT] if termios.VLNEXT < len(cc) else _POSIX_CTRL_CHARS['LNEXT'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'EOF': cc[termios.VEOF] if termios.VEOF < len(cc) else _POSIX_CTRL_CHARS['EOF'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'INTR': cc[termios.VINTR] if termios.VINTR < len(cc) else _POSIX_CTRL_CHARS['INTR'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Ctrl+A/E/K are not in termios, use POSIX defaults | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'SOH': _POSIX_CTRL_CHARS['SOH'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'ENQ': _POSIX_CTRL_CHARS['ENQ'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'VT': _POSIX_CTRL_CHARS['VT'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except (termios.error, OSError): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _POSIX_CTRL_CHARS.copy() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | |
| old = termios.tcgetattr(fd) | |
| cc = old[6] # Index 6 is the control characters array | |
| return { | |
| 'ERASE': cc[termios.VERASE] if termios.VERASE < len(cc) else _POSIX_CTRL_CHARS['ERASE'], | |
| 'KILL': cc[termios.VKILL] if termios.VKILL < len(cc) else _POSIX_CTRL_CHARS['KILL'], | |
| 'WERASE': cc[termios.VWERASE] if termios.VWERASE < len(cc) else _POSIX_CTRL_CHARS['WERASE'], | |
| 'LNEXT': cc[termios.VLNEXT] if termios.VLNEXT < len(cc) else _POSIX_CTRL_CHARS['LNEXT'], | |
| 'EOF': cc[termios.VEOF] if termios.VEOF < len(cc) else _POSIX_CTRL_CHARS['EOF'], | |
| 'INTR': cc[termios.VINTR] if termios.VINTR < len(cc) else _POSIX_CTRL_CHARS['INTR'], | |
| # Ctrl+A/E/K are not in termios, use POSIX defaults | |
| 'SOH': _POSIX_CTRL_CHARS['SOH'], | |
| 'ENQ': _POSIX_CTRL_CHARS['ENQ'], | |
| 'VT': _POSIX_CTRL_CHARS['VT'], | |
| } | |
| except (termios.error, OSError): | |
| return _POSIX_CTRL_CHARS.copy() | |
| res = _POSIX_CTRL_CHARS.copy() | |
| try: | |
| old = termios.tcgetattr(fd) | |
| cc = old[6] # Index 6 is the control characters array | |
| except (termios.error, OSError): | |
| return res | |
| # Ctrl+A/E/K are not in termios, use POSIX defaults | |
| for name in ["ERASE", "KILL", "WERASE", "LNEXT", "EOF", "INTR"] | |
| cap = getattr(termios, f"V{name}") | |
| if cap < len(cc): | |
| res[name] = cc[flag] | |
| return res |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion, there was some minor issue here, which I've fixed in my refactoring
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we have the POSIX defaults already decoded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored this in b8609bd
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| """Redraw the entire password line with asterisks.""" | |
| """Redraw the entire password line with *echo_char*.""" |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we adding 20 extra characters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The +20 was an arbitrary buffer and has been removed. The current implementation now uses just len(self.passwd) to clear only the necessary characters:
self.stream.write('\r' + ' ' * len(self.passwd) + '\r')
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reduce the number of continue, just use if-elif constructions here.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a suggestion: don't build a dispatch table via a function but make the editor handles it internally:
editor.handle(char)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming
cc[termios.VINTR]gives bytes right? in this case we can already decode it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
cc[termios.VINTR]returns bytes. This is now decoded inline in the refactored implementation:The
_POSIX_CTRL_CHARSdefaults are also now stored as already-decoded strings.