← Back to team overview

yade-dev team mailing list archive

Use of boost::python::extract() in Engine::action()

 

Hi,

In connection with the bug [*], I'm trying to familiarize myself with boost::python. As an example, I would like to extract in the c++ world a python integer, something I can achieve outside YADE but not within:


When building my own c++ program (see the attached testBP.cpp) the two following lines of code are OK:

// ***** c++ lines OK outside YADE, not within: *****
  boost::python::object pythonNumber(5);
  int cNumber = boost::python::extract<int>(pythonNumber);
// *****************


On the other hand, when inserted e.g. in NewtonIntegrator::action(), the two exact same lines will induce a segmentation fault, e.g. with the following MWE (note the funny difference between O.step() and O.run()..)

# ***** MWE of YADE script that seg faults with the above c++ lines e.g. in NewtonIntegrator::action() : ****
  O.bodies.append(sphere(Vector3(0,0,0),radius=1))
  O.run(1) #O.step() would work...
# *************


Would anyone have an understanding of this behavior ?

Thanks,

Jérôme

[*] https://bugs.launchpad.net/yade/+bug/1764424


------
Chargé de Recherche / Research Associate
Irstea, RECOVER
3275 route de Cezanne – CS 40061 13182 Aix-en-Provence Cedex 5 FRANCE
+33 (0)4 42 66 99 21

// a test using boost::python::object and boost::python::extract, to compile with:

// g++ -I /usr/include/python2.7/ testBP.cpp -o testBP -lboost_python-py27 -lpython2.7


#include <iostream>
#include <string>
#include <boost/python.hpp>

int main(){

  Py_Initialize(); // not necessary in YADE source code (Py_IsInitialized() already equals 1 therein, logically..)

  boost::python::object pythonNumber(5);
  int cNumber = boost::python::extract<int>(pythonNumber);
  
  std::cout << "Number extracted:" << cNumber <<  std::endl;
  return 0;
}

// ./testBP will output the expected message, wo any segmentation fault