Test ``input_`` to determine whether a read action will succeed. :param input_: Input stream object (file-like). :returns: ``True`` if a read should succeed, ``False`` otherwise. .. versionadded:: 1.0
(input_: IO)
| 202 | |
| 203 | |
| 204 | def ready_for_reading(input_: IO) -> bool: |
| 205 | """ |
| 206 | Test ``input_`` to determine whether a read action will succeed. |
| 207 | |
| 208 | :param input_: Input stream object (file-like). |
| 209 | |
| 210 | :returns: ``True`` if a read should succeed, ``False`` otherwise. |
| 211 | |
| 212 | .. versionadded:: 1.0 |
| 213 | """ |
| 214 | # A "real" terminal stdin needs select/kbhit to tell us when it's ready for |
| 215 | # a nonblocking read(). |
| 216 | # Otherwise, assume a "safer" file-like object that can be read from in a |
| 217 | # nonblocking fashion (e.g. a StringIO or regular file). |
| 218 | if not has_fileno(input_): |
| 219 | return True |
| 220 | if sys.platform == "win32": |
| 221 | return msvcrt.kbhit() |
| 222 | else: |
| 223 | reads, _, _ = select.select([input_], [], [], 0.0) |
| 224 | return bool(reads and reads[0] is input_) |
| 225 | |
| 226 | |
| 227 | def bytes_to_read(input_: IO) -> int: |
no test coverage detected
searching dependent graphs…