blob: 9762fb915d821c3fd9a1322f9ac471ac20a836ae [file] [log] [blame]
Thomas Hellerbabddfc2006-03-08 19:56:54 +00001import unittest, sys
2
3class SimpleTypesTestCase(unittest.TestCase):
4
5 def setUp(self):
6 import ctypes
7 try:
8 from _ctypes import set_conversion_mode
9 except ImportError:
10 pass
11 else:
12 self.prev_conv_mode = set_conversion_mode("ascii", "strict")
13
14 def tearDown(self):
15 try:
16 from _ctypes import set_conversion_mode
17 except ImportError:
18 pass
19 else:
20 set_conversion_mode(*self.prev_conv_mode)
Tim Peterse8d09e52006-03-09 01:15:05 +000021
Thomas Hellerbabddfc2006-03-08 19:56:54 +000022 def test_subclasses(self):
23 from ctypes import c_void_p, c_char_p
24 # ctypes 0.9.5 and before did overwrite from_param in SimpleType_new
25 class CVOIDP(c_void_p):
26 def from_param(cls, value):
27 return value * 2
28 from_param = classmethod(from_param)
29
30 class CCHARP(c_char_p):
31 def from_param(cls, value):
32 return value * 4
33 from_param = classmethod(from_param)
34
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000035 self.assertEqual(CVOIDP.from_param("abc"), "abcabc")
36 self.assertEqual(CCHARP.from_param("abc"), "abcabcabcabc")
Thomas Hellerbabddfc2006-03-08 19:56:54 +000037
38 try:
39 from ctypes import c_wchar_p
40 except ImportError:
41 return
42
43 class CWCHARP(c_wchar_p):
44 def from_param(cls, value):
45 return value * 3
46 from_param = classmethod(from_param)
47
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000048 self.assertEqual(CWCHARP.from_param("abc"), "abcabcabc")
Thomas Hellerbabddfc2006-03-08 19:56:54 +000049
50 # XXX Replace by c_char_p tests
51 def test_cstrings(self):
52 from ctypes import c_char_p, byref
53
54 # c_char_p.from_param on a Python String packs the string
55 # into a cparam object
Thomas Heller3d79dd92007-07-12 19:33:24 +000056 s = b"123"
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000057 self.assertTrue(c_char_p.from_param(s)._obj is s)
Thomas Hellerbabddfc2006-03-08 19:56:54 +000058
59 # new in 0.9.1: convert (encode) unicode to ascii
Victor Stinnercf448832010-07-28 00:15:03 +000060 self.assertEqual(c_char_p.from_param(b"123")._obj, b"123")
61 self.assertRaises(TypeError, c_char_p.from_param, "123\377")
Thomas Hellerbabddfc2006-03-08 19:56:54 +000062 self.assertRaises(TypeError, c_char_p.from_param, 42)
63
64 # calling c_char_p.from_param with a c_char_p instance
65 # returns the argument itself:
Victor Stinner42746df2010-07-27 23:36:41 +000066 a = c_char_p(b"123")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000067 self.assertTrue(c_char_p.from_param(a) is a)
Thomas Hellerbabddfc2006-03-08 19:56:54 +000068
69 def test_cw_strings(self):
Daniel Stutzbach061b14a2010-08-31 15:45:04 +000070 from ctypes import byref
Thomas Hellerbabddfc2006-03-08 19:56:54 +000071 try:
Daniel Stutzbach061b14a2010-08-31 15:45:04 +000072 from ctypes import c_wchar_p
Thomas Hellerbabddfc2006-03-08 19:56:54 +000073 except ImportError:
74## print "(No c_wchar_p)"
75 return
Daniel Stutzbach061b14a2010-08-31 15:45:04 +000076
Victor Stinnerd83f2262011-09-28 13:36:20 +020077 c_wchar_p.from_param("123")
78
79 self.assertRaises(TypeError, c_wchar_p.from_param, 42)
Victor Stinnercf448832010-07-28 00:15:03 +000080 self.assertRaises(TypeError, c_wchar_p.from_param, b"123\377")
Thomas Hellerbabddfc2006-03-08 19:56:54 +000081
Guido van Rossumef87d6e2007-05-02 19:09:54 +000082 pa = c_wchar_p.from_param(c_wchar_p("123"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000083 self.assertEqual(type(pa), c_wchar_p)
Thomas Hellerbabddfc2006-03-08 19:56:54 +000084
85 def test_int_pointers(self):
86 from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer
87 LPINT = POINTER(c_int)
88
89## p = pointer(c_int(42))
90## x = LPINT.from_param(p)
91 x = LPINT.from_param(pointer(c_int(42)))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000092 self.assertEqual(x.contents.value, 42)
93 self.assertEqual(LPINT(c_int(42)).contents.value, 42)
Thomas Hellerbabddfc2006-03-08 19:56:54 +000094
Thomas Heller69b639f2009-09-18 20:08:39 +000095 self.assertEqual(LPINT.from_param(None), None)
Thomas Hellerbabddfc2006-03-08 19:56:54 +000096
97 if c_int != c_long:
98 self.assertRaises(TypeError, LPINT.from_param, pointer(c_long(42)))
99 self.assertRaises(TypeError, LPINT.from_param, pointer(c_uint(42)))
100 self.assertRaises(TypeError, LPINT.from_param, pointer(c_short(42)))
101
102 def test_byref_pointer(self):
103 # The from_param class method of POINTER(typ) classes accepts what is
104 # returned by byref(obj), it type(obj) == typ
105 from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref
106 LPINT = POINTER(c_int)
107
108 LPINT.from_param(byref(c_int(42)))
109
110 self.assertRaises(TypeError, LPINT.from_param, byref(c_short(22)))
111 if c_int != c_long:
112 self.assertRaises(TypeError, LPINT.from_param, byref(c_long(22)))
113 self.assertRaises(TypeError, LPINT.from_param, byref(c_uint(22)))
114
115 def test_byref_pointerpointer(self):
116 # See above
117 from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref
118
119 LPLPINT = POINTER(POINTER(c_int))
120 LPLPINT.from_param(byref(pointer(c_int(42))))
121
122 self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_short(22))))
123 if c_int != c_long:
124 self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_long(22))))
125 self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_uint(22))))
126
127 def test_array_pointers(self):
128 from ctypes import c_short, c_uint, c_int, c_long, POINTER
129 INTARRAY = c_int * 3
130 ia = INTARRAY()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000131 self.assertEqual(len(ia), 3)
132 self.assertEqual([ia[i] for i in range(3)], [0, 0, 0])
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000133
134 # Pointers are only compatible with arrays containing items of
135 # the same type!
136 LPINT = POINTER(c_int)
137 LPINT.from_param((c_int*3)())
138 self.assertRaises(TypeError, LPINT.from_param, c_short*3)
139 self.assertRaises(TypeError, LPINT.from_param, c_long*3)
140 self.assertRaises(TypeError, LPINT.from_param, c_uint*3)
141
142## def test_performance(self):
143## check_perf()
144
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145 def test_noctypes_argtype(self):
146 import _ctypes_test
147 from ctypes import CDLL, c_void_p, ArgumentError
148
149 func = CDLL(_ctypes_test.__file__)._testfunc_p_p
150 func.restype = c_void_p
151 # TypeError: has no from_param method
152 self.assertRaises(TypeError, setattr, func, "argtypes", (object,))
153
154 class Adapter(object):
155 def from_param(cls, obj):
156 return None
157
158 func.argtypes = (Adapter(),)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000159 self.assertEqual(func(None), None)
160 self.assertEqual(func(object()), None)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000161
162 class Adapter(object):
163 def from_param(cls, obj):
164 return obj
165
166 func.argtypes = (Adapter(),)
167 # don't know how to convert parameter 1
168 self.assertRaises(ArgumentError, func, object())
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000169 self.assertEqual(func(c_void_p(42)), 42)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000170
171 class Adapter(object):
172 def from_param(cls, obj):
173 raise ValueError(obj)
174
175 func.argtypes = (Adapter(),)
176 # ArgumentError: argument 1: ValueError: 99
177 self.assertRaises(ArgumentError, func, 99)
178
179
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000180################################################################
181
182if __name__ == '__main__':
183 unittest.main()