Query stream ``input_`` to see how many bytes may be readable. .. note:: If we are unable to tell (e.g. if ``input_`` isn't a true file descriptor or isn't a valid TTY) we fall back to suggesting reading 1 byte only. :param input: Input stream object (file-like
(input_: IO)
| 225 | |
| 226 | |
| 227 | def bytes_to_read(input_: IO) -> int: |
| 228 | """ |
| 229 | Query stream ``input_`` to see how many bytes may be readable. |
| 230 | |
| 231 | .. note:: |
| 232 | If we are unable to tell (e.g. if ``input_`` isn't a true file |
| 233 | descriptor or isn't a valid TTY) we fall back to suggesting reading 1 |
| 234 | byte only. |
| 235 | |
| 236 | :param input: Input stream object (file-like). |
| 237 | |
| 238 | :returns: `int` number of bytes to read. |
| 239 | |
| 240 | .. versionadded:: 1.0 |
| 241 | """ |
| 242 | # NOTE: we have to check both possibilities here; situations exist where |
| 243 | # it's not a tty but has a fileno, or vice versa; neither is typically |
| 244 | # going to work re: ioctl(). |
| 245 | if not WINDOWS and isatty(input_) and has_fileno(input_): |
| 246 | fionread = fcntl.ioctl(input_, termios.FIONREAD, b" ") |
| 247 | return int(struct.unpack("h", fionread)[0]) |
| 248 | return 1 |
no test coverage detected
searching dependent graphs…