You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WeakSet has a bug in its '__len__' method, where it performs iteration though 'self.data' without ensuring _IterationGuard.
This leads to some hard to catch errors with the following traceback:
builtins.RuntimeError: Set changed size during iteration
File /usr/lib64/python3.2/_weakrefset.py, line 66, in __len__
return sum(x() is not None for x in self.data)
File /usr/lib64/python3.2/_weakrefset.py, line 66, in <genexpr>
return sum(x() is not None for x in self.data)
New patch addressing similar issue with weak dicts, and adding more tests.
YurySelivanov commented on Feb 29, 2012
YurySelivanovmannequin
MannequinAuthor
More actions
Yes, I couldn't reproduce it either. However it frequently occurs on our buildbot, so tomorrow I'll give you an update if this patch resolves the issue or not.
In any way, your patch fixes the current implementation at least from the performance perspective. I think it should be applied to 'WeakValueDictionary.__len__' as well.
Thanks!
YurySelivanov commented on Mar 1, 2012
YurySelivanovmannequin
MannequinAuthor
More actions
As expected, it seems that the patch fixes the issue.
python-dev commented on Mar 1, 2012
python-devmannequin
Mannequin
More actions
New changeset 1cd0688ff004 by Antoine Pitrou in branch '3.2':
Issue bpo-14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying. http://hg.python.org/cpython/rev/1cd0688ff004
New changeset b1b2a29d3d81 by Antoine Pitrou in branch 'default':
Issue bpo-14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying. http://hg.python.org/cpython/rev/b1b2a29d3d81
python-dev commented on Mar 1, 2012
python-devmannequin
Mannequin
More actions
New changeset b6acfbe2bdbe by Antoine Pitrou in branch '2.7':
Issue bpo-14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying. http://hg.python.org/cpython/rev/b6acfbe2bdbe
Activity
YurySelivanov commentedon Feb 29, 2012
WeakSet has a bug in its '__len__' method, where it performs iteration though 'self.data' without ensuring _IterationGuard.
This leads to some hard to catch errors with the following traceback:
builtins.RuntimeError: Set changed size during iteration
pitrou commentedon Feb 29, 2012
I can't find a way to reproduce as a unit test.
Here is a patch making __len__ O(1) as well (instead of O(n)). Could you try it?
(note : AFAICT, a similar issue exists for weak dicts)
pitrou commentedon Feb 29, 2012
New patch addressing similar issue with weak dicts, and adding more tests.
YurySelivanov commentedon Feb 29, 2012
Yes, I couldn't reproduce it either. However it frequently occurs on our buildbot, so tomorrow I'll give you an update if this patch resolves the issue or not.
In any way, your patch fixes the current implementation at least from the performance perspective. I think it should be applied to 'WeakValueDictionary.__len__' as well.
Thanks!
YurySelivanov commentedon Mar 1, 2012
As expected, it seems that the patch fixes the issue.
python-dev commentedon Mar 1, 2012
New changeset 1cd0688ff004 by Antoine Pitrou in branch '3.2':
Issue bpo-14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
http://hg.python.org/cpython/rev/1cd0688ff004
New changeset b1b2a29d3d81 by Antoine Pitrou in branch 'default':
Issue bpo-14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
http://hg.python.org/cpython/rev/b1b2a29d3d81
python-dev commentedon Mar 1, 2012
New changeset b6acfbe2bdbe by Antoine Pitrou in branch '2.7':
Issue bpo-14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
http://hg.python.org/cpython/rev/b6acfbe2bdbe
pitrou commentedon Mar 1, 2012
2.7 didn't have the weak dict issue, but I still backported the tests there. Closing, should be fixed now. Thanks for reporting!