I tried to play with extract::check() without much success : line 13 crashes !?... This is a very simple exemple : would like to get it to work. Could somebody help ? Still googling / searching for a solution
Le ven. 7 févr. 2020 à 22:43, stefan <ste...@seefeld.name> a écrit : > > On 2020-02-07 4:06 p.m., HOUSSEN Franck wrote: > > With boost python, how to pass a tuple of lists from python to C++ ? Code > attached : line 12 crashes ?! Googling but no fix for now. > > You are referring to this line: > > np::ndarray lsInt = boost::python::extract<np::ndarray>(t[0]); > > Note that this line is actually doing two things: > > 1) it creates a boost::python::extract<np::ndarray> object > > 2) it calls conversion operator on it to convert to np::ndarray > > Note that the conversion may fail (for example if the object doesn't > contain an object of that type). In case of doubt, you may want to call > extract::check() to see whether it's actually valid. > > See > https://www.boost.org/doc/libs/1_72_0/libs/python/doc/html/reference/to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract > for details. > > > > [image: Stefan] > > -- > > ...ich hab' noch einen Koffer in Berlin... > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig@python.org > https://mail.python.org/mailman/listinfo/cplusplus-sig > -- Bonne journée, Franck HOUSSEN
#include <iostream> #include <boost/python.hpp> #include <boost/python/numpy.hpp> namespace bp = boost::python; namespace np = boost::python::numpy; void doStuffs(boost::python::tuple & t) { std::cout << "doStuffs" << std::endl; boost::python::extract<np::ndarray> lsIntExt(t[0]); if (lsIntExt.check()) { // std::cerr << "t[0] is a np::ndarray" << std::endl; // np::ndarray lsInt = lsIntExt(); // Conversion to np::ndarray. // int nbInt = lsInt.shape(0); // std::cout << "nbInt " << nbInt << std::endl; // int * ptrInt = reinterpret_cast<int*>(lsInt.get_data()); // std::cout << "ptrInt " << ptrInt << std::endl; // for(auto k = 0; ptrInt && k < nbInt; k++) {std::cout << "ptrInt[" << k << "] = " << ptrInt[k] << std::endl;}; } //else { // std::cerr << "t[0] is not a np::ndarray" << std::endl; //} //boost::python::extract<np::ndarray> lsFloatExt(t[1]); //if (lsFloatExt.check()) { // std::cerr << "t[1] is a np::ndarray" << std::endl; // np::ndarray lsFloat = lsFloatExt(); // Conversion to np::ndarray. // int nbFloat = lsFloat.shape(0); // std::cout << "nbFloat " << nbFloat << std::endl; // float * ptrFloat = reinterpret_cast<float*>(lsFloat.get_data()); // std::cout << "ptrFloat " << ptrFloat << std::endl; // for(auto k = 0; k < nbFloat; k++) {std::cout << "ptrFloat[" << k << "] = " << ptrFloat[k] << std::endl;}; //} //else { // std::cerr << "t[1] is not a np::ndarray" << std::endl; //} } BOOST_PYTHON_MODULE(dummy) { def("doStuffs", doStuffs); }
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig