[Pyrex] Pythonic wrapping of libxml2
Lenard Lindstrom
len-l at telus.net
Wed Sep 29 19:05:35 CEST 2004
On Tue, 28 Sep 2004 23:24:09 -0400 vng1 at mac.com wrote:
> So - back to the topic at hand - how do you do function pointers in
> Pyrex?
>
> vic
>
> ---
> Don't be humble ... you're not that great. -- Golda Meir
>
> On 28-Sep-04, at 10:33 PM, Lenard Lindstrom wrote:
> > Function pointers are also used for callbacks and calling functions
> > in a dynamically loaded library.
> >
If you have a C function prototype, for example:
int foo(int x, int y);
then replace the identifier, foo in this example, with
(* <pointer-identifer>):
int (* fp_foo)(int x, int y);
In Pyrex just make sure the pointer declaration is part of a
cdef extern block:
cdef extern from "foo.h":
int (* fp_foo)(int x, int y)
or follows a cdef for a module local variable:
cdef int (* fp_foo)(int x, int y)
But I doubt you will need local function pointers.
You can treat an fp like any other function in Pyrex once it
is assigned to a function:
fp_foo = foo
z = fp_foo(3.0, 4.0) # Just another function call
Here is a complete example written in Pyrex. Just compile,
load the module, and call foo with some arguments:
--- funcptr.pyx ---
cdef int Cfoo(int x, int y):
return x + y
cdef int (* fp_Cfoo)(int x, int y)
fp_Cfoo = Cfoo
def foo(int x, int y):
return fp_Cfoo(x, y)
--- from a python command prompt:
Python 2.3.3 (#51, Jan 27 2004, 16:30:16) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import funcptr
>>> print funcptr.foo(12, 15)
27
Lenard Lindstrom
<len-l at telus.net>
More information about the Pyrex
mailing list