- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 33.2k
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 20 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|  | @@ -152,6 +152,37 @@ def test_read(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(data, os.read(rd, len(data))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(proc.wait(), 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_readinto(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rd, wr = os.pipe() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.addCleanup(os.close, rd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # wr closed explicitly by parent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # the payload below are smaller than PIPE_BUF, hence the writes will be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # atomic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| datas = [b"hello", b"world", b"spam"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bufs = [bytearray(5), bytearray(5), bytearray(4)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| code = '\n'.join(( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'import os, sys, time', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'wr = int(sys.argv[1])', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'datas = %r' % datas, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'sleep_time = %r' % self.sleep_time, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'for data in datas:', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ' # let the parent block on read()', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ' time.sleep(sleep_time)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ' os.write(wr, data)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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. 
        Suggested change
       
 Strictly speaking, data is already the plural form of datum. 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. For data -> datum the code is copied from  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| proc = self.subprocess(code, str(wr), pass_fds=[wr]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with kill_on_error(proc): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| os.close(wr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for data, buffer in zip(datas, bufs): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|         
                  cmaloney marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| os.readinto(rd, buffer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(data, buffer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|         
                  cmaloney marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(proc.wait(), 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_write(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rd, wr = os.pipe() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.addCleanup(os.close, wr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 without reaching end of stream. Will retry the underlying | ||
| system call when interrupted by a signal. Other errors will not be retried and | ||
| an error will be raised. | ||
|  | ||
| Returns 0 if the fd is at end of file or the provided buffer is length 0 (can be | ||
| used to check for errors without reading data). Never returns a negative value. | ||
| [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=2a0ac4256f469f93]*/ | ||
| { | ||
| 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.