Catch situations where currentframe() returns None. See SF patch #1447410, this is...
[python.git] / Lib / getpass.py
blobe96491f90bfe2808d14e9f12dc574d7af941ba03
1 """Utilities to get a password and/or the current user name.
3 getpass(prompt) - prompt for a password, with echo turned off
4 getuser() - get the user name from the environment or password database
6 On Windows, the msvcrt module will be used.
7 On the Mac EasyDialogs.AskPassword is used, if available.
9 """
11 # Authors: Piers Lauder (original)
12 # Guido van Rossum (Windows support and cleanup)
14 import sys
16 __all__ = ["getpass","getuser"]
18 def unix_getpass(prompt='Password: '):
19 """Prompt for a password, with echo turned off.
21 Restore terminal settings at end.
22 """
24 try:
25 fd = sys.stdin.fileno()
26 except:
27 return default_getpass(prompt)
29 old = termios.tcgetattr(fd) # a copy to save
30 new = old[:]
32 new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
33 try:
34 termios.tcsetattr(fd, termios.TCSADRAIN, new)
35 passwd = _raw_input(prompt)
36 finally:
37 termios.tcsetattr(fd, termios.TCSADRAIN, old)
39 sys.stdout.write('\n')
40 return passwd
43 def win_getpass(prompt='Password: '):
44 """Prompt for password with echo off, using Windows getch()."""
45 if sys.stdin is not sys.__stdin__:
46 return default_getpass(prompt)
47 import msvcrt
48 for c in prompt:
49 msvcrt.putch(c)
50 pw = ""
51 while 1:
52 c = msvcrt.getch()
53 if c == '\r' or c == '\n':
54 break
55 if c == '\003':
56 raise KeyboardInterrupt
57 if c == '\b':
58 pw = pw[:-1]
59 else:
60 pw = pw + c
61 msvcrt.putch('\r')
62 msvcrt.putch('\n')
63 return pw
66 def default_getpass(prompt='Password: '):
67 print "Warning: Problem with getpass. Passwords may be echoed."
68 return _raw_input(prompt)
71 def _raw_input(prompt=""):
72 # A raw_input() replacement that doesn't save the string in the
73 # GNU readline history.
74 prompt = str(prompt)
75 if prompt:
76 sys.stdout.write(prompt)
77 line = sys.stdin.readline()
78 if not line:
79 raise EOFError
80 if line[-1] == '\n':
81 line = line[:-1]
82 return line
85 def getuser():
86 """Get the username from the environment or password database.
88 First try various environment variables, then the password
89 database. This works on Windows as long as USERNAME is set.
91 """
93 import os
95 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
96 user = os.environ.get(name)
97 if user:
98 return user
100 # If this fails, the exception will "explain" why
101 import pwd
102 return pwd.getpwuid(os.getuid())[0]
104 # Bind the name getpass to the appropriate function
105 try:
106 import termios
107 # it's possible there is an incompatible termios from the
108 # McMillan Installer, make sure we have a UNIX-compatible termios
109 termios.tcgetattr, termios.tcsetattr
110 except (ImportError, AttributeError):
111 try:
112 import msvcrt
113 except ImportError:
114 try:
115 from EasyDialogs import AskPassword
116 except ImportError:
117 getpass = default_getpass
118 else:
119 getpass = AskPassword
120 else:
121 getpass = win_getpass
122 else:
123 getpass = unix_getpass