diff options
author | Fish <ltfish@users.noreply.github.com> | 2019-02-07 19:51:59 (GMT) |
---|---|---|
committer | Antoine Pitrou <pitrou@free.fr> | 2019-02-07 19:51:59 (GMT) |
commit | 96d37dbcd23e65a7a57819aeced9034296ef747e (patch) | |
tree | f8a5c4d702642dbc6da04a85d818ae7689d5a1db /Lib/weakref.py | |
parent | df8d2cde63c865446468351f8f648e1c7bd45109 (diff) | |
download | cpython-96d37dbcd23e65a7a57819aeced9034296ef747e.zip cpython-96d37dbcd23e65a7a57819aeced9034296ef747e.tar.gz cpython-96d37dbcd23e65a7a57819aeced9034296ef747e.tar.bz2 |
bpo-35615: Fix crashes when copying a Weak{Key,Value}Dictionary. (GH-11384)
Protect dict iterations by wrapping them with _IterationGuard in the
following methods:
- WeakValueDictionary.copy()
- WeakValueDictionary.__deepcopy__()
- WeakKeyDictionary.copy()
- WeakKeyDictionary.__deepcopy__()
Diffstat (limited to 'Lib/weakref.py')
-rw-r--r-- | Lib/weakref.py | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/Lib/weakref.py b/Lib/weakref.py index 99de2ea..753f072 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -171,10 +171,11 @@ class WeakValueDictionary(_collections_abc.MutableMapping): if self._pending_removals: self._commit_removals() new = WeakValueDictionary() - for key, wr in self.data.items(): - o = wr() - if o is not None: - new[key] = o + with _IterationGuard(self): + for key, wr in self.data.items(): + o = wr() + if o is not None: + new[key] = o return new __copy__ = copy @@ -184,10 +185,11 @@ class WeakValueDictionary(_collections_abc.MutableMapping): if self._pending_removals: self._commit_removals() new = self.__class__() - for key, wr in self.data.items(): - o = wr() - if o is not None: - new[deepcopy(key, memo)] = o + with _IterationGuard(self): + for key, wr in self.data.items(): + o = wr() + if o is not None: + new[deepcopy(key, memo)] = o return new def get(self, key, default=None): @@ -408,10 +410,11 @@ class WeakKeyDictionary(_collections_abc.MutableMapping): def copy(self): new = WeakKeyDictionary() - for key, value in self.data.items(): - o = key() - if o is not None: - new[o] = value + with _IterationGuard(self): + for key, value in self.data.items(): + o = key() + if o is not None: + new[o] = value return new __copy__ = copy @@ -419,10 +422,11 @@ class WeakKeyDictionary(_collections_abc.MutableMapping): def __deepcopy__(self, memo): from copy import deepcopy new = self.__class__() - for key, value in self.data.items(): - o = key() - if o is not None: - new[o] = deepcopy(value, memo) + with _IterationGuard(self): + for key, value in self.data.items(): + o = key() + if o is not None: + new[o] = deepcopy(value, memo) return new def get(self, key, default=None): |