C API : Creating a Py_Method object from a C function.

Hugh Macdonald hugh.macdonald at gmail.com
Tue Jul 12 09:50:36 EDT 2005


Thanks Martin - that worked wonderfully....


For the record (and for anyone searching for this in future), here's
the code that worked (with names changed to protect my job...)


myPython is the C++/Python interface class containing static methods
which pass on calls to the underlying python module. It's not really
commented, but if you're trying to do this kind of thing, it should
give you what you need.

----------------------------------------------------------------------------------------------------
// myPython.h

#ifndef ___MYPYTHON_H___
#define ___MYPYTHON_H___

#include <list>

using namespace std;

typedef struct _object PyObject;
typedef void   (*loadCallback)(string message, int progress, void
*callbackData);

static    PyObject         *myPython_doLoadCallback(PyObject *self,
PyObject *args);

class myPython {
public:
    static    list<string> *loadDetails(string file, loadCallback
callbackFunc = NULL, void *callbackData = NULL);
    static    void         doLoadCallback(string message, int
progress);
private:
    static    PyObject     *getResults(char *moduleName, char
*functionName, PyObject *args);

    static    loadCallback callbackFunc;
    static    void         *callbackData;
};

#endif // ___MYPYTHON_H___
----------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------
// myPython.cpp

#include <myPython.h>
#include <Python.h>

loadCallback    myPython::callbackFunc = NULL;
void            *myPython::callbackData = NULL;

list<string> *myPython::loadDetails(string file, loadCallback
newCallbackFunc, void *newCallbackData)
{
    PyObject *pArgs, *pResult;

    callbackFunc = newCallbackFunc;
    callbackData = newCallbackData;

    PyMethodDef *callbackFunctionDef = new PyMethodDef;
    callbackFunctionDef->ml_name = "doLoadCallback";
    callbackFunctionDef->ml_meth = &myPython_doLoadCallback;
    callbackFunctionDef->ml_flags = 1;

    PyObject *pyCallbackFunc = PyCFunction_New(callbackFunctionDef,
NULL);

    pArgs = Py_BuildValue("(sO)", file.c_str(), pyCallbackFunc);

    pResult = getResults("myPythonModule", "loadDetails", pArgs);

    if(!pResult)
    {
        Py_DECREF(pArgs);
        return NULL;
    }

    if(PyList_Check(pResult))
    {
        // Convert pResult into a list<string> and return that.
    }

    return NULL;
}

PyObject *myPython_doLoadCallback(PyObject *self, PyObject *args)
{
    char *message;
    int progress;

    if(!PyArg_ParseTuple(args, "si", &message, &progress))
        return NULL;

    myPython::doLoadCallback(message, progress);

    return Py_None;
}

void myPython::doLoadCallback(string message, int progress)
{
    if(callbackFunc)
        callbackFunc(message, progress, callbackData);
}
----------------------------------------------------------------------------------------------------




More information about the Python-list mailing list