~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Lib/ctypes/test/test_as_parameter.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import unittest
 
2
from ctypes import *
 
3
import _ctypes_test
 
4
 
 
5
dll = CDLL(_ctypes_test.__file__)
 
6
 
 
7
try:
 
8
    CALLBACK_FUNCTYPE = WINFUNCTYPE
 
9
except NameError:
 
10
    # fake to enable this test on Linux
 
11
    CALLBACK_FUNCTYPE = CFUNCTYPE
 
12
 
 
13
class POINT(Structure):
 
14
    _fields_ = [("x", c_int), ("y", c_int)]
 
15
 
 
16
class BasicWrapTestCase(unittest.TestCase):
 
17
    def wrap(self, param):
 
18
        return param
 
19
 
 
20
    def test_wchar_parm(self):
 
21
        try:
 
22
            c_wchar
 
23
        except NameError:
 
24
            return
 
25
        f = dll._testfunc_i_bhilfd
 
26
        f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
 
27
        result = f(self.wrap(1), self.wrap("x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0))
 
28
        self.assertEqual(result, 139)
 
29
        self.assertTrue(type(result), int)
 
30
 
 
31
    def test_pointers(self):
 
32
        f = dll._testfunc_p_p
 
33
        f.restype = POINTER(c_int)
 
34
        f.argtypes = [POINTER(c_int)]
 
35
 
 
36
        # This only works if the value c_int(42) passed to the
 
37
        # function is still alive while the pointer (the result) is
 
38
        # used.
 
39
 
 
40
        v = c_int(42)
 
41
 
 
42
        self.assertEqual(pointer(v).contents.value, 42)
 
43
        result = f(self.wrap(pointer(v)))
 
44
        self.assertEqual(type(result), POINTER(c_int))
 
45
        self.assertEqual(result.contents.value, 42)
 
46
 
 
47
        # This on works...
 
48
        result = f(self.wrap(pointer(v)))
 
49
        self.assertEqual(result.contents.value, v.value)
 
50
 
 
51
        p = pointer(c_int(99))
 
52
        result = f(self.wrap(p))
 
53
        self.assertEqual(result.contents.value, 99)
 
54
 
 
55
    def test_shorts(self):
 
56
        f = dll._testfunc_callback_i_if
 
57
 
 
58
        args = []
 
59
        expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
 
60
                    1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
 
61
 
 
62
        def callback(v):
 
63
            args.append(v)
 
64
            return v
 
65
 
 
66
        CallBack = CFUNCTYPE(c_int, c_int)
 
67
 
 
68
        cb = CallBack(callback)
 
69
        f(self.wrap(2**18), self.wrap(cb))
 
70
        self.assertEqual(args, expected)
 
71
 
 
72
    ################################################################
 
73
 
 
74
    def test_callbacks(self):
 
75
        f = dll._testfunc_callback_i_if
 
76
        f.restype = c_int
 
77
        f.argtypes = None
 
78
 
 
79
        MyCallback = CFUNCTYPE(c_int, c_int)
 
80
 
 
81
        def callback(value):
 
82
            #print "called back with", value
 
83
            return value
 
84
 
 
85
        cb = MyCallback(callback)
 
86
 
 
87
        result = f(self.wrap(-10), self.wrap(cb))
 
88
        self.assertEqual(result, -18)
 
89
 
 
90
        # test with prototype
 
91
        f.argtypes = [c_int, MyCallback]
 
92
        cb = MyCallback(callback)
 
93
 
 
94
        result = f(self.wrap(-10), self.wrap(cb))
 
95
        self.assertEqual(result, -18)
 
96
 
 
97
        result = f(self.wrap(-10), self.wrap(cb))
 
98
        self.assertEqual(result, -18)
 
99
 
 
100
        AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int)
 
101
 
 
102
        # check that the prototype works: we call f with wrong
 
103
        # argument types
 
104
        cb = AnotherCallback(callback)
 
105
        self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb))
 
106
 
 
107
    def test_callbacks_2(self):
 
108
        # Can also use simple datatypes as argument type specifiers
 
109
        # for the callback function.
 
110
        # In this case the call receives an instance of that type
 
111
        f = dll._testfunc_callback_i_if
 
112
        f.restype = c_int
 
113
 
 
114
        MyCallback = CFUNCTYPE(c_int, c_int)
 
