0

How can I extend an embedded interpreter with C++ code? I have embedded the interpreter and I can use boost.python to make a loadable module (as in a shared library) but I don't want the library floating around because I want to directly interface with my C++ application. Sorry if my writing is a bit incoherent.

1 Answer 1

2

At least for the 2.x interpreters: you write your methods as C-style code with PyObject* return values. They all basically look like:

PyObject* foo(PyObject *self, PyObject *args);

Then, you collect these methods in a static array of PyMethodDef:

static PyMethodDef MyMethods[] =
{
    {"mymethod", foo, METH_VARARGS, "What my method does"},
    {NULL, NULL, 0, NULL}
};

Then, after you've created and initialized the interpreter, you can add these methods "into" the interpreter via the following:

Py_InitModule("modulename", MyMethods);

You can refer now to your methods via the modulename you've declared here.

Some additional info here: http://www.eecs.tufts.edu/~awinsl02/py_c/

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

1 Comment

Thank you! I was looking at this kind of stuff for ages, it just didn't seem like what I wanted. Now that you've put it like this my puny mind can understand! Thank you!

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.