Back to index

python3.2  3.2.2
Public Member Functions
ctypes.test.test_functions.FunctionTestCase Class Reference

List of all members.

Public Member Functions

def test_mro
def test_wchar_parm
def test_wchar_result
def test_voidresult
def test_intresult
def test_floatresult
def test_doubleresult
def test_longdoubleresult
def test_longlongresult
def test_stringresult
def test_pointers
def test_errors
def test_shorts
def test_callbacks
def test_callbacks_2
def test_longlong_callbacks
def test_errors
def test_byval
def test_struct_return_2H
def test_struct_return_2H_stdcall
def test_struct_return_8H
def test_struct_return_8H_stdcall
def test_sf1651235

Detailed Description

Definition at line 27 of file test_functions.py.


Member Function Documentation

Definition at line 319 of file test_functions.py.

00319 
00320     def test_byval(self):
00321 
00322         # without prototype
00323         ptin = POINT(1, 2)
00324         ptout = POINT()
00325         # EXPORT int _testfunc_byval(point in, point *pout)
00326         result = dll._testfunc_byval(ptin, byref(ptout))
00327         got = result, ptout.x, ptout.y
00328         expected = 3, 1, 2
00329         self.assertEqual(got, expected)
00330 
00331         # with prototype
00332         ptin = POINT(101, 102)
00333         ptout = POINT()
00334         dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
00335         dll._testfunc_byval.restype = c_int
00336         result = dll._testfunc_byval(ptin, byref(ptout))
00337         got = result, ptout.x, ptout.y
00338         expected = 203, 101, 102
00339         self.assertEqual(got, expected)

Here is the call graph for this function:

Definition at line 250 of file test_functions.py.

00250 
00251     def test_callbacks(self):
00252         f = dll._testfunc_callback_i_if
00253         f.restype = c_int
00254 
00255         MyCallback = CFUNCTYPE(c_int, c_int)
00256 
00257         def callback(value):
00258             #print "called back with", value
00259             return value
00260 
00261         cb = MyCallback(callback)
00262         result = f(-10, cb)
00263         self.assertEqual(result, -18)
00264 
00265         # test with prototype
00266         f.argtypes = [c_int, MyCallback]
00267         cb = MyCallback(callback)
00268         result = f(-10, cb)
00269         self.assertEqual(result, -18)
00270 
00271         AnotherCallback = WINFUNCTYPE(c_int, c_int, c_int, c_int, c_int)
00272 
00273         # check that the prototype works: we call f with wrong
00274         # argument types
00275         cb = AnotherCallback(callback)
00276         self.assertRaises(ArgumentError, f, -10, cb)
00277 

Here is the call graph for this function:

Definition at line 278 of file test_functions.py.

00278 
00279     def test_callbacks_2(self):
00280         # Can also use simple datatypes as argument type specifiers
00281         # for the callback function.
00282         # In this case the call receives an instance of that type
00283         f = dll._testfunc_callback_i_if
00284         f.restype = c_int
00285 
00286         MyCallback = CFUNCTYPE(c_int, c_int)
00287 
00288         f.argtypes = [c_int, MyCallback]
00289 
00290         def callback(value):
00291             #print "called back with", value
00292             self.assertEqual(type(value), int)
00293             return value
00294 
00295         cb = MyCallback(callback)
00296         result = f(-10, cb)
00297         self.assertEqual(result, -18)

Here is the call graph for this function:

Definition at line 134 of file test_functions.py.

00134 
00135     def test_doubleresult(self):
00136         f = dll._testfunc_d_bhilfd
00137         f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
00138         f.restype = c_double
00139         result = f(1, 2, 3, 4, 5.0, 6.0)
00140         self.assertEqual(result, 21)
00141         self.assertEqual(type(result), float)
00142 
00143         result = f(-1, -2, -3, -4, -5.0, -6.0)
00144         self.assertEqual(result, -21)
00145         self.assertEqual(type(result), float)

Here is the call graph for this function:

Definition at line 220 of file test_functions.py.

00220 
00221     def test_errors(self):
00222         f = dll._testfunc_p_p
00223         f.restype = c_int
00224 
00225         class X(Structure):
00226             _fields_ = [("y", c_int)]
00227 
00228         self.assertRaises(TypeError, f, X()) #cannot convert parameter

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 315 of file test_functions.py.

00315 
00316     def test_errors(self):
00317         self.assertRaises(AttributeError, getattr, dll, "_xxx_yyy")
00318         self.assertRaises(ValueError, c_int.in_dll, dll, "_xxx_yyy")

Here is the call graph for this function:

Definition at line 122 of file test_functions.py.

00122 
00123     def test_floatresult(self):
00124         f = dll._testfunc_f_bhilfd
00125         f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
00126         f.restype = c_float
00127         result = f(1, 2, 3, 4, 5.0, 6.0)
00128         self.assertEqual(result, 21)
00129         self.assertEqual(type(result), float)
00130 
00131         result = f(-1, -2, -3, -4, -5.0, -6.0)
00132         self.assertEqual(result, -21)
00133         self.assertEqual(type(result), float)

Here is the call graph for this function:

Definition at line 96 of file test_functions.py.

00096 
00097     def test_intresult(self):
00098         f = dll._testfunc_i_bhilfd
00099         f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
00100         f.restype = c_int
00101         result = f(1, 2, 3, 4, 5.0, 6.0)
00102         self.assertEqual(result, 21)
00103         self.assertEqual(type(result), int)
00104 
00105         result = f(-1, -2, -3, -4, -5.0, -6.0)
00106         self.assertEqual(result, -21)
00107         self.assertEqual(type(result), int)
00108 
00109         # If we declare the function to return a short,
00110         # is the high part split off?
00111         f.restype = c_short
00112         result = f(1, 2, 3, 4, 5.0, 6.0)
00113         self.assertEqual(result, 21)
00114         self.assertEqual(type(result), int)
00115 
00116         result = f(1, 2, 3, 0x10004, 5.0, 6.0)
00117         self.assertEqual(result, 21)
00118         self.assertEqual(type(result), int)
00119 
00120         # You cannot assign character format codes as restype any longer
00121         self.assertRaises(TypeError, setattr, f, "restype", "i")

Here is the call graph for this function:

Definition at line 146 of file test_functions.py.

00146 
00147     def test_longdoubleresult(self):
00148         f = dll._testfunc_D_bhilfD
00149         f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_longdouble]
00150         f.restype = c_longdouble
00151         result = f(1, 2, 3, 4, 5.0, 6.0)
00152         self.assertEqual(result, 21)
00153         self.assertEqual(type(result), float)
00154 
00155         result = f(-1, -2, -3, -4, -5.0, -6.0)
00156         self.assertEqual(result, -21)
00157         self.assertEqual(type(result), float)

Here is the call graph for this function:

Definition at line 298 of file test_functions.py.

00298 
00299     def test_longlong_callbacks(self):
00300 
00301         f = dll._testfunc_callback_q_qf
00302         f.restype = c_longlong
00303 
00304         MyCallback = CFUNCTYPE(c_longlong, c_longlong)
00305 
00306         f.argtypes = [c_longlong, MyCallback]
00307 
00308         def callback(value):
00309             self.assertTrue(isinstance(value, int))
00310             return value & 0x7FFFFFFF
00311 
00312         cb = MyCallback(callback)
00313 
00314         self.assertEqual(13577625587, f(1000000000000, cb))

Here is the call graph for this function:

Definition at line 158 of file test_functions.py.

00158 
00159     def test_longlongresult(self):
00160         try:
00161             c_longlong
00162         except NameError:
00163             return
00164         f = dll._testfunc_q_bhilfd
00165         f.restype = c_longlong
00166         f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
00167         result = f(1, 2, 3, 4, 5.0, 6.0)
00168         self.assertEqual(result, 21)
00169 
00170         f = dll._testfunc_q_bhilfdq
00171         f.restype = c_longlong
00172         f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double, c_longlong]
00173         result = f(1, 2, 3, 4, 5.0, 6.0, 21)
00174         self.assertEqual(result, 42)

Here is the call graph for this function:

Definition at line 29 of file test_functions.py.

00029 
00030     def test_mro(self):
00031         # in Python 2.3, this raises TypeError: MRO conflict among bases classes,
00032         # in Python 2.2 it works.
00033         #
00034         # But in early versions of _ctypes.c, the result of tp_new
00035         # wasn't checked, and it even crashed Python.
00036         # Found by Greg Chapman.
00037 
00038         try:
00039             class X(object, Array):
00040                 _length_ = 5
00041                 _type_ = "i"
00042         except TypeError:
00043             pass
00044 
00045 
00046         from _ctypes import _Pointer
00047         try:
00048             class X(object, _Pointer):
00049                 pass
00050         except TypeError:
00051             pass
00052 
00053         from _ctypes import _SimpleCData
00054         try:
00055             class X(object, _SimpleCData):
00056                 _type_ = "i"
00057         except TypeError:
00058             pass
00059 
00060         try:
00061             class X(object, Structure):
00062                 _fields_ = []
00063         except TypeError:
00064             pass
00065 

Here is the call graph for this function:

Definition at line 185 of file test_functions.py.

00185 
00186     def test_pointers(self):
00187         f = dll._testfunc_p_p
00188         f.restype = POINTER(c_int)
00189         f.argtypes = [POINTER(c_int)]
00190 
00191         # This only works if the value c_int(42) passed to the
00192         # function is still alive while the pointer (the result) is
00193         # used.
00194 
00195         v = c_int(42)
00196 
00197         self.assertEqual(pointer(v).contents.value, 42)
00198         result = f(pointer(v))
00199         self.assertEqual(type(result), POINTER(c_int))
00200         self.assertEqual(result.contents.value, 42)
00201 
00202         # This on works...
00203         result = f(pointer(v))
00204         self.assertEqual(result.contents.value, v.value)
00205 
00206         p = pointer(c_int(99))
00207         result = f(p)
00208         self.assertEqual(result.contents.value, 99)
00209 
00210         arg = byref(v)
00211         result = f(arg)
00212         self.assertNotEqual(result.contents, v.value)
00213 
00214         self.assertRaises(ArgumentError, f, byref(c_short(22)))
00215 
00216         # It is dangerous, however, because you don't control the lifetime
00217         # of the pointer:
00218         result = f(byref(c_int(99)))
00219         self.assertNotEqual(result.contents, 99)

