-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-129205: Add os.readinto API for reading data into a caller provided buffer #129211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
148d2f7
3b34285
a56f337
b75bc9b
992d5f5
bbb0e6a
6512788
b4b4c28
4da3fec
c5f3df0
8cc70cd
6f8ad36
62f531c
c8f5800
2457b63
25125f2
52a83fa
a7a0775
e1aec8e
051b0c2
9356bc9
06e98e8
060a670
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add :func:`os.readinto` to read into a :ref:`buffer object <bufferobjects>` from a file descriptor. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11433,6 +11433,38 @@ os_read_impl(PyObject *module, int fd, Py_ssize_t length) | |
return buffer; | ||
} | ||
|
||
/*[clinic input] | ||
os.readinto -> Py_ssize_t | ||
fd: int | ||
buffer: Py_buffer(accept={rwbuffer}) | ||
/ | ||
|
||
Read into a buffer object from a file descriptor. | ||
|
||
The buffer should be mutable and bytes-like. On success, returns the number of | ||
bytes read. Less bytes may be read than the size of the buffer. The underlying | ||
system call will be retried when interrupted by a signal, unless the signal | ||
handler raises an exception. Other errors will not be retried and an error will | ||
be raised. | ||
|
||
Returns 0 if *fd* is at end of file or if the provided *buffer* has length 0 | ||
(which can be used to check for errors without reading data). Never returns | ||
negative. | ||
[clinic start generated code]*/ | ||
|
||
static Py_ssize_t | ||
os_readinto_impl(PyObject *module, int fd, Py_buffer *buffer) | ||
/*[clinic end generated code: output=8091a3513c683a80 input=d40074d0a68de575]*/ | ||
{ | ||
assert(buffer->len >= 0); | ||
Py_ssize_t result = _Py_read(fd, buffer->buf, buffer->len); | ||
/* Ensure negative is never returned without an error. Simplifies calling | ||
code. _Py_read should succeed, possibly reading 0 bytes, _or_ set an | ||
error. */ | ||
assert(result >= 0 || (result == -1 && PyErr_Occurred())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: Clinic wrapping code takes |
||
return result; | ||
} | ||
|
||
#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ | ||
|| defined(__APPLE__))) \ | ||
|| defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ | ||
|
@@ -16973,6 +17005,7 @@ static PyMethodDef posix_methods[] = { | |
OS_LOCKF_METHODDEF | ||
OS_LSEEK_METHODDEF | ||
OS_READ_METHODDEF | ||
OS_READINTO_METHODDEF | ||
OS_READV_METHODDEF | ||
OS_PREAD_METHODDEF | ||
OS_PREADV_METHODDEF | ||
|
Uh oh!
There was an error while loading. Please reload this page.