Let python call a C function pointer passed from the C Python API

Discussion in 'Python' started by hvictor, Nov 16, 2009.

  1. hvictor

    hvictor Guest

    I have C++ a void function pointer stored in a variable call. The
    pointed function takes an int and a char* as arguments.

    I have a python module containing this function:

    def yeah(x):
    x(int(0),"text argument")
    return "pointer called"

    As you can see I'm trying to use the argument x of the function like a
    method object.
    In the C++ side I'm doing following (note that I only paste relevant
    parts of code because this system is working fine, C++ is able to call
    function yeah and get its return value, but only with a string-
    oriented test):
    ....
    PyObject *pValue; // the argument for python

    pValue = PyCObject_FromVoidPtr(call,destr); // destr is a void fctn
    ptr, required from the api.

    PyObject *pFunc = PyObject_GetAttrString(pModule, "yeah");

    ....
    PyTuple_SetItem(pArgs, 0, pValue); // pArgs is a pyobject, the
    arguments, I insert pValue in it.

    pValue = PyObject_CallObject(pFunc, pArgs);

    ....

    It does not work. can anyone help me please? I just want python to
    call this function pointer.

    Thank you
    hvictor, Nov 16, 2009
    #1
    1. Advertising

  2. hvictor

    Carl Banks Guest

    On Nov 16, 5:04 am, hvictor <> wrote:
    > I have C++ a void function pointer stored in a variable call. The
    > pointed function takes an int and a char* as arguments.
    >
    > I have a python module containing this function:
    >
    > def yeah(x):
    >         x(int(0),"text argument")
    >         return "pointer called"
    >
    > As you can see I'm trying to use the argument x of the function like a
    > method object.
    > In the C++ side I'm doing following (note that I only paste relevant
    > parts of code because this system is working fine, C++ is able to call
    > function yeah and get its return value, but only with a string-
    > oriented test):
    > ...
    > PyObject *pValue; // the argument for python
    >
    > pValue = PyCObject_FromVoidPtr(call,destr); // destr is a void fctn
    > ptr, required from the api.
    >
    > PyObject *pFunc = PyObject_GetAttrString(pModule, "yeah");
    >
    > ...
    > PyTuple_SetItem(pArgs, 0, pValue); // pArgs is a pyobject, the
    > arguments, I insert pValue in it.
    >
    > pValue = PyObject_CallObject(pFunc, pArgs);
    >
    > ...
    >
    > It does not work. can anyone help me please? I just want python to
    > call this function pointer.


    Python can't call C function pointers. You have to write a function
    in C that accepts a CObject and some arguments, unpacks the arguments,
    retrieves the function pointer from the CObject, and calls it. Quick
    and dirty function that might do that (untested, not robust).


    static PyObject* call_user_void_ptr(PyObject* self, PyObject* args) {
    PyObject* cobj;
    int ival;
    char* sval;
    void (*func)(int,char*);
    if (!PyArg_ParseTuple("O!is",&PyCObject_Type,&cobj,&ival,&sval))
    return 0;
    func = PyCObject_AsVoidPtr(cobj);
    func(ival,sval);
    Py_RETURN_NONE;
    }


    def yeah(x):
    call_user_void_ptr(x,int(i),"text_argument")
    print "pointer called"


    In a pinch, you could call the function pointer from ctypes. Since
    you're already writing a C extension I wouldn't recommend it as a
    final solution, though.


    Carl Banks
    Carl Banks, Nov 16, 2009
    #2
    1. Advertising

  3. hvictor

    sturlamolden Guest

    On 16 Nov, 14:25, Carl Banks <> wrote:

    > Python can't call C function pointers.  


    Yes it can, use ctypes...
    sturlamolden, Nov 16, 2009
    #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. dee
    Replies:
    9
    Views:
    491
    Joseph Byrns
    Apr 15, 2005
  2. Luigi Donatello Asero

    Let or not let the text float

    Luigi Donatello Asero, Jan 15, 2004, in forum: HTML
    Replies:
    6
    Views:
    452
    Steve R.
    Jan 15, 2004
  3. jimjim
    Replies:
    3
    Views:
    389
    Buster
    Apr 16, 2004
  4. Anand
    Replies:
    2
    Views:
    885
    Anand
    Sep 11, 2003
  5. aneuryzma
    Replies:
    3
    Views:
    688
    Jim Langston
    Jun 16, 2008
Loading...

Share This Page