1 | n/a | import unittest |
---|
2 | n/a | from ctypes.test import need_symbol |
---|
3 | n/a | |
---|
4 | n/a | class SimpleTypesTestCase(unittest.TestCase): |
---|
5 | n/a | |
---|
6 | n/a | def setUp(self): |
---|
7 | n/a | import ctypes |
---|
8 | n/a | try: |
---|
9 | n/a | from _ctypes import set_conversion_mode |
---|
10 | n/a | except ImportError: |
---|
11 | n/a | pass |
---|
12 | n/a | else: |
---|
13 | n/a | self.prev_conv_mode = set_conversion_mode("ascii", "strict") |
---|
14 | n/a | |
---|
15 | n/a | def tearDown(self): |
---|
16 | n/a | try: |
---|
17 | n/a | from _ctypes import set_conversion_mode |
---|
18 | n/a | except ImportError: |
---|
19 | n/a | pass |
---|
20 | n/a | else: |
---|
21 | n/a | set_conversion_mode(*self.prev_conv_mode) |
---|
22 | n/a | |
---|
23 | n/a | def test_subclasses(self): |
---|
24 | n/a | from ctypes import c_void_p, c_char_p |
---|
25 | n/a | # ctypes 0.9.5 and before did overwrite from_param in SimpleType_new |
---|
26 | n/a | class CVOIDP(c_void_p): |
---|
27 | n/a | def from_param(cls, value): |
---|
28 | n/a | return value * 2 |
---|
29 | n/a | from_param = classmethod(from_param) |
---|
30 | n/a | |
---|
31 | n/a | class CCHARP(c_char_p): |
---|
32 | n/a | def from_param(cls, value): |
---|
33 | n/a | return value * 4 |
---|
34 | n/a | from_param = classmethod(from_param) |
---|
35 | n/a | |
---|
36 | n/a | self.assertEqual(CVOIDP.from_param("abc"), "abcabc") |
---|
37 | n/a | self.assertEqual(CCHARP.from_param("abc"), "abcabcabcabc") |
---|
38 | n/a | |
---|
39 | n/a | @need_symbol('c_wchar_p') |
---|
40 | n/a | def test_subclasses_c_wchar_p(self): |
---|
41 | n/a | from ctypes import c_wchar_p |
---|
42 | n/a | |
---|
43 | n/a | class CWCHARP(c_wchar_p): |
---|
44 | n/a | def from_param(cls, value): |
---|
45 | n/a | return value * 3 |
---|
46 | n/a | from_param = classmethod(from_param) |
---|
47 | n/a | |
---|
48 | n/a | self.assertEqual(CWCHARP.from_param("abc"), "abcabcabc") |
---|
49 | n/a | |
---|
50 | n/a | # XXX Replace by c_char_p tests |
---|
51 | n/a | def test_cstrings(self): |
---|
52 | n/a | from ctypes import c_char_p |
---|
53 | n/a | |
---|
54 | n/a | # c_char_p.from_param on a Python String packs the string |
---|
55 | n/a | # into a cparam object |
---|
56 | n/a | s = b"123" |
---|
57 | n/a | self.assertIs(c_char_p.from_param(s)._obj, s) |
---|
58 | n/a | |
---|
59 | n/a | # new in 0.9.1: convert (encode) unicode to ascii |
---|
60 | n/a | self.assertEqual(c_char_p.from_param(b"123")._obj, b"123") |
---|
61 | n/a | self.assertRaises(TypeError, c_char_p.from_param, "123\377") |
---|
62 | n/a | self.assertRaises(TypeError, c_char_p.from_param, 42) |
---|
63 | n/a | |
---|
64 | n/a | # calling c_char_p.from_param with a c_char_p instance |
---|
65 | n/a | # returns the argument itself: |
---|
66 | n/a | a = c_char_p(b"123") |
---|
67 | n/a | self.assertIs(c_char_p.from_param(a), a) |
---|
68 | n/a | |
---|
69 | n/a | @need_symbol('c_wchar_p') |
---|
70 | n/a | def test_cw_strings(self): |
---|
71 | n/a | from ctypes import c_wchar_p |
---|
72 | n/a | |
---|
73 | n/a | c_wchar_p.from_param("123") |
---|
74 | n/a | |
---|
75 | n/a | self.assertRaises(TypeError, c_wchar_p.from_param, 42) |
---|
76 | n/a | self.assertRaises(TypeError, c_wchar_p.from_param, b"123\377") |
---|
77 | n/a | |
---|
78 | n/a | pa = c_wchar_p.from_param(c_wchar_p("123")) |
---|
79 | n/a | self.assertEqual(type(pa), c_wchar_p) |
---|
80 | n/a | |
---|
81 | n/a | def test_int_pointers(self): |
---|
82 | n/a | from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer |
---|
83 | n/a | LPINT = POINTER(c_int) |
---|
84 | n/a | |
---|
85 | n/a | ## p = pointer(c_int(42)) |
---|
86 | n/a | ## x = LPINT.from_param(p) |
---|
87 | n/a | x = LPINT.from_param(pointer(c_int(42))) |
---|
88 | n/a | self.assertEqual(x.contents.value, 42) |
---|
89 | n/a | self.assertEqual(LPINT(c_int(42)).contents.value, 42) |
---|
90 | n/a | |
---|
91 | n/a | self.assertEqual(LPINT.from_param(None), None) |
---|
92 | n/a | |
---|
93 | n/a | if c_int != c_long: |
---|
94 | n/a | self.assertRaises(TypeError, LPINT.from_param, pointer(c_long(42))) |
---|
95 | n/a | self.assertRaises(TypeError, LPINT.from_param, pointer(c_uint(42))) |
---|
96 | n/a | self.assertRaises(TypeError, LPINT.from_param, pointer(c_short(42))) |
---|
97 | n/a | |
---|
98 | n/a | def test_byref_pointer(self): |
---|
99 | n/a | # The from_param class method of POINTER(typ) classes accepts what is |
---|
100 | n/a | # returned by byref(obj), it type(obj) == typ |
---|
101 | n/a | from ctypes import c_short, c_uint, c_int, c_long, POINTER, byref |
---|
102 | n/a | LPINT = POINTER(c_int) |
---|
103 | n/a | |
---|
104 | n/a | LPINT.from_param(byref(c_int(42))) |
---|
105 | n/a | |
---|
106 | n/a | self.assertRaises(TypeError, LPINT.from_param, byref(c_short(22))) |
---|
107 | n/a | if c_int != c_long: |
---|
108 | n/a | self.assertRaises(TypeError, LPINT.from_param, byref(c_long(22))) |
---|
109 | n/a | self.assertRaises(TypeError, LPINT.from_param, byref(c_uint(22))) |
---|
110 | n/a | |
---|
111 | n/a | def test_byref_pointerpointer(self): |
---|
112 | n/a | # See above |
---|
113 | n/a | from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref |
---|
114 | n/a | |
---|
115 | n/a | LPLPINT = POINTER(POINTER(c_int)) |
---|
116 | n/a | LPLPINT.from_param(byref(pointer(c_int(42)))) |
---|
117 | n/a | |
---|
118 | n/a | self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_short(22)))) |
---|
119 | n/a | if c_int != c_long: |
---|
120 | n/a | self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_long(22)))) |
---|
121 | n/a | self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_uint(22)))) |
---|
122 | n/a | |
---|
123 | n/a | def test_array_pointers(self): |
---|
124 | n/a | from ctypes import c_short, c_uint, c_int, c_long, POINTER |
---|
125 | n/a | INTARRAY = c_int * 3 |
---|
126 | n/a | ia = INTARRAY() |
---|
127 | n/a | self.assertEqual(len(ia), 3) |
---|
128 | n/a | self.assertEqual([ia[i] for i in range(3)], [0, 0, 0]) |
---|
129 | n/a | |
---|
130 | n/a | # Pointers are only compatible with arrays containing items of |
---|
131 | n/a | # the same type! |
---|
132 | n/a | LPINT = POINTER(c_int) |
---|
133 | n/a | LPINT.from_param((c_int*3)()) |
---|
134 | n/a | self.assertRaises(TypeError, LPINT.from_param, c_short*3) |
---|
135 | n/a | self.assertRaises(TypeError, LPINT.from_param, c_long*3) |
---|
136 | n/a | self.assertRaises(TypeError, LPINT.from_param, c_uint*3) |
---|
137 | n/a | |
---|
138 | n/a | def test_noctypes_argtype(self): |
---|
139 | n/a | import _ctypes_test |
---|
140 | n/a | from ctypes import CDLL, c_void_p, ArgumentError |
---|
141 | n/a | |
---|
142 | n/a | func = CDLL(_ctypes_test.__file__)._testfunc_p_p |
---|
143 | n/a | func.restype = c_void_p |
---|
144 | n/a | # TypeError: has no from_param method |
---|
145 | n/a | self.assertRaises(TypeError, setattr, func, "argtypes", (object,)) |
---|
146 | n/a | |
---|
147 | n/a | class Adapter(object): |
---|
148 | n/a | def from_param(cls, obj): |
---|
149 | n/a | return None |
---|
150 | n/a | |
---|
151 | n/a | func.argtypes = (Adapter(),) |
---|
152 | n/a | self.assertEqual(func(None), None) |
---|
153 | n/a | self.assertEqual(func(object()), None) |
---|
154 | n/a | |
---|
155 | n/a | class Adapter(object): |
---|
156 | n/a | def from_param(cls, obj): |
---|
157 | n/a | return obj |
---|
158 | n/a | |
---|
159 | n/a | func.argtypes = (Adapter(),) |
---|
160 | n/a | # don't know how to convert parameter 1 |
---|
161 | n/a | self.assertRaises(ArgumentError, func, object()) |
---|
162 | n/a | self.assertEqual(func(c_void_p(42)), 42) |
---|
163 | n/a | |
---|
164 | n/a | class Adapter(object): |
---|
165 | n/a | def from_param(cls, obj): |
---|
166 | n/a | raise ValueError(obj) |
---|
167 | n/a | |
---|
168 | n/a | func.argtypes = (Adapter(),) |
---|
169 | n/a | # ArgumentError: argument 1: ValueError: 99 |
---|
170 | n/a | self.assertRaises(ArgumentError, func, 99) |
---|
171 | n/a | |
---|
172 | n/a | |
---|
173 | n/a | ################################################################ |
---|
174 | n/a | |
---|
175 | n/a | if __name__ == '__main__': |
---|
176 | n/a | unittest.main() |
---|