[Python-bugs-list] [Bug #110655] getpass.getpass on Windows (PR#349)

noreply@sourceforge.net noreply@sourceforge.net
Thu, 14 Sep 2000 02:05:10 -0700

Bug #110655, was updated on 2000-Jul-31 14:10
Here is a current snapshot of the bug.

Project: Python
Category: None
Status: Closed
Resolution: Wont Fix
Bug Group: Platform-specific
Priority: 5
Summary: getpass.getpass on Windows (PR#349)

Details: Jitterbug-Id: 349
Submitted-By: ctalley@caci.com
Date: Thu,  8 Jun 2000 15:52:19 -0400 (EDT)
Version: 1.5.2
OS: W95

When using this code, the third line is skipped.  That is, the prompt
appears, but the program doesn't pause to accept input data.  It goes
right to the fourth line and pauses there.  I end up with a null string
for "fromaddr".

username=raw_input('Enter FTP server account username: ')
password=getpass.getpass('Enter FTP server account password: ')
fromaddr=raw_input('Enter your e-mail address: ')
toaddr=raw_input('Enter e-mail address for notification: ')

When using this code, everything works (although echo of the
password isn't suppressed.)

username=raw_input('Enter FTP server account username: ')
password=raw_input('Enter FTP server account password: ')
fromaddr=raw_input('Enter your e-mail address: ')
toaddr=raw_input('Enter e-mail address for notification: ')

The difference between the two code segments is that the first uses
the getpass method on the second line and the second uses raw_input.
What I think is happening is that getpass is leaving some garbage
in a buffer somewhere which is seen by raw_input as data.  raw_input
happily accepts the data and goes to the next line.  I've devised
several workarounds including "flush_input_buffer=raw_input('')"
immediately following the call to getpass.  It's ugly, but it works.

Carl Talley

Audit trail:
Tue Jul 11 08:25:59 2000	guido	moved from incoming to open


Date: 2000-Sep-07 15:04
By: jhylton

Please do triage on this bug.

Date: 2000-Sep-13 03:23
By: gvanrossum

Confirmed with Python 2.0b1 on Win98SE.
The bug does not occur on Linux.
Tim, the getpass code for Windows is different than on Unix, so please have a look!

Date: 2000-Sep-14 02:05
By: tim_one

Mark, I assigned this to you on the chance you know a better trick.  Note that I already closed it as "Won't Fix", so if you *don't* know a better trick you don't have to do anything here.

This can't be fixed on Windows.  As the MS docs say, "The console I/O routines are not compatible with stream I/O or low-level I/O library routines", and getpass uses the console I/O routines on Windows.  Mixing console I/O with C stdio is undefined, and C stdio doesn't provide a way to do non-echo'ing input itself.

About the best you can do is to define your own raw_input variant that uses console I/O too.  For example,

def phony_raw_input(prompt=""):
    from msvcrt import putch, getche
    for ch in prompt:
    s = ""
    while 1:
        c = getche()
        if c in "\r\n":
        elif c == '\003':
            raise KeyboardInterrupt
        elif c == '\b':
            s = s[:-1]
            s = s + c
    return s

Use phony_raw_input instead of raw_input in your example and it will work as you hoped.  It may screw up the *next* read from stdin, though!  All of the keyboard buffer, MS-DOS buffer, and stdio buffers get mixed into this, and MS doesn't define or guarantee the way all those interact across Windows flavors.


For detailed info, follow this link: