8000 gh-99952: [ctypes] fix refcount issues in from_param() result. (#100169) · python/cpython@dfad678 · GitHub
[go: up one dir, main page]

Skip to content

Commit dfad678

Browse files
authored
gh-99952: [ctypes] fix refcount issues in from_param() result. (#100169)
Fixes a reference counting issue with `ctypes.Structure` when a `from_param()` method call is used and the structure size is larger than a C pointer `sizeof(void*)`. This problem existed for a very long time, but became more apparent in 3.8+ by change likely due to garbage collection cleanup timing changes.
1 parent f5ad63f commit dfad678

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

Lib/test/test_ctypes/test_parameters.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,58 @@ def test_parameter_repr(self):
266266
self.assertRegex(repr(c_wchar_p.from_param('hihi')), r"^<cparam 'Z' \(0x[A-Fa-f0-9]+\)>$")
267267
self.assertRegex(repr(c_void_p.from_param(0x12)), r"^<cparam 'P' \(0x0*12\)>$")
268268

269+
@test.support.cpython_only
270+
def test_from_param_result_refcount(self):
271+
# Issue #99952
272+
import _ctypes_test
273+
from ctypes import PyDLL, c_int, c_void_p, py_object, Structure
274+
275+
class X(Structure):
276+
"""This struct size is <= sizeof(void*)."""
277+
_fields_ = [("a", c_void_p)]
278+
279+
def __del__(self):
280+
trace.append(4)
281+
282+
@classmethod
283+
def from_param(cls, value):
284+
trace.append(2)
285+
return cls()
286+
287+
PyList_Append = PyDLL(_ctypes_test.__file__)._testfunc_pylist_append
288+
PyList_Append.restype = c_int
289+
PyList_Append.argtypes = [py_object, py_object, X]
290+
291+
trace = []
292+
trace.append(1)
293+
PyList_Append(trace, 3, "dummy")
294+
trace.append(5)
295+
296+
self.assertEqual(trace, [1, 2, 3, 4, 5])
297+
298+
class Y(Structure):
299+
"""This struct size is > sizeof(void*)."""
300+
_fields_ = [("a", c_void_p), ("b", c_void_p)]
301+
302+
def __del__(self):
303+
trace.append(4)
304+
305+
@classmethod
306+
def from_param(cls, value):
307+
trace.append(2)
308+
return cls()
309+
310+
PyList_Append = PyDLL(_ctypes_test.__file__)._testfunc_pylist_append
311+
PyList_Append.restype = c_int
312+
PyList_Append.argtypes = [py_object, py_object, Y]
313+
314+
trace = []
315+
trace.append(1)
316+
PyList_Append(trace, 3, "dummy")
317+
trace.append(5)
318+
319+
self.assertEqual(trace, [1, 2, 3, 4, 5])
320+
269321
################################################################
270322

271323
if __name__ == '__main__':
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a reference undercounting issue in :class:`ctypes.Structure` with ``from_param()``
2+
results larger than a C pointer.

Modules/_ctypes/_ctypes.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,15 @@ _ctypes_alloc_format_string_with_shape(int ndim, const Py_ssize_t *shape,
412412
typedef struct {
413413
PyObject_HEAD
414414
void *ptr;
415+
PyObject *keep; // If set, a reference to the original CDataObject.
415416
} StructParamObject;
416417

417418

418419
static void
419420
StructParam_dealloc(PyObject *myself)
420421
{
421422
StructParamObject *self = (StructParamObject *)myself;
423+
Py_XDECREF(self->keep);
422424
PyMem_Free(self->ptr);
423425
Py_TYPE(self)->tp_free(myself);
424426
}
@@ -466,6 +468,7 @@ StructUnionType_paramfunc(CDataObject *self)
466468

467469
StructParamObject *struct_param = (StructParamObject *)obj;
468470
struct_param->ptr = ptr;
471+
struct_param->keep = Py_NewRef(self);
469472
} else {
470473
ptr = self->b_ptr;
471474
obj = Py_NewRef(self);

Modules/_ctypes/_ctypes_test.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,12 @@ EXPORT(long) _test_i38748_runCallback(_test_i38748_funcType callback, int a, int
10471047

10481048
#endif
10491049

1050+
EXPORT(int)
1051+
_testfunc_pylist_append(PyObject *list, PyObject *item)
1052+
{
1053+
return PyList_Append(list, item);
1054+
}
1055+
10501056
static struct PyModuleDef_Slot _ctypes_test_slots[] = {
10511057
{0, NULL}
10521058
};

0 commit comments

Comments
 (0)
0