According to the SWIG docs (section 5.4.9) there's no way to make a function pointer to a function in the target language.  I think you could hack around this using a typemap by always setting the C function pointer to the same wrapper function in C that then runs a Python callable object.  Since the C function can take a void * you could make a struct like

struct {
PyObject * pyCallable;
PyObject * pyArgs;
} PyCallbackStruct;

in the typemap and pass the C func a pointer to that.  The C func then runs the pyCallable using the pyArgs. You will probably need to make the C wrapper function a %constant too but that shouldn't matter since your Python code won't really see it anyway. - Greg

--- On Sun, 7/27/08, Simon Frost <> wrote:
From: Simon Frost <>
Subject: [Swig-user] Yet another Python callback problem
Date: Sunday, July 27, 2008, 11:56 PM

Dear SWIG List,

I'm trying to wrap a simple discrete event simulation package I wrote
using SWIG. It relies on passing around pointers to functions of the form:

typedef void (*FunctionPointer)(void*);

I have a C struct that looks like this:

typedef struct SimProcess {
FunctionPointer next_event;
} SimProcess;

I then can do things like (*s->next_event)(s) in order to execute the
simulation for a given process.

Essentially, I would like to wrap SimProcess, and be able to assign
Python functions to next_event, which are recognized as void (*)(void*)
by SWIG. I know I have to use the Python API to do this, but I'm unsure
of how to go about writing the appropriate typemaps. Any hints?


This SF.Net email is sponsored by the Moblin Your Move Developer's
Build the coolest Linux based applications with Moblin SDK & win great
Grand prize is a trip for two to an Open Source event anywhere in the world
Swig-user mailing list