Embedding Python and adding C functions to the interpreter

Posted by monoceres on Stack Overflow See other posts from Stack Overflow or by monoceres
Published on 2010-05-11T14:36:43Z Indexed on 2010/05/11 15:14 UTC
Read the original article Hit count: 247

Filed under:
|
|
|
|

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!

© Stack Overflow or respective owner

Related posts about python

Related posts about embedded