https://github.com/python/cpython/commit/180ee43bde99b8ce4c4f1d5237ab191e26118061
commit: 180ee43bde99b8ce4c4f1d5237ab191e26118061
branch: main
author: Cody Maloney <[email protected]>
committer: vstinner <[email protected]>
date: 2025-01-28T12:40:44+01:00
summary:
gh-129005: Avoid copy in _pyio.FileIO.readinto() (#129324)
`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.readinto()`.
`os.readinto()` uses `PyObject_GetBuffer()` to make sure the passed
in buffer is writeable and bytes-like, drop the manual check.
files:
A Misc/NEWS.d/next/Library/2025-01-26-10-01-21.gh-issue-129005.ncpLvw.rst
M Lib/_pyio.py
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 14961c39d3541d..023478aa78c6a0 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -1692,13 +1692,14 @@ def readall(self):
return bytes(result)
- def readinto(self, b):
+ def readinto(self, buffer):
"""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:
+ return os.readinto(self._fd, buffer)
+ 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-26-10-01-21.gh-issue-129005.ncpLvw.rst
b/Misc/NEWS.d/next/Library/2025-01-26-10-01-21.gh-issue-129005.ncpLvw.rst
new file mode 100644
index 00000000000000..a825e9d244d525
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-01-26-10-01-21.gh-issue-129005.ncpLvw.rst
@@ -0,0 +1 @@
+Optimize ``_pyio.FileIO.readinto`` by avoiding unnecessary objects and copies
using :func:`os.readinto`.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]