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