]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
odict: Fix pop method 119/head
authorRégis Behmo <regis@behmo.com>
Wed, 12 Nov 2014 17:09:34 +0000 (18:09 +0100)
committerRégis Behmo <regis@behmo.com>
Fri, 28 Aug 2015 11:26:35 +0000 (13:26 +0200)
The items() and iteritems() methods did not contain correct values after
a call to `pop(i)`.

Fixes https://github.com/mitsuhiko/babel/issues/196

babel/util.py
tests/test_util.py

index a65fce36e3169d772a29c0951db49ede48564530..a428bca37565450f9dbc6c484947ea43f506e508 100644 (file)
@@ -199,12 +199,15 @@ class odict(dict):
         return self._keys[:]
 
     def pop(self, key, default=missing):
-        if default is missing:
-            return dict.pop(self, key)
-        elif key not in self:
-            return default
-        self._keys.remove(key)
-        return dict.pop(self, key, default)
+        try:
+            value = dict.pop(self, key)
+            self._keys.remove(key)
+            return value
+        except KeyError as e:
+            if default == missing:
+                raise e
+            else:
+                return default
 
     def popitem(self, key):
         self._keys.remove(key)
index 321014c90bd28465f2984784c58bddecd6878519..6ec73dc52d0ba1011c3dcfa11cc7cdfda1b34da5 100644 (file)
@@ -11,8 +11,6 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://babel.edgewall.org/log/.
 
-import doctest
-import unittest
 
 from babel import util
 
@@ -28,3 +26,16 @@ def test_pathmatch():
     assert not util.pathmatch('**.py', 'templates/index.html')
     assert util.pathmatch('**/templates/*.html', 'templates/index.html')
     assert not util.pathmatch('**/templates/*.html', 'templates/foo/bar.html')
+
+def test_odict_pop():
+    odict = util.odict()
+    odict[0] = 1
+    value = odict.pop(0)
+    assert 1 == value
+    assert [] == list(odict.items())
+    assert odict.pop(2, None) is None
+    try:
+        odict.pop(2)
+        assert False
+    except KeyError:
+        assert True