Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.

Moreover, the implementation is now O(1) rather than O(n).
Thanks to Yury Selivanov for reporting.
This commit is contained in:
Antoine Pitrou 2012-03-01 16:26:35 +01:00
parent eb977dac9c
commit bbe2f60b3c
5 changed files with 115 additions and 3 deletions

View file

@ -63,7 +63,7 @@ class WeakSet:
yield item
def __len__(self):
return sum(x() is not None for x in self.data)
return len(self.data) - len(self._pending_removals)
def __contains__(self, item):
try: