Debian Codegraph @

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

raw symbols SCIP
1 from ctypes import *
2 from ctypes.test import need_symbol
3 import unittest
4 import sys
5
6 class Test(unittest.TestCase):
7
8 def test_array2pointer(self):
9 array = (c_int * 3)(42, 17, 2)
10
11 # casting an array to a pointer works.
12 ptr = cast(array, POINTER(c_int))
13 self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
14
15 if 2*sizeof(c_short) == sizeof(c_int):
16 ptr = cast(array, POINTER(c_short))
17 if sys.byteorder == "little":
18 self.assertEqual([ptr[i] for i in range(6)],
19 [42, 0, 17, 0, 2, 0])
20 else:
21 self.assertEqual([ptr[i] for i in range(6)],
22 [0, 42, 0, 17, 0, 2])
23
24 def test_address2pointer(self):
25 array = (c_int * 3)(42, 17, 2)
26
27 address = addressof(array)
28 ptr = cast(c_void_p(address), POINTER(c_int))
29 self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
30
31 ptr = cast(address, POINTER(c_int))
32 self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
33
34 def test_p2a_objects(self):
35 array = (c_char_p * 5)()
36 self.assertEqual(array._objects, None)
37 array[0] = b"foo bar"
38 self.assertEqual(array._objects, {'0': b"foo bar"})
39
40 p = cast(array, POINTER(c_char_p))
41 # array and p share a common _objects attribute
42 self.assertIs(p._objects, array._objects)
43 self.assertEqual(array._objects, {'0': b"foo bar", id(array): array})
44 p[0] = b"spam spam"
45 self.assertEqual(p._objects, {'0': b"spam spam", id(array): array})
46 self.assertIs(array._objects, p._objects)
47 p[1] = b"foo bar"
48 self.assertEqual(p._objects, {'1': b'foo bar', '0': b"spam spam", id(array): array})
49 self.assertIs(array._objects, p._objects)
50
51 def test_other(self):
52 p = cast((c_int * 4)(1, 2, 3, 4), POINTER(c_int))
53 self.assertEqual(p[:4], [1,2, 3, 4])
54 self.assertEqual(p[:4:], [1, 2, 3, 4])
55 self.assertEqual(p[3:-1:-1], [4, 3, 2, 1])
56 self.assertEqual(p[:4:3], [1, 4])
57 c_int()
58 self.assertEqual(p[:4], [1, 2, 3, 4])
59 self.assertEqual(p[:4:], [1, 2, 3, 4])
60 self.assertEqual(p[3:-1:-1], [4, 3, 2, 1])
61 self.assertEqual(p[:4:3], [1, 4])
62 p[2] = 96
63 self.assertEqual(p[:4], [1, 2, 96, 4])
64 self.assertEqual(p[:4:], [1, 2, 96, 4])
65 self.assertEqual(p[3:-1:-1], [4, 96, 2, 1])
66 self.assertEqual(p[:4:3], [1, 4])
67 c_int()
68 self.assertEqual(p[:4], [1, 2, 96, 4])
69 self.assertEqual(p[:4:], [1, 2, 96, 4])
70 self.assertEqual(p[3:-1:-1], [4, 96, 2, 1])
71 self.assertEqual(p[:4:3], [1, 4])
72
73 def test_char_p(self):
74 # This didn't work: bad argument to internal function
75 s = c_char_p(b"hiho")
76 self.assertEqual(cast(cast(s, c_void_p), c_char_p).value,
77 b"hiho")
78
79 @need_symbol('c_wchar_p')
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 if __name__ == "__main__":
99 unittest.main()