[Python] Embedding and Extending problem

I've tried to merge the Boost.Python tutorials for embedding and extending (see below), to end up with an executable that does both. The script fails to import "hello", so BOOST_PYTHON_MODULE is presumably not the correct way to do it. What is? Thanks, Keith MacDonald ====== The C++ code ====== #include <boost/python.hpp> using namespace boost::python; char const* greet() { return "hello, embedded world"; } BOOST_PYTHON_MODULE(hello) { def("greet", greet); } int main() { Py_Initialize(); object main_module(( handle<>(borrowed(PyImport_AddModule("__main__"))))); object main_namespace = main_module.attr("__dict__"); try { handle<> ignored((PyRun_String( "import hello\n" "print hello.greet()\n" , Py_file_input , main_namespace.ptr() , main_namespace.ptr()) )); } catch(error_already_set) { PyErr_Print(); } Py_Finalize(); return 0; } ====== The Jamfile ====== subproject libs/python/example/tutorial/embedded ; # location of the program # bring in the rules for python SEARCH on python.jam = $(BOOST_BUILD_PATH) ; include python.jam ; exe hello # name of the executable : #sources hello.cpp : # requirements <find-library>boost_python <library-path>c:\boost\libs\python $(PYTHON_PROPERTIES) <library-path>$(PYTHON_LIB_PATH) <find-library>$(PYTHON_EMBEDDED_LIBRARY) ;

Keith MacDonald wrote:
I've tried to merge the Boost.Python tutorials for embedding and extending (see below), to end up with an executable that does both. The script fails to import "hello", so BOOST_PYTHON_MODULE is presumably not the correct way to do it. What is?
Thanks, Keith MacDonald
====== The C++ code ====== #include <boost/python.hpp>
using namespace boost::python;
char const* greet() { return "hello, embedded world"; }
BOOST_PYTHON_MODULE(hello) { def("greet", greet); }
int main() { Py_Initialize();
object main_module(( handle<>(borrowed(PyImport_AddModule("__main__")))));
object main_namespace = main_module.attr("__dict__");
try { handle<> ignored((PyRun_String( "import hello\n"
I think you need something like: if (PyImport_AppendInittab("hello", init_hello) == -1) throw std::runtime_error("Failed to add embedded_hello to the interpreter's builtin modules"); after Py_Initialize - Volodya

Thanks for the suggestion. It results in "error C2065: 'init_hello' : undeclared identifier". I note that the documentation for PyImport_AppendInittab requires it be called before Py_Initialize, but that makes no difference. Is init_hello supposed to be part of BOOST_PYTHON_MODULE's magic? Thanks, Keith MacDonald "Vladimir Prus" <ghost@cs.msu.su> wrote in message news:d48dqs$6pd$1@sea.gmane.org...
I think you need something like:
if (PyImport_AppendInittab("hello", init_hello) == -1) throw std::runtime_error("Failed to add embedded_hello to the interpreter's builtin modules");
after Py_Initialize
- Volodya

Keith MacDonald wrote:
Thanks for the suggestion. It results in "error C2065: 'init_hello' : undeclared identifier". I note that the documentation for PyImport_AppendInittab requires it be called before Py_Initialize, but that makes no difference. Is init_hello supposed to be part of BOOST_PYTHON_MODULE's magic?
Should be "inithello", in fact. You can see the result of my earlier experiments with embedding + extending at http://zigzag.cs.msu.su/~ghost/e2.cpp At least it compiles ;-) - Volodya

That did the trick, thanks! I'll have a look at your source file. - Keith MacDonald "Vladimir Prus" <ghost@cs.msu.su> wrote in message news:d48g06$es7$1@sea.gmane.org...
Keith MacDonald wrote:
Should be "inithello", in fact. You can see the result of my earlier experiments with embedding + extending at
http://zigzag.cs.msu.su/~ghost/e2.cpp
At least it compiles ;-)
- Volodya
participants (2)
-
Keith MacDonald
-
Vladimir Prus