如何使用 boost::p ython::迭代器与return_internal_reference

How to use boost::python::iterator with return_internal_reference?

本文关键字:return internal reference ython 何使用 boost 迭代器      更新时间:2023-10-16

>我有一个无法复制的类Type,它不包含默认构造函数。我有二等A,充当上述类的集合。第二个类通过迭代器提供访问权限,我的迭代器具有取消引用运算符:

class A {
    class iterator {
        [...]
      public:
        Type & operator*()
        { 
            return instance;
        }
      private:
        Type instance;
    }
    [...]
};

现在要公开我编写了一个看起来像这样的boost::python代码:

class_<A>("A", [...])
    .def("__iter__", iterator<A, return_internal_reference<> >())
    .def("__len__", container_length_no_diff<A, A::iterator>)
;

在将打印消息添加到代码 Python 的所有迭代器操作(构造、赋值、取消引用、销毁)后,如下所示:

for o in AInstance:
    print o.key

我得到输出(修剪到重要部分):

construct 0xffffffff7fffd3e8
dereference: 0xffffffff7fffd3e8
destroy 0xffffffff7fffd3e8
get key 0xffffffff7fffd3e8

在上面的代码中,这些地址只是instance成员的地址(或方法调用中的this)。前三行由iterator生产,第四行由Type用吸气剂法打印。因此,不知何故boost::python以这样的方式包装所有内容:

  1. 创建迭代器
  2. 取消引用迭代器并存储引用
  3. 销毁迭代器(及其包含的对象)
  4. 使用在步骤二中获得的参考

很明显,return_internal_reference的行为并不像所述那样(请注意,它实际上只是 typedef over with_custodian_and_ward_postcall<> ),只要引用方法调用的结果,它就应该保留对象。

所以我的问题是我如何使用boost::python向 Python 公开这样的迭代器?

编辑:

正如有人指出的那样,可能不清楚:原始容器不包含类型 Type .它包含一些BaseType对象,我可以从中构造/修改Type对象。所以iterator在上面的例子中就像transform_iterator.

如果A是拥有Type实例的容器,那么请考虑让A::iterator包含Type的句柄,而不是Type

class iterator {
  [...]
private:
  Type* instance; // has a handle to a Type instance.
};

而不是:

class iterator {
  [...]
private:
  Type instance; // has a Type instance.
};

在 python 中,迭代器将包含对它迭代的容器的引用。 这将延长可迭代对象的生存期,并防止在迭代期间对可迭代对象进行垃圾回收。

>>> from sys import getrefcount
>>> x = [1,2,3]
>>> getrefcount(x)
2 # One for 'x' and one for the argument within the getrefcount function.
>>> iter = x.__iter__()
>>> getrefcount(x)
3 # One more, as iter contains a reference to 'x'.

boost::python支持此行为。 这是一个示例程序,Foo是无法复制的简单类型; FooContainer是一个可迭代的容器;并且FooContainer::iterator成为迭代器:

