Cleanly determine whether ``stream`` is a TTY. Specifically, first try calling ``stream.isatty()``, and if that fails (e.g. due to lacking the method entirely) fallback to `os.isatty`. .. note:: Most of the time, we don't actually care about true TTY-ness, but mere
(stream: IO)
| 95 | |
| 96 | |
| 97 | def isatty(stream: IO) -> Union[bool, Any]: |
| 98 | """ |
| 99 | Cleanly determine whether ``stream`` is a TTY. |
| 100 | |
| 101 | Specifically, first try calling ``stream.isatty()``, and if that fails |
| 102 | (e.g. due to lacking the method entirely) fallback to `os.isatty`. |
| 103 | |
| 104 | .. note:: |
| 105 | Most of the time, we don't actually care about true TTY-ness, but |
| 106 | merely whether the stream seems to have a fileno (per `has_fileno`). |
| 107 | However, in some cases (notably the use of `pty.fork` to present a |
| 108 | local pseudoterminal) we need to tell if a given stream has a valid |
| 109 | fileno but *isn't* tied to an actual terminal. Thus, this function. |
| 110 | |
| 111 | :param stream: A file-like object. |
| 112 | |
| 113 | :returns: |
| 114 | A boolean depending on the result of calling ``.isatty()`` and/or |
| 115 | `os.isatty`. |
| 116 | |
| 117 | .. versionadded:: 1.0 |
| 118 | """ |
| 119 | # If there *is* an .isatty, ask it. |
| 120 | if hasattr(stream, "isatty") and callable(stream.isatty): |
| 121 | return stream.isatty() |
| 122 | # If there wasn't, see if it has a fileno, and if so, ask os.isatty |
| 123 | elif has_fileno(stream): |
| 124 | return os.isatty(stream.fileno()) |
| 125 | # If we got here, none of the above worked, so it's reasonable to assume |
| 126 | # the darn thing isn't a real TTY. |
| 127 | return False |
| 128 | |
| 129 | |
| 130 | def helpline(obj: object) -> Optional[str]: |
no test coverage detected
searching dependent graphs…