[issue37426] getpass.getpass not working with on windows when ctrl+v is used to enter the string
钟旭尧
report at bugs.python.org
Fri Feb 25 09:04:29 EST 2022
钟旭尧 <python_zhong at qq.com> added the comment:
I have an idea to solve it. But I don't know how to get the clipboard data.
The code like this (Windows):
def win_getpass(prompt='Password: ', stream=None, echoChar='*'):
"""Prompt for password with echo off, using Windows getwch()."""
if not isinstance(echoChar, str) or len(echoChar) < 1 or len(echoChar) > 1:
raise ValueError("Argument 'echoChar' is incorrect. It should be a character.")
if sys.stdin is not sys.__stdin__:
return fallback_getpass(prompt, stream)
for c in prompt:
msvcrt.putwch(c)
pw = ""
while 1:
c = msvcrt.getwch()
if ord(c) == 22: # User pressed Ctrl-V
k = <Clipboard Info>
pw += k
for _ in range(len(k)):
msvcrt.putwch(echoChar)
if c == '\r' or c == '\n' or c == '\r\n':
break
if c == '\003':
raise KeyboardInterrupt
if c == '\b':
if pw != '':
pw = pw[:-1]
count = len(pw)
for _ in range(count+1):
msvcrt.putwch('\b')
for _ in range(count):
msvcrt.putwch(echoChar)
msvcrt.putwch(' ')
msvcrt.putwch('\b')
else:
pw = pw + c
msvcrt.putwch(echoChar)
msvcrt.putwch('\r')
msvcrt.putwch('\n')
return pw
----------
nosy: +W_M
versions: -Python 3.10, Python 3.8, Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37426>
_______________________________________
More information about the Python-bugs-list
mailing list