Am 18.11.2011 12:49, schrieb Pekka Kytölä:
What I'd like to do is that after fetching those SDK function
pointers I'd like to fire up .py/.pyc that snoops for possible
plugins written in python and registers those plugins and callbacks
and let them react to events. Obviously this python code needs access
to those SDK functions. Essentially it would show for the host app as
one .dll but registers multiple plugins from different python files.
Okay, so you want to export functions from your host application (it
doesn't matter if that is inside a DLL or not) to a bunch of Python
plugins. This is actually the embedding that I hinted at and documented,
but here's some example code (stripped of any error handling and
reference counting, so beware!):
// append line to debug log
static PyObject*
log_string(PyObject *self, PyObject *args)
{
char const* arg = 0;
if(!PyArg_ParseTuple(args, "s", &arg))
return NULL;
fprintf(debug_output, "%s", arg);
Py_RETURN_NONE;
}
// exported API
static PyMethodDef host_api[] =
{
{"log_string", log_string, METH_VARARGS,
"log_string(string) -> None\n"
"Write text to debug output."},
{NULL, NULL, 0, NULL}
};
Py_Initialize();
PyObject* host_api_module = Py_InitModule("host", host_api);
PyObject* test_module = PyImport_ImportModule("./test.py");
PyObject* main_function = PyObject_GetAttrString(test_module, "main");
PyObject* res = PyObject_CallObject(main_function, NULL);
Py_Finalize();
This has the disadvantage that it blocks, for multiple Python threads,
you need to program a dispatcher. Also, you can only have a single
interpreter loaded, so other parts of your program may not call
Py_Initialize().
It does the job for my uses, so I hope it helps!
Uli
--
http://mail.python.org/mailman/listinfo/python-list