[Python-Dev] PySet_Next (Was: PySet API)
Raymond Hettinger
raymond.hettinger at verizon.net
Sun Mar 26 20:24:25 CEST 2006
>> The difference is that the PySet_Next returns pointers to the table keys and
>> that the mutation occurs AFTER the call to PySet_Next, leaving pointers to
>> invalid addresses. IOW, the function cannot detect the mutation.
>
> I'm coming late to the discussion: where did anybody ever suggest that
> PySet_Next should return a pointer into the set? Looking over the entire
> discussion, I could not find any mentioning of a specific API.
Pardon, I bungled the terminology. PySet_Next returns a borrowed reference.
That is problematic is arbitrary Python code can be run afterwards (such as
PyObject_Hash in the example). We could make a version that returns a new
reference or immediately Py_INCREF the reference but then PySet_Next() loses its
charm and you might as well be using PyIter_Next().
Aside from bad pointers, the issue of mid-stream table mutation has other
reliability issues stemming from the contents of the table potentially changing
in the an arbitrary way as the iteration proceeds. That means you can make very
few guarantees about the meaningfulness of the results even if you don't crash
due to a bad pointer.
We have a perfectly good way to iterate with PyIter_Next(). It may take a
couple of extra lines, but it is easy to get correct and has no surprises. It
seems that the only issue is that Barry says that he refuses to use the iterator
protocol. Heck, just turn it into a list and index directly. There is no need
to muck-up the set api for this.
Raymond
More information about the Python-Dev
mailing list