From b040ac06ebba2348cece7390b88f746d2c91d07b Mon Sep 17 00:00:00 2001 From: Tobias Kroenke Date: Wed, 20 Apr 2016 10:36:59 +0200 Subject: [PATCH] Fixed #26520 -- Fixed a regression where SessionBase.pop() didn't return a KeyError. --- django/contrib/sessions/backends/base.py | 7 +++++-- docs/releases/1.9.6.txt | 3 +++ docs/topics/http/sessions.txt | 2 +- tests/sessions_tests/tests.py | 4 ++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py index e18b3c753f..acade4a512 100644 --- a/django/contrib/sessions/backends/base.py +++ b/django/contrib/sessions/backends/base.py @@ -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: diff --git a/docs/releases/1.9.6.txt b/docs/releases/1.9.6.txt index 8b48f803c7..e505ee5923 100644 --- a/docs/releases/1.9.6.txt +++ b/docs/releases/1.9.6.txt @@ -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`). diff --git a/docs/topics/http/sessions.txt b/docs/topics/http/sessions.txt index 5e7bd74d2b..34a1b28b73 100644 --- a/docs/topics/http/sessions.txt +++ b/docs/topics/http/sessions.txt @@ -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')`` diff --git a/tests/sessions_tests/tests.py b/tests/sessions_tests/tests.py index cf4c6dfaa2..286274e162 100644 --- a/tests/sessions_tests/tests.py +++ b/tests/sessions_tests/tests.py @@ -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')