Fixed #26520 -- Fixed a regression where SessionBase.pop() didn't return a KeyError.

This commit is contained in:
Tobias Kroenke 2016-04-20 10:36:59 +02:00 committed by Tim Graham
parent 973f393761
commit b040ac06eb
4 changed files with 13 additions and 3 deletions

View File

@ -42,6 +42,8 @@ class SessionBase(object):
TEST_COOKIE_NAME = 'testcookie'
TEST_COOKIE_VALUE = 'worked'
__not_given = object()
def __init__(self, session_key=None):
self._session_key = session_key
self.accessed = False
@ -65,9 +67,10 @@ class SessionBase(object):
def get(self, key, default=None):
return self._session.get(key, default)
def pop(self, key, default=None):
def pop(self, key, default=__not_given):
self.modified = self.modified or key in self._session
return self._session.pop(key, default)
args = () if default is self.__not_given else (default,)
return self._session.pop(key, *args)
def setdefault(self, key, value):
if key in self._session:

View File

@ -18,3 +18,6 @@ Bugfixes
* Prevented ``makemigrations`` from generating infinite migrations for a model
field that references a ``functools.partial`` (:ticket:`26475`).
* Fixed a regression where ``SessionBase.pop()`` returned ``None`` rather than
raising a ``KeyError`` for nonexistent values (:ticket:`26520`).

View File

@ -205,7 +205,7 @@ You can edit it multiple times.
Example: ``fav_color = request.session.get('fav_color', 'red')``
.. method:: pop(key, default=None)
.. method:: pop(key, default=__not_given)
Example: ``fav_color = request.session.pop('fav_color', 'blue')``

View File

@ -88,6 +88,10 @@ class SessionTestsMixin(object):
self.assertTrue(self.session.accessed)
self.assertFalse(self.session.modified)
def test_pop_no_default_keyerror_raised(self):
with self.assertRaises(KeyError):
self.session.pop('some key')
def test_setdefault(self):
self.assertEqual(self.session.setdefault('foo', 'bar'), 'bar')
self.assertEqual(self.session.setdefault('foo', 'baz'), 'bar')