115
 
 
116
        f.argtypes = [c_int, MyCallback]
 
117
 
 
118
        def callback(value):
 
119
            #print "called back with", value
 
120
            self.assertEqual(type(value), int)
 
121
            return value
 
122
 
 
123
        cb = MyCallback(callback)
 
124
        result = f(self.wrap(-10), self.wrap(cb))
 
125
        self.assertEqual(result, -18)
 
126
 
 
127
    def test_longlong_callbacks(self):
 
128
 
 
129
        f = dll._testfunc_callback_q_qf
 
130
        f.restype = c_longlong
 
131
 
 
132
        MyCallback = CFUNCTYPE(c_longlong, c_longlong)
 
133
 
 
134
        f.argtypes = [c_longlong, MyCallback]
 
135
 
 
136
        def callback(value):
 
137
            self.assertIsInstance(value, int)
 
138
            return value & 0x7FFFFFFF
 
139
 
 
140
        cb = MyCallback(callback)
 
141
 
 
142
        self.assertEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb))))
 
143
 
 
144
    def test_byval(self):
 
145
        # without prototype
 
146
        ptin = POINT(1, 2)
 
147
        ptout = POINT()
 
148
        # EXPORT int _testfunc_byval(point in, point *pout)
 
149
        result = dll._testfunc_byval(ptin, byref(ptout))
 
150
        got = result, ptout.x, ptout.y
 
151
        expected = 3, 1, 2
 
152
        self.assertEqual(got, expected)
 
153
 
 
154
        # with prototype
 
155
        ptin = POINT(101, 102)
 
156
        ptout = POINT()
 
157
        dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
 
158
        dll._testfunc_byval.restype = c_int
 
159
        result = dll._testfunc_byval(self.wrap(ptin), byref(ptout))
 
160
        got = result, ptout.x, ptout.y
 
161
        expected = 203, 101, 102
 
162
        self.assertEqual(got, expected)
 
163
 
 
164
    def test_struct_return_2H(self):
 
165
        class S2H(Structure):
 
166
            _fields_ = [("x", c_short),
 
167
                        ("y", c_short)]
 
168
        dll.ret_2h_func.restype = S2H
 
169
        dll.ret_2h_func.argtypes = [S2H]
 
170
        inp = S2H(99, 88)
 
171
        s2h = dll.ret_2h_func(self.wrap(inp))
 
172
        self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
 
173
 
 
174
    def test_struct_return_8H(self):
 
175
        class S8I(Structure):
 
176
            _fields_ = [("a", c_int),
 
177
                        ("b", c_int),
 
178
                        ("c", c_int),
 
179
                        ("d", c_int),
 
180
                        ("e", c_int),
 
181
                        ("f", c_int),
 
182
                        ("g", c_int),
 
183
                        ("h", c_int)]
 
184
        dll.ret_8i_func.restype = S8I
 
185
        dll.ret_8i_func.argtypes = [S8I]
 
186
        inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
 
187
        s8i = dll.ret_8i_func(self.wrap(inp))
 
188
        self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
 
189
                             (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
 
190
 
 
191
    def test_recursive_as_param(self):
 
192
        from ctypes import c_int
 
193
 
 
194
        class A(object):
 
195
            pass
 
196
 
 
197
        a = A()
 
198
        a._as_parameter_ = a
 
199
        with self.assertRaises(RuntimeError):
 
200
            c_int.from_param(a)
 
201
 
 
202
 
 
203
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
204
 
 
205
class AsParamWrapper(object):
 
206
    def __init__(self, param):
 
207
        self._as_parameter_ = param
 
208
 
 
209
class AsParamWrapperTestCase(BasicWrapTestCase):
 
210
    wrap = AsParamWrapper
 
211
 
 
212
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
213
 
 
214
class AsParamPropertyWrapper(object):
 
215
    def __init__(self, param):
 
216
        self._param = param
 
217
 
 
218
    def getParameter(self):
 
219
        return self._param
 
220
    _as_parameter_ = property(getParameter)
 
221
 
 
222
class AsParamPropertyWrapperTestCase(BasicWrapTestCase):
 
223
    wrap = AsParamPropertyWrapper
 
224
 
 
225
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
226
 
 
227
if __name__ == '__main__':
 
228
    unittest.main()