-
-
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 12 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 protocol <bufferobjects>` from a file descriptor. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.