~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

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.failUnlessEqual(result, 139)
 
29
        self.failUnless(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.failUnlessEqual(pointer(v).contents.value, 42)
 
43
        result = f(self.wrap(pointer(v)))
 
44
        self.failUnlessEqual(type(result), POINTER(c_int))
 
45
        self.failUnlessEqual(result.contents.value, 42)
 
46
 
 
47
        # This on works...
 
48
        result = f(self.wrap(pointer(v)))
 
49
        self.failUnlessEqual(result.contents.value, v.value)
 
50
 
 
51
        p = pointer(c_int(99))
 
52
        result = f(self.wrap(p))
 
53
        self.failUnlessEqual(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.failUnlessEqual(args, expected)
 
71
 
 
72
    ################################################################
 
73
 
 
74
    def test_callbacks(self):
 
75
        f = dll._testfunc_callback_i_if
 
76
        f.restype = c_int
 
77
 
 
78
        MyCallback = CFUNCTYPE(c_int, c_int)
 
79
 
 
80
        def callback(value):
 
81
            #print "called back with", value
 
82
            return value
 
83
 
 
84
        cb = MyCallback(callback)
 
85
 
 
86
        result = f(self.wrap(-10), self.wrap(cb))
 
87
        self.failUnlessEqual(result, -18)
 
88
 
 
89
        # test with prototype
 
90
        f.argtypes = [c_int, MyCallback]
 
91
        cb = MyCallback(callback)
 
92
 
 
93
        result = f(self.wrap(-10), self.wrap(cb))
 
94
        self.failUnlessEqual(result, -18)
 
95
 
 
96
        result = f(self.wrap(-10), self.wrap(cb))
 
97
        self.failUnlessEqual(result, -18)
 
98
 
 
99
        AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int)
 
100
 
 
101
        # check that the prototype works: we call f with wrong
 
102
        # argument types
 
103
        cb = AnotherCallback(callback)
 
104
        self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb))
 
105
 
 
106
    def test_callbacks_2(self):
 
107
        # Can also use simple datatypes as argument type specifiers
 
108
        # for the callback function.
 
109
        # In this case the call receives an instance of that type
 
110
        f = dll._testfunc_callback_i_if
 
111
        f.restype = c_int
 
112
 
 
113
        MyCallback = CFUNCTYPE(c_int, c_int)
 
114
 
 
115
        f.argtypes = [c_int, MyCallback]
 
116
 
 
117
        def callback(value):
 
118
            #print "called back with", value
 
119
            self.failUnlessEqual(type(value), int)
 
120
            return value
 
121
 
 
122
        cb = MyCallback(callback)
 
123
        result = f(self.wrap(-10), self.wrap(cb))
 
124
        self.failUnlessEqual(result, -18)
 
125
 
 
126
    def test_longlong_callbacks(self):
 
127
 
 
128
        f = dll._testfunc_callback_q_qf
 
129
        f.restype = c_longlong
 
130
 
 
131
        MyCallback = CFUNCTYPE(c_longlong, c_longlong)
 
132
 
 
133
        f.argtypes = [c_longlong, MyCallback]
 
134
 
 
135
        def callback(value):
 
136
            self.failUnless(isinstance(value, int))
 
137
            return value & 0x7FFFFFFF
 
138
 
 
139
        cb = MyCallback(callback)
 
140
 
 
141
        self.failUnlessEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb))))
 
142
 
 
143
    def test_byval(self):
 
144
        # without prototype
 
145
        ptin = POINT(1, 2)
 
146
        ptout = POINT()
 
147
        # EXPORT int _testfunc_byval(point in, point *pout)
 
148
        result = dll._testfunc_byval(ptin, byref(ptout))
 
149
        got = result, ptout.x, ptout.y
 
150
        expected = 3, 1, 2
 
151
        self.failUnlessEqual(got, expected)
 
152
 
 
153
        # with prototype
 
154
        ptin = POINT(101, 102)
 
155
        ptout = POINT()
 
156
        dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
 
157
        dll._testfunc_byval.restype = c_int
 
158
        result = dll._testfunc_byval(self.wrap(ptin), byref(ptout))
 
159
        got = result, ptout.x, ptout.y
 
160
        expected = 203, 101, 102
 
161
        self.failUnlessEqual(got, expected)
 
162
 
 
163
    def test_struct_return_2H(self):
 
164
        class S2H(Structure):
 
165
            _fields_ = [("x", c_short),
 
166
                        ("y", c_short)]
 
167
        dll.ret_2h_func.restype = S2H
 
168
        dll.ret_2h_func.argtypes = [S2H]
 
169
        inp = S2H(99, 88)
 
170
        s2h = dll.ret_2h_func(self.wrap(inp))
 
171
        self.failUnlessEqual((s2h.x, s2h.y), (99*2, 88*3))
 
172
 
 
173
    def test_struct_return_8H(self):
 
174
        class S8I(Structure):
 
175
            _fields_ = [("a", c_int),
 
176
                        ("b", c_int),
 
177
                        ("c", c_int),
 
178
                        ("d", c_int),
 
179
                        ("e", c_int),
 
180
                        ("f", c_int),
 
181
                        ("g", c_int),
 
182
                        ("h", c_int)]
 
183
        dll.ret_8i_func.restype = S8I
 
184
        dll.ret_8i_func.argtypes = [S8I]
 
185
        inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
 
186
        s8i = dll.ret_8i_func(self.wrap(inp))
 
187
        self.failUnlessEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
 
188
                             (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
 
189
 
 
190
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
191
 
 
192
class AsParamWrapper(object):
 
193
    def __init__(self, param):
 
194
        self._as_parameter_ = param
 
195
 
 
196
class AsParamWrapperTestCase(BasicWrapTestCase):
 
197
    wrap = AsParamWrapper
 
198
 
 
199
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
200
 
 
201
class AsParamPropertyWrapper(object):
 
202
    def __init__(self, param):
 
203
        self._param = param
 
204
 
 
205
    def getParameter(self):
 
206
        return self._param
 
207
    _as_parameter_ = property(getParameter)
 
208
 
 
209
class AsParamPropertyWrapperTestCase(BasicWrapTestCase):
 
210
    wrap = AsParamPropertyWrapper
 
211
 
 
212
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
213
 
 
214
if __name__ == '__main__':
 
215
    unittest.main()