iterating over arbitrary python object in c++

hi- i'm writing a C++ class that i want to expose as a python class. one method in my class has an argument that takes in a python object that "implements" the iterator interface (that is the python object has defined "__iter__" and "__next__" and can be used in a "for x in y" loop). how do i go about iterating over that python object in my C++ code? for example: void my_class::method(object my_iterable_obj) { // iterate over "my_iterable_obj" } one method i tried was calling the "__next__" method explicity: my_iterable_obj.attr("__next__")() however, "__next__" raises a StopIteration exception when there are no more objects in the iterator. i could not find any examples in the boost python tutorials for handling python exceptions from c++. is there another way i can go about this? thanks, augusto.

Augusto Callejas wrote:
hi-
i'm writing a C++ class that i want to expose as a python class. one method in my class has an argument that takes in a python object that "implements" the iterator interface (that is the python object has defined "__iter__" and "__next__" and can be used in a "for x in y" loop).
how do i go about iterating over that python object in my C++ code?
for example:
void my_class::method(object my_iterable_obj) { // iterate over "my_iterable_obj" }
one method i tried was calling the "__next__" method explicity:
my_iterable_obj.attr("__next__")()
however, "__next__" raises a StopIteration exception when there are no more objects in the iterator. i could not find any examples in the boost python tutorials for handling python exceptions from c++.
is there another way i can go about this?
Boost 1.34 (the next version) will come with python::stl_input_iterator, which does exactly what you are trying to do. You can get the code now from Boost main CVS, or look at the implementation online. The interesting part is here: http://tinyurl.com/ybugtp. Thing to note: if you use PyIter_Next directly instead of obj.attr("__next__")(), you don't get an exception. -- Eric Niebler Boost Consulting www.boost-consulting.com

You can use the __length_hint__() method stop the iter's loop. Like is:
while(extract<int>(my_iterable_obj.attr("__length_hint__")()) > 0):
{
...
}
This way can useful at list, tuple and as other Python's containter.
2006/11/11, Augusto Callejas
hi-
i'm writing a C++ class that i want to expose as a python class. one method in my class has an argument that takes in a python object that "implements" the iterator interface (that is the python object has defined "__iter__" and "__next__" and can be used in a "for x in y" loop).
how do i go about iterating over that python object in my C++ code?
for example:
void my_class::method(object my_iterable_obj) { // iterate over "my_iterable_obj" }
one method i tried was calling the "__next__" method explicity:
my_iterable_obj.attr("__next__")()
however, "__next__" raises a StopIteration exception when there are no more objects in the iterator. i could not find any examples in the boost python tutorials for handling python exceptions from c++.
is there another way i can go about this?
thanks, augusto.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- 欢迎访问: http://blog.csdn.net/ccat 刘鑫 March.Liu
participants (3)
-
Augusto Callejas
-
Eric Niebler
-
刘鑫