Debian Codegraph @

Lib/test/test_ctypes/test_incomplete.py

raw
1 import ctypes
2 import unittest
3 import warnings
4 from ctypes import Structure, POINTER, pointer, c_char_p
5
6
7 # The incomplete pointer example from the tutorial
8 class TestSetPointerType(unittest.TestCase):
9 def tearDown(self):
10 # to not leak references, we must clean _pointer_type_cache
11 ctypes._reset_cache()
12
13 def test_incomplete_example(self):
14 lpcell = POINTER("cell")
15 class cell(Structure):
16 _fields_ = [("name", c_char_p),
17 ("next", lpcell)]
18
19 with warnings.catch_warnings():
20 warnings.simplefilter('ignore', DeprecationWarning)
21 ctypes.SetPointerType(lpcell, cell)
22
23 c1 = cell()
24 c1.name = b"foo"
25 c2 = cell()
26 c2.name = b"bar"
27
28 c1.next = pointer(c2)
29 c2.next = pointer(c1)
30
31 p = c1
32
33 result = []
34 for i in range(8):
35 result.append(p.name)
36 p = p.next[0]
37 self.assertEqual(result, [b"foo", b"bar"] * 4)
38
39 def test_deprecation(self):
40 lpcell = POINTER("cell")
41 class cell(Structure):
42 _fields_ = [("name", c_char_p),
43 ("next", lpcell)]
44
45 with self.assertWarns(DeprecationWarning):
46 ctypes.SetPointerType(lpcell, cell)
47
48
49 if __name__ == '__main__':
50 unittest.main()