[Pyrex] Function pointer question

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Dec 5 01:06:20 CET 2005


vanitha at cs.wisc.edu wrote:

> I have a function in C that takes a function pointer as the argument, but
> the function that is being passed in is implemented in Python. Can I use
> Pyrex to wrap this C function? How do I pass the Python function in as a
> function pointer to the C Function?

You can't use the Python function directly as a C
function, but you can pass a C function stub that calls
the Python function.

The C function needs to be able to find the Python
function somehow. If the C function has provision for
supplying a pointer to user data, you can cast the
Python function reference to the appropriate type
and pass it in that way. If not, you will have to
store the Python function reference in a global, and
live with only being able to have one Python callback
in effect at a time.

There's an example in the Demos/callback folder of the
Pyrex distribution.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+



More information about the Pyrex mailing list