Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-156124: Fix a crash when deleting ctypes Pointer.contents (GH-156125)
In the free-threaded build the setter passed the value to
Py_BEGIN_CRITICAL_SECTION2() before checking it for NULL.
(cherry picked from commit ca6e733)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
serhiy-storchaka authored and miss-islington committed Aug 21, 2026
commit 4b4b38c720b45bd7e48ccdf87f33b2fa77ac5f1d
7 changes: 6 additions & 1 deletion Lib/test/test_ctypes/test_delattr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from ctypes import Structure, c_char, c_int
from ctypes import POINTER, Structure, c_char, c_int


class X(Structure):
Expand All @@ -16,6 +16,11 @@ def test_chararray(self):
with self.assertRaises(TypeError):
del chararray.value

def test_pointer_contents(self):
ptr = POINTER(c_int)(c_int(42))
with self.assertRaises(TypeError):
del ptr.contents

def test_struct(self):
struct = X()
with self.assertRaises(TypeError):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in the free-threaded build when deleting the :attr:`!contents`
attribute of a :mod:`ctypes` pointer.
10 changes: 5 additions & 5 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5710,11 +5710,6 @@ Pointer_set_contents_lock_held(PyObject *op, PyObject *value, void *closure)
PyObject *keep;
CDataObject *self = _CDataObject_CAST(op);

if (value == NULL) {
PyErr_SetString(PyExc_TypeError,
"Pointer does not support item deletion");
return -1;
}
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self)));
StgInfo *stginfo;
if (PyStgInfo_FromObject(st, op, &stginfo) < 0) {
Expand Down Expand Up @@ -5758,6 +5753,11 @@ Pointer_set_contents_lock_held(PyObject *op, PyObject *value, void *closure)
static int
Pointer_set_contents(PyObject *op, PyObject *value, void *closure)
{
if (value == NULL) {
PyErr_SetString(PyExc_TypeError,
"Pointer does not support item deletion");
return -1;
}
int res;
Py_BEGIN_CRITICAL_SECTION2(op, value);
res = Pointer_set_contents_lock_held(op, value, closure);
Expand Down
Loading