Back to index

python3.2  3.2.2
Classes | Functions | Variables
getpass Namespace Reference

Classes

class  GetPassWarning

Functions

def unix_getpass
def win_getpass
def fallback_getpass
def _raw_input
def getuser

Variables

list __all__ = ["getpass","getuser","GetPassWarning"]
 getpass = fallback_getpass

Detailed Description

Utilities to get a password and/or the current user name.

getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
getuser() - Get the user name from the environment or password database.

GetPassWarning - This UserWarning is issued when getpass() cannot prevent
         echoing of the password contents while reading.

On Windows, the msvcrt module will be used.
On the Mac EasyDialogs.AskPassword is used, if available.

Class Documentation

class getpass::GetPassWarning

Definition at line 23 of file getpass.py.


Function Documentation

def getpass._raw_input (   prompt = "",
  stream = None,
  input = None 
) [private]

Definition at line 121 of file getpass.py.

00121 
00122 def _raw_input(prompt="", stream=None, input=None):
00123     # This doesn't save the string in the GNU readline history.
00124     if not stream:
00125         stream = sys.stderr
00126     if not input:
00127         input = sys.stdin
00128     prompt = str(prompt)
00129     if prompt:
00130         stream.write(prompt)
00131         stream.flush()
00132     # NOTE: The Python C API calls flockfile() (and unlock) during readline.
00133     line = input.readline()
00134     if not line:
00135         raise EOFError
00136     if line[-1] == '\n':
00137         line = line[:-1]
00138     return line
00139 

Here is the caller graph for this function:

def getpass.fallback_getpass (   prompt = 'Password: ',
  stream = None 
)

Definition at line 112 of file getpass.py.

00112 
00113 def fallback_getpass(prompt='Password: ', stream=None):
00114     warnings.warn("Can not control echo on the terminal.", GetPassWarning,
00115                   stacklevel=2)
00116     if not stream:
00117         stream = sys.stderr
00118     print("Warning: Password input may be echoed.", file=stream)
00119     return _raw_input(prompt, stream)
00120 

Here is the call graph for this function:

Here is the caller graph for this function:

def getpass.getuser ( )
Get the username from the environment or password database.

First try various environment variables, then the password
database.  This works on Windows as long as USERNAME is set.

Definition at line 140 of file getpass.py.

00140 
00141 def getuser():
00142     """Get the username from the environment or password database.
00143 
00144     First try various environment variables, then the password
00145     database.  This works on Windows as long as USERNAME is set.
00146 
00147     """
00148 
00149     import os
00150 
00151     for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
00152         user = os.environ.get(name)
00153         if user:
00154             return user
00155 
00156     # If this fails, the exception will "explain" why
00157     import pwd
00158     return pwd.getpwuid(os.getuid())[0]
00159 
00160 # Bind the name getpass to the appropriate function
00161 try:
00162     import termios
00163     # it's possible there is an incompatible termios from the
00164     # McMillan Installer, make sure we have a UNIX-compatible termios
    termios.tcgetattr, termios.tcsetattr

Here is the call graph for this function:

def getpass.unix_getpass (   prompt = 'Password: ',
  stream = None 
)
Prompt for a password, with echo turned off.

Args:
  prompt: Written on stream to ask for the input.  Default: 'Password: '
  stream: A writable file object to display the prompt.  Defaults to
          the tty.  If no tty is available defaults to sys.stderr.
Returns:
  The seKr3t input.
Raises:
  EOFError: If our input tty or stdin was closed.
  GetPassWarning: When we were unable to turn echo off on the input.

Always restores terminal settings before returning.

Definition at line 26 of file getpass.py.

00026 
00027 def unix_getpass(prompt='Password: ', stream=None):
00028     """Prompt for a password, with echo turned off.
00029 
00030     Args:
00031       prompt: Written on stream to ask for the input.  Default: 'Password: '
00032       stream: A writable file object to display the prompt.  Defaults to
00033               the tty.  If no tty is available defaults to sys.stderr.
00034     Returns:
00035       The seKr3t input.
00036     Raises:
00037       EOFError: If our input tty or stdin was closed.
00038       GetPassWarning: When we were unable to turn echo off on the input.
00039 
00040     Always restores terminal settings before returning.
00041     """
00042     fd = None
00043     tty = None
00044     try:
00045         # Always try reading and writing directly on the tty first.
00046         fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
00047         tty = os.fdopen(fd, 'w+', 1)
00048         input = tty
00049         if not stream:
00050             stream = tty
00051     except EnvironmentError as e:
00052         # If that fails, see if stdin can be controlled.
00053         try:
00054             fd = sys.stdin.fileno()
00055         except (AttributeError, ValueError):
00056             passwd = fallback_getpass(prompt, stream)
00057         input = sys.stdin
00058         if not stream:
00059             stream = sys.stderr
00060 
00061     if fd is not None:
00062         passwd = None
00063         try:
00064             old = termios.tcgetattr(fd)     # a copy to save
00065             new = old[:]
00066             new[3] &= ~termios.ECHO  # 3 == 'lflags'
00067             tcsetattr_flags = termios.TCSAFLUSH
00068             if hasattr(termios, 'TCSASOFT'):
00069                 tcsetattr_flags |= termios.TCSASOFT
00070             try:
00071                 termios.tcsetattr(fd, tcsetattr_flags, new)
00072                 passwd = _raw_input(prompt, stream, input=input)
00073             finally:
00074                 termios.tcsetattr(fd, tcsetattr_flags, old)
00075                 stream.flush()  # issue7208
00076         except termios.error as e:
00077             if passwd is not None:
00078                 # _raw_input succeeded.  The final tcsetattr failed.  Reraise
00079                 # instead of leaving the terminal in an unknown state.
00080                 raise
00081             # We can't control the tty or stdin.  Give up and use normal IO.
00082             # fallback_getpass() raises an appropriate warning.
00083             del input, tty  # clean up unused file objects before blocking
00084             passwd = fallback_getpass(prompt, stream)
00085 
00086     stream.write('\n')
00087     return passwd
00088 

Here is the call graph for this function:

def getpass.win_getpass (   prompt = 'Password: ',
  stream = None 
)
Prompt for password with echo off, using Windows getch().

Definition at line 89 of file getpass.py.

00089 
00090 def win_getpass(prompt='Password: ', stream=None):
00091     """Prompt for password with echo off, using Windows getch()."""
00092     if sys.stdin is not sys.__stdin__:
00093         return fallback_getpass(prompt, stream)
00094     import msvcrt
00095     for c in prompt:
00096         msvcrt.putwch(c)
00097     pw = ""
00098     while 1:
00099         c = msvcrt.getwch()
00100         if c == '\r' or c == '\n':
00101             break
00102         if c == '\003':
00103             raise KeyboardInterrupt
00104         if c == '\b':
00105             pw = pw[:-1]
00106         else:
00107             pw = pw + c
00108     msvcrt.putwch('\r')
00109     msvcrt.putwch('\n')
00110     return pw
00111 

Here is the call graph for this function:


Variable Documentation

list getpass.__all__ = ["getpass","getuser","GetPassWarning"]

Definition at line 20 of file getpass.py.

Definition at line 169 of file getpass.py.