diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 14961c39d3541d..6fea290df30d50 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1695,10 +1695,20 @@ def readall(self): def readinto(self, b): """Same as RawIOBase.readinto().""" m = memoryview(b).cast('B') - data = self.read(len(m)) - n = len(data) - m[:n] = data - return n + self._checkClosed() + self._checkReadable() + try: + if hasattr(os, 'readv'): + return os.readv(self._fd, (m, )) + + data = os.read(self._fd, len(m)) + n = len(data) + m[:n] = data + return n + except BlockingIOError: + return None + + def write(self, b): """Write bytes b to file, return number written. diff --git a/Misc/NEWS.d/next/Library/2025-01-18-15-29-08.gh-issue-129005.mM5dmV.rst b/Misc/NEWS.d/next/Library/2025-01-18-15-29-08.gh-issue-129005.mM5dmV.rst new file mode 100644 index 00000000000000..fabb593ae58513 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-18-15-29-08.gh-issue-129005.mM5dmV.rst @@ -0,0 +1 @@ +Optimize ``_pyio.FileIO.readinto`` by using :func:`os.readv` on platforms that support it which avoids an additional copy.