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
    }
}
4
  • 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 Commented Sep 15, 2017 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 <numpy/ndarrayobject.h>" instead of "#include <boost/python/numeric.hpp>" and then use some array container within ndarrayobject, but it looks like this won't be possible. Commented Sep 15, 2017 at 11:59
  • If "it's <strike>a</strike> 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 Commented Sep 15, 2017 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. Commented Sep 15, 2017 at 12:16

1 Answer 1

0

The solution is to comment out:

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

and replace it with:

#include <boost/python/numpy.hpp> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.