Skip to content

Commit 52c6013

Browse files
committed
Add os._readinto, _pyio.readinto using it
1 parent 67d804b commit 52c6013

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed

Lib/_pyio.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1694,11 +1694,13 @@ def readall(self):
16941694

16951695
def readinto(self, b):
16961696
"""Same as RawIOBase.readinto()."""
1697+
self._checkClosed()
1698+
self._checkReadable()
16971699
m = memoryview(b).cast('B')
1698-
data = self.read(len(m))
1699-
n = len(data)
1700-
m[:n] = data
1701-
return n
1700+
try:
1701+
return os.readinto(self._fd, m)
1702+
except BlockingIOError:
1703+
return None
17021704

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

Modules/clinic/posixmodule.c.h

+52-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

+29
Original file line numberDiff line numberDiff line change
@@ -11433,6 +11433,34 @@ os_read_impl(PyObject *module, int fd, Py_ssize_t length)
1143311433
return buffer;
1143411434
}
1143511435

11436+
/*[clinic input]
11437+
os.readinto -> Py_ssize_t
11438+
fd: int
11439+
buffer: Py_buffer(accept={rwbuffer})
11440+
/
11441+
11442+
Read from a file descriptor into a provided buffer.
11443+
11444+
The buffer shold be a mutable buffer accepting bytes. On success, returns the
11445+
number of read bytes. It may be lower than the size of the buffer. When
11446+
interrupted by a signal (read() fails with EINTR), retries the read. For other
11447+
errors, read will not be retried and -1 will be returned.
11448+
[clinic start generated code]*/
11449+
11450+
static Py_ssize_t
11451+
os_readinto_impl(PyObject *module, int fd, Py_buffer *buffer)
11452+
/*[clinic end generated code: output=8091a3513c683a80 input=272ce8579f551995]*/
11453+
{
11454+
// Need a buffer to read into, return `-1` if there isn't one so that there
11455+
// is a uniform return type.
11456+
if (buffer->len <= 0) {
11457+
errno = EINVAL;
11458+
return -1;
11459+
}
11460+
11461+
return _Py_read(fd, buffer->buf, buffer->len);
11462+
}
11463+
1143611464
#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
1143711465
|| defined(__APPLE__))) \
1143811466
|| defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
@@ -16973,6 +17001,7 @@ static PyMethodDef posix_methods[] = {
1697317001
OS_LOCKF_METHODDEF
1697417002
OS_LSEEK_METHODDEF
1697517003
OS_READ_METHODDEF
17004+
OS_READINTO_METHODDEF
1697617005
OS_READV_METHODDEF
1697717006
OS_PREAD_METHODDEF
1697817007
OS_PREADV_METHODDEF

0 commit comments

Comments
 (0)