Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
ctypes.test.test_as_parameter.AsParamWrapperTestCase Class Reference
Inheritance diagram for ctypes.test.test_as_parameter.AsParamWrapperTestCase:
Inheritance graph
[legend]
Collaboration diagram for ctypes.test.test_as_parameter.AsParamWrapperTestCase:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def wrap
def test_wchar_parm
def test_pointers
def test_shorts
def test_callbacks
def test_callbacks_2
def test_longlong_callbacks
def test_byval
def test_struct_return_2H
def test_struct_return_8H
def test_recursive_as_param

Static Public Attributes

 wrap = AsParamWrapper

Detailed Description

Definition at line 208 of file test_as_parameter.py.


Member Function Documentation

Definition at line 143 of file test_as_parameter.py.

00143 
00144     def test_byval(self):
00145         # without prototype
00146         ptin = POINT(1, 2)
00147         ptout = POINT()
00148         # EXPORT int _testfunc_byval(point in, point *pout)
00149         result = dll._testfunc_byval(ptin, byref(ptout))
00150         got = result, ptout.x, ptout.y
00151         expected = 3, 1, 2
00152         self.assertEqual(got, expected)
00153 
00154         # with prototype
00155         ptin = POINT(101, 102)
00156         ptout = POINT()
00157         dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
00158         dll._testfunc_byval.restype = c_int
00159         result = dll._testfunc_byval(self.wrap(ptin), byref(ptout))
00160         got = result, ptout.x, ptout.y
00161         expected = 203, 101, 102
00162         self.assertEqual(got, expected)

Here is the call graph for this function:

Definition at line 74 of file test_as_parameter.py.

00074 
00075     def test_callbacks(self):
00076         f = dll._testfunc_callback_i_if
00077         f.restype = c_int
00078 
00079         MyCallback = CFUNCTYPE(c_int, c_int)
00080 
00081         def callback(value):
00082             #print "called back with", value
00083             return value
00084 
00085         cb = MyCallback(callback)
00086 
00087         result = f(self.wrap(-10), self.wrap(cb))
00088         self.assertEqual(result, -18)
00089 
00090         # test with prototype
00091         f.argtypes = [c_int, MyCallback]
00092         cb = MyCallback(callback)
00093 
00094         result = f(self.wrap(-10), self.wrap(cb))
00095         self.assertEqual(result, -18)
00096 
00097         result = f(self.wrap(-10), self.wrap(cb))
00098         self.assertEqual(result, -18)
00099 
00100         AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int)
00101 
00102         # check that the prototype works: we call f with wrong
00103         # argument types
00104         cb = AnotherCallback(callback)
00105         self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb))

Here is the call graph for this function:

Definition at line 106 of file test_as_parameter.py.

00106 
00107     def test_callbacks_2(self):
00108         # Can also use simple datatypes as argument type specifiers
00109         # for the callback function.
00110         # In this case the call receives an instance of that type
00111         f = dll._testfunc_callback_i_if
00112         f.restype = c_int
00113 
00114         MyCallback = CFUNCTYPE(c_int, c_int)
00115 
00116         f.argtypes = [c_int, MyCallback]
00117 
00118         def callback(value):
00119             #print "called back with", value
00120             self.assertEqual(type(value), int)
00121             return value
00122 
00123         cb = MyCallback(callback)
00124         result = f(self.wrap(-10), self.wrap(cb))
00125         self.assertEqual(result, -18)

Here is the call graph for this function:

Definition at line 126 of file test_as_parameter.py.

00126 
00127     def test_longlong_callbacks(self):
00128 
00129         f = dll._testfunc_callback_q_qf
00130         f.restype = c_longlong
00131 
00132         MyCallback = CFUNCTYPE(c_longlong, c_longlong)
00133 
00134         f.argtypes = [c_longlong, MyCallback]
00135 
00136         def callback(value):
00137             self.assertTrue(isinstance(value, int))
00138             return value & 0x7FFFFFFF
00139 
00140         cb = MyCallback(callback)
00141 
00142         self.assertEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb))))