Here is the call graph for this function:

Definition at line 396 of file test_functions.py.

00396 
00397     def test_sf1651235(self):
00398         # see http://www.python.org/sf/1651235
00399 
00400         proto = CFUNCTYPE(c_int, RECT, POINT)
00401         def callback(*args):
00402             return 0
00403 
00404         callback = proto(callback)
00405         self.assertRaises(ArgumentError, lambda: callback((1, 2, 3, 4), POINT()))

Here is the call graph for this function:

Definition at line 230 of file test_functions.py.

00230 
00231     def test_shorts(self):
00232         f = dll._testfunc_callback_i_if
00233 
00234         args = []
00235         expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
00236                     1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
00237 
00238         def callback(v):
00239             args.append(v)
00240             return v
00241 
00242         CallBack = CFUNCTYPE(c_int, c_int)
00243 
00244         cb = CallBack(callback)
00245         f(2**18, cb)
00246         self.assertEqual(args, expected)

Here is the call graph for this function:

Definition at line 175 of file test_functions.py.

00175 
00176     def test_stringresult(self):
00177         f = dll._testfunc_p_p
00178         f.argtypes = None
00179         f.restype = c_char_p
00180         result = f(b"123")
00181         self.assertEqual(result, b"123")
00182 
00183         result = f(None)
00184         self.assertEqual(result, None)

Here is the call graph for this function:

Definition at line 340 of file test_functions.py.

00340 
00341     def test_struct_return_2H(self):
00342         class S2H(Structure):
00343             _fields_ = [("x", c_short),
00344                         ("y", c_short)]
00345         dll.ret_2h_func.restype = S2H
00346         dll.ret_2h_func.argtypes = [S2H]
00347         inp = S2H(99, 88)
00348         s2h = dll.ret_2h_func(inp)
00349         self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))

Here is the call graph for this function:

Definition at line 351 of file test_functions.py.

00351 
00352         def test_struct_return_2H_stdcall(self):
00353             class S2H(Structure):
00354                 _fields_ = [("x", c_short),
00355                             ("y", c_short)]
00356 
00357             windll.s_ret_2h_func.restype = S2H
00358             windll.s_ret_2h_func.argtypes = [S2H]
00359             s2h = windll.s_ret_2h_func(S2H(99, 88))
00360             self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))

Here is the call graph for this function:

Definition at line 361 of file test_functions.py.

00361 
00362     def test_struct_return_8H(self):
00363         class S8I(Structure):
00364             _fields_ = [("a", c_int),
00365                         ("b", c_int),
00366                         ("c", c_int),
00367                         ("d", c_int),
00368                         ("e", c_int),
00369                         ("f", c_int),
00370                         ("g", c_int),
00371                         ("h", c_int)]
00372         dll.ret_8i_func.restype = S8I
00373         dll.ret_8i_func.argtypes = [S8I]
00374         inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
00375         s8i = dll.ret_8i_func(inp)
00376         self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
00377                              (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 379 of file test_functions.py.

00379 
00380         def test_struct_return_8H_stdcall(self):
00381             class S8I(Structure):
00382                 _fields_ = [("a", c_int),
00383                             ("b", c_int),
00384                             ("c", c_int),
00385                             ("d", c_int),
00386                             ("e", c_int),
00387                             ("f", c_int),
00388                             ("g", c_int),
00389                             ("h", c_int)]
00390             windll.s_ret_8i_func.restype = S8I
00391             windll.s_ret_8i_func.argtypes = [S8I]
00392             inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
00393             s8i = windll.s_ret_8i_func(inp)
00394             self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
00395                                  (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 88 of file test_functions.py.

00088 
00089     def test_voidresult(self):
00090         f = dll._testfunc_v
00091         f.restype = None
00092         f.argtypes = [c_int, c_int, POINTER(c_int)]
00093         result = c_int()
00094         self.assertEqual(None, f(1, 2, byref(result)))
00095         self.assertEqual(result.value, 3)

Here is the call graph for this function:

Definition at line 66 of file test_functions.py.

00066 
00067     def test_wchar_parm(self):
00068         try:
00069             c_wchar
00070         except NameError:
00071             return
00072         f = dll._testfunc_i_bhilfd
00073         f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
00074         result = f(1, "x", 3, 4, 5.0, 6.0)
00075         self.assertEqual(result, 139)
00076         self.assertEqual(type(result), int)

Here is the call graph for this function:

Definition at line 77 of file test_functions.py.

00077 
00078     def test_wchar_result(self):
00079         try:
00080             c_wchar
00081         except NameError:
00082             return
00083         f = dll._testfunc_i_bhilfd
00084         f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
00085         f.restype = c_wchar
00086         result = f(0, 0, 0, 0, 0, 0)
00087         self.assertEqual(result, '\x00')

Here is the call graph for this function:


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