version becomes 3.1.2
[python/dscho.git] / Lib / getpass.py
blob2eb01fac3daf03c44cde5767b7af832609ea4970
1 """Utilities to get a password and/or the current user name.
3 getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
4 getuser() - Get the user name from the environment or password database.
6 GetPassWarning - This UserWarning is issued when getpass() cannot prevent
7 echoing of the password contents while reading.
9 On Windows, the msvcrt module will be used.
10 On the Mac EasyDialogs.AskPassword is used, if available.
12 """
14 # Authors: Piers Lauder (original)
15 # Guido van Rossum (Windows support and cleanup)
16 # Gregory P. Smith (tty support & GetPassWarning)
18 import os, sys, warnings
20 __all__ = ["getpass","getuser","GetPassWarning"]
23 class GetPassWarning(UserWarning): pass
26 def unix_getpass(prompt='Password: ', stream=None):
27 """Prompt for a password, with echo turned off.
29 Args:
30 prompt: Written on stream to ask for the input. Default: 'Password: '
31 stream: A writable file object to display the prompt. Defaults to
32 the tty. If no tty is available defaults to sys.stderr.
33 Returns:
34 The seKr3t input.
35 Raises:
36 EOFError: If our input tty or stdin was closed.
37 GetPassWarning: When we were unable to turn echo off on the input.
39 Always restores terminal settings before returning.
40 """
41 fd = None
42 tty = None
43 try:
44 # Always try reading and writing directly on the tty first.
45 fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
46 tty = os.fdopen(fd, 'w+', 1)
47 input = tty
48 if not stream:
49 stream = tty
50 except EnvironmentError as e:
51 # If that fails, see if stdin can be controlled.
52 try:
53 fd = sys.stdin.fileno()
54 except (AttributeError, ValueError):
55 passwd = fallback_getpass(prompt, stream)
56 input = sys.stdin
57 if not stream:
58 stream = sys.stderr
60 if fd is not None:
61 passwd = None
62 try:
63 old = termios.tcgetattr(fd) # a copy to save
64 new = old[:]
65 new[3] &= ~(termios.ECHO|termios.ISIG) # 3 == 'lflags'
66 tcsetattr_flags = termios.TCSAFLUSH
67 if hasattr(termios, 'TCSASOFT'):
68 tcsetattr_flags |= termios.TCSASOFT
69 try:
70 termios.tcsetattr(fd, tcsetattr_flags, new)
71 passwd = _raw_input(prompt, stream, input=input)
72 finally:
73 termios.tcsetattr(fd, tcsetattr_flags, old)
74 stream.flush() # issue7208
75 except termios.error as e:
76 if passwd is not None:
77 # _raw_input succeeded. The final tcsetattr failed. Reraise
78 # instead of leaving the terminal in an unknown state.
79 raise
80 # We can't control the tty or stdin. Give up and use normal IO.
81 # fallback_getpass() raises an appropriate warning.
82 del input, tty # clean up unused file objects before blocking
83 passwd = fallback_getpass(prompt, stream)
85 stream.write('\n')
86 return passwd
89 def win_getpass(prompt='Password: ', stream=None):
90 """Prompt for password with echo off, using Windows getch()."""
91 if sys.stdin is not sys.__stdin__:
92 return fallback_getpass(prompt, stream)
93 import msvcrt
94 for c in prompt:
95 msvcrt.putwch(c)
96 pw = ""
97 while 1:
98 c = msvcrt.getwch()
99 if c == '\r' or c == '\n':
100 break
101 if c == '\003':
102 raise KeyboardInterrupt
103 if c == '\b':
104 pw = pw[:-1]
105 else:
106 pw = pw + c
107 msvcrt.putwch('\r')
108 msvcrt.putwch('\n')
109 return pw
112 def fallback_getpass(prompt='Password: ', stream=None):
113 warnings.warn("Can not control echo on the terminal.", GetPassWarning,
114 stacklevel=2)
115 if not stream:
116 stream = sys.stderr
117 print("Warning: Password input may be echoed.", file=stream)
118 return _raw_input(prompt, stream)
121 def _raw_input(prompt="", stream=None, input=None):
122 # This doesn't save the string in the GNU readline history.
123 if not stream:
124 stream = sys.stderr
125 if not input:
126 input = sys.stdin
127 prompt = str(prompt)
128 if prompt:
129 stream.write(prompt)
130 stream.flush()
131 # NOTE: The Python C API calls flockfile() (and unlock) during readline.
132 line = input.readline()
133 if not line:
134 raise EOFError
135 if line[-1] == '\n':
136 line = line[:-1]
137 return line
140 def getuser():
141 """Get the username from the environment or password database.
143 First try various environment variables, then the password
144 database. This works on Windows as long as USERNAME is set.
148 import os
150 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
151 user = os.environ.get(name)
152 if user:
153 return user
155 # If this fails, the exception will "explain" why
156 import pwd
157 return pwd.getpwuid(os.getuid())[0]
159 # Bind the name getpass to the appropriate function
160 try:
161 import termios
162 # it's possible there is an incompatible termios from the
163 # McMillan Installer, make sure we have a UNIX-compatible termios
164 termios.tcgetattr, termios.tcsetattr
165 except (ImportError, AttributeError):
166 try:
167 import msvcrt
168 except ImportError:
169 try:
170 from EasyDialogs import AskPassword
171 except ImportError:
172 getpass = fallback_getpass
173 else:
174 getpass = AskPassword
175 else:
176 getpass = win_getpass
177 else:
178 getpass = unix_getpass