【问题标题】:Return same underlying Python boost::python::object from function returning shared_ptr从返回 shared_ptr 的函数返回相同的底层 Python boost::python::object
【发布时间】:2012-12-28 23:59:23
【问题描述】:

在我的应用程序中,我通过 boost::shared_ptr 通过 boost::python 包装 C++ 对象。

我遇到的问题是,每当我在 C++ 中返回一个引用时,boost 都会分配一个新对象来指向该引用,而不是重用现有的 Python 对象。除其他外,这会破坏可能已在 Python 中设置的相等测试和自定义属性。

每次我给它一个指向同一个底层对象的 shared_ptr 时,是否有一种干净、简单的方法可以让 boost::python 自动重用现有的 boost::python::object(或至少 PyObject)?

示例代码:

C++:

#include <boost/python.hpp>
using namespace boost::python;

class Apple {};

boost::shared_ptr<Apple> theMoldyApple(new Apple());

boost::shared_ptr<Apple> getMoldyApple() {
    //pretend we do some logic here that won't always return theMoldyApple 
    //exactly
    return theMoldyApple;
}

BOOST_PYTHON_MODULE(bobtest) {
    class_<Apple, boost::shared_ptr<Apple>>("Apple", init<>());

    def("getMoldyApple", getMoldyApple);
}

Python:

from bobtest import *

# Set a custom property on the Python object:
ma = getMoldyApple()
ma.customProp = 11

# This works fine because ma is the same PyObject is was above:
print "ma.customProp:", ma.customProp

# This doesn't work because boost::python wraps a copy of the shared_ptr
# (that points to theMoldyApple) with a new PyObject each time I call
# getMoldyApple()
print "getMoldyApple().customProp:", getMoldyApple().customProp

结果:

ma.customProp: 11
getMoldyApple().customProp:
Traceback (most recent call last):
  File "<string>", line 14, in <module>
AttributeError: 'Apple' object has no attribute 'customProp'

想要的结果:

ma.customProp: 11
getMoldyApple().customProp: 11

【问题讨论】:

  • 你有没有找到一个优雅的解决方案来解决这个问题?我在尝试向 Boost.Python 返回的对象添加一些 Python 属性时遇到了同样的事情。

标签: boost-python


【解决方案1】:

你需要定义如下:

class_<Apple>("Apple", no_init)
    .def("__init__", make_constructor(getMoldyApple))
;

【讨论】:

    【解决方案2】:

    您需要创建一个包含 Python 对象的包装类,并使用has_back_reference 通知 Boost。

    http://www.boost.org/doc/libs/1_55_0/libs/python/doc/v2/has_back_reference.html

    【讨论】:

      【解决方案3】:

      我在代码审查中发布了一个使用自定义转换器和地图作为缓存的通用解决方案:

      https://codereview.stackexchange.com/questions/130044/same-pyobject-for-a-shared-ptr-c-object

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-28
        • 2020-09-07
        • 2016-04-26
        相关资源
        最近更新 更多