pointer can t set contents has no type c code res 54345 c code int x 12321 res x a function type receiving a pointer this function expects a function pointer and calls this with an integer pointer as parameter the int pointer points to a table containing the numbers 1 10 p contents is the same as p0 although a pointer can be indexed it has no length test that a character pointertopointer is correctly passed dll cdllctypestest file func dll testfunccpp func restype ccharp argv ccharp 2 argc cint 2 argv0 b hello argv1 b world result func byrefargc argv self assertequalresult b world def testbug1467852self http sourceforge nettracker funcdetailatid532154aid1467852groupid71702 x cint5 dummy for i in range32000 dummy appendcinti y cint6 p pointerx pp pointerp q pointery pp0 q self assertequalp0 6 def testcvoidpself http sourceforge nettracker funcdetailaid1518190groupid5470atid105470 if sizeofcvoidp 4 self assertequalcvoidp0xffffffff value cvoidp1 value self assertequalcvoidp0xffffffffffffffff value cvoidp1 value elif sizeofcvoidp 8 self assertequalcvoidp0xffffffff value 0xffffffff self assertequalcvoidp0xffffffffffffffff value cvoidp1 value self assertequalcvoidp0xffffffffffffffffffffffff value cvoidp1 value self assertraisestypeerror cvoidp 3 14 make sure floats are not accepted self assertraisestypeerror cvoidp object nor other objects def testpointersboolself null pointers have a boolean false value nonnull pointers true self assertequalboolpointercint false self assertequalboolpointercint true self assertequalboolcfunctypenone0 false self assertequalboolcfunctypenone42 true com methods are boolean true if sys platform win32 mth ctypes winfunctypenone42 name none self assertequalboolmth true def testpointertypenameself largenamedtype type t 2 25 structure self asserttruepointerlargenamedtype to not leak references we must clean pointertypecache del pointertypecachelargenamedtype def testpointertypestrnameself largestring t 2 25 p pointerlargestring self asserttruep to not leak references we must clean pointertypecache del pointertypecacheidp def testabstractself self assertraisestypeerror pointer settype 42 if name main unittest main pointer can t set contents has no _type_ c code res 54345 c code int x 12321 res x a function type receiving a pointer this function expects a function pointer and calls this with an integer pointer as parameter the int pointer points to a table containing the numbers 1 10 p contents is the same as p 0 although a pointer can be indexed it has no length test that a character pointer to pointer is correctly passed http sourceforge net tracker func detail atid 532154 aid 1467852 group_id 71702 http sourceforge net tracker func detail aid 1518190 group_id 5470 atid 105470 make sure floats are not accepted nor other objects null pointers have a boolean false value non null pointers true com methods are boolean true to not leak references we must clean _pointer_type_cache to not leak references we must clean _pointer_type_cache
import _ctypes_test
import array
import ctypes
import sys
import unittest
from ctypes import (CDLL, CFUNCTYPE, Structure,
                    POINTER, pointer, _Pointer, _pointer_type_cache,
                    byref, sizeof,
                    c_void_p, c_char_p,
                    c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
                    c_long, c_ulong, c_longlong, c_ulonglong,
                    c_float, c_double)


ctype_types = [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
                 c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float]
python_types = [int, int, int, int, int, int,
                int, int, int, int, float, float]


class PointersTestCase(unittest.TestCase):
    def test_pointer_crash(self):

        class A(POINTER(c_ulong)):
            pass

        POINTER(c_ulong)(c_ulong(22))
        
        self.assertRaises(TypeError, A, c_ulong(33))

    def test_pass_pointers(self):
        dll = CDLL(_ctypes_test.__file__)
        func = dll._testfunc_p_p
        if sizeof(c_longlong) == sizeof(c_void_p):
            func.restype = c_longlong
        else:
            func.restype = c_long

        i = c_int(12345678)
        address = func(byref(i))
        self.assertEqual(c_int.from_address(address).value, 12345678)

        func.restype = POINTER(c_int)
        res = func(pointer(i))
        self.assertEqual(res.contents.value, 12345678)
        self.assertEqual(res[0], 12345678)

    def test_change_pointers(self):
        dll = CDLL(_ctypes_test.__file__)
        func = dll._testfunc_p_p

        i = c_int(87654)
        func.restype = POINTER(c_int)
        func.argtypes = (POINTER(c_int),)

        res = func(pointer(i))
        self.assertEqual(res[0], 87654)
        self.assertEqual(res.contents.value, 87654)

        
        res[0] = 54345
        self.assertEqual(i.value, 54345)

        
        
        
        x = c_int(12321)
        res.contents = x
        self.assertEqual(i.value, 54345)

        x.value = -99
        self.assertEqual(res.contents.value, -99)

    def test_callbacks_with_pointers(self):
        
        PROTOTYPE = CFUNCTYPE(c_int, POINTER(c_int))

        self.result = []

        def func(arg):
            for i in range(10):
                self.result.append(arg[i])
            return 0
        callback = PROTOTYPE(func)

        dll = CDLL(_ctypes_test.__file__)
        
        
        
        doit = dll._testfunc_callback_with_pointer

        doit(callback)
        doit(callback)

    def test_basics(self):
        for ct, pt in zip(ctype_types, python_types):
            i = ct(42)
            p = pointer(i)
            self.assertIs(type(p.contents), ct)
            

            with self.assertRaises(TypeError):
                del p[0]

    def test_from_address(self):
        a = array.array('i', [100, 200, 300, 400, 500])
        addr = a.buffer_info()[0]
        p = POINTER(POINTER(c_int))

    def test_other(self):
        class Table(Structure):
            _fields_ = [("a", c_int),
                        ("b", c_int),
                        ("c", c_int)]

        pt = pointer(Table(1, 2, 3))

        self.assertEqual(pt.contents.a, 1)
        self.assertEqual(pt.contents.b, 2)
        self.assertEqual(pt.contents.c, 3)

        pt.contents.c = 33

        del _pointer_type_cache[Table]

    def test_basic(self):
        p = pointer(c_int(42))
        
        self.assertRaises(TypeError, len, p)
        self.assertEqual(p[0], 42)
        self.assertEqual(p[0:1], [42])
        self.assertEqual(p.contents.value, 42)

    def test_charpp(self):
        
        dll = CDLL(_ctypes_test.__file__)
        func = dll._testfunc_c_p_p
        func.restype = c_char_p
        argv = (c_char_p * 2)()
        argc = c_int( 2 )
        argv[0] = b'hello'
        argv[1] = b'world'
        result = func( byref(argc), argv )
        self.assertEqual(result, b'world')

    def test_bug_1467852(self):
        
        x = c_int(5)
        dummy = []
        for i in range(32000):
            dummy.append(c_int(i))
        y = c_int(6)
        p = pointer(x)
        pp = pointer(p)
        q = pointer(y)
        pp[0] = q         
        self.assertEqual(p[0], 6)
    def test_c_void_p(self):
        
        if sizeof(c_void_p) == 4:
            self.assertEqual(c_void_p(0xFFFFFFFF).value,
                                 c_void_p(-1).value)
            self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value,
                                 c_void_p(-1).value)
        elif sizeof(c_void_p) == 8:
            self.assertEqual(c_void_p(0xFFFFFFFF).value,
                                 0xFFFFFFFF)
            self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value,
                                 c_void_p(-1).value)
            self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFFFFFFFFFF).value,
                                 c_void_p(-1).value)

        self.assertRaises(TypeError, c_void_p, 3.14) 
        self.assertRaises(TypeError, c_void_p, object()) 

    def test_pointers_bool(self):
        
        self.assertEqual(bool(POINTER(c_int)()), False)
        self.assertEqual(bool(pointer(c_int())), True)

        self.assertEqual(bool(CFUNCTYPE(None)(0)), False)
        self.assertEqual(bool(CFUNCTYPE(None)(42)), True)

        
        if sys.platform == "win32":
            mth = ctypes.WINFUNCTYPE(None)(42, "name", (), None)
            self.assertEqual(bool(mth), True)

    def test_pointer_type_name(self):
        LargeNamedType = type('T' * 2 ** 25, (Structure,), {})
        self.assertTrue(POINTER(LargeNamedType))

        
        del _pointer_type_cache[LargeNamedType]

    def test_pointer_type_str_name(self):
        large_string = 'T' * 2 ** 25
        P = POINTER(large_string)
        self.assertTrue(P)

        
        del _pointer_type_cache[id(P)]

    def test_abstract(self):
        self.assertRaises(TypeError, _Pointer.set_type, 42)


if __name__ == '__main__':
    unittest.main()