Skip to content

Implement ctypes.buffer_at() #112016

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2109,6 +2109,19 @@ Utility functions
.. audit-event:: ctypes.wstring_at address,size ctypes.wstring_at


.. function:: buffer_at(address, size, allow_write=False)

This function returns a memoryview object that references the
memory starting at *address* up to (but not including) *address +
size*. If *allow_write* is set to a truthy value then the
memoryview object is mutable.

This function is similar to :funct:`string_at` with the key difference
of not making a copy of the specified memory.

.. audit-event:: ctypes.buffer_at address,size,allow_write ctypes.buffer_at


.. _ctypes-data-types:

Data types
Expand Down
8 changes: 8 additions & 0 deletions Lib/ctypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,14 @@ def wstring_at(ptr, size=-1):
Return the string at addr."""
return _wstring_at(ptr, size)

from _ctypes import _buffer_at_addr

_buffer_at = PYFUNCTYPE(py_object, c_void_p, c_int, c_int)(_buffer_at_addr)
def buffer_at(ptr, size, allow_write=False):
"""buffer_at(addr, size[, allow_write]) -> memoryview

Return the buffer at addr."""
return _buffer_at(ptr, size, bool(allow_write))

if _os.name == "nt": # COM stuff
def DllGetClassObject(rclsid, riid, ppv):
Expand Down
12 changes: 12 additions & 0 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5642,6 +5642,17 @@ wstring_at(const wchar_t *ptr, int size)
return PyUnicode_FromWideChar(ptr, ssize);
}

static PyObject *
buffer_at(char *ptr, int size, int allow_write)
{
if (PySys_Audit("ctypes.buffer_at", "nii", (Py_ssize_t)ptr, size,
allow_write) < 0) {
return NULL;
}

return PyMemoryView_FromMemory(ptr, size,
allow_write ? PyBUF_WRITE : PyBUF_READ);
}

static struct PyModuleDef _ctypesmodule = {
PyModuleDef_HEAD_INIT,
Expand Down Expand Up @@ -5777,6 +5788,7 @@ _ctypes_add_objects(PyObject *mod)
MOD_ADD("_string_at_addr", PyLong_FromVoidPtr(string_at));
MOD_ADD("_cast_addr", PyLong_FromVoidPtr(cast));
MOD_ADD("_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
MOD_ADD("_buffer_at_addr", PyLong_FromVoidPtr(buffer_at));

/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */
#if !HAVE_DECL_RTLD_LOCAL
Expand Down