C Ext Questions

Discussion in 'Python' started by djw, Jul 21, 2004.

  1. djw

    djw Guest

    Hello, I have a couple of questions about some code in a C extension module.
    First, how would I convert a Python string return value from a call to
    PyObject_CallFunction into a C void * buffer? Second, do I need to call
    Py_XINCREF() on my callback function? (Sorry, the code is kind of
    preliminary and rough)

    Thanks,

    Don


    ext.c
    -----

    PyObject * callback = NULL;

    PyObject * set_callback( PyObject * self, PyObject * args )
    {
    if( !PyArg_ParseTuple( args, "O", &callback ) )
    {
    return Py_BuildValue( "i", 1 );
    }

    if( !PyCallable_Check( callback ))
    {
    return Py_BuildValue( "i", 2 );
    }

    Py_XINCREF( callback );
    }

    int call_callback( int param1, int param2, void * buf, int size )
    {
    PyObject * result;

    if( callback )
    {
    result = PyObject_CallFunction( callback, "ii", param1, param2 );
    // Need to convert return Python string into buf....????
    }
    }

    PyObject * do_something( PyObject * self, PyObject * args )
    {
    char buf[512];
    call_callback( 1, 1, (void *)&buf, sizeof(buf) );
    // buf should now contain: "This is the callback return string"
    }

    ....


    wrapper.py
    ----------

    import ext

    def f1():
    return "This is the callback return string"

    ext.set_callback( f1 )
    ext.do_something()

    ....
     
    djw, Jul 21, 2004
    #1
    1. Advertising

  2. djw <> writes:

    > Hello, I have a couple of questions about some code in a C extension module.
    > First, how would I convert a Python string return value from a call to
    > PyObject_CallFunction into a C void * buffer?


    PyString_AsString(). Do check for NULL return in case the the object
    isn't a string...

    > Second, do I need to call Py_XINCREF() on my callback function?


    Yes, though the code as posted would be fine with Py_INCREF().

    Have you read the fine (well, ish) manuals on this sort of thing?

    Cheers,
    mwh

    --
    ARTHUR: Ford, you're turning into a penguin, stop it.
    -- The Hitch-Hikers Guide to the Galaxy, Episode 2
     
    Michael Hudson, Jul 21, 2004
    #2
    1. Advertising

  3. > PyObject * set_callback( PyObject * self, PyObject * args )
    > {
    > if( !PyArg_ParseTuple( args, "O", &callback ) )
    > {
    > return Py_BuildValue( "i", 1 );
    > }
    > if( !PyCallable_Check( callback ))
    > {
    > return Py_BuildValue( "i", 2 );
    > }
    > Py_XINCREF( callback );
    > }


    You are increasing the refcount here, but before you assign a new
    callback, you will want to decrease the reference to the old callback.
    Your other problem is that if PyArg_ParseTuple fails, it is raising an
    exception. Your function _must_ return NULL or clear the exception.
    Better to return NULL and let the exception get raised.


    > int call_callback( int param1, int param2, void * buf, int size )
    > {
    > PyObject * result;
    >
    > if( callback )
    > {
    > result = PyObject_CallFunction( callback, "ii", param1, param2 );
    > // Need to convert return Python string into buf....????
    > }
    > }


    You will need to get a char* pointer and copy that into your buffer.
    char * str;
    str = PyString_AsString(result);
    if(!str)
    {
    Py_XDECREF(result);
    return NULL;
    }
    strncpy((char*)buf, str, size-1);
    ((char*)buf)[size-1] = 0;
    Py_DECREF(result);
    return 1;


    One last warning, if your program involves multiple threads, make sure
    the Python callback only gets called from the primary thread. You'll
    have a lot of "non-beginner" work to do to safely call the Python
    callback from any thread. Anyways, this is probably not an issue.
     
    Pete Shinners, Jul 22, 2004
    #3
    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. Navain Goksin

    hosting that provides iis app ext mappings

    Navain Goksin, Jul 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    332
    Craig Deelsnyder
    Jul 12, 2004
  2. Gerry Hickman
    Replies:
    2
    Views:
    1,530
    Gerry Hickman
    Dec 14, 2005
  3. Jindal, Pankaj

    ['ext.IsDOMString', 'ext.SplitQName']

    Jindal, Pankaj, Dec 22, 2004, in forum: Python
    Replies:
    0
    Views:
    329
    Jindal, Pankaj
    Dec 22, 2004
  4. Replies:
    11
    Views:
    612
  5. Une bévue
    Replies:
    4
    Views:
    106
    Une bévue
    Sep 1, 2006
Loading...

Share This Page