Skip to content

Commit 6ac84c7

Browse files
Add test for creating types with factory
1 parent aedc4b2 commit 6ac84c7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Lib/test/test_ctypes/test_pointers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import array
22
import ctypes
3+
import gc
34
import sys
45
import unittest
56
from ctypes import (CDLL, CFUNCTYPE, Structure,
@@ -10,6 +11,7 @@
1011
c_long, c_ulong, c_longlong, c_ulonglong,
1112
c_float, c_double)
1213
from test.support import import_helper
14+
from weakref import WeakSet
1315
_ctypes_test = import_helper.import_module("_ctypes_test")
1416
from ._support import (_CData, PyCPointerType, Py_TPFLAGS_DISALLOW_INSTANTIATION,
1517
Py_TPFLAGS_IMMUTABLETYPE)
@@ -246,5 +248,34 @@ def test_pointer_set_python_type(self):
246248
with self.assertRaisesRegex(TypeError, "must have storage info"):
247249
p1.set_type(int)
248250

251+
def test_pointer_types_factory(self):
252+
"""Shouldn't leak"""
253+
def factory():
254+
class Cls(Structure):
255+
_fields_ = (
256+
('a', c_int),
257+
('b', c_float),
258+
)
259+
260+
return Cls
261+
262+
ws_typ = WeakSet()
263+
ws_ptr = WeakSet()
264+
for _ in range(10):
265+
typ = factory()
266+
ptr = POINTER(typ)
267+
268+
ws_typ.add(typ)
269+
ws_ptr.add(ptr)
270+
271+
typ = None
272+
ptr = None
273+
274+
gc.collect()
275+
276+
self.assertEqual(len(ws_typ), 0, ws_typ)
277+
self.assertEqual(len(ws_ptr), 0, ws_ptr)
278+
279+
249280
if __name__ == '__main__':
250281
unittest.main()

0 commit comments

Comments
 (0)