Skip to content

Commit aa88a61

Browse files
committed
gh-99952: [ctypes] fix refcount issues in from_param() result.
1 parent 2e279e8 commit aa88a61

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

Lib/test/test_ctypes/test_parameters.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import unittest
23
from test.test_ctypes import need_symbol
34
import test.support
@@ -243,6 +244,58 @@ def test_parameter_repr(self):
243244
self.assertRegex(repr(c_wchar_p.from_param('hihi')), r"^<cparam 'Z' \(0x[A-Fa-f0-9]+\)>$")
244245
self.assertRegex(repr(c_void_p.from_param(0x12)), r"^<cparam 'P' \(0x0*12\)>$")
245246

247+
@test.support.cpython_only
248+
@unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
249+
def test_from_param_result_refcount(self):
250+
# Issue #99952
251+
import sys
252+
import _ctypes_test
253+
from ctypes import PyDLL, c_int, c_void_p, py_object, Structure
254+
255+
class X(Structure):
256+
_fields_ = [("a", c_void_p)]
257+
258+
def __del__(self):
259+
trace.append(4)
260+
261+
@classmethod
262+
def from_param(cls, value):
263+
trace.append(2)
264+
return cls()
265+
266+
PyList_Append = PyDLL("python", handle=sys.dllhandle).PyList_Append
267+
PyList_Append.restype = c_int
268+
PyList_Append.argtypes = [py_object, py_object, X]
269+
270+
trace = []
271+
trace.append(1)
272+
PyList_Append(trace, 3, "dummy")
273+
trace.append(5)
274+
275+
self.assertEqual(trace, [1, 2, 3, 4, 5])
276+
277+
class Y(Structure):
278+
_fields_ = [("a", c_void_p), ("b", c_void_p)]
279+
280+
def __del__(self):
281+
trace.append(4)
282+
283+
@classmethod
284+
def from_param(cls, value):
285+
trace.append(2)
286+
return cls()
287+
288+
PyList_Append = PyDLL("python", handle=sys.dllhandle).PyList_Append
289+
PyList_Append.restype = c_int
290+
PyList_Append.argtypes = [py_object, py_object, Y]
291+
292+
trace = []
293+
trace.append(1)
294+
PyList_Append(trace, 3, "dummy")
295+
trace.append(5)
296+
297+
self.assertEqual(trace, [1, 2, 3, 4, 5])
298+
246299
################################################################
247300

248301
if __name__ == '__main__':

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;
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);

0 commit comments

Comments
 (0)