summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLasse Schuirmann <lasse@schuirmann.net>2015-09-10 15:33:39 +0200
committerLasse Schuirmann <lasse@schuirmann.net>2015-09-10 15:33:39 +0200
commite9373d98a5b2fda701d927087e5cb950bab1961c (patch)
tree825f672cd05810605eafad94015fb7499bc94cf8
parenteb0986cdf4a5268d6caa0fe9b05036f05a1adefe (diff)
parent0a9e97e3ae4ffb7431406a5c7166e337573dfa92 (diff)
downloadbabel-e9373d98a5b2fda701d927087e5cb950bab1961c.tar.gz
Merge pull request #119 from regisb/regisb/fix-odict-pop
Fix odict's pop method
-rw-r--r--babel/util.py15
-rw-r--r--tests/test_util.py15
2 files changed, 22 insertions, 8 deletions
diff --git a/babel/util.py b/babel/util.py
index e7df8fe..c366214 100644
--- a/babel/util.py
+++ b/babel/util.py
@@ -202,12 +202,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)
diff --git a/tests/test_util.py b/tests/test_util.py
index 321014c..6ec73dc 100644
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -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