passing callback function to c-extension

Discussion in 'Python' started by Diez B. Roggisch, Jan 14, 2004.

  1. 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
     
    Diez B. Roggisch, Jan 14, 2004
    #1
    1. Advertising

  2. Diez B. Roggisch

    Eric Brunel Guest

    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/api/object.html#l2h-189

    HTH
    --
    - Eric Brunel <eric dot brunel at pragmadev dot com> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
     
    Eric Brunel, Jan 14, 2004
    #2
    1. Advertising

  3. 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/api/object.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
     
    Diez B. Roggisch, Jan 14, 2004
    #3
  4. 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
     
    Skip Montanaro, Jan 14, 2004
    #4
  5. > 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
     
    Diez B. Roggisch, Jan 14, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prettysmurfed
    Replies:
    6
    Views:
    715
    prettysmurfed
    Jul 22, 2003
  2. Evan Burkitt
    Replies:
    2
    Views:
    1,072
    Evan Burkitt
    Aug 24, 2007
  3. Angus
    Replies:
    32
    Views:
    989
    Richard
    Apr 15, 2008
  4. Replies:
    3
    Views:
    220
  5. Janus
    Replies:
    10
    Views:
    226
    Richard Cornford
    Jan 11, 2007
Loading...

Share This Page