Prompt for password with echo off, using Windows getwch().
(prompt='Password: ', stream=None, *, echo_char=None)
| 103 | |
| 104 | |
| 105 | def win_getpass(prompt='Password: ', stream=None, *, echo_char=None): |
| 106 | """Prompt for password with echo off, using Windows getwch().""" |
| 107 | if sys.stdin is not sys.__stdin__: |
| 108 | return fallback_getpass(prompt, stream) |
| 109 | _check_echo_char(echo_char) |
| 110 | |
| 111 | for c in prompt: |
| 112 | msvcrt.putwch(c) |
| 113 | pw = "" |
| 114 | while 1: |
| 115 | c = msvcrt.getwch() |
| 116 | if c == '\r' or c == '\n': |
| 117 | break |
| 118 | if c == '\003': |
| 119 | raise KeyboardInterrupt |
| 120 | if c == '\b': |
| 121 | if echo_char and pw: |
| 122 | msvcrt.putwch('\b') |
| 123 | msvcrt.putwch(' ') |
| 124 | msvcrt.putwch('\b') |
| 125 | pw = pw[:-1] |
| 126 | else: |
| 127 | pw = pw + c |
| 128 | if echo_char: |
| 129 | msvcrt.putwch(echo_char) |
| 130 | msvcrt.putwch('\r') |
| 131 | msvcrt.putwch('\n') |
| 132 | return pw |
| 133 | |
| 134 | |
| 135 | def fallback_getpass(prompt='Password: ', stream=None, *, echo_char=None): |
nothing calls this directly
no test coverage detected