MCPcopy Index your code
hub / github.com/python/cpython / readall

Method readall

Lib/_pyio.py:1698–1742  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1696 return None
1697
1698 def readall(self):
1699 """Read all data from the file, returned as bytes.
1700
1701 Reads until either there is an error or read() returns size 0
1702 (indicates EOF). If the file is already at EOF, returns an
1703 empty bytes object.
1704
1705 In non-blocking mode, returns as much data as could be read
1706 before EAGAIN. If no data is available (EAGAIN is returned
1707 before bytes are read) returns None.
1708 """
1709 self._checkClosed()
1710 self._checkReadable()
1711 if self._stat_atopen is None or self._stat_atopen.st_size <= 0:
1712 bufsize = DEFAULT_BUFFER_SIZE
1713 else:
1714 # In order to detect end of file, need a read() of at least 1
1715 # byte which returns size 0. Oversize the buffer by 1 byte so the
1716 # I/O can be completed with two read() calls (one for all data, one
1717 # for EOF) without needing to resize the buffer.
1718 bufsize = self._stat_atopen.st_size + 1
1719
1720 if self._stat_atopen.st_size > 65536:
1721 try:
1722 pos = os.lseek(self._fd, 0, SEEK_CUR)
1723 if self._stat_atopen.st_size >= pos:
1724 bufsize = self._stat_atopen.st_size - pos + 1
1725 except OSError:
1726 pass
1727
1728 result = bytearray(bufsize)
1729 bytes_read = 0
1730 try:
1731 while n := os.readinto(self._fd, memoryview(result)[bytes_read:]):
1732 bytes_read += n
1733 if bytes_read >= len(result):
1734 result.resize(_new_buffersize(bytes_read))
1735 except BlockingIOError:
1736 if not bytes_read:
1737 return None
1738
1739 assert len(result) - bytes_read >= 1, \
1740 "os.readinto buffer size 0 will result in erroneous EOF / returns 0"
1741 result.resize(bytes_read)
1742 return result.take_bytes()
1743
1744 def readinto(self, buffer):
1745 """Same as RawIOBase.readinto()."""

Callers 3

readMethod · 0.95
_read_unlockedMethod · 0.45
readMethod · 0.45

Calls 6

_checkReadableMethod · 0.95
_new_buffersizeFunction · 0.85
take_bytesMethod · 0.80
_checkClosedMethod · 0.45
readintoMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected