mirror of
https://github.com/python/cpython.git
synced 2025-10-06 15:11:58 +00:00
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:
parent
ca3263c50c
commit
c1ee488962
3 changed files with 53 additions and 5 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue