summaryrefslogtreecommitdiff
path: root/Lib/_weakrefset.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-03-01 16:26:35 +0100
committerAntoine Pitrou <solipsis@pitrou.net>2012-03-01 16:26:35 +0100
commit82c5a2efd164eb1649471ea64af956777e3099f0 (patch)
tree0beb3a3d23decd1b776120127c545fcb45391daa /Lib/_weakrefset.py
parentd3148329913e050dcb55e40b268a64e0422bc261 (diff)
downloadcpython-82c5a2efd164eb1649471ea64af956777e3099f0.tar.gz
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.
Diffstat (limited to 'Lib/_weakrefset.py')
-rw-r--r--Lib/_weakrefset.py2
1 files changed, 1 insertions, 1 deletions
diff --git a/Lib/_weakrefset.py b/Lib/_weakrefset.py
index 42653699a3..f34aa864bc 100644
--- a/Lib/_weakrefset.py
+++ b/Lib/_weakrefset.py
@@ -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: