summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-03-19 19:59:58 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-03-19 19:59:58 (GMT)
commit2412299be96bf590b46d7b0784cdb620170a8d7a (patch)
treefb440a9fef80b259b8866a314b4d447ccd4e0619 /Lib
parenta0b8d9ad2da71658ce20448bbb3f081bcd39f446 (diff)
downloadcpython-2412299be96bf590b46d7b0784cdb620170a8d7a.zip
cpython-2412299be96bf590b46d7b0784cdb620170a8d7a.tar.gz
cpython-2412299be96bf590b46d7b0784cdb620170a8d7a.tar.bz2
* Add clearer comment to initialization code.
* Add optional argument to popitem() -- modeled after Anthon van der Neut's C version. * Fix method markup in docs.
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