gh-112015: Implement ctypes.memoryview_at() (GH-112018)

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 commit is contained in:
Rian Hunter 2025-01-03 05:07:07 -08:00 committed by GitHub
parent f21af186bf
commit b4f799b1e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 120 additions and 1 deletions

View file

@ -524,6 +524,7 @@ elif sizeof(c_ulonglong) == sizeof(c_void_p):
# functions
from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr
from _ctypes import _memoryview_at_addr
## void *memmove(void *, const void *, size_t);
memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr)
@ -549,6 +550,14 @@ def string_at(ptr, size=-1):
Return the byte string at void *ptr."""
return _string_at(ptr, size)
_memoryview_at = PYFUNCTYPE(
py_object, c_void_p, c_ssize_t, c_int)(_memoryview_at_addr)
def memoryview_at(ptr, size, readonly=False):
"""memoryview_at(ptr, size[, readonly]) -> memoryview
Return a memoryview representing the memory at void *ptr."""
return _memoryview_at(ptr, size, bool(readonly))
try:
from _ctypes import _wstring_at_addr
except ImportError: