annotate Lib/getpass.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents a3b4887edba4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Utilities to get a password and/or the current user name.
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
3 getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
4 getuser() - Get the user name from the environment or password database.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
5
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
6 GetPassWarning - This UserWarning is issued when getpass() cannot prevent
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
7 echoing of the password contents while reading.
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
11389
895102eea485 print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 10142
diff changeset
9 On Windows, the msvcrt module will be used.
895102eea485 print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 10142
diff changeset
10 On the Mac EasyDialogs.AskPassword is used, if available.
895102eea485 print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 10142
diff changeset
11
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 """
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
11870
d7eca5b0960a Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents: 11389
diff changeset
14 # Authors: Piers Lauder (original)
d7eca5b0960a Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents: 11389
diff changeset
15 # Guido van Rossum (Windows support and cleanup)
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
16 # Gregory P. Smith (tty support & GetPassWarning)
11870
d7eca5b0960a Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents: 11389
diff changeset
17
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
18 import os, sys, warnings
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
19
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
20 __all__ = ["getpass","getuser","GetPassWarning"]
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
22
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
23 class GetPassWarning(UserWarning): pass
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
24
16072
bae889f44cd1 more __all__ updates
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
25
37343
fe3b93c22481 Bug #1445068: getpass.getpass() can now be given an explicit stream
Georg Brandl <georg@python.org>
parents: 26308
diff changeset
26 def unix_getpass(prompt='Password: ', stream=None):
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
27 """Prompt for a password, with echo turned off.
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
29 Args:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
30 prompt: Written on stream to ask for the input. Default: 'Password: '
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
31 stream: A writable file object to display the prompt. Defaults to
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
32 the tty. If no tty is available defaults to sys.stderr.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
33 Returns:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
34 The seKr3t input.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
35 Raises:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
36 EOFError: If our input tty or stdin was closed.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
37 GetPassWarning: When we were unable to turn echo off on the input.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
38
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
39 Always restores terminal settings before returning.
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
40 """
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
41 fd = None
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
42 tty = None
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
43 try:
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
44 # Always try reading and writing directly on the tty first.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
45 fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
46 tty = os.fdopen(fd, 'w+', 1)
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
47 input = tty
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
48 if not stream:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
49 stream = tty
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
50 except EnvironmentError, e:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
51 # If that fails, see if stdin can be controlled.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
52 try:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
53 fd = sys.stdin.fileno()
56026
77b96a5424a2 kill bare except
Benjamin Peterson <benjamin@python.org>
parents: 46531
diff changeset
54 except (AttributeError, ValueError):
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
55 passwd = fallback_getpass(prompt, stream)
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
56 input = sys.stdin
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
57 if not stream:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
58 stream = sys.stderr
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
60 if fd is not None:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
61 passwd = None
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
62 try:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
63 old = termios.tcgetattr(fd) # a copy to save
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
64 new = old[:]
69568
a3b4887edba4 issue11236 getpass.getpass to respond ctrl-c or ctrl-z
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
65 new[3] &= ~termios.ECHO # 3 == 'lflags'
57030
5925900edf16 Merged revisions 76000 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 46531
diff changeset
66 tcsetattr_flags = termios.TCSAFLUSH
5925900edf16 Merged revisions 76000 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 46531
diff changeset
67 if hasattr(termios, 'TCSASOFT'):
5925900edf16 Merged revisions 76000 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 46531
diff changeset
68 tcsetattr_flags |= termios.TCSASOFT
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
69 try:
57030
5925900edf16 Merged revisions 76000 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 46531
diff changeset
70 termios.tcsetattr(fd, tcsetattr_flags, new)
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
71 passwd = _raw_input(prompt, stream, input=input)
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
72 finally:
57030
5925900edf16 Merged revisions 76000 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 46531
diff changeset
73 termios.tcsetattr(fd, tcsetattr_flags, old)
5925900edf16 Merged revisions 76000 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 46531
diff changeset
74 stream.flush() # issue7208
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
75 except termios.error, e:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
76 if passwd is not None:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
77 # _raw_input succeeded. The final tcsetattr failed. Reraise
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
78 # instead of leaving the terminal in an unknown state.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
79 raise
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
80 # We can't control the tty or stdin. Give up and use normal IO.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
81 # fallback_getpass() raises an appropriate warning.
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
82 del input, tty # clean up unused file objects before blocking
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
83 passwd = fallback_getpass(prompt, stream)
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
37343
fe3b93c22481 Bug #1445068: getpass.getpass() can now be given an explicit stream
Georg Brandl <georg@python.org>
parents: 26308
diff changeset
85 stream.write('\n')
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
86 return passwd
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
37343
fe3b93c22481 Bug #1445068: getpass.getpass() can now be given an explicit stream
Georg Brandl <georg@python.org>
parents: 26308
diff changeset
89 def win_getpass(prompt='Password: ', stream=None):
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
90 """Prompt for password with echo off, using Windows getch()."""
19606
6d5ac3c43416 win_getpass(): if sys.stdin is not sys.__stdin__, use
Guido van Rossum <guido@python.org>
parents: 19226
diff changeset
91 if sys.stdin is not sys.__stdin__:
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
92 return fallback_getpass(prompt, stream)
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
93 import msvcrt
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
94 for c in prompt:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
95 msvcrt.putch(c)
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
96 pw = ""
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
97 while 1:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
98 c = msvcrt.getch()
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
99 if c == '\r' or c == '\n':
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
100 break
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
101 if c == '\003':
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
102 raise KeyboardInterrupt
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
103 if c == '\b':
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
104 pw = pw[:-1]
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
105 else:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
106 pw = pw + c
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
107 msvcrt.putch('\r')
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
108 msvcrt.putch('\n')
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
109 return pw
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
112 def fallback_getpass(prompt='Password: ', stream=None):
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
113 warnings.warn("Can not control echo on the terminal.", GetPassWarning,
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
114 stacklevel=2)
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
115 if not stream:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
116 stream = sys.stderr
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
117 print >>stream, "Warning: Password input may be echoed."
37343
fe3b93c22481 Bug #1445068: getpass.getpass() can now be given an explicit stream
Georg Brandl <georg@python.org>
parents: 26308
diff changeset
118 return _raw_input(prompt, stream)
8775
d39678d22b4a Don't use raw_input() to ask for the password; this puts the password
Guido van Rossum <guido@python.org>
parents: 8495
diff changeset
119
d39678d22b4a Don't use raw_input() to ask for the password; this puts the password
Guido van Rossum <guido@python.org>
parents: 8495
diff changeset
120
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
121 def _raw_input(prompt="", stream=None, input=None):
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
122 # A raw_input() replacement that doesn't save the string in the
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
123 # GNU readline history.
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
124 if not stream:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
125 stream = sys.stderr
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
126 if not input:
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
127 input = sys.stdin
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
128 prompt = str(prompt)
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
129 if prompt:
37343
fe3b93c22481 Bug #1445068: getpass.getpass() can now be given an explicit stream
Georg Brandl <georg@python.org>
parents: 26308
diff changeset
130 stream.write(prompt)
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
131 stream.flush()
57030
5925900edf16 Merged revisions 76000 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 46531
diff changeset
132 # NOTE: The Python C API calls flockfile() (and unlock) during readline.
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
133 line = input.readline()
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
134 if not line:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
135 raise EOFError
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
136 if line[-1] == '\n':
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
137 line = line[:-1]
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
138 return line
8042
4919dee18781 Be nicer to systems that have neither termios nor msvcrt.
Guido van Rossum <guido@python.org>
parents: 7941
diff changeset
139
4919dee18781 Be nicer to systems that have neither termios nor msvcrt.
Guido van Rossum <guido@python.org>
parents: 7941
diff changeset
140
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 def getuser():
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
142 """Get the username from the environment or password database.
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
144 First try various environment variables, then the password
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
145 database. This works on Windows as long as USERNAME is set.
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
147 """
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
149 import os
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
151 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
152 user = os.environ.get(name)
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
153 if user:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
154 return user
7941
91ecda9d5245 Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
156 # If this fails, the exception will "explain" why
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
157 import pwd
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
158 return pwd.getpwuid(os.getuid())[0]
11389
895102eea485 print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 10142
diff changeset
159
895102eea485 print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 10142
diff changeset
160 # Bind the name getpass to the appropriate function
895102eea485 print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 10142
diff changeset
161 try:
16807
74fd1959cf44 Get the needed constants from termios, not TERMIOS.
Fred Drake <fdrake@acm.org>
parents: 16072
diff changeset
162 import termios
26308
403dcbc9043f Fix SF #640094, on win32 getpass runs into unix_getpass
Neal Norwitz <nnorwitz@gmail.com>
parents: 19606
diff changeset
163 # it's possible there is an incompatible termios from the
403dcbc9043f Fix SF #640094, on win32 getpass runs into unix_getpass
Neal Norwitz <nnorwitz@gmail.com>
parents: 19606
diff changeset
164 # McMillan Installer, make sure we have a UNIX-compatible termios
403dcbc9043f Fix SF #640094, on win32 getpass runs into unix_getpass
Neal Norwitz <nnorwitz@gmail.com>
parents: 19606
diff changeset
165 termios.tcgetattr, termios.tcsetattr
403dcbc9043f Fix SF #640094, on win32 getpass runs into unix_getpass
Neal Norwitz <nnorwitz@gmail.com>
parents: 19606
diff changeset
166 except (ImportError, AttributeError):
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
167 try:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
168 import msvcrt
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
169 except ImportError:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
170 try:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
171 from EasyDialogs import AskPassword
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
172 except ImportError:
46531
88a8def58007 Major improvements:
Gregory P. Smith <greg@mad-scientist.com>
parents: 46523
diff changeset
173 getpass = fallback_getpass
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
174 else:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
175 getpass = AskPassword
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
176 else:
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
177 getpass = win_getpass
11389
895102eea485 print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 10142
diff changeset
178 else:
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11870
diff changeset
179 getpass = unix_getpass