Example #1
0
    def test_convert_pointers(self):
        strlen = CFUNCTYPE(c_int, c_void_p)()
        strlen.__name__ = 'strlen'
        def ll_strlen_from_void_p(adr):
            i = 0
            while adr.char[i] != '\x00':
                i += 1
            return i
        strlen.llinterp_friendly_version = ll_strlen_from_void_p
        PTR = c_char_p("hello")
        BUF = create_string_buffer(10)
        BUF.value = "hello"
        ARR = (c_byte * 10)(65, 66, 67)

        def func(n):
            # constant arguments XXX in-progress
            ##   assert strlen("hello") == 5
            ##   assert strlen(PTR) == 5
            ##   assert strlen(BUF) == 5
            ##   assert strlen(ARR) == 3
            # variable arguments
            s = chr(n) + 'bc'
            assert strlen(s) == 3
            assert strlen(c_char_p(s)) == 3
            assert strlen((c_char * 6)('a', 'b')) == 2
            # XXX Bytes are not chars in llinterp.
            # assert strlen((c_byte * 6)(104,101,108,108,111)) == 5
            buf = create_string_buffer(10)
            buf.value = "hello"
            assert strlen(buf) == 5

        interpret(func, [65])
Example #2
0
    def test_convert_pointers(self):
        py.test.skip("in-progress")
        from pypy.rpython.rctypes.rchar_p import ll_strlen
        strlen = CFUNCTYPE(c_int, c_char_p)()   # not directly executable!
        strlen.__name__ = 'strlen'
        strlen.llinterp_friendly_version = ll_strlen
        PTR = c_char_p("hello")
        BUF = create_string_buffer(10)
        BUF.value = "hello"

        def func(n):
            # constant arguments
            assert strlen("hello") == 5
            assert strlen(PTR) == 5
            assert strlen(BUF) == 5
            # variable arguments
            s = chr(n) + 'bc'
            assert strlen(s) == 3
            assert strlen(c_char_p(s)) == 3
            assert strlen((c_char * 6)('a', 'b')) == 2
            buf = create_string_buffer(10)
            buf.value = "hello"
            assert strlen(buf) == 5

        interpret(func, [65])
Example #3
0
def _declare(name, *params, **kwargs):
    params = list(map(_to_param, params))
    argtypes = tuple(param.typ for param in params)
    paramflags = tuple(param.paramflag for param in params)
    restype = kwargs.get('restype')
    errcheck = kwargs.get('errcheck')
    func = CFUNCTYPE(restype, *argtypes)((name, _lib), paramflags)
    func.__name__ = name
    if errcheck:
        func.errcheck = errcheck
    globals()[name] = func
Example #4
0
def _declare(name, *params, **kwargs):
    params = list(map(_to_param, params))
    argtypes = tuple(param.typ for param in params)
    paramflags = tuple(param.paramflag for param in params)
    restype = kwargs.get('restype')
    errcheck = kwargs.get('errcheck')
    func = CFUNCTYPE(restype, *argtypes)((name, _lib), paramflags)
    func.__name__ = name
    if errcheck:
        func.errcheck = errcheck
    globals()[name] = func
Example #5
0
    def test_ctypes_newstyle_dict(self):
        #print "test_ctypes_newstyle_dict"
        from ctypes import CFUNCTYPE, c_int

        test = CFUNCTYPE(c_int, c_int)()
        dct = test.__dict__
        test.__name__ = "testName"
        dct = None
        self.assertTrue(hasattr(test, "__name__"))
        self.assertEqual(test.__name__, "testName")
        runGC()
        runGC()
        self.assertTrue(hasattr(test, "__name__"))
        self.assertEqual(test.__name__, "testName")
Example #6
0
	def test_ctypes_newstyle_dict(self):
		#print "test_ctypes_newstyle_dict"
		from ctypes import CFUNCTYPE, c_int

		test = CFUNCTYPE(c_int, c_int)()
		dct = test.__dict__
		test.__name__ = "testName"
		dct = None
		self.assertTrue(hasattr(test, "__name__"))
		self.assertEqual(test.__name__, "testName")
		runGC()
		runGC()
		self.assertTrue(hasattr(test, "__name__"))
		self.assertEqual(test.__name__, "testName")