This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: getpass.getpass not working with on windows when ctrl+v is used to enter the string
Type: behavior Stage:
Components: Documentation Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Atul Bagga, W_M, brianhrutledge, docs@python, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2019-06-27 09:49 by Atul Bagga, last changed 2022-04-11 14:59 by admin.

Messages (8)
msg346721 - (view) Author: Atul Bagga (Atul Bagga) Date: 2019-06-27 09:49
Detailed issue filed here - https://github.com/microsoft/knack/issues/160
msg346727 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-06-27 11:00
getpass() reads from the console via msvcrt.getwch(). This is a raw console read, so Ctrl+V is read as the SYN (0x16) control character. Only the following keys are handled specially by getpass: 

* Ctrl+C - raise KeyboardInterrupt
* Ctrl+H (backspace) - delete the previous character
* Ctrl+J (linefeed) and Ctrl+M (return) - return

getpass() behaves pretty much the same in a Linux terminal emulator, except Ctrl+H isn't backspace. 

Recent versions of the console host in Windows 10 have an option to support pasting via Ctrl+Shift+V, regardless of the input mode. This is pretty common in Unix terminal emulators as well. You can change this setting in the Properties->Options dialog for a particular window or shortcut, or the default setting in the Defaults->Options dialog. In the "HKCU\Console" registry key, the DWORD value name is "InterceptCopyPaste".
msg346867 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-06-28 21:19
If anything, this seems to be a getpass doc issue.
msg347126 - (view) Author: Atul Bagga (Atul Bagga) Date: 2019-07-02 10:04
Suprisingly this works fine on ConEMU which I commonly use on windows though internally I still use powershell on conemu. 
https://conemu.github.io/

It does not work on CMD and Powershell consoles.
msg375819 - (view) Author: Brian Rutledge (brianhrutledge) Date: 2020-08-23 17:46
In addition to Ctrl+V, Shift+Insert also doesn't work. This behavior is the same Command Prompt and PowerShell on Windows 10.

Workarounds include:

- Clicking `Edit > Paste` from the window menu
- Enabling `Properties > Options > Use Ctrl+Shift+C/V as Copy/Paste` from the menu
- Use right-click to paste
- Using the new Windows Terminal: https://www.microsoft.com/en-us/p/windows-terminal/9n0dx20hk701

I stumbled upon this issue while investigating https://github.com/pypa/twine/issues/671. Thanks for writing it up!
msg413993 - (view) Author: 钟旭尧 (W_M) Date: 2022-02-25 14:04
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
msg414012 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-02-25 16:02
> Clicking `Edit > Paste` from the window menu
> Use right-click to paste

In particular, if the console has quick-edit mode enabled, then you can paste text by right-clicking. Also, if text is selected in quick-edit mode, right-clicking copies to the clipboard.

> Enabling `Properties > Options > Use Ctrl+Shift+C/V as Copy/Paste` from the menu

I prefer this setting because it matches the behavior of terminals on other platforms. I suggest setting it in the "Defaults" dialog instead of "Properties". Otherwise, you'll have to configure it individually for each shell link (.LNK file) or session title. (The console session title defaults to the executable path, unless set in STARTUPINFO.)

> Using the new Windows Terminal

Terminal allows configuring the actions for keyboard shortcuts. By default it grabs Ctrl+C (copy) and Ctrl+V (paste). I disable these mappings. I leave the default mappings in place for Ctrl+Shift+C (copy), Ctrl+Shift+V (paste), Ctrl+Insert (copy), and Shift+Insert (paste).  

> This behavior is the same Command Prompt and PowerShell

The behavior has to be the same when the parent process is a normal console shell such as CMD or PowerShell. Python inherits its console session from the shell, and that's the extent of the shell's involvement. 

A console session is hosted by an instance of conhost.exe or openconsole.exe. If the host is running in pseudoconsole (headless) mode, then the user interface for the session is hosted by another application (e.g. Windows Terminal). Even in pseudoconsole mode, however, the console host a lot to do in order to manage the session state for the console API.
msg414024 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-02-25 17:19
> I have an idea to solve it. But I don't know how to get the 
> clipboard data.

In Windows, using the window manager entails extending the process and the current thread with GUI-related structures in the kernel and then connecting the process to a window station (usually "WinSta0", which contains the clipboard) and connecting the thread to a desktop (usually "Default"). This permanently changes how the OS sees the process. I think whether or not the process should be a GUI process is something for the application to decide, not the standard library. Thus getpass should not read text from the clipboard.

The docs could note that terminals may need to be configured to support Ctrl+Shift+C (copy) and Ctrl+Shift+V (paste) shortcuts, and that some terminals provide alternate ways to paste text, such as a right-click action or context menu. I don't think the docs should provide detailed explanations and configuration details for particular terminals.
History
Date User Action Args
2022-04-11 14:59:17adminsetgithub: 81607
2022-02-25 20:01:48terry.reedysetnosy: - terry.reedy

versions: + Python 3.11
2022-02-25 17:19:21eryksunsetmessages: + msg414024
2022-02-25 16:02:26eryksunsetmessages: + msg414012
2022-02-25 14:04:29W_Msetnosy: + W_M

messages: + msg413993
versions: - Python 3.8, Python 3.9, Python 3.10
2020-08-23 19:01:16terry.reedysetversions: - Python 3.6, Python 3.7
2020-08-23 17:46:22brianhrutledgesetnosy: + brianhrutledge

messages: + msg375819
versions: + Python 3.7, Python 3.8, Python 3.9, Python 3.10
2019-07-02 10:04:03Atul Baggasetmessages: + msg347126
2019-06-28 21:19:47terry.reedysetnosy: + terry.reedy, docs@python
messages: + msg346867

assignee: docs@python
components: + Documentation, - Library (Lib), Windows
2019-06-27 11:00:55eryksunsetnosy: + eryksun
messages: + msg346727
2019-06-27 10:32:50xtreaksetnosy: + tim.golden, steve.dower, zach.ware, paul.moore
components: + Windows
2019-06-27 09:49:37Atul Baggacreate