From: Régis Behmo Date: Wed, 12 Nov 2014 17:09:34 +0000 (+0100) Subject: odict: Fix pop method X-Git-Tag: dev-2a51c9b95d06~28^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0a9e97e3ae4ffb7431406a5c7166e337573dfa92;p=thirdparty%2Fbabel.git odict: Fix pop method The items() and iteritems() methods did not contain correct values after a call to `pop(i)`. Fixes https://github.com/mitsuhiko/babel/issues/196 --- diff --git a/babel/util.py b/babel/util.py index a65fce36..a428bca3 100644 --- a/babel/util.py +++ b/babel/util.py @@ -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) diff --git a/tests/test_util.py b/tests/test_util.py index 321014c9..6ec73dc5 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