Back to index

python3.2  3.2.2
Classes | Functions | Variables
ctypes.test.test_parameters Namespace Reference

Classes

class  SimpleTypesTestCase

Functions

def test_int_pointers
def test_byref_pointer
def test_byref_pointerpointer
def test_array_pointers
def test_noctypes_argtype
 def test_performance(self): check_perf()

Variables

string s = "123"
 print "(No c_wchar_p)"
tuple pa = c_wchar_p.from_param(c_wchar_p("123"))
tuple x = LPINT.from_param(pointer(c_int(42)))
 p = pointer(c_int(42)) x = LPINT.from_param(p)

Function Documentation

Definition at line 130 of file test_parameters.py.

00130 
00131     def test_array_pointers(self):
00132         from ctypes import c_short, c_uint, c_int, c_long, POINTER
00133         INTARRAY = c_int * 3
00134         ia = INTARRAY()
00135         self.assertEqual(len(ia), 3)
00136         self.assertEqual([ia[i] for i in range(3)], [0, 0, 0])
00137 
00138         # Pointers are only compatible with arrays containing items of
00139         # the same type!
00140         LPINT = POINTER(c_int)
00141         LPINT.from_param((c_int*3)())
00142         self.assertRaises(TypeError, LPINT.from_param, c_short*3)
00143         self.assertRaises(TypeError, LPINT.from_param, c_long*3)
00144         self.assertRaises(TypeError, LPINT.from_param, c_uint*3)

Definition at line 105 of file test_parameters.py.

00105 
00106     def test_byref_pointer(self):
00107         # The from_param class method of POINTER(typ) classes accepts what is
00108         # returned by byref(obj), it type(obj) == typ
00109         from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref
00110         LPINT = POINTER(c_int)
00111 
00112         LPINT.from_param(byref(c_int(42)))
00113 
00114         self.assertRaises(TypeError, LPINT.from_param, byref(c_short(22)))
00115         if c_int != c_long:
00116             self.assertRaises(TypeError, LPINT.from_param, byref(c_long(22)))
00117         self.assertRaises(TypeError, LPINT.from_param, byref(c_uint(22)))

Here is the call graph for this function:

Definition at line 118 of file test_parameters.py.

00118 
00119     def test_byref_pointerpointer(self):
00120         # See above
00121         from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref
00122 
00123         LPLPINT = POINTER(POINTER(c_int))
00124         LPLPINT.from_param(byref(pointer(c_int(42))))
00125 
00126         self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_short(22))))
00127         if c_int != c_long:
00128             self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_long(22))))
00129         self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_uint(22))))

Here is the call graph for this function:

Definition at line 88 of file test_parameters.py.

00088 
00089     def test_int_pointers(self):
00090         from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer
00091         LPINT = POINTER(c_int)

def test_performance(self): check_perf()

Definition at line 148 of file test_parameters.py.

00148 
00149     def test_noctypes_argtype(self):
00150         import _ctypes_test
00151         from ctypes import CDLL, c_void_p, ArgumentError
00152 
00153         func = CDLL(_ctypes_test.__file__)._testfunc_p_p
00154         func.restype = c_void_p
00155         # TypeError: has no from_param method
00156         self.assertRaises(TypeError, setattr, func, "argtypes", (object,))
00157 
00158         class Adapter(object):
00159             def from_param(cls, obj):
00160                 return None
00161 
00162         func.argtypes = (Adapter(),)
00163         self.assertEqual(func(None), None)
00164         self.assertEqual(func(object()), None)
00165 
00166         class Adapter(object):
00167             def from_param(cls, obj):
00168                 return obj
00169 
00170         func.argtypes = (Adapter(),)
00171         # don't know how to convert parameter 1
00172         self.assertRaises(ArgumentError, func, object())
00173         self.assertEqual(func(c_void_p(42)), 42)
00174 
00175         class Adapter(object):
00176             def from_param(cls, obj):
00177                 raise ValueError(obj)
00178 
00179         func.argtypes = (Adapter(),)
00180         # ArgumentError: argument 1: ValueError: 99
00181         self.assertRaises(ArgumentError, func, 99)
00182 


Variable Documentation

tuple ctypes.test.test_parameters.pa = c_wchar_p.from_param(c_wchar_p("123"))

Definition at line 85 of file test_parameters.py.

print "(No c_wchar_p)"

Definition at line 76 of file test_parameters.py.

tuple ctypes.test.test_parameters.x = LPINT.from_param(pointer(c_int(42)))

p = pointer(c_int(42)) x = LPINT.from_param(p)

Definition at line 94 of file test_parameters.py.