Debian Codegraph @

cpython27/Lib/ctypes/test/test_parameters.py

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