From: Raymond Hettinger Date: Sat, 3 Apr 2010 03:14:28 +0000 (+0000) Subject: Improve clear() method. Keeps key/value refcnts >= 1 until final dict.clear() so... X-Git-Tag: v2.7b1~113 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6b96ecb0ab8d06b7c11d1b82baebc47452a7b13e;p=python Improve clear() method. Keeps key/value refcnts >= 1 until final dict.clear() so that decrefs to zero won't trigger arbitrary code . Also runs a bit faster. --- diff --git a/Lib/collections.py b/Lib/collections.py index 1f1f51072b..79f6696bb8 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -107,8 +107,14 @@ class OrderedDict(dict, MutableMapping): def clear(self): 'od.clear() -> None. Remove all items from od.' - for k in dict.keys(self): - del self[k] + try: + for node in self.__map.itervalues(): + del node[:] + self.__root[:] = [self.__root, self.__root, None] + self.__map.clear() + except AttributeError: + pass + dict.clear(self) setdefault = MutableMapping.setdefault update = MutableMapping.update