Read all data from the file, returned as bytes. Reads until either there is an error or read() returns size 0 (indicates EOF). If the file is already at EOF, returns an empty bytes object. In non-blocking mode, returns as much data as could be read before EA
(self)
| 1675 | return None |
| 1676 | |
| 1677 | def readall(self): |
| 1678 | """Read all data from the file, returned as bytes. |
| 1679 | |
| 1680 | Reads until either there is an error or read() returns size 0 |
| 1681 | (indicates EOF). If the file is already at EOF, returns an |
| 1682 | empty bytes object. |
| 1683 | |
| 1684 | In non-blocking mode, returns as much data as could be read |
| 1685 | before EAGAIN. If no data is available (EAGAIN is returned |
| 1686 | before bytes are read) returns None. |
| 1687 | """ |
| 1688 | self._checkClosed() |
| 1689 | self._checkReadable() |
| 1690 | if self._stat_atopen is None or self._stat_atopen.st_size <= 0: |
| 1691 | bufsize = DEFAULT_BUFFER_SIZE |
| 1692 | else: |
| 1693 | # In order to detect end of file, need a read() of at least 1 |
| 1694 | # byte which returns size 0. Oversize the buffer by 1 byte so the |
| 1695 | # I/O can be completed with two read() calls (one for all data, one |
| 1696 | # for EOF) without needing to resize the buffer. |
| 1697 | bufsize = self._stat_atopen.st_size + 1 |
| 1698 | |
| 1699 | if self._stat_atopen.st_size > 65536: |
| 1700 | try: |
| 1701 | pos = os.lseek(self._fd, 0, SEEK_CUR) |
| 1702 | if self._stat_atopen.st_size >= pos: |
| 1703 | bufsize = self._stat_atopen.st_size - pos + 1 |
| 1704 | except OSError: |
| 1705 | pass |
| 1706 | |
| 1707 | result = bytearray(bufsize) |
| 1708 | bytes_read = 0 |
| 1709 | try: |
| 1710 | while n := os.readinto(self._fd, memoryview(result)[bytes_read:]): |
| 1711 | bytes_read += n |
| 1712 | if bytes_read >= len(result): |
| 1713 | result.resize(_new_buffersize(bytes_read)) |
| 1714 | except BlockingIOError: |
| 1715 | if not bytes_read: |
| 1716 | return None |
| 1717 | |
| 1718 | assert len(result) - bytes_read >= 1, \ |
| 1719 | "os.readinto buffer size 0 will result in erroneous EOF / returns 0" |
| 1720 | result.resize(bytes_read) |
| 1721 | return bytes(result) |
| 1722 | |
| 1723 | def readinto(self, buffer): |
| 1724 | """Same as RawIOBase.readinto().""" |
no test coverage detected