>>> from ctypes importPOINTER, c_int
>>> ptr = POINTER(c_int)(c_int(42))
>>> del ptr.contents
Segmentation fault
Pointer_set_contents() passes the new value to Py_BEGIN_CRITICAL_SECTION2(), which dereferences it to get the mutex, but the value is NULL when the attribute is deleted. The check for NULL is in Pointer_set_contents_lock_held(), which is only called after the critical section is entered.
With the GIL it raises TypeError: Pointer does not support item deletion.
The critical section was added in gh-128182, so this affects 3.14 and newer.
Activity
gh-156124: Fix a crash when deleting ctypes Pointer.contents (GH-156125)
[3.14] gh-156124: Fix a crash when deleting ctypes Pointer.contents (G…
[3.15] gh-156124: Fix a crash when deleting ctypes Pointer.contents (G…