Ticket #16997: fix-contib-session-py3.patch

File fix-contib-session-py3.patch, 4.2 KB (added by adsworth, 4 years ago)
  • django/contrib/sessions/backends/base.py

    diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py
    index ad2aa54..394caef 100644
    a b import hashlib 
    33import os
    44import random
    55import time
     6from django.utils.encoding import smart_str
    67from django.utils.py3 import b
    78from datetime import datetime, timedelta
    89
    class SessionBase(object): 
    9697        return base64.encodestring(hash + b(":") + pickled)
    9798
    9899    def decode(self, session_data):
    99         encoded_data = base64.decodestring(session_data.encode('ascii'))
     100        encoded_data = base64.decodestring(b(session_data))
    100101        try:
    101102            # could produce ValueError if there is no ':'
    102             hash, pickled = encoded_data.split(':', 1)
     103            hash, pickled = encoded_data.split(b(':'), 1)
    103104            expected_hash = self._hash(pickled)
    104105            if not constant_time_compare(hash, expected_hash):
    105106                raise SuspiciousOperation("Session data corrupted")
  • django/contrib/sessions/tests.py

    diff --git a/django/contrib/sessions/tests.py b/django/contrib/sessions/tests.py
    index 95e8e90..fb32bb8 100644
    a b class SessionTestsMixin(object): 
    8585        self.assertFalse(self.session.modified)
    8686
    8787    def test_values(self):
    88         self.assertEqual(self.session.values(), [])
     88        self.assertEqual(list(self.session.values()), [])
    8989        self.assertTrue(self.session.accessed)
    9090        self.session['some key'] = 1
    91         self.assertEqual(self.session.values(), [1])
     91        self.assertEqual(list(self.session.values()), [1])
    9292
    9393    def test_iterkeys(self):
    9494        self.session['x'] = 1
  • django/core/cache/backends/base.py

    diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py
    index 9824fc4..d56eb60 100644
    a b def default_key_func(key, key_prefix, version): 
    2424    the `key_prefix'. KEY_FUNCTION can be used to specify an alternate
    2525    function with custom key making behavior.
    2626    """
    27     return ':'.join([key_prefix, str(version), smart_str(key)])
     27    return smart_str(':'.join([key_prefix, str(version), key]))
    2828
    2929def get_key_func(key_func):
    3030    """
    class BaseCache(object): 
    6363        except (ValueError, TypeError):
    6464            self._cull_frequency = 3
    6565
    66         self.key_prefix = smart_str(params.get('KEY_PREFIX', ''))
     66        self.key_prefix = params.get('KEY_PREFIX', '')
    6767        self.version = params.get('VERSION', 1)
    6868        self.key_func = get_key_func(params.get('KEY_FUNCTION', None))
    6969
    class BaseCache(object): 
    199199                    '%s (longer than %s)' % (key, MEMCACHE_MAX_KEY_LENGTH),
    200200                    CacheKeyWarning)
    201201        for char in key:
     202            #in Python3 iterating over a byte string yields ints.
     203            if type(char) == int:
     204                char = chr(char)
    202205            if ord(char) < 33 or ord(char) == 127:
    203206                warnings.warn('Cache key contains characters that will cause '
    204207                        'errors if used with memcached: %r' % key,
  • django/core/signing.py

    diff --git a/django/core/signing.py b/django/core/signing.py
    index 7f92d61..755cd50 100644
    a b import zlib 
    3939from django.conf import settings
    4040from django.core.exceptions import ImproperlyConfigured
    4141from django.utils import baseconv, simplejson
     42from django.utils.py3 import b
    4243from django.utils.crypto import constant_time_compare, salted_hmac
    4344from django.utils.encoding import force_unicode, smart_str
    4445from django.utils.importlib import import_module
    class SignatureExpired(BadSignature): 
    5960
    6061
    6162def b64_encode(s):
    62     return base64.urlsafe_b64encode(s).strip('=')
     63    return base64.urlsafe_b64encode(s).strip(b('='))
    6364
    6465
    6566def b64_decode(s):
    def loads(s, key=None, salt='django.core.signing', serializer=JSONSerializer, ma 
    149150
    150151class Signer(object):
    151152    def __init__(self, key=None, sep=':', salt=None):
    152         self.sep = sep
     153        self.sep = b(sep)
    153154        self.key = key or settings.SECRET_KEY
    154155        self.salt = salt or ('%s.%s' %
    155156            (self.__class__.__module__, self.__class__.__name__))
Back to Top