TypeError from simple callback using structs
Created originally on Bitbucket by iuliatamas (Iulia Tamas)
Error:
#!python
Callback in C
Traceback (most recent call last):
File "cffi-callback-bug.py", line 30, in <module>
lib._add_callback(set_struct) # this doesn't work
TypeError: initializer for ctype 'struct CStruct *(*)(int)' must be a pointer to same type, not cdata 'struct CStruct *(*)(int)'
Code that leads to it (also attached):
cffi-callback-bug.py:
#!python
from cffi import FFI
ffi = FFI()
ffi.set_source("_example",
"""
#include <stdio.h>
struct CStruct;
void _add_callback ( struct CStruct * (* ptr) (int) );
""",
runtime_library_dirs=['.'],
libraries=['mylib'])
ffi.cdef('''
void _add_callback ( struct CStruct * (* ptr) (int) );
''')
@ffi.callback("struct CStruct * (int)")
def set_struct(x):
return 0
if __name__ == "__main__":
ffi.compile()
from _example import ffi, lib
lib._add_callback(ffi.cast("void * ", set_struct)) # this works
lib._add_callback(set_struct) # this doesn't work
cffi-callback-bug.c:
#!c
// To create library:
// gcc -arch i386 -arch x86_64 -dynamiclib -fPIC -lc -lstdc++ cffi-callback-bug.c -o libmylib.dylib
#include <stdio.h>
#include "cffi-callback-bug.h"
struct CStruct * _add_callback (struct CStruct * (*ptr) (int)) {
printf("Callback in C\n");
return NULL;
}
cffi-callback-bug.h:
#!c
struct CStruct {
int hi;
};