[Python-Dev] PySet API

Alex Martelli aleaxit at gmail.com
Sun Mar 26 18:55:03 CEST 2006


On Mar 26, 2006, at 8:43 AM, Raymond Hettinger wrote:

> [Aahz]
>> 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.

Sure, accidentally mutating underlying iterables is a subtle (but  
alas frequent) bug, but I don't see why it should be any harsher when  
the loop is using a hypothetical PySet_Next than when it is using  
PyIter_Next -- whatever precautions the latter takes to detect the  
bug and raise an exception instead of crashing, wouldn't it be at  
least as feasible for PySet_Next to take similar precautions  
(probably easier, since PySet_Next need only worry about one concrete  
case rather than an arbitrary variety)? What does PyDict_Next do in  
similar cases, and why couldn't PySet_Next behave similarly?  (Yes, I  
could/should look it up myself, but I'm supposed to be working on the  
2nd Ed of the Nutshell, whose deadline is getting worryingly  
close...;-).


Alex



More information about the Python-Dev mailing list