MCPcopy Index your code
hub / github.com/python/cpython / unix_getpass

Function unix_getpass

Lib/getpass.py:68–153  ·  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. echo_char: A sing

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

Source from the content-addressed store, hash-verified

66
67
68def unix_getpass(prompt='Password: ', stream=None, *, echo_char=None):
69 """Prompt for a password, with echo turned off.
70
71 Args:
72 prompt: Written on stream to ask for the input. Default: 'Password: '
73 stream: A writable file object to display the prompt. Defaults to
74 the tty. If no tty is available defaults to sys.stderr.
75 echo_char: A single ASCII character to mask input (e.g., '*').
76 If None, input is hidden.
77 Returns:
78 The seKr3t input.
79 Raises:
80 EOFError: If our input tty or stdin was closed.
81 GetPassWarning: When we were unable to turn echo off on the input.
82
83 Always restores terminal settings before returning.
84 """
85 _check_echo_char(echo_char)
86
87 passwd = None
88 with contextlib.ExitStack() as stack:
89 try:
90 # Always try reading and writing directly on the tty first.
91 fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
92 tty = io.FileIO(fd, 'w+')
93 stack.enter_context(tty)
94 input = io.TextIOWrapper(tty)
95 stack.enter_context(input)
96 if not stream:
97 stream = input
98 except OSError:
99 # If that fails, see if stdin can be controlled.
100 stack.close()
101 try:
102 fd = sys.stdin.fileno()
103 except (AttributeError, ValueError):
104 fd = None
105 passwd = fallback_getpass(prompt, stream)
106 input = sys.stdin
107 if not stream:
108 stream = sys.stderr
109
110 if fd is not None:
111 try:
112 old = termios.tcgetattr(fd) # a copy to save
113 new = old[:]
114 new[3] &= ~termios.ECHO # 3 == 'lflags'
115 # Extract control characters before changing terminal mode.
116 term_ctrl_chars = None
117 if echo_char:
118 # ICANON enables canonical (line-buffered) mode where
119 # the terminal handles line editing. Disable it so we
120 # can read input char by char and handle editing ourselves.
121 new[3] &= ~termios.ICANON
122 # IEXTEN enables implementation-defined input processing
123 # such as LNEXT (Ctrl+V). Disable it so the terminal
124 # driver does not intercept these characters before our
125 # code can handle them.

Callers

nothing calls this directly

Calls 10

_check_echo_charFunction · 0.85
fallback_getpassFunction · 0.85
_get_terminal_ctrl_charsFunction · 0.85
_raw_inputFunction · 0.85
enter_contextMethod · 0.80
openMethod · 0.45
closeMethod · 0.45
filenoMethod · 0.45
flushMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…