Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41c5d2f

Browse files
author
Thomas Heller
committed
Merged revisions 78382 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ................ r78382 | thomas.heller | 2010-02-23 21:25:02 +0100 (Di, 23 Feb 2010) | 11 lines Merged revisions 78380 via svnmerge from svn+ssh://[email protected]/python/trunk ........ r78380 | thomas.heller | 2010-02-23 21:11:44 +0100 (Di, 23 Feb 2010) | 4 lines ctypes CThunkObject was not registered correctly with the cycle garbage collector, leading to possible leaks when using callback functions. ........ ................
1 parent f99f67b commit 41c5d2f

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

Lib/ctypes/test/test_callbacks.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ def test_unsupported_restype_2(self):
118118
prototype = self.functype.__func__(object)
119119
self.assertRaises(TypeError, prototype, lambda: None)
120120

121+
def test_issue_7959(self):
122+
proto = self.functype.__func__(None)
123+
124+
class X(object):
125+
def func(self): pass
126+
def __init__(self):
127+
self.v = proto(self.func)
128+
129+
import gc
130+
for i in range(32):
131+
X()
132+
gc.collect()
133+
live = [x for x in gc.get_objects()
134+
if isinstance(x, X)]
135+
self.assertEqual(len(live), 0)
136+
121137
try:
122138
WINFUNCTYPE
123139
except NameError:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ Core and Builtins
8686
Library
8787
-------
8888

89+
- Issue #7959: ctypes callback functions are now registered correctly
90+
with the cylce garbage collector.
91+
8992
- Issue #6666: fix bug in trace.py that applied the list of directories
9093
to be ignored only to the first file. Noted by Bogdan Opanchuk.
9194

Modules/_ctypes/callbacks.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ CThunkObject_dealloc(PyObject *_self)
1818
Py_XDECREF(self->restype);
1919
if (self->pcl)
2020
_ctypes_free_closure(self->pcl);
21-
PyObject_Del(self);
21+
PyObject_GC_Del(self);
2222
}
2323

2424
static int
@@ -61,7 +61,7 @@ PyTypeObject PyCThunk_Type = {
6161
0, /* tp_getattro */
6262
0, /* tp_setattro */
6363
0, /* tp_as_buffer */
64-
Py_TPFLAGS_DEFAULT, /* tp_flags */
64+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
6565
"CThunkObject", /* tp_doc */
6666
CThunkObject_traverse, /* tp_traverse */
6767
CThunkObject_clear, /* tp_clear */
@@ -364,7 +364,7 @@ static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
364364
CThunkObject *p;
365365
int i;
366366

367-
p = PyObject_NewVar(CThunkObject, &PyCThunk_Type, nArgs);
367+
p = PyObject_GC_NewVar(CThunkObject, &PyCThunk_Type, nArgs);
368368
if (p == NULL) {
369369
PyErr_NoMemory();
370370
return NULL;
@@ -379,6 +379,7 @@ static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
379379

380380
for (i = 0; i < nArgs + 1; ++i)
381381
p->atypes[i] = NULL;
382+
PyObject_GC_Track((PyObject *)p);
382383
return p;
383384
}
384385

0 commit comments

Comments
 (0)