1
# This tests the internal _objects attribute
4
from sys import getrefcount as grc
6
# XXX This test must be reviewed for correctness!!!
9
ctypes' types are container types.
11
They have an internal memory block, which only consists of some bytes,
12
but it has to keep references to other objects as well. This is not
13
really needed for trivial C types like int or char, but it is important
14
for aggregate types like strings or pointers in particular.
20
class ObjectsTestCase(unittest.TestCase):
21
def failUnlessSame(self, a, b):
22
self.failUnlessEqual(id(a), id(b))
28
self.failUnlessEqual(rc, grc(i))
29
self.failUnlessEqual(ci._objects, None)
31
def test_c_char_p(self):
35
self.failUnlessEqual(rc + 1, grc(s))
36
self.failUnlessSame(cs._objects, s)
38
def test_simple_struct(self):
40
_fields_ = [("a", c_int), ("b", c_int)]
45
self.failUnlessEqual(x._objects, None)
48
self.failUnlessEqual(x._objects, None)
50
def test_embedded_structs(self):
52
_fields_ = [("a", c_int), ("b", c_int)]
55
_fields_ = [("x", X), ("y", X)]
58
self.failUnlessEqual(y._objects, None)
62
self.failUnlessEqual(y._objects, {"0": {}, "1": {}})
64
self.failUnlessEqual(y._objects, {"0": {}, "1": {}})
68
_fields_ = [("a", c_char_p), ("b", c_char_p)]
71
_fields_ = [("x", X), ("y", X)]
79
self.failUnlessEqual(x._objects, {"0": bytes(s1, "ascii"),
80
"1": bytes(s2, "ascii")})
84
self.failUnlessEqual(y._objects, {"0": {"0": bytes(s1, "ascii"),
85
"1": bytes(s2, "ascii")}})
88
## print x._b_base_._objects
90
def test_ptr_struct(self):
92
_fields_ = [("data", POINTER(c_int))]
96
self.failUnlessEqual(a._objects, None)
100
##XXX print x._objects
101
##XXX print x.data[0]
102
##XXX print x.data._objects
104
if __name__ == '__main__':