CPython 2.7: Weakset data changing size during internal iteration

Terry Reedy tjreedy at udel.edu
Sat Jun 2 02:27:56 EDT 2012


On 6/1/2012 7:40 PM, Temia Eszteri wrote:

>> Given that len(weakset) is defined (sensibly) as the number of currently
>> active members, it must count. weakset should really have .__bool__
>> method that uses any() instead of sum(). That might reduce, but not
>> necessarily eliminate your problem.
>
> Think it might be worth looking into submitting a patch for the next
> minor releases for Python if it turns out to solve the problem?

I think a patch would be worthwhile even if this is not the source of 
your problem. If bool is defined as 'if any ...', that should be the code.

>> I can think of two reasons:
>>
>> 1. You are using multiple threads and another thread does something to
>> change the size of the set during the iteration. Solution? put a lock
>> around the if-statement so no other thread can change self.data during
>> the iteration.
>>
>> 2. Weakset members remove themselves from the set before returning None.
>> (Just a thought, in case you are not using threads).

In other words, it is possible that weakset.__len__ is buggy. Since you 
are sure that 1) is not your problem, that seems more likely now.

> If the weak references removing themselves is the case, it seems like
> a kind of silly problem - one would imagine they'd wrap the data check
> in _IterationGuard in the _weakrefset.py file like they do for calls
> to __iter__(). Very strange.

While looking into the weakset code, you might check the tracker for 
weakset issues. And also check the test code. I have *no* idea how well 
that class has been exercised and tested. Please do submit a patch if 
you can if one is needed.

-- 
Terry Jan Reedy




More information about the Python-list mailing list