Debian Codegraph @

lib-python/3/ctypes/test/test_keeprefs.py

raw symbols SCIP
1 from ctypes import *
2 import unittest
3
4 class SimpleTestCase(unittest.TestCase):
5 def test_cint(self):
6 x = c_int()
7 self.assertEqual(x._objects, None)
8 x.value = 42
9 self.assertEqual(x._objects, None)
10 x = c_int(99)
11 self.assertEqual(x._objects, None)
12
13 def test_ccharp(self):
14 x = c_char_p()
15 self.assertEqual(x._objects, None)
16 x.value = b"abc"
17 self.assertEqual(x._objects, b"abc")
18 x = c_char_p(b"spam")
19 self.assertEqual(x._objects, b"spam")
20
21 class StructureTestCase(unittest.TestCase):
22 def test_cint_struct(self):
23 class X(Structure):
24 _fields_ = [("a", c_int),
25 ("b", c_int)]
26
27 x = X()
28 self.assertEqual(x._objects, None)
29 x.a = 42
30 x.b = 99
31 self.assertEqual(x._objects, None)
32
33 def test_ccharp_struct(self):
34 class X(Structure):
35 _fields_ = [("a", c_char_p),
36 ("b", c_char_p)]
37 x = X()
38 self.assertEqual(x._objects, None)
39
40 x.a = b"spam"
41 x.b = b"foo"
42 self.assertEqual(x._objects, {"0": b"spam", "1": b"foo"})
43
44 def test_struct_struct(self):
45 class POINT(Structure):
46 _fields_ = [("x", c_int), ("y", c_int)]
47 class RECT(Structure):
48 _fields_ = [("ul", POINT), ("lr", POINT)]
49
50 r = RECT()
51 r.ul.x = 0
52 r.ul.y = 1
53 r.lr.x = 2
54 r.lr.y = 3
55 self.assertEqual(r._objects, None)
56
57 r = RECT()
58 pt = POINT(1, 2)
59 r.ul = pt
60 self.assertEqual(r._objects, {'0': {}})
61 r.ul.x = 22
62 r.ul.y = 44
63 self.assertEqual(r._objects, {'0': {}})
64 r.lr = POINT()
65 self.assertEqual(r._objects, {'0': {}, '1': {}})
66
67 class ArrayTestCase(unittest.TestCase):
68 def test_cint_array(self):
69 INTARR = c_int * 3
70
71 ia = INTARR()
72 self.assertEqual(ia._objects, None)
73 ia[0] = 1
74 ia[1] = 2
75 ia[2] = 3
76 self.assertEqual(ia._objects, None)
77
78 class X(Structure):
79 _fields_ = [("x", c_int),
80 ("a", INTARR)]
81
82 x = X()
83 x.x = 1000
84 x.a[0] = 42
85 x.a[1] = 96
86 self.assertEqual(x._objects, None)
87 x.a = ia
88 self.assertEqual(x._objects, {'1': {}})
89
90 class PointerTestCase(unittest.TestCase):
91 def test_p_cint(self):
92 i = c_int(42)
93 x = pointer(i)
94 self.assertEqual(x._objects, {'1': i})
95
96
97 class PointerToStructure(unittest.TestCase):
98 def test(self):
99 class POINT(Structure):
100 _fields_ = [("x", c_int), ("y", c_int)]
101 class RECT(Structure):
102 _fields_ = [("a", POINTER(POINT)),
103 ("b", POINTER(POINT))]
104 r = RECT()
105 p1 = POINT(1, 2)
106
107 r.a = pointer(p1)
108 r.b = pointer(p1)
109 ## from pprint import pprint as pp
110 ## pp(p1._objects)
111 ## pp(r._objects)
112
113 r.a[0].x = 42
114 r.a[0].y = 99
115
116 # to avoid leaking when tests are run several times
117 # clean up the types left in the cache.
118 from ctypes import _pointer_type_cache
119 del _pointer_type_cache[POINT]
120
121 if __name__ == "__main__":
122 unittest.main()