Issue #19542: Fix bugs in WeakValueDictionary.setdefault() and WeakValueDictionary.pop()

when a GC collection happens in another thread.

Original patch and report by Armin Rigo.
This commit is contained in:
Antoine Pitrou 2016-12-19 10:56:40 +01:00
parent ca3263c50c
commit c1ee488962
3 changed files with 53 additions and 5 deletions

View file

@ -239,24 +239,27 @@ class WeakValueDictionary(collections.MutableMapping):
try:
o = self.data.pop(key)()
except KeyError:
o = None
if o is None:
if args:
return args[0]
raise
if o is None:
raise KeyError(key)
else:
raise KeyError(key)
else:
return o
def setdefault(self, key, default=None):
try:
wr = self.data[key]
o = self.data[key]()
except KeyError:
o = None
if o is None:
if self._pending_removals:
self._commit_removals()
self.data[key] = KeyedRef(default, self._remove, key)
return default
else:
return wr()
return o
def update(*args, **kwargs):
if not args: