Hi all,

I'm having some trouble using boost.python to expose an input iterator that I've
implemented using boost::iterator_facade.  I've included an example below.  The
code will print garbage when iterating in python and segfault for more
complicated iterators, but works fine if std::input_iterator_tag is changed to
forward_iterator_tag.  I've noticed this happens in VC9 and gcc.

The problem goes away if line 65 in boost/python/object/iterator.hpp is changed
from "return *self.m_start++;"  to something like:

result_type r = *self.m_start;
++self.m_start;
return r;

It seems that this change should be made for input iterators, since the postfix
increment operator is allowed to return void.  However, I don't fully
understand why this is a problem in my case since iterator_facade returns a
proxy iterator that can be dereferenced.

Thanks for any help,

Alec

#include <boost/python.hpp>
#include <boost/iterator/iterator_facade.hpp>

using namespace boost::python;

class container
{
public:
        class iter : public boost::iterator_facade<iter, int, 
std::input_iterator_tag>
        {
        public:
                iter(int* i) : m_i(i) {}

        private:
                friend class boost::iterator_core_access;
                void increment() { m_i++; }
                int& dereference() const { return *m_i; }
                bool equal(const iter& other) const { return m_i == other.m_i; }
                int* m_i;
        };

        container() : m_data() {}
        iter begin() { return iter(m_data); }
        iter end() { return iter(m_data+5); }
private:
        int m_data[5];
};

BOOST_PYTHON_MODULE(iter_example)
{
        class_<container>("container")
                .add_property("data", range(&container::begin, &container::end))
        ;
}

------------------------------------------

import iter_example
c = iter_example.container()
for x in c.data:
    print x





_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to