Torsten Bronger wrote: > Hallöchen! > > I'd like to script C++ funtions by an embedded Python interpreter. > So far, my C++ main() function contains: > > Py_Initialize(); > Py_InitModule("pp3", PythonMethods); > PyRun_SimpleString("from pp3 import *"); > PyRun_AnyFile(stdin, NULL); > Py_Finalize(); > > "PythonMethods" is the vector of type PyMethodDef that contains the > function descriptors: > > static PyMethodDef PythonMethods[] = { > {"toll", py_toll, METH_VARARGS, ""}, > {NULL, NULL, 0, NULL} > }; > > Then I say "toll()" in the input script which calls py_toll() in the > C++ source. > > > It works. However, is there a way to avoid this dummy "pp3" module > and add the C++ functions directy to the main namespace in the > Python script?
Yes. You can import __builtin__, and add methods to it. This is a snippet from the bdist_wininst code, which embeds Python: http://svn.python.org/view/python/trunk/PC/bdist_wininst/install.c?rev=38414&view=markup PyMethodDef meth[] = { {"create_shortcut", CreateShortcut, METH_VARARGS, NULL}, {"get_special_folder_path", GetSpecialFolderPath, METH_VARARGS, NULL}, {"get_root_hkey", (PyCFunction)GetRootHKey, METH_NOARGS, NULL}, {"file_created", FileCreated, METH_VARARGS, NULL}, {"directory_created", DirectoryCreated, METH_VARARGS, NULL}, {"message_box", PyMessageBox, METH_VARARGS, NULL}, }; ... mod = PyImport_ImportModule("__builtin__"); if (mod) { int i; for (i = 0; i < DIM(meth); ++i) { PyObject_SetAttrString(mod, meth[i].ml_name, PyCFunction_New(&meth[i], NULL)); } } ... Thomas -- http://mail.python.org/mailman/listinfo/python-list