Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > passing callback function to c-extension

Reply
Thread Tools

passing callback function to c-extension

 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      01-14-2004
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
 
Reply With Quote
 
 
 
 
Eric Brunel
Guest
Posts: n/a
 
      01-14-2004
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

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      01-14-2004
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
 
Reply With Quote
 
Skip Montanaro
Guest
Posts: n/a
 
      01-14-2004

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

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      01-14-2004
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I create a function in my library for passing user callback function Angus C Programming 32 04-15-2008 02:28 PM
Passing static member function as callback to Windows DLL Evan Burkitt C++ 2 08-24-2007 10:37 PM
Passing arguments to callback function in addEventListener Janus Javascript 10 01-11-2007 01:23 PM
Using function pointer in callback function pvdm C++ 1 09-09-2003 12:26 PM
Howto: Class member function as callback function for dialog box prettysmurfed C++ 6 07-22-2003 06:17 PM



Advertisments