[Python-Dev] PySet API

Raymond Hettinger raymond.hettinger at verizon.net
Sun Mar 26 18:43:35 CEST 2006

> Speaking as a person who does relatively little C programming, I don't
> see much difference between them.  The first example is more Pythonic --
> for Python.  I agree with Barry that it's not much of a virtue for C
> code.

It was a trick question.  Everyone is supposed to be attracted to the _next 
version because it is shorter, faster, and takes less ref counting management. 
However, the _next version has a hard-to-find bug.  The call to PyObject_Hash() 
can trigger arbitrary Python code and possibly mutate the table, leaving 
pointers to invalid memory addresses.  It would likely take Armin less than five 
minutes to write a pure Python crasher for the code.  And THAT is why 
PySet_Next() should never come into being.

The iterator form is more duck-typable and re-usable than the set specific _next 
version, but the example was chosen to take that issue off of the table and just 
focus on mutation issues.

> However, I do have one nitpick with both your examples; I don't know
> whether this is an artifact of them being examples:
>>                 hash ^= h * 3644798167;
> Seems to me that magic numbers like this need to be made constants and
> explained with a comment

FWIW, the actual code does have comments.  I stripped them out of the posting 
because they weren't relevant to the code comparison.


More information about the Python-Dev mailing list