[C++-sig] call python iterable (handle StopIteration?)

Neal Becker ndbecker2 at gmail.com
Tue Jul 17 16:00:59 CEST 2007


Stefan Seefeld wrote:

> Neal Becker wrote:
>> I want to call a python iterable from c++.  Can I do this?
>>   while (true) {
>>     T n = extract<T> (o.attr ("next")());
>>     tmp.push_back (n);
>>   }
>> 
>> How do I (can I?) catch the StopIteration exception?
> 
> Assuming boost.python does not have special provisions for
> any particular exception type you'd need to catch error_already_set
> and then query (using the python C API) the exact exception type in
> order to break out of the loop.
> It would be worthwhile thinking about better handling of this (as
> well as adding more exception-related APIs) for 1.35... </wink>
> 
> Regards,
> Stefan
> 

Interestingly, this _seems_ to work:

try {
  while (true) {
    o.attr ("next")()...
  }
}
catch (error_already_set) {
  use result..
}




More information about the Cplusplus-sig mailing list