Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.46 KB | None | 0 0
  1. # uncompyle6 version 2.9.7
  2. # Python bytecode 3.5 (3350)
  3. # Decompiled from: Python 2.7.12 (default, Nov  7 2016, 11:55:55)
  4. # [GCC 6.2.1 20160830]
  5. # Embedded file name: getpass.py
  6. """Utilities to get a password and/or the current user name.
  7.  
  8. getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
  9. getuser() - Get the user name from the environment or password database.
  10.  
  11. GetPassWarning - This UserWarning is issued when getpass() cannot prevent
  12.                 echoing of the password contents while reading.
  13.  
  14. On Windows, the msvcrt module will be used.
  15. On the Mac EasyDialogs.AskPassword is used, if available.
  16.  
  17. """
  18. import contextlib
  19. import io
  20. import os
  21. import sys
  22. import warnings
  23. __all__ = [
  24.  'getpass', 'getuser', 'GetPassWarning']
  25.  
  26. class GetPassWarning(UserWarning):
  27.     pass
  28.  
  29.  
  30. def unix_getpass(prompt='Password: ', stream=None):
  31.     """Prompt for a password, with echo turned off.
  32.    
  33.    Args:
  34.      prompt: Written on stream to ask for the input.  Default: 'Password: '
  35.      stream: A writable file object to display the prompt.  Defaults to
  36.              the tty.  If no tty is available defaults to sys.stderr.
  37.    Returns:
  38.      The seKr3t input.
  39.    Raises:
  40.      EOFError: If our input tty or stdin was closed.
  41.      GetPassWarning: When we were unable to turn echo off on the input.
  42.    
  43.    Always restores terminal settings before returning.
  44.    """
  45.     passwd = None
  46.     with contextlib.ExitStack() as stack:
  47.         try:
  48.             fd = os.open('/dev/tty', os.O_RDWR | os.O_NOCTTY)
  49.             tty = io.FileIO(fd, 'w+')
  50.             stack.enter_context(tty)
  51.             input = io.TextIOWrapper(tty)
  52.             stack.enter_context(input)
  53.             if not stream:
  54.                 pass
  55.             stream = input
  56.         except OSError as e:
  57.             stack.close()
  58.             try:
  59.                 fd = sys.stdin.fileno()
  60.             except (AttributeError, ValueError):
  61.                 fd = None
  62.                 passwd = fallback_getpass(prompt, stream)
  63.  
  64.             input = sys.stdin
  65.             if not stream:
  66.                 pass
  67.             stream = sys.stderr
  68.  
  69.         if fd is not None:
  70.             try:
  71.                 old = termios.tcgetattr(fd)
  72.                 new = old[:]
  73.                 new[3] &= ~termios.ECHO
  74.                 tcsetattr_flags = termios.TCSAFLUSH
  75.                 if hasattr(termios, 'TCSASOFT'):
  76.                     tcsetattr_flags |= termios.TCSASOFT
  77.                     try:
  78.                         termios.tcsetattr(fd, tcsetattr_flags, new)
  79.                         passwd = _raw_input(prompt, stream, input=input)
  80.                     finally:
  81.                         termios.tcsetattr(fd, tcsetattr_flags, old)
  82.                         stream.flush()
  83.  
  84.             except termios.error:
  85.                 if passwd is not None:
  86.                     raise
  87.                     if stream is not input:
  88.                         stack.close()
  89.                     passwd = fallback_getpass(prompt, stream)
  90.  
  91.             stream.write('\n')
  92.             return passwd
  93.  
  94.  
  95. def win_getpass(prompt='Password: ', stream=None):
  96.     """Prompt for password with echo off, using Windows getch()."""
  97.     if sys.stdin is not sys.__stdin__:
  98.         return fallback_getpass(prompt, stream)
  99.     for c in prompt:
  100.         msvcrt.putwch(c)
  101.  
  102.     pw = ''
  103.     while True:
  104.         c = msvcrt.getwch()
  105.         if c == '\r' or c == '\n':
  106.             break
  107.             if c == '\x03':
  108.                 raise KeyboardInterrupt
  109.                 if c == '\x08':
  110.                     pass
  111.             pw = pw[:-1]
  112.         else:
  113.             pw = pw + c
  114.  
  115.     msvcrt.putwch('\r')
  116.     msvcrt.putwch('\n')
  117.     return pw
  118.  
  119.  
  120. def fallback_getpass(prompt='Password: ', stream=None):
  121.     warnings.warn('Can not control echo on the terminal.', GetPassWarning, stacklevel=2)
  122.     if not stream:
  123.         stream = sys.stderr
  124.         print('Warning: Password input may be echoed.', file=stream)
  125.     return _raw_input(prompt, stream)
  126.  
  127.  
  128. def _raw_input(prompt='', stream=None, input=None):
  129.     if not stream:
  130.         stream = sys.stderr
  131.         if not input:
  132.             input = sys.stdin
  133.             prompt = str(prompt)
  134.             if prompt:
  135.                 try:
  136.                     stream.write(prompt)
  137.                 except UnicodeEncodeError:
  138.                     prompt = prompt.encode(stream.encoding, 'replace')
  139.                     prompt = prompt.decode(stream.encoding)
  140.                     stream.write(prompt)
  141.  
  142.                 stream.flush()
  143.                 line = input.readline()
  144.                 if not line:
  145.                     raise EOFError
  146.                     if line[-1] == '\n':
  147.                         pass
  148.         line = line[:-1]
  149.     return line
  150.  
  151.  
  152. def getuser():
  153.     """Get the username from the environment or password database.
  154.    
  155.    First try various environment variables, then the password
  156.    database.  This works on Windows as long as USERNAME is set.
  157.    
  158.    """
  159.     for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
  160.         user = os.environ.get(name)
  161.         if user:
  162.             return user
  163.  
  164.     import pwd
  165.     return pwd.getpwuid(os.getuid())[0]
  166.  
  167.  
  168. try:
  169.     import termios
  170.     (
  171.      termios.tcgetattr, termios.tcsetattr)
  172. except (ImportError, AttributeError):
  173.     try:
  174.         import msvcrt
  175.     except ImportError:
  176.         getpass = fallback_getpass
  177.     else:
  178.         getpass = win_getpass
  179.  
  180. else:
  181.     getpass = unix_getpass
  182. # okay decompiling getpass.pyc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement