summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/collections.py6
-rw-r--r--Lib/test/test_collections.py7
2 files changed, 10 insertions, 3 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 1193ebf..8c0b426 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -30,7 +30,7 @@ class OrderedDict(dict, MutableMapping):
def clear(self):
self.__end = end = []
- end += [None, end, end] # null entry
+ end += [None, end, end] # sentinel node for doubly linked list
self.__map = {} # key --> [key, prev, next]
dict.clear(self)
@@ -61,10 +61,10 @@ class OrderedDict(dict, MutableMapping):
yield curr[0]
curr = curr[1]
- def popitem(self):
+ def popitem(self, last=True):
if not self:
raise KeyError('dictionary is empty')
- key = next(reversed(self))
+ key = next(reversed(self)) if last else next(iter(self))
value = self.pop(key)
return key, value
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index f232df4..9e984ba 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -768,12 +768,19 @@ class TestOrderedDict(unittest.TestCase):
class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
type2test = OrderedDict
+ def test_popitem(self):
+ d = self._empty_mapping()
+ self.assertRaises(KeyError, d.popitem)
+
class MyOrderedDict(OrderedDict):
pass
class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol):
type2test = MyOrderedDict
+ def test_popitem(self):
+ d = self._empty_mapping()
+ self.assertRaises(KeyError, d.popitem)
import doctest, collections