[C++-sig] [Py++] ctypes generator produces wrong Python code for function pointers in structs

Roman Yakovenko roman.yakovenko at gmail.com
Sat Dec 26 09:55:52 CET 2009


Thanks. I will try to fix the problem this evening.

On Sat, Dec 26, 2009 at 12:43 AM, Nikolaus Rath <Nikolaus at 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 at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/


More information about the Cplusplus-sig mailing list