1 | n/a | from ctypes import * |
---|
2 | n/a | import unittest |
---|
3 | n/a | |
---|
4 | n/a | class SimpleTestCase(unittest.TestCase): |
---|
5 | n/a | def test_cint(self): |
---|
6 | n/a | x = c_int() |
---|
7 | n/a | self.assertEqual(x._objects, None) |
---|
8 | n/a | x.value = 42 |
---|
9 | n/a | self.assertEqual(x._objects, None) |
---|
10 | n/a | x = c_int(99) |
---|
11 | n/a | self.assertEqual(x._objects, None) |
---|
12 | n/a | |
---|
13 | n/a | def test_ccharp(self): |
---|
14 | n/a | x = c_char_p() |
---|
15 | n/a | self.assertEqual(x._objects, None) |
---|
16 | n/a | x.value = b"abc" |
---|
17 | n/a | self.assertEqual(x._objects, b"abc") |
---|
18 | n/a | x = c_char_p(b"spam") |
---|
19 | n/a | self.assertEqual(x._objects, b"spam") |
---|
20 | n/a | |
---|
21 | n/a | class StructureTestCase(unittest.TestCase): |
---|
22 | n/a | def test_cint_struct(self): |
---|
23 | n/a | class X(Structure): |
---|
24 | n/a | _fields_ = [("a", c_int), |
---|
25 | n/a | ("b", c_int)] |
---|
26 | n/a | |
---|
27 | n/a | x = X() |
---|
28 | n/a | self.assertEqual(x._objects, None) |
---|
29 | n/a | x.a = 42 |
---|
30 | n/a | x.b = 99 |
---|
31 | n/a | self.assertEqual(x._objects, None) |
---|
32 | n/a | |
---|
33 | n/a | def test_ccharp_struct(self): |
---|
34 | n/a | class X(Structure): |
---|
35 | n/a | _fields_ = [("a", c_char_p), |
---|
36 | n/a | ("b", c_char_p)] |
---|
37 | n/a | x = X() |
---|
38 | n/a | self.assertEqual(x._objects, None) |
---|
39 | n/a | |
---|
40 | n/a | x.a = b"spam" |
---|
41 | n/a | x.b = b"foo" |
---|
42 | n/a | self.assertEqual(x._objects, {"0": b"spam", "1": b"foo"}) |
---|
43 | n/a | |
---|
44 | n/a | def test_struct_struct(self): |
---|
45 | n/a | class POINT(Structure): |
---|
46 | n/a | _fields_ = [("x", c_int), ("y", c_int)] |
---|
47 | n/a | class RECT(Structure): |
---|
48 | n/a | _fields_ = [("ul", POINT), ("lr", POINT)] |
---|
49 | n/a | |
---|
50 | n/a | r = RECT() |
---|
51 | n/a | r.ul.x = 0 |
---|
52 | n/a | r.ul.y = 1 |
---|
53 | n/a | r.lr.x = 2 |
---|
54 | n/a | r.lr.y = 3 |
---|
55 | n/a | self.assertEqual(r._objects, None) |
---|
56 | n/a | |
---|
57 | n/a | r = RECT() |
---|
58 | n/a | pt = POINT(1, 2) |
---|
59 | n/a | r.ul = pt |
---|
60 | n/a | self.assertEqual(r._objects, {'0': {}}) |
---|
61 | n/a | r.ul.x = 22 |
---|
62 | n/a | r.ul.y = 44 |
---|
63 | n/a | self.assertEqual(r._objects, {'0': {}}) |
---|
64 | n/a | r.lr = POINT() |
---|
65 | n/a | self.assertEqual(r._objects, {'0': {}, '1': {}}) |
---|
66 | n/a | |
---|
67 | n/a | class ArrayTestCase(unittest.TestCase): |
---|
68 | n/a | def test_cint_array(self): |
---|
69 | n/a | INTARR = c_int * 3 |
---|
70 | n/a | |
---|
71 | n/a | ia = INTARR() |
---|
72 | n/a | self.assertEqual(ia._objects, None) |
---|
73 | n/a | ia[0] = 1 |
---|
74 | n/a | ia[1] = 2 |
---|
75 | n/a | ia[2] = 3 |
---|
76 | n/a | self.assertEqual(ia._objects, None) |
---|
77 | n/a | |
---|
78 | n/a | class X(Structure): |
---|
79 | n/a | _fields_ = [("x", c_int), |
---|
80 | n/a | ("a", INTARR)] |
---|
81 | n/a | |
---|
82 | n/a | x = X() |
---|
83 | n/a | x.x = 1000 |
---|
84 | n/a | x.a[0] = 42 |
---|
85 | n/a | x.a[1] = 96 |
---|
86 | n/a | self.assertEqual(x._objects, None) |
---|
87 | n/a | x.a = ia |
---|
88 | n/a | self.assertEqual(x._objects, {'1': {}}) |
---|
89 | n/a | |
---|
90 | n/a | class PointerTestCase(unittest.TestCase): |
---|
91 | n/a | def test_p_cint(self): |
---|
92 | n/a | i = c_int(42) |
---|
93 | n/a | x = pointer(i) |
---|
94 | n/a | self.assertEqual(x._objects, {'1': i}) |
---|
95 | n/a | |
---|
96 | n/a | class DeletePointerTestCase(unittest.TestCase): |
---|
97 | n/a | @unittest.skip('test disabled') |
---|
98 | n/a | def test_X(self): |
---|
99 | n/a | class X(Structure): |
---|
100 | n/a | _fields_ = [("p", POINTER(c_char_p))] |
---|
101 | n/a | x = X() |
---|
102 | n/a | i = c_char_p("abc def") |
---|
103 | n/a | from sys import getrefcount as grc |
---|
104 | n/a | print("2?", grc(i)) |
---|
105 | n/a | x.p = pointer(i) |
---|
106 | n/a | print("3?", grc(i)) |
---|
107 | n/a | for i in range(320): |
---|
108 | n/a | c_int(99) |
---|
109 | n/a | x.p[0] |
---|
110 | n/a | print(x.p[0]) |
---|
111 | n/a | ## del x |
---|
112 | n/a | ## print "2?", grc(i) |
---|
113 | n/a | ## del i |
---|
114 | n/a | import gc |
---|
115 | n/a | gc.collect() |
---|
116 | n/a | for i in range(320): |
---|
117 | n/a | c_int(99) |
---|
118 | n/a | x.p[0] |
---|
119 | n/a | print(x.p[0]) |
---|
120 | n/a | print(x.p.contents) |
---|
121 | n/a | ## print x._objects |
---|
122 | n/a | |
---|
123 | n/a | x.p[0] = "spam spam" |
---|
124 | n/a | ## print x.p[0] |
---|
125 | n/a | print("+" * 42) |
---|
126 | n/a | print(x._objects) |
---|
127 | n/a | |
---|
128 | n/a | class PointerToStructure(unittest.TestCase): |
---|
129 | n/a | def test(self): |
---|
130 | n/a | class POINT(Structure): |
---|
131 | n/a | _fields_ = [("x", c_int), ("y", c_int)] |
---|
132 | n/a | class RECT(Structure): |
---|
133 | n/a | _fields_ = [("a", POINTER(POINT)), |
---|
134 | n/a | ("b", POINTER(POINT))] |
---|
135 | n/a | r = RECT() |
---|
136 | n/a | p1 = POINT(1, 2) |
---|
137 | n/a | |
---|
138 | n/a | r.a = pointer(p1) |
---|
139 | n/a | r.b = pointer(p1) |
---|
140 | n/a | ## from pprint import pprint as pp |
---|
141 | n/a | ## pp(p1._objects) |
---|
142 | n/a | ## pp(r._objects) |
---|
143 | n/a | |
---|
144 | n/a | r.a[0].x = 42 |
---|
145 | n/a | r.a[0].y = 99 |
---|
146 | n/a | |
---|
147 | n/a | # to avoid leaking when tests are run several times |
---|
148 | n/a | # clean up the types left in the cache. |
---|
149 | n/a | from ctypes import _pointer_type_cache |
---|
150 | n/a | del _pointer_type_cache[POINT] |
---|
151 | n/a | |
---|
152 | n/a | if __name__ == "__main__": |
---|
153 | n/a | unittest.main() |
---|