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.
+2
A:
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/
Joe
2010-10-17 00:35:06
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!
2010-10-17 00:57:06