FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-129205: Add os.readinto API for reading data into a caller provided buffer by cmaloney · Pull Request #129211 · python/cpython · GitHub

Repository navigation

gh-129205: Add os.readinto API for reading data into a caller provided buffer - #129211

Merged
vstinner merged 23 commits into
python:mainfrom
cmaloney:cmaloney/add_readinto
Jan 26, 2025
Merged

vstinner merged 23 commits into
python:mainfrom
cmaloney:cmaloney/add_readinto

Conversation

cmaloney commented Jan 23, 2025 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

Adds os.readinto API as well as associated tests. Most functionality is provided by _Py_read which is fairly well exercised by os.read tests.

For testing I have been running:

make buildbottest
python -bb -E -Wd -m test -M32g -uall test_os _test_eintr  -vvv

I skipped adding to test_largefile, that path should get exercised in part by test_os.FileTests.test_large_readinto, and will get exercised reading 2GB+ of bytes as move _pyio to use os.readinto (test_largefile.PyLargeFileTest).

Not sure how to make a negative length buffer for testing that case getting handled as expected (_Py_read takes a size_t and Py_buffer.len is a Py_ssize_t)

Should probably have buildbots run once reviewed. This should work across platforms, but I've only tested on Linux and things like large file tests aren't run by all bots.

Add a new OS api which will read data directly into a caller provided
writeable buffer protocol object.

Copy link
Copy Markdown
Contributor Author

@vstinner ready for you to take a look

Comment thread Lib/test/test_os.py Outdated
Comment thread Modules/posixmodule.c Outdated
Comment thread Modules/posixmodule.c Outdated
Comment thread Modules/posixmodule.c Outdated

picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Some test nits

Comment thread Lib/test/test_os.py Outdated
Comment thread Doc/library/os.rst Outdated
Comment thread Doc/library/os.rst Outdated
Comment thread Doc/library/os.rst Outdated
Comment thread Doc/library/os.rst
Comment thread Doc/library/os.rst Outdated
Comment thread Lib/test/test_os.py
Comment thread Doc/library/os.rst Outdated
Comment thread Doc/library/os.rst Outdated
Comment thread Doc/library/os.rst
Comment thread Doc/whatsnew/3.14.rst Outdated
Comment thread Modules/posixmodule.c Outdated
Comment thread Modules/posixmodule.c Outdated
Comment thread Modules/posixmodule.c
/* 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()));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

note: Clinic wrapping code takes -1 return + PyErr_Occurred to indicate error and will return a NULL PyObject* for the function. So should never get back a PyLong object which is negative.

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM. Thanks for the update, the implementation now looks better :-)

picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Just some wording comments and LGTM.

Comment thread Doc/library/os.rst Outdated
Comment thread Doc/whatsnew/3.14.rst
Comment thread Lib/test/_test_eintr.py
Comment on lines +162 to +176
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)',
))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
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)',
))
data = [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])',
f'data = {data!r}',
f'sleep_time = {self.sleep_time!r}',
'',
'for datum in data:',
' # let the parent block on read()',
' time.sleep(sleep_time)',
' os.write(wr, datum)',
))

Strictly speaking, data is already the plural form of datum.

cmaloney Jan 24, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

For data -> datum the code is copied from test_read just before with minor tweaks; I think the two being very close in code is useful for maintenance more than naming. I think I can get rid of bufs and zip though which will remove the ambiguities / make the code read better

Comment thread Lib/test/_test_eintr.py Outdated
Comment thread Lib/test/_test_eintr.py Outdated
cmaloney and others added 3 commits January 24, 2025 11:24
vstinner merged commit 1ed4487 into python:main Jan 26, 2025

Copy link
Copy Markdown
Member

Merged, thank you!

Copy link
Copy Markdown
Member

As the initial author of test_eintr, I'm likely the one who wrote datas typo. So I wrote PR gh-129316 to fix grammar and modernize the code base (use f-strings) 😁

cmaloney deleted the cmaloney/add_readinto branch January 26, 2025 16:30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL