1 | n/a | # This tests the internal _objects attribute |
---|
2 | n/a | import unittest |
---|
3 | n/a | from ctypes import * |
---|
4 | n/a | from sys import getrefcount as grc |
---|
5 | n/a | |
---|
6 | n/a | # XXX This test must be reviewed for correctness!!! |
---|
7 | n/a | |
---|
8 | n/a | # ctypes' types are container types. |
---|
9 | n/a | # |
---|
10 | n/a | # They have an internal memory block, which only consists of some bytes, |
---|
11 | n/a | # but it has to keep references to other objects as well. This is not |
---|
12 | n/a | # really needed for trivial C types like int or char, but it is important |
---|
13 | n/a | # for aggregate types like strings or pointers in particular. |
---|
14 | n/a | # |
---|
15 | n/a | # What about pointers? |
---|
16 | n/a | |
---|
17 | n/a | class ObjectsTestCase(unittest.TestCase): |
---|
18 | n/a | def assertSame(self, a, b): |
---|
19 | n/a | self.assertEqual(id(a), id(b)) |
---|
20 | n/a | |
---|
21 | n/a | def test_ints(self): |
---|
22 | n/a | i = 42000123 |
---|
23 | n/a | refcnt = grc(i) |
---|
24 | n/a | ci = c_int(i) |
---|
25 | n/a | self.assertEqual(refcnt, grc(i)) |
---|
26 | n/a | self.assertEqual(ci._objects, None) |
---|
27 | n/a | |
---|
28 | n/a | def test_c_char_p(self): |
---|
29 | n/a | s = b"Hello, World" |
---|
30 | n/a | refcnt = grc(s) |
---|
31 | n/a | cs = c_char_p(s) |
---|
32 | n/a | self.assertEqual(refcnt + 1, grc(s)) |
---|
33 | n/a | self.assertSame(cs._objects, s) |
---|
34 | n/a | |
---|
35 | n/a | def test_simple_struct(self): |
---|
36 | n/a | class X(Structure): |
---|
37 | n/a | _fields_ = [("a", c_int), ("b", c_int)] |
---|
38 | n/a | |
---|
39 | n/a | a = 421234 |
---|
40 | n/a | b = 421235 |
---|
41 | n/a | x = X() |
---|
42 | n/a | self.assertEqual(x._objects, None) |
---|
43 | n/a | x.a = a |
---|
44 | n/a | x.b = b |
---|
45 | n/a | self.assertEqual(x._objects, None) |
---|
46 | n/a | |
---|
47 | n/a | def test_embedded_structs(self): |
---|
48 | n/a | class X(Structure): |
---|
49 | n/a | _fields_ = [("a", c_int), ("b", c_int)] |
---|
50 | n/a | |
---|
51 | n/a | class Y(Structure): |
---|
52 | n/a | _fields_ = [("x", X), ("y", X)] |
---|
53 | n/a | |
---|
54 | n/a | y = Y() |
---|
55 | n/a | self.assertEqual(y._objects, None) |
---|
56 | n/a | |
---|
57 | n/a | x1, x2 = X(), X() |
---|
58 | n/a | y.x, y.y = x1, x2 |
---|
59 | n/a | self.assertEqual(y._objects, {"0": {}, "1": {}}) |
---|
60 | n/a | x1.a, x2.b = 42, 93 |
---|
61 | n/a | self.assertEqual(y._objects, {"0": {}, "1": {}}) |
---|
62 | n/a | |
---|
63 | n/a | def test_xxx(self): |
---|
64 | n/a | class X(Structure): |
---|
65 | n/a | _fields_ = [("a", c_char_p), ("b", c_char_p)] |
---|
66 | n/a | |
---|
67 | n/a | class Y(Structure): |
---|
68 | n/a | _fields_ = [("x", X), ("y", X)] |
---|
69 | n/a | |
---|
70 | n/a | s1 = b"Hello, World" |
---|
71 | n/a | s2 = b"Hallo, Welt" |
---|
72 | n/a | |
---|
73 | n/a | x = X() |
---|
74 | n/a | x.a = s1 |
---|
75 | n/a | x.b = s2 |
---|
76 | n/a | self.assertEqual(x._objects, {"0": s1, "1": s2}) |
---|
77 | n/a | |
---|
78 | n/a | y = Y() |
---|
79 | n/a | y.x = x |
---|
80 | n/a | self.assertEqual(y._objects, {"0": {"0": s1, "1": s2}}) |
---|
81 | n/a | ## x = y.x |
---|
82 | n/a | ## del y |
---|
83 | n/a | ## print x._b_base_._objects |
---|
84 | n/a | |
---|
85 | n/a | def test_ptr_struct(self): |
---|
86 | n/a | class X(Structure): |
---|
87 | n/a | _fields_ = [("data", POINTER(c_int))] |
---|
88 | n/a | |
---|
89 | n/a | A = c_int*4 |
---|
90 | n/a | a = A(11, 22, 33, 44) |
---|
91 | n/a | self.assertEqual(a._objects, None) |
---|
92 | n/a | |
---|
93 | n/a | x = X() |
---|
94 | n/a | x.data = a |
---|
95 | n/a | ##XXX print x._objects |
---|
96 | n/a | ##XXX print x.data[0] |
---|
97 | n/a | ##XXX print x.data._objects |
---|
98 | n/a | |
---|
99 | n/a | if __name__ == '__main__': |
---|
100 | n/a | unittest.main() |
---|