boost::python embedding error - runtime - Mac OS X - 1.38
Hi, I am trying to run the following program: #include <iostream> #include <Python.h> #include <boost/python.hpp> 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: 0x00000000 0x00000000 in ?? () (gdb) where #0 0x00000000 in ?? () #1 0x00bb618f in PyEval_GetGlobals () #2 0x00bce0dd in PyImport_Import () #3 0x00bce2b0 in PyImport_ImportModule () #4 0x008127d4 in boost::python::import (name=@0xbffff4a8) at libs/python/src/import.cpp:20 #5 0x00004446 in main (argc=1, argv=0xbffff510) 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.
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 <ikarpov@gmail.com> wrote:
Hi,
I am trying to run the following program:
#include <iostream> #include <Python.h>
#include <boost/python.hpp> 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: 0x00000000 0x00000000 in ?? () (gdb) where #0 0x00000000 in ?? () #1 0x00bb618f in PyEval_GetGlobals () #2 0x00bce0dd in PyImport_Import () #3 0x00bce2b0 in PyImport_ImportModule () #4 0x008127d4 in boost::python::import (name=@0xbffff4a8) at libs/python/src/import.cpp:20 #5 0x00004446 in main (argc=1, argv=0xbffff510) 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.
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
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 <Python.h> #include <boost/python.hpp> 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 <rwgk@yahoo.com> 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
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 <ikarpov@gmail.com> To: Ralf W. Grosse-Kunstleve <rwgk@yahoo.com> Cc: Development of Python/C++ integration <cplusplus-sig@python.org> 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 <Python.h> #include <boost/python.hpp> 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 <rwgk@yahoo.com> 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
Hi, On Thu, Mar 5, 2009 at 10:46 PM, Igor Karpov <ikarpov@gmail.com> wrote:
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 <Python.h> #include <boost/python.hpp> using namespace boost::python;
Not sure it will make a difference in your case, but the boost.python documentation does specify that you should never include Python.h yourself. boost.python does it for you, taking care of several #define issues: http://www.boost.org/doc/libs/1_38_0/libs/python/doc/building.html#include-i... - Thomas
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 <rwgk@yahoo.com> 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
Was this problem ever resolved? I am getting exactly the same issue on OSX Mavericks. I create a new Xcode commandline project, link libpython3.4.1 and libboost_python dylibs, add relevant header-search-paths. Then I modify main.c to: #include <boost/python.hpp> int main( int argc, const char* argv[] ) { Py_Initialize(); using namespace boost::python; object main_module = import("__main__"); // <-- EXC_BAD_ACCESS(code=1, address=0x0) } In fact everything below the error can be removed, giving a minimal failure example. -- View this message in context: http://boost.2283326.n4.nabble.com/boost-python-embedding-error-runtime-Mac-... Sent from the Python - c++-sig mailing list archive at Nabble.com.
participants (4)
-
Igor Karpov -
pi -
Ralf W. Grosse-Kunstleve -
Thomas Berg