[pypy-issue] [issue850] ctypes: c struct in callback gives exception

viblo tracker at bugs.pypy.org
Sun Aug 28 20:58:45 CEST 2011

New submission from viblo <pypy at viblo.se>:

When running the attached code in pypy-1.6, it fails with this exception:

ubuntu at ubuntu-desktop:/media/sf_progr/pypy_callbacktest$
~/pypy/pypy-1.6/bin/pypy callback_fail.py
  File "/home/ubuntu/pypy/pypy-1.6/lib_pypy/_ctypes/function.py", line 274, in f
    for argtype, arg in zip(argtypes, args)]
  File "/home/ubuntu/pypy/pypy-1.6/lib_pypy/_ctypes/structure.py", line 140, in
    instance.__dict__['_buffer'] = self._ffistruct.fromaddress(address)
TypeError: unsupported operand type for int(): 'StructureInstance'

files: callback_fail.py
messages: 3037
nosy: pypy-issue, viblo
priority: bug
status: unread
title: ctypes: c struct in callback gives exception

PyPy bug tracker <tracker at bugs.pypy.org>
-------------- next part --------------
import ctypes
import os.path

path = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(path, 'callback_fail.so') #insert c library here
lib = ctypes.cdll.LoadLibrary(path)
class vec1(ctypes.Structure):
vec1._fields_ = [('x', ctypes.c_float)]
callbackFunc = ctypes.CFUNCTYPE(None, ctypes.c_int, vec1)

doTest = lib.doTest
doTest.restype = None
doTest.argtypes = [callbackFunc]

def f(self, i,x):
    print "hello #" + str(i)
    print "x:" + str(x),x.x
class C:
    def doTest(self,f):
        def _impl(x,y):
            return f(self,x,y)

c = C()

More information about the pypy-issue mailing list