Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
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
11 changes: 10 additions & 1 deletion Doc/library/getpass.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

The :mod:`getpass` module provides two functions:

.. function:: getpass(prompt='Password: ', stream=None)
.. function:: getpass(prompt='Password: ', stream=None, *, echochar=None)

Prompt the user for a password without echoing. The user is prompted using
the string *prompt*, which defaults to ``'Password: '``. On Unix, the
Expand All @@ -25,6 +25,12 @@ The :mod:`getpass` module provides two functions:
(:file:`/dev/tty`) or if that is unavailable to ``sys.stderr`` (this
argument is ignored on Windows).

The *echochar* argument controls how user input is displayed while typing.
If *echochar* is ``None`` (default), input remains hidden. If *echochar* is
a string, each typed character is replaced with the given string.
For example, ``echochar='*'`` will display asterisks instead of the actual
input.

If echo free input is unavailable getpass() falls back to printing
a warning message to *stream* and reading from ``sys.stdin`` and
issuing a :exc:`GetPassWarning`.
Expand All @@ -33,6 +39,9 @@ The :mod:`getpass` module provides two functions:
If you call getpass from within IDLE, the input may be done in the
terminal you launched IDLE from rather than the idle window itself.

.. versionchanged:: next
Added the *echochar* parameter for keyboard feedback.

.. exception:: GetPassWarning

A :exc:`UserWarning` subclass issued when password input may be echoed.
Expand Down
10 changes: 10 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,16 @@ getopt
* Add support for returning intermixed options and non-option arguments in order.
(Contributed by Serhiy Storchaka in :gh:`126390`.)


getpass
-------

* Support keyboard feedback by :func:`getpass.getpass` via the keyword-only
optional argument ``echochar``. Placeholder characters are rendered whenever
a character is entered, and removed when a character is deleted.
(Contributed by Semyon Moroz in :gh:`77065`.)


http
----

Expand Down
59 changes: 55 additions & 4 deletions Lib/getpass.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Utilities to get a password and/or the current user name.

getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
getpass(prompt[, stream[, echochar]]) - Prompt for a password, with echo
turned off and optional keyboard feedback.
getuser() - Get the user name from the environment or password database.

GetPassWarning - This UserWarning is issued when getpass() cannot prevent
Expand All @@ -25,13 +26,15 @@
class GetPassWarning(UserWarning): pass


def unix_getpass(prompt='Password: ', stream=None):
def unix_getpass(prompt='Password: ', stream=None, *, echochar=None):
"""Prompt for a password, with echo turned off.

Args:
prompt: Written on stream to ask for the input. Default: 'Password: '
stream: A writable file object to display the prompt. Defaults to
the tty. If no tty is available defaults to sys.stderr.
echochar: A string used to mask input (e.g., '*'). If None, input is
hidden.
Returns:
The seKr3t input.
Raises:
Expand All @@ -40,6 +43,10 @@ def unix_getpass(prompt='Password: ', stream=None):

Always restores terminal settings before returning.
"""
if echochar and not echochar.isascii():
return ValueError(f"Invalid echochar: {echochar}. "
"ASCII character expected.")

passwd = None
with contextlib.ExitStack() as stack:
try:
Expand Down Expand Up @@ -68,12 +75,20 @@ def unix_getpass(prompt='Password: ', stream=None):
old = termios.tcgetattr(fd) # a copy to save
new = old[:]
new[3] &= ~termios.ECHO # 3 == 'lflags'
if echochar:
new[3] &= ~termios.ICANON
tcsetattr_flags = termios.TCSAFLUSH
if hasattr(termios, 'TCSASOFT'):
tcsetattr_flags |= termios.TCSASOFT
try:
termios.tcsetattr(fd, tcsetattr_flags, new)
passwd = _raw_input(prompt, stream, input=input)
if not echochar:
passwd = _raw_input(prompt, stream, input=input)
stream.write('\n')
return passwd

passwd = _input_with_echochar(prompt, stream, input,
echochar)
finally:
termios.tcsetattr(fd, tcsetattr_flags, old)
stream.flush() # issue7208
Expand All @@ -93,10 +108,13 @@ def unix_getpass(prompt='Password: ', stream=None):
return passwd


def win_getpass(prompt='Password: ', stream=None):
def win_getpass(prompt='Password: ', stream=None, *, echochar=None):
"""Prompt for password with echo off, using Windows getwch()."""
if sys.stdin is not sys.__stdin__:
return fallback_getpass(prompt, stream)
if echochar and not echochar.isascii():
return ValueError(f"Invalid echochar: {echochar}. "
"ASCII character expected.")

for c in prompt:
msvcrt.putwch(c)
Expand All @@ -108,9 +126,15 @@ def win_getpass(prompt='Password: ', stream=None):
if c == '\003':
raise KeyboardInterrupt
if c == '\b':
if echochar and pw:
msvcrt.putwch('\b')
msvcrt.putwch(' ')
msvcrt.putwch('\b')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
msvcrt.putwch('\b')
msvcrt.putwch(' ')
msvcrt.putwch('\b')
msvcrt.putch('\b')
msvcrt.putch(' ')
msvcrt.putch('\b')

Those are not wide chars, so we don't need to use putwch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@picnixz I apologize again, as I was so eager to add this to 3.14, but I only have Windows now

the thing is that putch does not accept control characters, so you need to use putwch.
you can easily check this by simply trying to erase the character, and it will immediately give you an error:

msvcrt.putch('\b')
~~~~~~~~~~~~~^^^^^^
TypeError: putch() argument must be a byte string of length 1, not str

Copy link
Member

Choose a reason for hiding this comment

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

Oh my bad!

pw = pw[:-1]
else:
pw = pw + c
if echochar:
msvcrt.putwch(echochar)
msvcrt.putwch('\r')
msvcrt.putwch('\n')
return pw
Expand Down Expand Up @@ -151,6 +175,33 @@ def _raw_input(prompt="", stream=None, input=None):
return line


def _input_with_echochar(prompt, stream, input, echochar):
Copy link
Member

Choose a reason for hiding this comment

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

rather than duplicating so much of _raw_input's logic, I suggest making a _readline_with_echochar() function and just conditionally calling that vs the existing input.readline() call within _raw_input().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds reasonable, please check my diff

if not stream:
stream = sys.stderr
if not input:
input = sys.stdin
prompt = str(prompt)
stream.write(prompt)
stream.flush()
passwd = ""
while True:
char = input.read(1)
if char == '\n' or char == '\r':
break
if char == '\x03':
raise KeyboardInterrupt
if char == '\x7f' or char == '\b':
if passwd:
stream.write("\b \b")
stream.flush()
passwd = passwd[:-1]
else:
passwd += char
stream.write(echochar)
stream.flush()
return passwd


def getuser():
"""Get the username from the environment or password database.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add keyword-only optional argument *echochar* for :meth:`getpass.getpass`
for optional visual keyboard feedback support. Patch by Semyon Moroz.
Loading