Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   passing callback function to c-extension (http://www.velocityreviews.com/forums/t326950-passing-callback-function-to-c-extension.html)

Diez B. Roggisch 01-14-2004 01:43 PM

passing callback function to c-extension
 
Hi,

I'm currently writing my first c-extension module. First of all: distutils
and the tutorial for embedding/extending rock! Great work, guys.

However, I didn't find a hint how to pass a python callback function.
Googling didn't turn out much useful stuff, so I'd be glad if someone could
post a hint on how to do this.

Regards,

Diez

Eric Brunel 01-14-2004 03:36 PM

Re: passing callback function to c-extension
 
Diez B. Roggisch wrote:
> Hi,
>
> I'm currently writing my first c-extension module. First of all: distutils
> and the tutorial for embedding/extending rock! Great work, guys.
>
> However, I didn't find a hint how to pass a python callback function.
> Googling didn't turn out much useful stuff, so I'd be glad if someone could
> post a hint on how to do this.


A Python function is just a regular Python object, so it is represented at C
level by a PyObject*. To call it from C, just use one of the PyObject_Call
functions - see http://www.python.org/doc/current/ap...t.html#l2h-189

HTH
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com


Diez B. Roggisch 01-14-2004 04:45 PM

Re: passing callback function to c-extension
 
Hi,

> A Python function is just a regular Python object, so it is represented at
> C level by a PyObject*. To call it from C, just use one of the
> PyObject_Call functions - see
> http://www.python.org/doc/current/ap...t.html#l2h-189


Thanks - works like a charm. I'm now looking for a way to ensure that the
passed object is actually a callable one - and how to throw exceptions. But
I just started looking for that, so I'll be back if I really need help.

Diez

Skip Montanaro 01-14-2004 05:02 PM

Re: passing callback function to c-extension
 

Diez> I'm now looking for a way to ensure that the passed object is
Diez> actually a callable one

Try it and see. If an exception is raised, just return NULL. The
appropriate exception stuff will have been set.

Alternatively, you can test the value of

PyCallable_Check(obj)

to see if obj is really callable and do your own error recovery.

- and how to throw exceptions.

The C API manual is your friend here:

http://www.python.org/doc/api/api.html

Search for "exception".

Skip


Diez B. Roggisch 01-14-2004 06:25 PM

Re: passing callback function to c-extension
 
> Diez> I'm now looking for a way to ensure that the passed object is
> Diez> actually a callable one
>
> Try it and see. If an exception is raised, just return NULL. The
> appropriate exception stuff will have been set.


Thanks - I already found out about that. Its incredible - a few hours of
work (mostly because I've actually have to _learn_ something new, not
because of the api makes things complicated) and I've got my wrapper. If I
can keep up that pace, I'll release my wrapper this night, or tomorrow :)

Regards,

Diez


All times are GMT. The time now is 04:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.