Mercurial > cpython-lru_cache-weakref
annotate Lib/getpass.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 6d5ac3c43416 |
| children | 403dcbc9043f |
| 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 |
|
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 getpass(prompt) - prompt for a password, with echo turned off |
|
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 getuser() - get the user name from the environment or password database |
|
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
11389
895102eea485
print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
10142
diff
changeset
|
6 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
|
7 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
|
8 |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 """ |
|
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
11870
d7eca5b0960a
Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents:
11389
diff
changeset
|
11 # 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
|
12 # Guido van Rossum (Windows support and cleanup) |
|
d7eca5b0960a
Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents:
11389
diff
changeset
|
13 |
|
11389
895102eea485
print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
10142
diff
changeset
|
14 import sys |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
| 16072 | 16 __all__ = ["getpass","getuser"] |
| 17 | |
|
11389
895102eea485
print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
10142
diff
changeset
|
18 def unix_getpass(prompt='Password: '): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
19 """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
|
20 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
21 Restore terminal settings at end. |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
22 """ |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
24 try: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
25 fd = sys.stdin.fileno() |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
26 except: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
27 return default_getpass(prompt) |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
29 old = termios.tcgetattr(fd) # a copy to save |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
30 new = old[:] |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 |
|
16807
74fd1959cf44
Get the needed constants from termios, not TERMIOS.
Fred Drake <fdrake@acm.org>
parents:
16072
diff
changeset
|
32 new[3] = new[3] & ~termios.ECHO # 3 == 'lflags' |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
33 try: |
|
16807
74fd1959cf44
Get the needed constants from termios, not TERMIOS.
Fred Drake <fdrake@acm.org>
parents:
16072
diff
changeset
|
34 termios.tcsetattr(fd, termios.TCSADRAIN, new) |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
35 passwd = _raw_input(prompt) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
36 finally: |
|
16807
74fd1959cf44
Get the needed constants from termios, not TERMIOS.
Fred Drake <fdrake@acm.org>
parents:
16072
diff
changeset
|
37 termios.tcsetattr(fd, termios.TCSADRAIN, old) |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
39 sys.stdout.write('\n') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
40 return passwd |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 |
|
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 def win_getpass(prompt='Password: '): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
44 """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
|
45 if sys.stdin is not sys.__stdin__: |
|
6d5ac3c43416
win_getpass(): if sys.stdin is not sys.__stdin__, use
Guido van Rossum <guido@python.org>
parents:
19226
diff
changeset
|
46 return default_getpass(prompt) |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
47 import msvcrt |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
48 for c in prompt: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
49 msvcrt.putch(c) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
50 pw = "" |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
51 while 1: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
52 c = msvcrt.getch() |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
53 if c == '\r' or c == '\n': |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
54 break |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
55 if c == '\003': |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
56 raise KeyboardInterrupt |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
57 if c == '\b': |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
58 pw = pw[:-1] |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
59 else: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
60 pw = pw + c |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
61 msvcrt.putch('\r') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
62 msvcrt.putch('\n') |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
63 return pw |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 |
|
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 |
|
8042
4919dee18781
Be nicer to systems that have neither termios nor msvcrt.
Guido van Rossum <guido@python.org>
parents:
7941
diff
changeset
|
66 def default_getpass(prompt='Password: '): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
67 print "Warning: Problem with getpass. Passwords may be echoed." |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
68 return _raw_input(prompt) |
|
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
|
69 |
|
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
|
70 |
|
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
|
71 def _raw_input(prompt=""): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
72 # 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
|
73 # GNU readline history. |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
74 prompt = str(prompt) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
75 if prompt: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
76 sys.stdout.write(prompt) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
77 line = sys.stdin.readline() |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
78 if not line: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
79 raise EOFError |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
80 if line[-1] == '\n': |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
81 line = line[:-1] |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
82 return line |
|
8042
4919dee18781
Be nicer to systems that have neither termios nor msvcrt.
Guido van Rossum <guido@python.org>
parents:
7941
diff
changeset
|
83 |
|
4919dee18781
Be nicer to systems that have neither termios nor msvcrt.
Guido van Rossum <guido@python.org>
parents:
7941
diff
changeset
|
84 |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 def getuser(): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
86 """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
|
87 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
88 First try various environment variables, then the password |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
89 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
|
90 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
91 """ |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
93 import os |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
95 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
96 user = os.environ.get(name) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
97 if user: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
98 return user |
|
7941
91ecda9d5245
Another new utility: getpass() prompts for a password, with echo off.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
100 # If this fails, the exception will "explain" why |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
101 import pwd |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
102 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
|
103 |
|
895102eea485
print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
10142
diff
changeset
|
104 # 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
|
105 try: |
|
16807
74fd1959cf44
Get the needed constants from termios, not TERMIOS.
Fred Drake <fdrake@acm.org>
parents:
16072
diff
changeset
|
106 import termios |
|
11389
895102eea485
print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
10142
diff
changeset
|
107 except ImportError: |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
108 try: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
109 import msvcrt |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
110 except ImportError: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
111 try: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
112 from EasyDialogs import AskPassword |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
113 except ImportError: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
114 getpass = default_getpass |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
115 else: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
116 getpass = AskPassword |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
117 else: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
118 getpass = win_getpass |
|
11389
895102eea485
print a warning if the password will be echoed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
10142
diff
changeset
|
119 else: |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11870
diff
changeset
|
120 getpass = unix_getpass |
