summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorRian Hunter <rianhunter@users.noreply.github.com>2025-01-03 13:07:07 (GMT)
committerGitHub <noreply@github.com>2025-01-03 13:07:07 (GMT)
commitb4f799b1e78ede17b41de9a2bc51b437a7e6dd74 (patch)
tree6b9c169612eefd8ee78b113bb39f2cb9c3aa24b0 /Modules
parentf21af186bf21c1c554209ac67d78d3cf99f7d7c0 (diff)
downloadcpython-b4f799b1e78ede17b41de9a2bc51b437a7e6dd74.zip
cpython-b4f799b1e78ede17b41de9a2bc51b437a7e6dd74.tar.gz
cpython-b4f799b1e78ede17b41de9a2bc51b437a7e6dd74.tar.bz2
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>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_ctypes/_ctypes.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index ac520ff..ede95bd 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -5791,6 +5791,22 @@ wstring_at(const wchar_t *ptr, int size)
return PyUnicode_FromWideChar(ptr, ssize);
}
+static PyObject *
+memoryview_at(void *ptr, Py_ssize_t size, int readonly)
+{
+ if (PySys_Audit("ctypes.memoryview_at", "nni",
+ (Py_ssize_t)ptr, size, readonly) < 0) {
+ return NULL;
+ }
+ if (size < 0) {
+ PyErr_Format(PyExc_ValueError,
+ "memoryview_at: size is negative (or overflowed): %zd",
+ size);
+ return NULL;
+ }
+ return PyMemoryView_FromMemory(ptr, size,
+ readonly ? PyBUF_READ : PyBUF_WRITE);
+}
static int
_ctypes_add_types(PyObject *mod)
@@ -5919,6 +5935,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("_memoryview_at_addr", PyLong_FromVoidPtr(memoryview_at));
/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */
#if !HAVE_DECL_RTLD_LOCAL