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)
| 129 | |
| 130 | |
| 131 | def 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 | |
| 155 | def cbreak_already_set(stream: IO) -> bool: |
no test coverage detected
searching dependent graphs…