Finally able to reproduce the "real" problem with a "dummy" example : seems that, at python side, when you use "np.append" C++ get screwed data ?!... (note that with or without "np.append" all is OK at python side). Can somebody help here ? Known problem ? Possible workaround or fix ? Should I open a bug for that ? (if yes where)
Franck >> make; python dummyTest.py ; python dummyTest2.py g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp -lboost_python -lboost_numpy [1 2 3] [1.2 2.3 3.4] ptrInt[0] = 1, 0x5643649d5640 ptrInt[1] = 2, 0x5643649d5644 ptrInt[2] = 3, 0x5643649d5648 ptrFloat[0] = 1.2, 0x5643649d5660 ptrFloat[1] = 2.3, 0x5643649d5664 ptrFloat[2] = 3.4, 0x5643649d5668 [1 2 3] [1.2 2.3 3.4] ptrInt[0] = 1, 0x559e0710e550 ptrInt[1] = 0, 0x559e0710e554 ptrInt[2] = 2, 0x559e0710e558 ptrFloat[0] = 4.17233e-08, 0x559e0710e570 ptrFloat[1] = 1.9, 0x559e0710e574 ptrFloat[2] = 2.72008e+23, 0x559e0710e578 Le dim. 9 févr. 2020 à 10:58, HOUSSEN Franck <fghous...@gmail.com> a écrit : > Oh men ! Totally missed / forget that... Thanks Sefan : the dummy simple > example works now :D > Unfortunately, this dummy example was extracted from a much more complex > one which seems to do things like dummy but still does not work... so I am > still debugging the real case... > Anyway, thanks for the help on the dummy case ! Hope this could help some > other people > > Franck > > Le dim. 9 févr. 2020 à 05:04, Stefan Seefeld <ste...@seefeld.name> a > écrit : > >> >> On 2020-02-08 12:07 p.m., HOUSSEN Franck wrote: >> >> 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 >> >> You need to call np::initialize() in your module, i.e. insert >> >> `np::initialize();` >> >> at the top of your `dummy` module definition. >> >> [image: Stefan] >> -- >> >> ...ich hab' noch einen Koffer in Berlin... >> >> > >
Makefile
Description: Binary data
#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) { boost::python::extract<np::ndarray> lsIntExt(t[0]); if (lsIntExt.check()) { np::ndarray lsInt = lsIntExt(); // Conversion to np::ndarray. int nbInt = lsInt.shape(0); int * ptrInt = reinterpret_cast<int*>(lsInt.get_data()); for(auto k = 0; ptrInt && k < nbInt; k++) {std::cout << "ptrInt[" << k << "] = " << ptrInt[k] << ", " << ptrInt+k << std::endl;}; } boost::python::extract<np::ndarray> lsFloatExt(t[1]); if (lsFloatExt.check()) { np::ndarray lsFloat = lsFloatExt(); // Conversion to np::ndarray. int nbFloat = lsFloat.shape(0); float * ptrFloat = reinterpret_cast<float*>(lsFloat.get_data()); for(auto k = 0; k < nbFloat; k++) {std::cout << "ptrFloat[" << k << "] = " << ptrFloat[k] << ", " << ptrFloat+k << std::endl;}; } } BOOST_PYTHON_MODULE(dummy) { np::initialize(); def("doStuffs", doStuffs); }
#!/usr/bin/env python import numpy as np lsInt = np.array([], dtype='int32') lsInt = np.append(lsInt, 1) lsInt = np.append(lsInt, 2) lsInt = np.append(lsInt, 3) lsFloat = np.array([], dtype='float32') lsFloat = np.append(lsFloat, 1.2) lsFloat = np.append(lsFloat, 2.3) lsFloat = np.append(lsFloat, 3.4) t = (lsInt, lsFloat) import dummy print(lsInt) print(lsFloat) dummy.doStuffs(t)
#!/usr/bin/env python import numpy as np lsInt = np.array([1, 2, 3], dtype='int32') lsFloat = np.array([1.2, 2.3, 3.4], dtype='float32') t = (lsInt, lsFloat) import dummy print(lsInt) print(lsFloat) dummy.doStuffs(t)
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig