Back to index

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

Classes

class  PointersTestCase

Functions

def test_change_pointers
def test_callbacks_with_pointers
def test_basics
 i = c_int(42) callback(byref(i)) self.assertTrue(i.value == 84)
def test_from_address
 print type(p.contents), ct
def test_other
 print dir(p) print p.from_address print p.from_address(addr)[0][0]
def test_basic
def test_charpp
def test_bug_1467852
def test_c_void_p
def test_pointers_bool

Variables

list ctype_types
list python_types
tuple address = func(byref(i))
 func.argtypes = (POINTER(c_int),)
tuple res = func(pointer(i))
tuple callback = PROTOTYPE(func)
 print arg[i],
tuple dll = CDLL(_ctypes_test.__file__)
 doit = dll._testfunc_callback_with_pointer
 result

Function Documentation

Definition at line 129 of file test_pointers.py.

00129 
00130     def test_basic(self):
00131         p = pointer(c_int(42))
00132         # Although a pointer can be indexed, it ha no length
00133         self.assertRaises(TypeError, len, p)
00134         self.assertEqual(p[0], 42)
00135         self.assertEqual(p.contents.value, 42)

i = c_int(42) callback(byref(i)) self.assertTrue(i.value == 84)

print self.result print self.result

Definition at line 88 of file test_pointers.py.

00088 
00089     def test_basics(self):
00090         from operator import delitem
00091         for ct, pt in zip(ctype_types, python_types):
00092             i = ct(42)
            p = pointer(i)

Definition at line 148 of file test_pointers.py.

00148 
00149     def test_bug_1467852(self):
00150         # http://sourceforge.net/tracker/?func=detail&atid=532154&aid=1467852&group_id=71702
00151         x = c_int(5)
00152         dummy = []
00153         for i in range(32000):
00154             dummy.append(c_int(i))
00155         y = c_int(6)
00156         p = pointer(x)
00157         pp = pointer(p)
00158         q = pointer(y)
00159         pp[0] = q         # <==
        self.assertEqual(p[0], 6)

Definition at line 160 of file test_pointers.py.

00160 
00161     def test_c_void_p(self):
00162         # http://sourceforge.net/tracker/?func=detail&aid=1518190&group_id=5470&atid=105470
00163         if sizeof(c_void_p) == 4:
00164             self.assertEqual(c_void_p(0xFFFFFFFF).value,
00165                                  c_void_p(-1).value)
00166             self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value,
00167                                  c_void_p(-1).value)
00168         elif sizeof(c_void_p) == 8:
00169             self.assertEqual(c_void_p(0xFFFFFFFF).value,
00170                                  0xFFFFFFFF)
00171             self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value,
00172                                  c_void_p(-1).value)
00173             self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFFFFFFFFFF).value,
00174                                  c_void_p(-1).value)
00175 
00176         self.assertRaises(TypeError, c_void_p, 3.14) # make sure floats are NOT accepted
00177         self.assertRaises(TypeError, c_void_p, object()) # nor other objects

Definition at line 59 of file test_pointers.py.

00059 
00060     def test_callbacks_with_pointers(self):
00061         # a function type receiving a pointer
00062         PROTOTYPE = CFUNCTYPE(c_int, POINTER(c_int))
00063 
00064         self.result = []
00065 
00066         def func(arg):
            for i in range(10):

Here is the call graph for this function:

Definition at line 37 of file test_pointers.py.

00037 
00038     def test_change_pointers(self):
00039         dll = CDLL(_ctypes_test.__file__)
00040         func = dll._testfunc_p_p
00041 
00042         i = c_int(87654)
00043         func.restype = POINTER(c_int)
00044         func.argtypes = (POINTER(c_int),)
00045 
00046         res = func(pointer(i))
00047         self.assertEqual(res[0], 87654)
00048         self.assertEqual(res.contents.value, 87654)
00049 
00050         # C code: *res = 54345
00051         res[0] = 54345
00052         self.assertEqual(i.value, 54345)
00053 
00054         # C code:
00055         #   int x = 12321;
00056         #   res = &x
00057         res.contents = c_int(12321)
00058         self.assertEqual(i.value, 54345)

Test that a character pointer-to-pointer is correctly passed

Definition at line 136 of file test_pointers.py.

00136 
00137     def test_charpp(self):
00138         """Test that a character pointer-to-pointer is correctly passed"""
00139         dll = CDLL(_ctypes_test.__file__)
00140         func = dll._testfunc_c_p_p
00141         func.restype = c_char_p
00142         argv = (c_char_p * 2)()
00143         argc = c_int( 2 )
00144         argv[0] = b'hello'
00145         argv[1] = b'world'
00146         result = func( byref(argc), argv )
00147         self.assertEqual(result, b'world')

Here is the call graph for this function:

print type(p.contents), ct

print p.contents self.assertTrue(p.contents == 42) self.assertTrue(p[0] == 42)

Definition at line 102 of file test_pointers.py.

00102 
00103     def test_from_address(self):
00104         from array import array
00105         a = array('i', [100, 200, 300, 400, 500])
00106         addr = a.buffer_info()[0]
00107 
        p = POINTER(POINTER(c_int))

print dir(p) print p.from_address print p.from_address(addr)[0][0]

Definition at line 112 of file test_pointers.py.

00112 
00113     def test_other(self):
00114         class Table(Structure):
00115             _fields_ = [("a", c_int),
00116                         ("b", c_int),
00117                         ("c", c_int)]
00118 
00119         pt = pointer(Table(1, 2, 3))
00120 
00121         self.assertEqual(pt.contents.a, 1)
00122         self.assertEqual(pt.contents.b, 2)
00123         self.assertEqual(pt.contents.c, 3)
00124 
00125         pt.contents.c = 33
00126 
00127         from ctypes import _pointer_type_cache
00128         del _pointer_type_cache[Table]

Definition at line 178 of file test_pointers.py.

00178 
00179     def test_pointers_bool(self):
00180         # NULL pointers have a boolean False value, non-NULL pointers True.
00181         self.assertEqual(bool(POINTER(c_int)()), False)
00182         self.assertEqual(bool(pointer(c_int())), True)
00183 
00184         self.assertEqual(bool(CFUNCTYPE(None)(0)), False)
00185         self.assertEqual(bool(CFUNCTYPE(None)(42)), True)
00186 
00187         # COM methods are boolean True:
00188         if sys.platform == "win32":
00189             mth = WINFUNCTYPE(None)(42, "name", (), None)
00190             self.assertEqual(bool(mth), True)

Here is the call graph for this function:


Variable Documentation

func.argtypes = (POINTER(c_int),)

Definition at line 29 of file test_pointers.py.

tuple ctypes.test.test_pointers.callback = PROTOTYPE(func)

print arg[i],

print

Definition at line 71 of file test_pointers.py.

Initial value:
00001 [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
00002                  c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float]

Definition at line 6 of file test_pointers.py.

tuple ctypes.test.test_pointers.dll = CDLL(_ctypes_test.__file__)

Definition at line 73 of file test_pointers.py.

ctypes.test.test_pointers.doit = dll._testfunc_callback_with_pointer

Definition at line 77 of file test_pointers.py.

Initial value:
00001 [int, int, int, int, int, int,
00002                 int, int, int, int, float, float]

Definition at line 8 of file test_pointers.py.

Definition at line 33 of file test_pointers.py.

Definition at line 63 of file test_pointers.py.