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