views:

83

answers:

1

I'm currently writing an applications that embedds the python interpreter. The idea is to have the program call user specified scripts on certain events in the program. I managed this part but now I want the scripts to be able to call functions in my program.

Here's my code so far:

#include "python.h"


static PyObject* myTest(PyObject* self,PyObject *args)
{
    return Py_BuildValue("s","123456789");
}

static PyMethodDef myMethods[] = {{"myTest",myTest},{NULL,NULL}};

int main()
{

    Py_Initialize();
    Py_InitModule("PROGRAM",myMethods);

    PyRun_SimpleString("print PROGRAM.myTest()");


    Py_Finalize();
}

Thanks!

+1  A: 

You need to bind that function to some module, see http://docs.python.org/extending/embedding.html#extending-embedded-python

Edit: Basicly your code should work. Whats not working?

evilpie
Haha, a single PyRun_SimpleString("import PROGRAM"); was all that needed to get it to work!
monoceres
Oh right, i didnt recognize that either.
evilpie