intermittent error returning an internal reference with boost.python

北慕城南 提交于 2019-12-11 03:12:16

问题


I have the following class:

#include <array>

template<unsigned short D>
class Point {
private:
    std::array<float, D> coordinates;
public:
    Point() { for(int i=D-1; i>=0; --i) coordinates[i] = 0.0; }
    Point(const Point& rhs) = default;
    Point& operator=(const Point& rhs) = default;
    ~Point() = default;

    float& get_ref(const unsigned short dimension)
        { return coordinates[dimension-1]; }
};

I'm trying to wrap it with:

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(fernpy) {
    using namespace boost::python;

    class_< Point<2> >("point")
        .def("__call__", &Point<2>::get_ref, return_internal_reference<>());
}

I'm using gcc-4.7 to compile for boost 1.48, python-2.7 on Fedora 17. All the code is a file called testpy.cpp. I'm using these commands to compile:

g++ -std=c++11 -g -fPIC -I/usr/include/python2.7 -c testpy.cpp
g++ -shared -g -lpython2.7 -lboost_python -o libfern.so testpy.o

The compiler returns a bunch of boost internal errors, too many to post here. This excerpt seems to be the core of it. There are a bunch of "required from"s before it and "note"s after.

/usr/include/boost/python/object/make_instance.hpp:27:9: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::or_<boost::is_class<float>, boost::is_union<float>, mpl_::bool_<false>, mpl_::bool_<false>, mpl_::bool_<false> >::************)’

It works just fine if I return a plain float from get_ref and remove the return_internal_reference<>() argument from the .def line of the wrapper. It's weird because I'm doing the same thing with another, more complicated class template and it works just fine there too. I've been Googling and banging my head against this for almost a full day now. Anybody have any idea what the heck is going on?

UPDATE:

I ended up using the "getitem" and "setitem" special methods from python, a la this link. The link shows how to define a nifty struct template with static wrappers for access functions, so you don't have to mess with the interface to your original C++ class.


回答1:


From a python point-of-view, floats are an immutable-type. As such, python does not allow changing the value.

For example, the following occurs in python:

coordinates = [ 5, 10, 15 ]
x = cooardinates[ 2 ] # Bind x to refer to the int(15) object.
x = 5                 # Rebind x to refer to the int(5) object. 
                      # Does not modify coordinates.

Now, consider the following:

from fernpy import point
p = point()
x = p(2) # Bind x to refer to the float(p(2)) object.
x = 5    # Rebind x to refer to the int(5) object.
         # Does not set p.coordinates[2] to 5.

Thus, boost::python prevents returning reference to types that will be immutable in python because Python does not support it. x does not store the value 5; instead, it contains a reference to the 5 object. If assigning to x did not rebind x, then nonsensical statements, such as 6 = 5 would be possible.

The compile error is a static check that limits return_internal_reference to only work with classes or unions, as those will be mutable types within Python. I imagine that the 'more complicated class template' that works is returning a reference to a user-type.




回答2:


The short answer is probably that you don't want to return an internal reference to a float. In Python, numbers are immutable, so just returning a copy is safer, and sacrifices no features or speed.

If you want to return something more complicated (such as a list or reference to another wrapped class), you can, but it's still almost always not what you want; the dependency of one object on the other introduces fragility. If you want to be able to modify the internal state of an object, you're probably better off using getters and setters, and copying the data in and out.



来源:https://stackoverflow.com/questions/11143634/intermittent-error-returning-an-internal-reference-with-boost-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!