[C++-sig] boost::python embedding error - runtime - Mac OS X - 1.38

Igor Karpov ikarpov at gmail.com
Thu Mar 5 19:47:14 CET 2009


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.
-------------- next part --------------
# 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})
-------------- next part --------------
A non-text attachment was scrubbed...
Name: embed.cc
Type: application/octet-stream
Size: 934 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20090305/b2abdeba/attachment.obj>


More information about the Cplusplus-sig mailing list