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 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Haha, a single PyRun_SimpleString("import PROGRAM"); was all that needed to get it to work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.