From e1bedf72ce5a0b1d6671b9154e678f1fcdd3e931 Mon Sep 17 00:00:00 2001 From: lipengyu Date: Wed, 1 Jul 2026 10:40:09 +0800 Subject: [PATCH 1/2] Fix PyErr_NoMemory errors in _ctypes.c --- Modules/_ctypes/_ctypes.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index e891249668c20f5..adfdf44e53604e3 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -673,6 +673,7 @@ StructUnionType_paramfunc(ctypes_state *st, CDataObject *self) if ((size_t)self->b_size > sizeof(void*)) { ptr = PyMem_Malloc(self->b_size); if (ptr == NULL) { + PyErr_NoMemory(); return NULL; } memcpy(ptr, self->b_ptr, self->b_size); From ed74ad89d4e8a250a8389144f966ec01d2bdb6ab Mon Sep 17 00:00:00 2001 From: lipengyu Date: Wed, 1 Jul 2026 11:48:51 +0800 Subject: [PATCH 2/2] add NEWS --- .../Library/2026-07-01-11-29-13.gh-issue-151126.eElGy5.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-07-01-11-29-13.gh-issue-151126.eElGy5.rst diff --git a/Misc/NEWS.d/next/Library/2026-07-01-11-29-13.gh-issue-151126.eElGy5.rst b/Misc/NEWS.d/next/Library/2026-07-01-11-29-13.gh-issue-151126.eElGy5.rst new file mode 100644 index 000000000000000..16a7f4d360ff3fd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-01-11-29-13.gh-issue-151126.eElGy5.rst @@ -0,0 +1,3 @@ +Fix crash on unset :exc:`MemoryError` on allocation failure when passing +:class:`ctypes.Structure` or :class:`ctypes.Union` instances by value to +ctypes foreign functions.