[C++-sig] (no subject)
David Abrahams
dave at boost-consulting.com
Wed Oct 12 02:53:49 CEST 2005
"jan.walter.berlin at t-online.de" <jan.walter.berlin at t-online.de> writes:
> Hi,
>
> Here is a question about Boost.Python and embedding/extending:
>
> I would like to read a Python script from a file within a plugin of
> a 3D application. The Python script should contain some commands
> (functions) which are NOT part of Python but should be implemented
> in C++ within the SAME plugin.
What do you mean by "not part of Python?" Do you want your Python
script to contain C++ code?
> I've read the Tutorial Introduction ( http://
> www.boost.org/libs/python/doc/tutorial/doc/html/index.html ) and
> understand how to extend Python (creating a shared library) and how
> to use the interpreter from within my plugin (embedding) but how can
> I mix both approaches?
>
> Something like this:
>
> #include <Python.h>
> ...
> #include <boost/python.hpp>
> using namespace boost::python;
> ...
> char const* greet()
> {
> return "hello, world";
> }
>
> BOOST_PYTHON_MODULE(hello)
> {
> def("greet", greet);
> }
> ...
> void myCallback()
> {
> ...
> Py_Initialize();
> object main_module((handle<>(borrowed(PyImport_AddModule("__main__")))));
> object main_namespace = main_module.attr("__dict__");
> handle<> result((allow_null(PyRun_String(
> "import greet\n"
> "print hello.greet()",
> Py_file_input,
> main_namespace.ptr(),
> main_namespace.ptr()))));
> if (!result) PyErr_Print();
> ...
> Py_Finalize();
Don't call Py_Finalize; it's not compatible with Boost.Python (or
vice-versa).
> ...
> }
>
> This results in:
>
> Traceback (most recent call last):
> File "<string>", line 1, in ?
> ImportError: No module named greet
Of course it does; your module is called "hello."
> I know that I could compile the extending part separately and put
> the shared library somewhere where Python can find it but what I
> really want is that I end up calling some C++ functions in my plugin
> code by using a Python script which will run within the embedded
> Python interpreter ... Any ideas?
I don't understand the question, I'm afraid.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list