MCPcopy Create free account
hub / github.com/pyinvoke/invoke / stdin_is_foregrounded_tty

Function stdin_is_foregrounded_tty

invoke/terminals.py:131–152  ·  view source on GitHub ↗

Detect if given stdin ``stream`` seems to be in the foreground of a TTY. Specifically, compares the current Python process group ID to that of the stream's file descriptor to see if they match; if they do not match, it is likely that the process has been placed in the background.

(stream: IO)

Source from the content-addressed store, hash-verified

129
130
131def stdin_is_foregrounded_tty(stream: IO) -> bool:
132 """
133 Detect if given stdin ``stream`` seems to be in the foreground of a TTY.
134
135 Specifically, compares the current Python process group ID to that of the
136 stream's file descriptor to see if they match; if they do not match, it is
137 likely that the process has been placed in the background.
138
139 This is used as a test to determine whether we should manipulate an active
140 stdin so it runs in a character-buffered mode; touching the terminal in
141 this way when the process is backgrounded, causes most shells to pause
142 execution.
143
144 .. note::
145 Processes that aren't attached to a terminal to begin with, will always
146 fail this test, as it starts with "do you have a real ``fileno``?".
147
148 .. versionadded:: 1.0
149 """
150 if not has_fileno(stream):
151 return False
152 return os.getpgrp() == os.tcgetpgrp(stream.fileno())
153
154
155def cbreak_already_set(stream: IO) -> bool:

Callers 1

character_bufferedFunction · 0.85

Calls 1

has_filenoFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…