#include <boost/python.hpp>
#include <iterator>
// Simple example type.
class Foo
{
public:
  Foo()  { std::cout << "Foo constructed: " << this << std::endl; }
  ~Foo() { std::cout << "Foo destroyed:   " << this << std::endl; }
  void set_x( int x ) { x_ = x;    }
  int  get_x()        { return x_; }
private:
  Foo( const Foo& );            // Prevent copy.
  Foo& operator=( const Foo& ); // Prevent assignment.
private:
  int x_;  
};
// Container for Foo objects.
class FooContainer
{
private:
  enum { ARRAY_SIZE = 3 };
public:
  // Default constructor.
  FooContainer()
  {
    std::cout << "FooContainer constructed: " << this << std::endl;
    for ( int i = 0; i < ARRAY_SIZE; ++i )
    {
      foos_[ i ].set_x( ( i + 1 ) * 10 );
    }
  }
  ~FooContainer()
  {
    std::cout << "FooContainer destroyed:   " << this << std::endl;
  }
  // Iterator for Foo types.  
  class iterator
    : public std::iterator< std::forward_iterator_tag, Foo >
  {
    public:
      // Constructors.
      iterator()                      : foo_( 0 )        {} // Default (empty).
      iterator( const iterator& rhs ) : foo_( rhs.foo_ ) {} // Copy.
      explicit iterator(Foo* foo)     : foo_( foo )      {} // With position.
      // Dereference.
      Foo& operator*() { return *foo_; }
      // Pre-increment
      iterator& operator++() { ++foo_; return *this; }
      // Post-increment.     
      iterator  operator++( int )
      {
        iterator tmp( foo_ );
        operator++();
        return tmp;
      }
      // Comparison.
      bool operator==( const iterator& rhs ) { return foo_ == rhs.foo_; }
      bool operator!=( const iterator& rhs )
      {
        return !this->operator==( rhs );
      }
    private:
      Foo* foo_; // Contain a handle to foo; FooContainer owns Foo.
  };
  // begin() and end() are requirements for the boost::python's 
  // iterator< container > spec.
  iterator begin() { return iterator( foos_ );              }
  iterator end()   { return iterator( foos_ + ARRAY_SIZE ); }
private:
  FooContainer( const FooContainer& );            // Prevent copy.
  FooContainer& operator=( const FooContainer& ); // Prevent assignment.
private:
  Foo foos_[ ARRAY_SIZE ];
};
BOOST_PYTHON_MODULE(iterator_example)
{
  using namespace boost::python;
  class_< Foo, boost::noncopyable >( "Foo" )
    .def( "get_x", &Foo::get_x )
    ;
  class_< FooContainer, boost::noncopyable >( "FooContainer" )
    .def("__iter__", iterator< FooContainer, return_internal_reference<> >())
    ;
}

下面是示例输出:

>>> from iterator_example import FooContainer
>>> fc = FooContainer()
Foo constructed: 0x8a78f88
Foo constructed: 0x8a78f8c
Foo constructed: 0x8a78f90
FooContainer constructed: 0x8a78f88
>>> for foo in fc:
...   print foo.get_x()
... 
10
20
30
>>> fc = foo = None
FooContainer destroyed:   0x8a78f88
Foo destroyed:   0x8a78f90
Foo destroyed:   0x8a78f8c
Foo destroyed:   0x8a78f88
>>> 
>>> fc = FooContainer()
Foo constructed: 0x8a7ab48
Foo constructed: 0x8a7ab4c
Foo constructed: 0x8a7ab50
FooContainer constructed: 0x8a7ab48
>>> iter = fc.__iter__()
>>> fc = None
>>> iter.next().get_x()
10
>>> iter.next().get_x()
20
>>> iter = None
FooContainer destroyed:   0x8a7ab48
Foo destroyed:   0x8a7ab50
Foo destroyed:   0x8a7ab4c
Foo destroyed:   0x8a7ab48
我认为

整个问题是我没有完全理解iterator类应该提供什么语义。似乎只要容器存在,迭代器返回的值就必须有效,而不是迭代器。

这意味着boost::python行为正确,有两种解决方案:

  • 使用boost::shared_ptr
  • 按值返回

比我尝试的方法效率低一些,但看起来没有其他方法。

编辑:我已经制定了一个解决方案(不仅可能,而且似乎运行良好):提升 python 容器、迭代器和项目生命周期

以下是相关示例:https://wiki.python.org/moin/boost.python/iterator。
您可以通过常量/非常量引用返回迭代器值

...
.def("__iter__"
     , range<return_value_policy<copy_non_const_reference> >(
           &my_sequence<heavy>::begin
         , &my_sequence<heavy>::end))

这个想法是,正如你提到的,你应该绑定到容器生存期,而不是返回值的迭代器生存期。