On 08/28/2014 06:19 PM, Brian Bruggeman wrote: > Hi all, > > I am having trouble with a really simple example, and I am hoping > someone on this list can help me. > > I have created a github repo for code in question: > > git clone g...@github.com:brianbruggeman/boost_python_hello_world.git > > I have also created a stack overflow question: > > http://stackoverflow.com/q/25533329/631199 > > The basic problem is as follows: > > Currently, I am able to compile cleanly, but when executing the code, > I receive a segmentation fault. I've narrowed the seg-fault down to > the line which actually uses boost::python::extract.
As a first step I suggest wrapping the code in a try-block, to catch Python exceptions. You may also try extract::check() to verify whether the object can in fact be converted to the specified type. (In your version a conversion failure would result in an exception.) I would also rewrite the code to use more of the Boost.Python API (import, exec, etc.) All that being said, the code compiles and runs fine for me (Fedora 20), and I don't see anything wrong. Regards, Stefan > > Code: > > #include <boost/python.hpp> > #include <iostream> > > namespace bp = boost::python; > > // Embedding python > int main(int argc, char** argv) { > int data = 0; > Py_Initialize(); > PyRun_SimpleString("data = 1"); > bp::object > module(bp::handle<>(bp::borrowed(PyImport_AddModule("__main__")))); > bp::object dictionary = module.attr("__dict__"); > bp::object data_obj = dictionary["data"]; > // Error: The following line has the segmentation fault... > data = bp::extract<int>(data_obj); > std::cout << "data = " << data << std::endl; > Py_Finalize(); > return 0; > } > > CMakeLists.txt: > > project(hello) > cmake_minimum_required(VERSION 2.8) > > FIND_PACKAGE(PythonInterp) > FIND_PACKAGE(PythonLibs) > FIND_PACKAGE(Boost COMPONENTS python) > > include_directories(${PYTHON_INCLUDE_DIRS} ${Boost_INCLUD_DIRS}) > link_directories(${PYTHON_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS}) > > add_executable(hello say_hello.cpp) > target_link_libraries(hello > ${Boost_LIBRARIES} > ${PYTHON_LIBRARIES}) > > Also for completeness… > > I installed Python and Boost::Python using the following… > > brew install python > brew install boost —with-python > > Thanks so much in advance! > > Brian > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig@python.org > https://mail.python.org/mailman/listinfo/cplusplus-sig -- ...ich hab' noch einen Koffer in Berlin... _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig