]> granicus.if.org Git - python/commitdiff
print a warning if the password will be echoed.
authorJeremy Hylton <jeremy@alum.mit.edu>
Mon, 18 Oct 1999 22:25:22 +0000 (22:25 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Mon, 18 Oct 1999 22:25:22 +0000 (22:25 +0000)
At import time, getpass will be bound to the appropriate
platform-specific function.  If the platform's echo-disabler is not
available, default_getpass, which prints the warning, will be used

Lib/getpass.py

index 952e023d1efe1b3b6f30e84e56515c2bb7737439..19843d73c59274ebd03401f62452af9449daf575 100644 (file)
@@ -3,43 +3,27 @@
 getpass(prompt) - prompt for a password, with echo turned off
 getuser() - get the user name from the environment or password database
 
+On Windows, the msvcrt module will be used.
+On the Mac EasyDialogs.AskPassword is used, if available.
+
 Authors: Piers Lauder (original)
          Guido van Rossum (Windows support and cleanup)
 """
 
+import sys
 
-def getpass(prompt='Password: '):
+def unix_getpass(prompt='Password: '):
        """Prompt for a password, with echo turned off.
 
        Restore terminal settings at end.
-
-       On Windows, this calls win_getpass(prompt) which uses the
-       msvcrt module to get the same effect.
-       
-       On the Mac EasyDialogs.AskPassword is used, if available.
-
        """
 
-       import sys
        try:
                fd = sys.stdin.fileno()
        except:
                return default_getpass(prompt)
-       try:
-               import termios, TERMIOS
-       except ImportError:
-               try:
-                       import msvcrt
-               except ImportError:
-                       try:
-                               from EasyDialogs import AskPassword
-                       except ImportError:
-                               return default_getpass(prompt)
-                       else:
-                               return AskPassword(prompt)
-               else:
-                       return win_getpass(prompt)
 
+       getpass = default_getpass
        old = termios.tcgetattr(fd)     # a copy to save
        new = old[:]
 
@@ -76,6 +60,7 @@ def win_getpass(prompt='Password: '):
 
 
 def default_getpass(prompt='Password: '):
+       print "Warning: Problem with getpass. Passwords may be echoed."
        return _raw_input(prompt)
 
 
@@ -112,3 +97,22 @@ def getuser():
        # If this fails, the exception will "explain" why
        import pwd
        return pwd.getpwuid(os.getuid())[0]
+
+# Bind the name getpass to the appropriate function
+try:
+       import termios, TERMIOS
+except ImportError:
+       try:
+               import msvcrt
+       except ImportError:
+               try:
+                       from EasyDialogs import AskPassword
+               except ImportError:
+                       getpass = default_getpass
+               else:
+                       getpass = AskPassword
+       else:
+               getpass = win_getpass
+else:
+       getpass = unix_getpass
+