Browse files

[py3] Fix encoding issues in contrib.sessions

  • Loading branch information...
1 parent ac37c9e commit 8a1f439d3aca3a021a43ba6c4235c3ac68908657 @claudep claudep committed Aug 12, 2012
Showing with 8 additions and 6 deletions.
  1. +7 −4 django/contrib/sessions/backends/base.py
  2. +1 −2 django/contrib/sessions/backends/db.py
View
11 django/contrib/sessions/backends/base.py
@@ -1,3 +1,5 @@
+from __future__ import unicode_literals
+
import base64
import time
from datetime import datetime, timedelta
@@ -12,6 +14,7 @@
from django.utils.crypto import get_random_string
from django.utils.crypto import salted_hmac
from django.utils import timezone
+from django.utils.encoding import smart_bytes
class CreateError(Exception):
"""
@@ -78,15 +81,15 @@ def encode(self, session_dict):
"Returns the given session dictionary pickled and encoded as a string."
pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
hash = self._hash(pickled)
- return base64.encodestring(hash + ":" + pickled)
+ return base64.encodestring(hash.encode() + b":" + pickled)
def decode(self, session_data):
- encoded_data = base64.decodestring(session_data)
+ encoded_data = base64.decodestring(smart_bytes(session_data))
try:
# could produce ValueError if there is no ':'
- hash, pickled = encoded_data.split(':', 1)
+ hash, pickled = encoded_data.split(b':', 1)
expected_hash = self._hash(pickled)
- if not constant_time_compare(hash, expected_hash):
+ if not constant_time_compare(hash.decode(), expected_hash):
raise SuspiciousOperation("Session data corrupted")
else:
return pickle.loads(pickled)
View
3 django/contrib/sessions/backends/db.py
@@ -1,7 +1,6 @@
from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.core.exceptions import SuspiciousOperation
from django.db import IntegrityError, transaction, router
-from django.utils.encoding import force_text
from django.utils import timezone
@@ -18,7 +17,7 @@ def load(self):
session_key = self.session_key,
expire_date__gt=timezone.now()
)
- return self.decode(force_text(s.session_data))
+ return self.decode(s.session_data)
except (Session.DoesNotExist, SuspiciousOperation):
self.create()
return {}

0 comments on commit 8a1f439

Please sign in to comment.