Here is the call graph for this function:

Definition at line 31 of file test_as_parameter.py.

00031 
00032     def test_pointers(self):
00033         f = dll._testfunc_p_p
00034         f.restype = POINTER(c_int)
00035         f.argtypes = [POINTER(c_int)]
00036 
00037         # This only works if the value c_int(42) passed to the
00038         # function is still alive while the pointer (the result) is
00039         # used.
00040 
00041         v = c_int(42)
00042 
00043         self.assertEqual(pointer(v).contents.value, 42)
00044         result = f(self.wrap(pointer(v)))
00045         self.assertEqual(type(result), POINTER(c_int))
00046         self.assertEqual(result.contents.value, 42)
00047 
00048         # This on works...
00049         result = f(self.wrap(pointer(v)))
00050         self.assertEqual(result.contents.value, v.value)
00051 
00052         p = pointer(c_int(99))
00053         result = f(self.wrap(p))
00054         self.assertEqual(result.contents.value, 99)

Here is the call graph for this function:

Definition at line 190 of file test_as_parameter.py.

00190 
00191     def test_recursive_as_param(self):
00192         from ctypes import c_int
00193 
00194         class A(object):
00195             pass
00196 
00197         a = A()
00198         a._as_parameter_ = a
00199         with self.assertRaises(RuntimeError):
00200             c_int.from_param(a)
00201 
00202 
00203 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Definition at line 55 of file test_as_parameter.py.

00055 
00056     def test_shorts(self):
00057         f = dll._testfunc_callback_i_if
00058 
00059         args = []
00060         expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
00061                     1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
00062 
00063         def callback(v):
00064             args.append(v)
00065             return v
00066 
00067         CallBack = CFUNCTYPE(c_int, c_int)
00068 
00069         cb = CallBack(callback)
00070         f(self.wrap(2**18), self.wrap(cb))
00071         self.assertEqual(args, expected)

Here is the call graph for this function:

Definition at line 163 of file test_as_parameter.py.

00163 
00164     def test_struct_return_2H(self):
00165         class S2H(Structure):
00166             _fields_ = [("x", c_short),
00167                         ("y", c_short)]
00168         dll.ret_2h_func.restype = S2H
00169         dll.ret_2h_func.argtypes = [S2H]
00170         inp = S2H(99, 88)
00171         s2h = dll.ret_2h_func(self.wrap(inp))
00172         self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))

Here is the call graph for this function:

Definition at line 173 of file test_as_parameter.py.

00173 
00174     def test_struct_return_8H(self):
00175         class S8I(Structure):
00176             _fields_ = [("a", c_int),
00177                         ("b", c_int),
00178                         ("c", c_int),
00179                         ("d", c_int),
00180                         ("e", c_int),
00181                         ("f", c_int),
00182                         ("g", c_int),
00183                         ("h", c_int)]
00184         dll.ret_8i_func.restype = S8I
00185         dll.ret_8i_func.argtypes = [S8I]
00186         inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
00187         s8i = dll.ret_8i_func(self.wrap(inp))
00188         self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
00189                              (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))

Here is the call graph for this function:

Definition at line 20 of file test_as_parameter.py.

00020 
00021     def test_wchar_parm(self):
00022         try:
00023             c_wchar
00024         except NameError:
00025             return
00026         f = dll._testfunc_i_bhilfd
00027         f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
00028         result = f(self.wrap(1), self.wrap("x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0))
00029         self.assertEqual(result, 139)
00030         self.assertTrue(type(result), int)

Here is the call graph for this function:

def ctypes.test.test_as_parameter.BasicWrapTestCase.wrap (   self,
  param 
) [inherited]

Definition at line 17 of file test_as_parameter.py.

00017 
00018     def wrap(self, param):
00019         return param

Here is the caller graph for this function:


Member Data Documentation

Definition at line 209 of file test_as_parameter.py.


The documentation for this class was generated from the following file: