[C++-sig] boost::python embedding error - runtime - Mac OS X - 1.38
Hi, I am trying to run the following program: #include #include #include using namespace boost::python; using namespace std; int main(int argc, char** argv) { { // Using Python/C Py_Initialize(); PyObject* main_module = PyImport_ImportModule("__main__"); PyObject* globals = PyEval_GetGlobals(); PyObject* locals = PyEval_GetLocals(); PyRun_SimpleString("print 'Hello World, from Python/C!'\n"); Py_Finalize(); } { // Using boost::python Py_Initialize(); object main_module = import("__main__"); object main_namespace = main_module.attr("__dict__"); try { object ignored = exec("print 'Hello World, from boost::python!'\n", main_namespace); Py_Finalize(); } catch (error_already_set const& e) { PyErr_Print(); return 1; } } return 0; } on Linux (both x86 and x86_64), with Ubuntu's default version of Boost (1.34.1) installed, I am able to do so as expected: $ ./embed Hello World, from Python/C! Hello World, from boost::python! on Mac OS X, I get a runtime error: $ ./embed Hello World, from Python/C! Bus Error Running with gdb, this is the stack trace: Hello World, from Python/C! Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x 0x in ?? () (gdb) where #0 0x in ?? () #1 0x00bb618f in PyEval_GetGlobals () #2 0x00bce0dd in PyImport_Import () #3 0x00bce2b0 in PyImport_ImportModule () #4 0x008127d4 in boost::python::import (na...@0xb4a8) at libs/python/src/import.cpp:20 #5 0x4446 in main (argc=1, argv=0xb510) at embed.cc:22 For this Mac, boost version is latest (1.38) and python version is: Python 2.5.4 (r254:67917, Dec 23 2008, 14:57:27) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin Is this a bug? I attached the source and the cmake file I am using to build, for convenience. Thanks, --Igor. # require at least cmake 2.6 IF(COMMAND cmake_minimum_required) CMAKE_MINIMUM_REQUIRED(VERSION 2.6) ENDIF(COMMAND cmake_minimum_required) # the project is called OpenNERO PROJECT(embed) # find the Boost C++ libraries SET(Boost_ADDITIONAL_VERSIONS 1.37 1.37.0 1.38 1.38.0) FIND_PACKAGE (Boost COMPONENTS python filesystem serialization system date_time) IF (${Boost_MINOR_VERSION} LESS 35) FIND_PACKAGE (Boost COMPONENTS python filesystem serialization date_time) ENDIF (${Boost_MINOR_VERSION} LESS 35) IF (Boost_FOUND) MESSAGE(STATUS "Found boost libraries in ${Boost_INCLUDE_DIR} as ${Boost_LIBRARIES}") ELSE (Boost_FOUND) MESSAGE(FATAL_ERROR "Boost libraries were not found") ENDIF (Boost_FOUND) # Find the Python libraries FIND_PACKAGE ( PythonLibs ) IF (NOT PYTHON_FOUND AND PYTHON_LIBRARIES) SET(PYTHON_FOUND "YES") ELSE (NOT PYTHON_FOUND AND PYTHON_LIBRARIES) SET(PYTHON_FOUND "NO") ENDIF(NOT PYTHON_FOUND AND PYTHON_LIBRARIES) IF (PYTHON_FOUND) MESSAGE(STATUS "Found Python libraries in ${PYTHON_INCLUDE_PATH} as ${PYTHON_LIBRARIES}") ELSE (PYTHON_FOUND) MESSAGE(FATAL_ERROR "Python libraries not found") ENDIF (PYTHON_FOUND) INCLUDE_DIRECTORIES ( ${Boost_INCLUDE_DIR} ) INCLUDE_DIRECTORIES ( ${PYTHON_INCLUDE_PATH} ) ADD_EXECUTABLE(embed embed.cc) TARGET_LINK_LIBRARIES (embed ${PYTHON_LIBRARIES}) TARGET_LINK_LIBRARIES (embed ${Boost_LIBRARIES}) embed.cc Description: Binary data ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] boost::python embedding error - runtime - Mac OS X - 1.38
BTW, this seems specific to 1.38.0 version of Boost - I just downgraded to 1.34.1 on the same mac, and got the expected "Hello World from boost::python!". --Igor. On Thu, Mar 5, 2009 at 12:47 PM, Igor Karpov wrote: > Hi, > > I am trying to run the following program: > > #include > #include > > #include > using namespace boost::python; > > using namespace std; > > int main(int argc, char** argv) { > > { // Using Python/C > Py_Initialize(); > PyObject* main_module = PyImport_ImportModule("__main__"); > PyObject* globals = PyEval_GetGlobals(); > PyObject* locals = PyEval_GetLocals(); > PyRun_SimpleString("print 'Hello World, from Python/C!'\n"); > Py_Finalize(); > } > > { // Using boost::python > Py_Initialize(); > object main_module = import("__main__"); > object main_namespace = main_module.attr("__dict__"); > try { > object ignored = exec("print 'Hello World, from boost::python!'\n", > main_namespace); > Py_Finalize(); > } catch (error_already_set const& e) { > PyErr_Print(); > return 1; > } > } > > return 0; > } > > on Linux (both x86 and x86_64), with Ubuntu's default version of Boost > (1.34.1) installed, I am able to do so as expected: > > $ ./embed > Hello World, from Python/C! > Hello World, from boost::python! > > on Mac OS X, I get a runtime error: > $ ./embed > Hello World, from Python/C! > Bus Error > > Running with gdb, this is the stack trace: > > Hello World, from Python/C! > > Program received signal EXC_BAD_ACCESS, Could not access memory. > Reason: KERN_PROTECTION_FAILURE at address: 0x > 0x in ?? () > (gdb) where > #0 0x in ?? () > #1 0x00bb618f in PyEval_GetGlobals () > #2 0x00bce0dd in PyImport_Import () > #3 0x00bce2b0 in PyImport_ImportModule () > #4 0x008127d4 in boost::python::import (na...@0xb4a8) at > libs/python/src/import.cpp:20 > #5 0x4446 in main (argc=1, argv=0xb510) at embed.cc:22 > > For this Mac, boost version is latest (1.38) and python version is: > > Python 2.5.4 (r254:67917, Dec 23 2008, 14:57:27) > [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin > > Is this a bug? I attached the source and the cmake file I am using to > build, for convenience. > > Thanks, > > --Igor. > ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] boost::python embedding error - runtime - Mac OS X - 1.38
>Py_Finalize(); Boost.Python doesn't support Py_Finalize(). Could you try again without? It is a long-standing known issue, e.g. http://mail.python.org/pipermail/cplusplus-sig/2005-November/009543.html ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] boost::python embedding error - runtime - Mac OS X - 1.38
Well, the version with Py_Finalize() runs on the same machine with 1.34.1, so I don't think that's the problem. However, paring down the code to: #include #include using namespace boost::python; int main(int argc, char** argv) { { // Using boost::python Py_Initialize(); object main_module = import("__main__"); object main_namespace = main_module.attr("__dict__"); try { object ignored = exec("print 'Hello World, from boost::python!'\n", main_namespace); } catch (error_already_set const& e) { PyErr_Print(); return 1; } } return 0; } Will re-create the same Bus error at runtime... --Igor. On Thu, Mar 5, 2009 at 3:26 PM, Ralf W. Grosse-Kunstleve wrote: > >> Py_Finalize(); > > > Boost.Python doesn't support Py_Finalize(). Could you try again without? > It is a long-standing known issue, e.g. > http://mail.python.org/pipermail/cplusplus-sig/2005-November/009543.html > ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] boost::python embedding error - runtime - Mac OS X - 1.38
Sorry I cannot help any further; I've never used embedding myself. I hope someone else will step in. For the archives: http://www.boost.org/doc/libs/1_38_0/libs/python/todo.html#pyfinalize-safety I believe you're just getting lucky if Py_Finalize() doesn't crash the process. - Original Message From: Igor Karpov To: Ralf W. Grosse-Kunstleve Cc: Development of Python/C++ integration Sent: Thursday, March 5, 2009 1:46:26 PM Subject: Re: [C++-sig] boost::python embedding error - runtime - Mac OS X - 1.38 Well, the version with Py_Finalize() runs on the same machine with 1.34.1, so I don't think that's the problem. However, paring down the code to: #include #include using namespace boost::python; int main(int argc, char** argv) { { // Using boost::python Py_Initialize(); object main_module = import("__main__"); object main_namespace = main_module.attr("__dict__"); try { object ignored = exec("print 'Hello World, from boost::python!'\n", main_namespace); } catch (error_already_set const& e) { PyErr_Print(); return 1; } } return 0; } Will re-create the same Bus error at runtime... --Igor. On Thu, Mar 5, 2009 at 3:26 PM, Ralf W. Grosse-Kunstleve wrote: > >>Py_Finalize(); > > > Boost.Python doesn't support Py_Finalize(). Could you try again without? > It is a long-standing known issue, e.g. > http://mail.python.org/pipermail/cplusplus-sig/2005-November/009543.html > ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig