boost python异常处理_间歇性的错误返回与Boost.Python的内部参考

I have the following class:

#include

template

class Point {

private:

std::array 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_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_<:is_class>, boost::is_union, mpl_::bool_, mpl_::bool_, mpl_::bool_ >::************)’

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值