Boost Python Tuple Iterator

326 Views Asked by At

I am trying to export an iterator over tuples from C++ to Python, using the next code:

class PyContainer
{
public:
    PyContainer(int maxSize) : maxSize(maxSize) { cout << "Constructor called with maxSize = " << maxSize << endl; }

    boost::python::tuple AllocateTuple(int idx)
    {
        cout << "Allocating pyRecord " << idx << endl;
        return boost::python::make_tuple(idx);
    }

    class iterator : public std::iterator<std::input_iterator_tag, boost::python::tuple>
    {
    public:
        iterator(const iterator& rhs) : idx(rhs.idx), currTuple(rhs.currTuple), self(rhs.self) { cout << "copy iterator(rhs.idx=" << rhs.idx << ", currTuple=" << currTuple.ptr()<< ")" << endl; }
        iterator(PyContainer *self, int idx) : idx(idx), currTuple(), self(self) { cout << "iterator(idx=" << idx << ")" << endl; }
        ~iterator() { cout << "~iterator " << currTuple.ptr()<< endl; }

        boost::python::tuple& operator*() 
        { 
            cout << "operator* " << currTuple.ptr()<< endl;
            return currTuple;
        }

        // Pre-increment
        iterator& operator++() 
        { 
            cout << "preinc " << currTuple.ptr()<< endl;
            ++idx;
            return *this; 
        }

        // Post-increment.     
        iterator operator++(int)
        {
            currTuple = self->AllocateTuple(idx);

            cout << "postinc " << currTuple.ptr()<< endl;
            iterator tmp(*this);
            operator++();
            return tmp;
        }

        // Comparison.
        bool operator==(const iterator& rhs) { return this->idx == rhs.idx; }
        bool operator!=(const iterator& rhs) { return !this->operator==(rhs); }

    private:
        int idx;
        boost::python::tuple currTuple;
        PyContainer* self;
    };
    iterator begin() { return iterator(this, 0); }
    iterator end() { return iterator(this, maxSize); }
private:
    int maxSize;
};

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    class_<PyContainer, boost::noncopyable>("PyContainer", init<int>())
        .def("__iter__", boost::python::iterator<PyContainer, return_internal_reference<>>())
        ;
}

In python I use the iterator like this:

import hello_ext as a
r = a.PyContainer(2)
for i in r:
    print i

The output from C++ looks like this one:

Constructor called 
iterator(idx=2) 
copy iterator(rhs.idx=2, currentRecord=005D1030) 
~iterator 005D1030
iterator(idx=0)
copy iterator(rhs.idx=0, currentRecord=005D1030)
~iterator 005D1030
copy iterator(rhs.idx=0, currentRecord=005D1030)
copy iterator(rhs.idx=2, currentRecord=005D1030)
~iterator 005D1030
~iterator 005D1030
copy iterator(rhs.idx=0, currentRecord=005D1030)
copy iterator(rhs.idx=2, currentRecord=005D1030)
~iterator 005D1030
~iterator 005D1030
0==2
Allocating pyRecord 0
postinc 008F7970
copy iterator(rhs.idx=0, currentRecord=008F7970)
preinc 008F7970
copy iterator(rhs.idx=0, currentRecord=008F7970)
~iterator 008F7970
operator* 008F7970
~iterator 008F7970

And finally, the error received is this one: Assert Error

Do I have to manage the object's lifetime from Python?

0

There are 0 best solutions below