Skip to content

Commit

Permalink
Fixed #261 (iterator must not be incremented while value is used)
Browse files Browse the repository at this point in the history
  • Loading branch information
klayoutmatthias committed Jun 18, 2019
1 parent d3f93cc commit d3c2c0f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/pya/pya/pyaHelpers.cc
Expand Up @@ -356,7 +356,18 @@ pya_plain_iterator_next (PyObject *self)
{
PYAIteratorObject *iter = (PYAIteratorObject *) self;

if (! iter->iter || iter->iter->at_end ()) {
if (! iter->iter) {
PyErr_SetNone (PyExc_StopIteration);
return NULL;
}

// increment on first visit
if (! iter->first) {
iter->iter->inc ();
}
iter->first = false;

if (iter->iter->at_end ()) {
PyErr_SetNone (PyExc_StopIteration);
return NULL;
}
Expand All @@ -368,8 +379,6 @@ pya_plain_iterator_next (PyObject *self)
iter->iter->get (args);
PythonRef obj = pop_arg (*iter->value_type, args, 0, heap);

iter->iter->inc ();

return obj.release ();
}

Expand Down Expand Up @@ -425,6 +434,7 @@ PYAIteratorObject::create (PyObject *origin, gsi::IterAdaptorAbstractBase *iter,
iter_obj->origin = origin;
iter_obj->iter = iter;
iter_obj->value_type = value_type;
iter_obj->first = true;
}
return iter_obj;
}
Expand Down
1 change: 1 addition & 0 deletions src/pya/pya/pyaHelpers.h
Expand Up @@ -101,6 +101,7 @@ struct PYAIteratorObject
static PYAIteratorObject *create (PyObject *origin, gsi::IterAdaptorAbstractBase *iter, const gsi::ArgType *value_type);

PyObject *origin;
bool first;
gsi::IterAdaptorAbstractBase *iter;
const gsi::ArgType *value_type;

Expand Down

0 comments on commit d3c2c0f

Please sign in to comment.