ctypes: pointer to method

Martin Landa landa.martin at gmail.com
Fri Aug 6 16:10:42 EDT 2010


Hi,

On Aug 5, 9:32 pm, Nobody <nob... at nowhere.com> wrote:
> I don't know about methods, but it works for functions.
>
> > Sample code:
>
> >     ...
> >     G_set_error_routine(byref(self._print_error))
>
> This won't work; you have to be more explicit, e.g.:
>
>         errtype = CFUNCTYPE(c_int, POINTER(c_char), POINTER(c_int))
>         errfunc = errtype(print_error)
>         G_set_error_routine(errfunc)

the C function is defined as

    G_set_error_routine = _libs['grass_gis.
7.0.svn'].G_set_error_routine
    G_set_error_routine.restype = None
    G_set_error_routine.argtypes = [CFUNCTYPE(UNCHECKED(c_int),
String, c_int)]

I defined in Python function print_error()

def print_error(self, msg, type):
    print msg, type

and

        errtype = CFUNCTYPE(UNCHECKED(c_int), String, c_int)
        errfunc = errtype(print_error)
        G_set_error_routine(errfunc)

unfortunately the application crashes when print_error() is called
from C library

static void vfprint_error(int type, const char *template, va_list ap)
{
    char buffer[2000];		/* G_asprintf does not work */

    vsprintf(buffer, template, ap);

    G_debug(5, "print_error(): msg = \"%s\" type = %d", buffer, type);
    print_error(buffer, type);
}

Any idea how to solve it. Thanks, Martin




More information about the Python-list mailing list