0

I need to integrate a cpp extension into python. Therefore I think boost::python might be a good approach. After having a simple example get to run I'm currently having some problems I do not understand. Have a look at the following code:

betainv.cpp

#include <boost/python.hpp>
#include <boost/math/distributions/beta.hpp>

using namespace boost::python;

class betainvClass {
    public: double betainv(double p, double a, double b);
};

double betainvClass::betainv(double p, double a, double b) { 
    return boost::math::ibeta_inv(a, b, p);
}

// Expose classes and methods to Python
BOOST_PYTHON_MODULE(betainv) {
    class_<betainvClass> ("create_betainv_instance")
        .def("betainv", &betainvClass::betainv)
    ;
}

And the corresponding python implementation:

import betainv

beta = betainv.create_betainv_instance()
print "0.25, 0.0342, 170 -> " + str(beta.betainv(0.25, 0.0342, 170))
print "0.25, 0.0342, 171 -> " + str(beta.betainv(0.25, 0.0342, 171))
print "0.25, 0.0342, 172 -> " + str(beta.betainv(0.25, 0.0342, 172))

And the Makefile

TARGET = betainv
PYTHON = /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/include/python2.7
BOOST_INC = /usr/local/include
BOOST_LIB = /usr/local/lib

$(TARGET).so: $(TARGET).o
    g++ -shared -Wl \
    $(TARGET).o -L$(BOOST_LIB) -lboost_python \
    -L/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -lpython2.7 \
    -o betainv.so

$(TARGET).o: $(TARGET).cpp
    g++ -I$(PYTHON) -I$(BOOST_INC) -c $(TARGET).cpp

clean:
    rm -f *.o *.a *.so *~ core

Compilation works fine. However, when executing the program I get the following error:

python betainv.py 
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6

Does anybody have an idea what I am doing wrong here?

toom
  • 12,864
  • 27
  • 89
  • 128

1 Answers1

1

Okay, I got it. Actually the code above is correct. However, I'm using Mac OS X and I linked the boost library against a python installation of Homebrew. After compilation I executed the python code by using the original Apple version of python. Therefore there was a binary incompatibility (for more info see also here: Homebrew + Python on mac os x 10.8: Fatal Python error: PyThreadState_Get: no current thread importing mapnik )

Running the code with the Homebrew python installation works fine.

Thanks for reading any way.

Community
  • 1
  • 1
toom
  • 12,864
  • 27
  • 89
  • 128