From 345c49b16b093399ad504711d55bad700ad7a33b Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 1 Jan 2011 23:51:55 +0000 Subject: Fix OrderedDic.pop() to work for subclasses that define __missing__(). --- Lib/collections.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'Lib/collections.py') diff --git a/Lib/collections.py b/Lib/collections.py index d0a44c2828..36ee18a2b6 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -22,7 +22,7 @@ from reprlib import recursive_repr as _recursive_repr class _Link(object): __slots__ = 'prev', 'next', 'key', '__weakref__' -class OrderedDict(dict, MutableMapping): +class OrderedDict(dict): 'Dictionary that remembers insertion order' # An inherited dict maps keys to values. # The inherited dict provides __getitem__, __len__, __contains__, and get. @@ -172,12 +172,22 @@ class OrderedDict(dict, MutableMapping): return size update = __update = MutableMapping.update - pop = MutableMapping.pop keys = MutableMapping.keys values = MutableMapping.values items = MutableMapping.items __ne__ = MutableMapping.__ne__ + __marker = object() + + def pop(self, key, default=__marker): + if key in self: + result = self[key] + del self[key] + return result + if default is self.__marker: + raise KeyError(key) + return default + def setdefault(self, key, default=None): 'OD.setdefault(k[,d]) -> OD.get(k,d), also set OD[k]=d if k not in OD' if key in self: -- cgit v1.2.1