Skip to content

Provide ctypes.memoryview_at() #112015

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

Closed
rianhunter opened this issue Nov 13, 2023 · 3 comments · Fixed by #112018
Closed

Provide ctypes.memoryview_at() #112015

rianhunter opened this issue Nov 13, 2023 · 3 comments · Fixed by #112018
Labels
stdlib Python modules in the Lib dir topic-ctypes type-feature A feature request or enhancement

Comments

@rianhunter
Copy link
Contributor

rianhunter commented Nov 13, 2023

Feature or enhancement

Proposal:

It should be possible to easily make memoryview objects from pointer-able ctypes objects with an arbitrary length. In the same way we can currently use ctypes.string_at() to create bytes objects. The advantage of using memoryview objects is that we can elide a buffer copy.

import ctypes
a = (ctypes.c_ubyte * 10)()
ctypes.buffer_at(a, 10, True)[0] = 1
assert a[0] == 1
a[0] = 2
assert ctypes.buffer_at(a, 10, True)[0] == 2

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@rianhunter rianhunter added the type-feature A feature request or enhancement label Nov 13, 2023
@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 27, 2023
@rianhunter
Copy link
Contributor Author

Abandoned

@ncoghlan
Copy link
Contributor

@encukou is taking up work on the associated PR. Thanks for taking it this far @rianhunter!

@ncoghlan ncoghlan reopened this Sep 26, 2024
@ncoghlan ncoghlan changed the title Provide ctypes.buffer_at() Provide ctypes.memoryview_at() Sep 26, 2024
encukou added a commit that referenced this issue Jan 3, 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>
@encukou
Copy link
Member

encukou commented Jan 3, 2025

And it's in!
Thank you @rianhunter for the proposal and implementation, and all the reviewers for their insights.

WolframAlph pushed a commit to WolframAlph/cpython that referenced this issue 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 issue 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-ctypes type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants