MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / unix_getpass

Function unix_getpass

tools/python-3.11.9-amd64/Lib/getpass.py:29–94  ·  view source on GitHub ↗

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:

(prompt='Password: ', stream=None)

Source from the content-addressed store, hash-verified

27
28
29def unix_getpass(prompt='Password: ', stream=None):
30 """Prompt for a password, with echo turned off.
31
32 Args:
33 prompt: Written on stream to ask for the input. Default: 'Password: '
34 stream: A writable file object to display the prompt. Defaults to
35 the tty. If no tty is available defaults to sys.stderr.
36 Returns:
37 The seKr3t input.
38 Raises:
39 EOFError: If our input tty or stdin was closed.
40 GetPassWarning: When we were unable to turn echo off on the input.
41
42 Always restores terminal settings before returning.
43 """
44 passwd = None
45 with contextlib.ExitStack() as stack:
46 try:
47 # Always try reading and writing directly on the tty first.
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 stream = input
55 except OSError:
56 # If that fails, see if stdin can be controlled.
57 stack.close()
58 try:
59 fd = sys.stdin.fileno()
60 except (AttributeError, ValueError):
61 fd = None
62 passwd = fallback_getpass(prompt, stream)
63 input = sys.stdin
64 if not stream:
65 stream = sys.stderr
66
67 if fd is not None:
68 try:
69 old = termios.tcgetattr(fd) # a copy to save
70 new = old[:]
71 new[3] &= ~termios.ECHO # 3 == 'lflags'
72 tcsetattr_flags = termios.TCSAFLUSH
73 if hasattr(termios, 'TCSASOFT'):
74 tcsetattr_flags |= termios.TCSASOFT
75 try:
76 termios.tcsetattr(fd, tcsetattr_flags, new)
77 passwd = _raw_input(prompt, stream, input=input)
78 finally:
79 termios.tcsetattr(fd, tcsetattr_flags, old)
80 stream.flush() # issue7208
81 except termios.error:
82 if passwd is not None:
83 # _raw_input succeeded. The final tcsetattr failed. Reraise
84 # instead of leaving the terminal in an unknown state.
85 raise
86 # We can't control the tty or stdin. Give up and use normal IO.

Callers

nothing calls this directly

Calls 8

fallback_getpassFunction · 0.85
_raw_inputFunction · 0.85
openMethod · 0.45
enter_contextMethod · 0.45
closeMethod · 0.45
filenoMethod · 0.45
flushMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected