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.

This commit is contained in:
Raymond Hettinger 2010-04-03 03:14:28 +00:00
parent 93232660bb
commit 6b96ecb0ab
1 changed files with 8 additions and 2 deletions

View File

@ -107,8 +107,14 @@ class OrderedDict(dict, MutableMapping):
def clear(self): def clear(self):
'od.clear() -> None. Remove all items from od.' 'od.clear() -> None. Remove all items from od.'
for k in dict.keys(self): try:
del self[k] 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 setdefault = MutableMapping.setdefault
update = MutableMapping.update update = MutableMapping.update