1

I'm using Python 2.7.10 on a Mac (10.12.6). Apologies in advance for any silly comments: I am a Python newbie and also am not that experienced with C++.

I need to compile an old C++-based code that interfaces with Python. A couple of the .cpp files use the deprecated "boost/python/numeric.hpp" as an include and for making arrays:

#include <boost/python/numeric.hpp>
const py::numeric::array& X

I have spent a couple of days now on trying to figure out how to deal with this, but just can't turn up any documentation that makes sense as a newbie. I would be very grateful if someone would please point me in the right direction. One thing I did come across was this post:

how to return numpy.array from boost::python?

where one suggestion involves using

#include <numpy/ndarrayobject.h>

However, I just cannot figure out how to use it to translate my old code.

Thank you for any help.


Edit in response to sehe: The code is too large to post in its entirety, but the errors all seem to be related to:

#include <boost/python/numeric.hpp>

I'm pretty sure I need to use:

#include <numpy/ndarrayobject.h>

However, I can't figure out how to change the code that refers to the arrays. This seems like such a simple thing, but I haven't been able to find any documentation. Below are specific examples of where the deprecated code is an issue (please let me know if I can make this more understandable)

In the main() file "PyMain.cpp":

// Include from Boost
#include <boost/python.hpp>
namespace py = boost::python;         

// Other includes for the code PyCalculator
# XXX...
# XXX...

BOOST_PYTHON_MODULE(PyCalculator)
{
    // Set numpy as the numeric::array engine
    py::numeric::array::set_module_and_type("numpy", "ndarray");

XXX // (Other non-problematic code
}

Here's an example of another file, "Auxiliary .cpp"

The error shown in the terminal is:

PyAux.cpp: fatal error: 
          'boost/python/numeric.hpp' file not found


#include "PyAux.hpp"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#include <numpy/ndarrayobject.h>

// Boost includes
#include <boost/python.hpp>
#include <boost/python/numeric.hpp>
#include <boost/python/slice.hpp>
#include <boost/smart_ptr.hpp>
namespace py = boost::python;

// Other includes
#include <PyAux/Math/Other/Core>

namespace PyCalculator {
... XXX
// There are functions like:
    static auto initializeWithArray(const py::numeric::array& array) -> boost::shared_ptr<VectorType>
    { //...XXX 
        const py::numeric::array& X
    }
}
Ant
  • 753
  • 1
  • 9
  • 24
  • If you have a small, self-contained example of a minimal C++ module and Python user-code, I (or someone else) could try my hand at it – sehe Sep 15 '17 at 11:52
  • Thank you, sehe. I would really appreciate that. I'll try to prepare one (it's a huge code). I was hoping that it would be possible to simply use "#include " instead of "#include " and then use some array container within ndarrayobject, but it looks like this won't be possible. – Ant Sep 15 '17 at 11:59
  • If "it's a huge code" then clearly it's beyond the scope of your question. Your question is specifically about the interface code, which should be doable <40 LoC – sehe Sep 15 '17 at 12:11
  • Yes, you're right, sehe. All the errors are specifically related to boost/python/numeric.hpp. My challenge is coming up with a representative example as a newbie. I'm working on it, but it might take me a little while! Thanks. – Ant Sep 15 '17 at 12:16

1 Answers1

0

The solution is to comment out:

//#include <boost/python/numeric.hp

and replace it with:

#include <boost/python/numpy.hpp> 
Brndn
  • 676
  • 1
  • 7
  • 21