From 887f3d3219b9f8192d27314eceee27ab1f89c5cc Mon Sep 17 00:00:00 2001 From: Adam Zapletal Date: Mon, 8 Aug 2016 12:01:25 -0500 Subject: [PATCH] Fixed #26764 -- Fixed Session.cycle_key() crash on unaccessed session. --- django/contrib/sessions/backends/base.py | 5 ++++- tests/sessions_tests/tests.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py index acade4a512..40c557ba0b 100644 --- a/django/contrib/sessions/backends/base.py +++ b/django/contrib/sessions/backends/base.py @@ -306,7 +306,10 @@ class SessionBase(object): """ Creates a new session key, while retaining the current session data. """ - data = self._session_cache + try: + data = self._session_cache + except AttributeError: + data = {} key = self.session_key self.create() self._session_cache = data diff --git a/tests/sessions_tests/tests.py b/tests/sessions_tests/tests.py index 480eb72c8f..ffc7e9d942 100644 --- a/tests/sessions_tests/tests.py +++ b/tests/sessions_tests/tests.py @@ -188,6 +188,10 @@ class SessionTestsMixin(object): self.assertNotEqual(self.session.session_key, prev_key) self.assertEqual(list(self.session.items()), prev_data) + def test_cycle_with_no_session_cache(self): + self.assertFalse(hasattr(self.session, '_session_cache')) + self.session.cycle_key() + def test_save_doesnt_clear_data(self): self.session['a'] = 'b' self.session.save()