Skip to content

Commit fa3dd18

Browse files
committed
pythongh-129005: Avoid copy in _pyio.FileIO.readinto
`os.read` allocated and filled a buffer by calling `read(2)`, than that data was copied into the user provied buffer. Read directly into the caller's buffer instead by using `os.readv`. `self.read()` was doing the closed and readable checks so move those into `readinto`
1 parent 61b35f7 commit fa3dd18

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Lib/_pyio.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1695,10 +1695,12 @@ def readall(self):
16951695
def readinto(self, b):
16961696
"""Same as RawIOBase.readinto()."""
16971697
m = memoryview(b).cast('B')
1698-
data = self.read(len(m))
1699-
n = len(data)
1700-
m[:n] = data
1701-
return n
1698+
self._checkClosed()
1699+
self._checkReadable()
1700+
try:
1701+
return os.readv(self._fd, (m, ))
1702+
except BlockingIOError:
1703+
return None
17021704

17031705
def write(self, b):
17041706
"""Write bytes b to file, return number written.

0 commit comments

Comments
 (0)