-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
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 5 commits
bb60653
31e35e4
b8609bd
2691ad9
6a59f3b
d280ce9
3f1a861
537392c
e1e4aa3
3e05fa3
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,40 @@ | |||||||||||||||||||||||
| class GetPassWarning(UserWarning): pass | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| # Default POSIX control character mappings | ||||||||||||||||||||||||
| _POSIX_CTRL_CHARS = { | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| 'ERASE': '\x7f', # DEL/Backspace | ||||||||||||||||||||||||
| 'KILL': '\x15', # Ctrl+U - kill line | ||||||||||||||||||||||||
| 'WERASE': '\x17', # Ctrl+W - erase word | ||||||||||||||||||||||||
| 'LNEXT': '\x16', # Ctrl+V - literal next | ||||||||||||||||||||||||
| 'EOF': '\x04', # Ctrl+D - EOF | ||||||||||||||||||||||||
| 'INTR': '\x03', # Ctrl+C - interrupt | ||||||||||||||||||||||||
| 'SOH': '\x01', # Ctrl+A - start of heading (beginning of line) | ||||||||||||||||||||||||
| 'ENQ': '\x05', # Ctrl+E - enquiry (end of line) | ||||||||||||||||||||||||
| 'VT': '\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 str values. | ||||||||||||||||||||||||
| Falls back to POSIX defaults if termios isn't available. | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| return res | |
| # Ctrl+A/E/K are not in termios, use POSIX defaults | |
| return res | |
| # Ctrl+A/E/K are not in termios, use POSIX defaults |
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.
You should move Ctrl+A/E/K are not in termios, use POSIX defaults comment the for loop.
You may add the names:
Ctrl+A/E/K (SOH/ENQ/VT) are not in termios, use POSIX defaults
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.
Would you mind to add a comment to explain ICANON and IEXTEN flags?
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.
You may rename the attribute to self.password.
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.
Is it needed to get a copy? _PasswordLineEditor doesn't seem to modify the dictionary. If _POSIX_CTRL_CHARS is modified to become a frozendict, it may make things clearer.
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.
You should move this code (including while True:) to a new _PasswordLineEditor.readline() method.
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.
| # Dispatch to handler or insert as normal character | |
| elif not editor.handle(char): | |
| editor._insert_char(char) | |
| editor.eof_pressed = False | |
| elif editor.handle(char): | |
| # Dispatched to handler | |
| pass | |
| else: | |
| # Insert as normal character | |
| editor._insert_char(char) | |
| editor.eof_pressed = False |
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.