Skip to content

Commit 0ae2b81

Browse files
committed
Rename argument
1 parent 9ac2ae2 commit 0ae2b81

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

Doc/library/getpass.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

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

19-
.. function:: getpass(prompt='Password: ', stream=None, mask=None)
19+
.. function:: getpass(prompt='Password: ', stream=None, *, echochar=None)
2020

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

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

3334
If echo free input is unavailable getpass() falls back to printing
3435
a warning message to *stream* and reading from ``sys.stdin`` and

Doc/whatsnew/3.14.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,9 @@ getopt
562562
getpass
563563
-------
564564

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

568570

Lib/getpass.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Utilities to get a password and/or the current user name.
22
3-
getpass(prompt[, stream[, mask]]) - Prompt for a password, with echo
4-
turned off.
3+
getpass(prompt[, stream[, echochar]]) - Prompt for a password, with echo
4+
turned off and optional keyboard feedback.
55
getuser() - Get the user name from the environment or password database.
66
77
GetPassWarning - This UserWarning is issued when getpass() cannot prevent
@@ -26,14 +26,15 @@
2626
class GetPassWarning(UserWarning): pass
2727

2828

29-
def unix_getpass(prompt='Password: ', stream=None, mask=None):
29+
def unix_getpass(prompt='Password: ', stream=None, *, echochar=None):
3030
"""Prompt for a password, with echo turned off.
3131
3232
Args:
3333
prompt: Written on stream to ask for the input. Default: 'Password: '
3434
stream: A writable file object to display the prompt. Defaults to
3535
the tty. If no tty is available defaults to sys.stderr.
36-
mask: A string used to mask input (e.g., '*'). If None, input is hidden.
36+
echochar: A string used to mask input (e.g., '*'). If None, input is
37+
hidden.
3738
Returns:
3839
The seKr3t input.
3940
Raises:
@@ -70,14 +71,14 @@ def unix_getpass(prompt='Password: ', stream=None, mask=None):
7071
old = termios.tcgetattr(fd) # a copy to save
7172
new = old[:]
7273
new[3] &= ~termios.ECHO # 3 == 'lflags'
73-
if mask:
74+
if echochar:
7475
new[3] &= ~termios.ICANON
7576
tcsetattr_flags = termios.TCSAFLUSH
7677
if hasattr(termios, 'TCSASOFT'):
7778
tcsetattr_flags |= termios.TCSASOFT
7879
try:
7980
termios.tcsetattr(fd, tcsetattr_flags, new)
80-
if not mask:
81+
if not echochar:
8182
passwd = _raw_input(prompt, stream, input=input)
8283
stream.write('\n')
8384
return passwd
@@ -92,14 +93,14 @@ def unix_getpass(prompt='Password: ', stream=None, mask=None):
9293
if char == '\x03':
9394
raise KeyboardInterrupt
9495
if char == '\x7f' or char == '\b':
95-
if mask and passwd:
96-
stream.write("\b \b" * len(mask))
96+
if echochar and passwd:
97+
stream.write("\b \b" * len(echochar))
9798
stream.flush()
9899
passwd = passwd[:-1]
99100
else:
100101
passwd += char
101-
if mask:
102-
stream.write(mask)
102+
if echochar:
103+
stream.write(echochar)
103104
stream.flush()
104105
finally:
105106
termios.tcsetattr(fd, tcsetattr_flags, old)
@@ -120,7 +121,7 @@ def unix_getpass(prompt='Password: ', stream=None, mask=None):
120121
return passwd
121122

122123

123-
def win_getpass(prompt='Password: ', stream=None, mask=None):
124+
def win_getpass(prompt='Password: ', stream=None, *, echochar=None):
124125
"""Prompt for password with echo off, using Windows getwch()."""
125126
if sys.stdin is not sys.__stdin__:
126127
return fallback_getpass(prompt, stream)
@@ -135,16 +136,16 @@ def win_getpass(prompt='Password: ', stream=None, mask=None):
135136
if c == '\003':
136137
raise KeyboardInterrupt
137138
if c == '\b':
138-
if mask and pw:
139-
for _ in mask:
139+
if echochar and pw:
140+
for _ in echochar:
140141
msvcrt.putwch('\b')
141142
msvcrt.putwch(' ')
142143
msvcrt.putwch('\b')
143144
pw = pw[:-1]
144145
else:
145146
pw = pw + c
146-
if mask:
147-
msvcrt.putwch(mask)
147+
if echochar:
148+
msvcrt.putwch(echochar)
148149
msvcrt.putwch('\r')
149150
msvcrt.putwch('\n')
150151
return pw
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Added optional argument *mask* for :meth:`getpass.getpass`. Patch by
2-
Semyon Moroz.
1+
Add keyword-only optional argument *mask* for :meth:`getpass.getpass`.
2+
Patch by Semyon Moroz.

0 commit comments

Comments
 (0)