[Py++] ctypes generator produces wrong Python code for function pointers in structs
Hello, Py++ translates the struct typedef int cb_fun_t(char *); struct info { cb_fun_t* cb_fun; int flag; }; into the Python Structure class info(ctypes.Structure): """class info""" info._fields_ = [ #class info ("cb_fun", ctypes.POINTER( ctypes.CFUNCTYPE( ctypes.c_int, ctypes.c_char_p ) )), ("flag", ctypes.c_int), ] This is wrong and produces segfaults. The correct version is class info(ctypes.Structure): """class info""" info._fields_ = [ #class info ("cb_fun", ctypes.CFUNCTYPE( ctypes.c_int, ctypes.c_char_p ) ), ("flag", ctypes.c_int), ] I have attached a small example that demonstrates the problem. lib.c is a small C library, build_api.py creates the broken api.py with Py++. api_correct.py is the manually corrected API with the correct structure. call.py demonstrates that the first API segfaults and the second API works: $ python call.py Calling do_callback with flag=7 do_callback called with flag=7 cb_fun received 'Hello World' cb_fun returns 42 cb_fun returned 42 do_callback returned 42 Calling do_callback with flag=7 do_callback called with flag=7 Segmentation fault Please let me know if you can reproduce the problem. It took me quite some time to figure this out. Best, -Nikolaus -- »Time flies like an arrow, fruit flies like a Banana.« PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C
Thanks. I will try to fix the problem this evening. On Sat, Dec 26, 2009 at 12:43 AM, Nikolaus Rath <Nikolaus@rath.org> wrote:
Hello,
Py++ translates the struct
typedef int cb_fun_t(char *); struct info { cb_fun_t* cb_fun; int flag; };
into the Python Structure
class info(ctypes.Structure): """class info""" info._fields_ = [ #class info ("cb_fun", ctypes.POINTER( ctypes.CFUNCTYPE( ctypes.c_int, ctypes.c_char_p ) )), ("flag", ctypes.c_int), ]
This is wrong and produces segfaults. The correct version is
class info(ctypes.Structure): """class info""" info._fields_ = [ #class info ("cb_fun", ctypes.CFUNCTYPE( ctypes.c_int, ctypes.c_char_p ) ), ("flag", ctypes.c_int), ]
I have attached a small example that demonstrates the problem. lib.c is a small C library, build_api.py creates the broken api.py with Py++. api_correct.py is the manually corrected API with the correct structure. call.py demonstrates that the first API segfaults and the second API works:
$ python call.py Calling do_callback with flag=7 do_callback called with flag=7 cb_fun received 'Hello World' cb_fun returns 42 cb_fun returned 42 do_callback returned 42 Calling do_callback with flag=7 do_callback called with flag=7 Segmentation fault
Please let me know if you can reproduce the problem. It took me quite some time to figure this out.
Best,
-Nikolaus
-- »Time flies like an arrow, fruit flies like a Banana.«
PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
-- Roman Yakovenko C++ Python language binding http://www.language-binding.net/
participants (2)
-
Nikolaus Rath -
Roman Yakovenko