[C++-sig] expose python iterators to c++?

Neal Becker ndbecker2 at gmail.com
Thu Feb 15 02:19:31 CET 2007


François Duranleau wrote:

> On Sat, 10 Feb 2007, Neal Becker wrote:
> 
>> Has anyone looked into exposing python iterators to c++?
>>
>> It would be very nice if a python object implementing the python iterator
>> protocol was able to be used as a boost::range.  Then c++ algorithms
>> conforming to boost::range requirements could accept a python object
>> conforming to the python iterator protocol.
> 
> I did recently, although I did not fully test it. I put the code and a
> sample test here:
> 
> http://tinyurl.com/24dpdf
> 
> It supports SinglePassRange and ForwardRange (although I don't like my
> solution so far for this type, that is, accumulate the values in a vector
> and replay the iteration with that). Making SinglePassRange is not
> difficult, but for other types, it's trickier, because Python's iterators
> are really single pass iterators.
> 

Looks very interesting.  One thing I'm not sure about.  Do I understand
correctly that an python object is convertible to a c++ range iff
PyObject_GetIter( obj )?  I think it needs also that deref the iterator
produces the correct type.

In range_test.cpp:
    typedef ::pyfdl::range< int > range_int_type ;

Don't we need different converters for range<int>, range<double>, etc?





More information about the Cplusplus-sig mailing list