Debian Codegraph @

Lib/test/test_ctypes/test_cast.py

raw
1 import sys
2 import unittest
3 from ctypes import (Structure, Union, POINTER, cast, sizeof, addressof,
4 c_void_p, c_char_p, c_wchar_p,
5 c_byte, c_short, c_int)
6
7
8 class Test(unittest.TestCase):
9 def test_array2pointer(self):
10 array = (c_int * 3)(42, 17, 2)
11
12 # casting an array to a pointer works.
13 ptr = cast(array, POINTER(c_int))
14 self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
15
16 if 2 * sizeof(c_short) == sizeof(c_int):
17 ptr = cast(array, POINTER(c_short))
18 if sys.byteorder == "little":
19 self.assertEqual([ptr[i] for i in range(6)],
20 [42, 0, 17, 0, 2, 0])
21 else:
22 self.assertEqual([ptr[i] for i in range(6)],
23 [0, 42, 0, 17, 0, 2])
24
25 def test_address2pointer(self):
26 array = (c_int * 3)(42, 17, 2)
27
28 address = addressof(array)
29 ptr = cast(c_void_p(address), POINTER(c_int))
30 self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
31
32 ptr = cast(address, POINTER(c_int))
33 self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
34
35 def test_p2a_objects(self):
36 array = (c_char_p * 5)()
37 self.assertEqual(array._objects, None)
38 array[0] = b"foo bar"
39 self.assertEqual(array._objects, {'0': b"foo bar"})
40
41 p = cast(array, POINTER(c_char_p))
42 # array and p share a common _objects attribute
43 self.assertIs(p._objects, array._objects)
44 self.assertEqual(array._objects, {'0': b"foo bar", id(array): array})
45 p[0] = b"spam spam"
46 self.assertEqual(p._objects, {'0': b"spam spam", id(array): array})
47 self.assertIs(array._objects, p._objects)
48 p[1] = b"foo bar"
49 self.assertEqual(p._objects, {'1': b'foo bar', '0': b"spam spam", id(array): array})
50 self.assertIs(array._objects, p._objects)
51
52 def test_other(self):
53 p = cast((c_int * 4)(1, 2, 3, 4), POINTER(c_int))
54 self.assertEqual(p[:4], [1,2, 3, 4])
55 self.assertEqual(p[:4:], [1, 2, 3, 4])
56 self.assertEqual(p[3:-1:-1], [4, 3, 2, 1])
57 self.assertEqual(p[:4:3], [1, 4])
58 c_int()
59 self.assertEqual(p[:4], [1, 2, 3, 4])
60 self.assertEqual(p[:4:], [1, 2, 3, 4])
61 self.assertEqual(p[3:-1:-1], [4, 3, 2, 1])
62 self.assertEqual(p[:4:3], [1, 4])
63 p[2] = 96
64 self.assertEqual(p[:4], [1, 2, 96, 4])
65 self.assertEqual(p[:4:], [1, 2, 96, 4])
66 self.assertEqual(p[3:-1:-1], [4, 96, 2, 1])
67 self.assertEqual(p[:4:3], [1, 4])
68 c_int()
69 self.assertEqual(p[:4], [1, 2, 96, 4])
70 self.assertEqual(p[:4:], [1, 2, 96, 4])
71 self.assertEqual(p[3:-1:-1], [4, 96, 2, 1])
72 self.assertEqual(p[:4:3], [1, 4])
73
74 def test_char_p(self):
75 # This didn't work: bad argument to internal function
76 s = c_char_p(b"hiho")
77 self.assertEqual(cast(cast(s, c_void_p), c_char_p).value,
78 b"hiho")
79
80 def test_wchar_p(self):
81 s = c_wchar_p("hiho")
82 self.assertEqual(cast(cast(s, c_void_p), c_wchar_p).value,
83 "hiho")
84
85 def test_bad_type_arg(self):
86 # The type argument must be a ctypes pointer type.
87 array_type = c_byte * sizeof(c_int)
88 array = array_type()
89 self.assertRaises(TypeError, cast, array, None)
90 self.assertRaises(TypeError, cast, array, array_type)
91 class Struct(Structure):
92 _fields_ = [("a", c_int)]
93 self.assertRaises(TypeError, cast, array, Struct)
94 class MyUnion(Union):
95 _fields_ = [("a", c_int)]
96 self.assertRaises(TypeError, cast, array, MyUnion)
97
98
99 if __name__ == "__main__":
100 unittest.main()