gh-112015: Implement ctypes.memoryview_at()#112018
Merged
encukou merged 25 commits intopython:mainfrom Jan 3, 2025
Merged
Conversation
ctypes currently has no way to easily create a buffer object from a pointer and a dynamic size. It is possible to create memoryview objects of array objects (e.g. memoryview((c_ubyte * 10)())) but this is excessively slow when implementing a callback function in Python that is passed a dynamic void * and a size_t. `ctypes.buffer_at()` fills that gap in the API. This is similar to `ffi.buffer()` in the cffi module.
ctypes.buffer_at()ctypes.buffer_at()
Member
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Please add tests and What's New entry.
And I think that it is worth to support large buffers.
Misc/NEWS.d/next/Library/2023-11-12-21-53-40.gh-issue-112015.2WPRxE.rst
Outdated
Show resolved
Hide resolved
Misc/NEWS.d/next/Library/2023-11-12-21-53-40.gh-issue-112015.2WPRxE.rst
Outdated
Show resolved
Hide resolved
Documentation improvements Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
It's a more descriptive and precise name.
It's more common that the user will want a memoryview object that can be mutated. An immutable object is more rare, so make the default case return a mutable object.
Contributor
Author
|
When merging, just flatten all the commits down to a single commit. |
ctypes.buffer_at()ctypes.memoryview_at()
encukou
reviewed
Sep 18, 2024
Contributor
Author
|
Abandoned |
Member
No worries. Thank you for your work so far! |
Contributor
Author
|
No problem, feel free |
…tics. Improve tests and docs.
Member
|
OK. I went back to the |
Member
|
Does this look interesting to you, @ZeroIntensity or @picnixz? |
picnixz
reviewed
Dec 16, 2024
Member
|
I'll review sometime soon (today or in the next few days). |
encukou
reviewed
Dec 19, 2024
Misc/NEWS.d/next/Library/2023-11-12-21-53-40.gh-issue-112015.2WPRxE.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
WolframAlph
pushed a commit
to WolframAlph/cpython
that referenced
this pull request
Jan 4, 2025
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
srinivasreddy
pushed a commit
to srinivasreddy/cpython
that referenced
this pull request
Jan 8, 2025
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ctypes currently has no way to easily create a buffer object from a pointer and a dynamic size. It is possible to create memoryview objects of array objects that refer to existing memory (e.g.
memoryview((c_byte * size).from_address(address))) but this is inefficient and the resulting memoryview object doesn't allow editing via python (invalid format errors are thrown, this is due to how ctypes objects implements the buffer protocol).ctypes.memoryview_at()fills that gap in the API. This is similar toffi.buffer()in the cffi module.Fixes #112015
📚 Documentation preview 📚: https://cpython-previews--112018.org.readthedocs.build/
ctypes.memoryview_at()#112015