Skip to content

getpass's echo_char should be restricted to a single ASCII character #138514

@benjaminjohnson01

Description

@benjaminjohnson01

Bug report

Bug description:

Running getpass.getpass(echo_char="***") works until a character is entered, at which point a traceback error occurs.

import getpass
getpass.getpass(echo_char="***")
Password: Traceback (most recent call last):
  File "<python-input-10>", line 1, in <module>
    getpass.getpass(echo_char="***")
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "...\pythoncore-3.14-64\Lib\getpass.py", line 129, in win_getpass
    msvcrt.putwch(echo_char)
    ~~~~~~~~~~~~~^^^^^^^^^^^
TypeError: putwch(): argument must be a unicode character, not a string of length 3

Validation should occur upon input for this argument, and getpass should raise a ValueError stating: "'echo_char' must be a single-character ASCII string, not a string of length 3" (with ASCII only being bolded in this write-up for emphasis that a Unicode character cannot be used).

Additionally, the error message language should be revised when a non-ASCII string is entered:

import getpass
getpass.getpass(echo_char="Æ")
Traceback (most recent call last):
  File "<python-input-11>", line 1, in <module>
    getpass.getpass(echo_char="Æ")
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "...\pythoncore-3.14-64\Lib\getpass.py", line 109, in win_getpass
    _check_echo_char(echo_char)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "...\pythoncore-3.14-64\Lib\getpass.py", line 149, in _check_echo_char
    raise ValueError("'echo_char' must be a printable ASCII string, "
                     f"got: {echo_char!r}")
ValueError: 'echo_char' must be a printable ASCII string, got: 'Æ'

Better language would be: "'echo_char' must be a single-character ASCII string, got: 'Æ'".

With agreement, I can submit a pull request.

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Labels

3.14bugs and security fixes3.15new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions