Ticket #18852: 18852-1.diff

File 18852-1.diff, 1.9 KB (added by Claude Paroz, 12 years ago)

Sign() returns bytestring on python 2

  • django/core/signing.py

    diff --git a/django/core/signing.py b/django/core/signing.py
    index 6fc76bc..d99dd5a 100644
    a b from django.conf import settings  
    4343from django.core.exceptions import ImproperlyConfigured
    4444from django.utils import baseconv
    4545from django.utils.crypto import constant_time_compare, salted_hmac
    46 from django.utils.encoding import smart_bytes
     46from django.utils.encoding import force_bytes, force_str, force_text
    4747from django.utils.importlib import import_module
    4848
    4949
    class SignatureExpired(BadSignature):  
    6262
    6363
    6464def b64_encode(s):
    65     return base64.urlsafe_b64encode(smart_bytes(s)).decode('ascii').strip('=')
     65    return base64.urlsafe_b64encode(force_bytes(s)).decode('ascii').strip('=')
    6666
    6767
    6868def b64_decode(s):
    6969    pad = '=' * (-len(s) % 4)
    70     return base64.urlsafe_b64decode(smart_bytes(s + pad)).decode('ascii')
     70    return base64.urlsafe_b64decode(force_bytes(s + pad)).decode('ascii')
    7171
    7272
    7373def base64_hmac(salt, value, key):
    def dumps(obj, key=None, salt='django.core.signing', serializer=JSONSerializer,  
    123123
    124124    if compress:
    125125        # Avoid zlib dependency unless compress is being used
    126         compressed = zlib.compress(smart_bytes(data))
     126        compressed = zlib.compress(force_bytes(data))
    127127        if len(compressed) < (len(data) - 1):
    128128            data = compressed
    129129            is_compressed = True
    class Signer(object):  
    160160        return base64_hmac(self.salt + 'signer', value, self.key)
    161161
    162162    def sign(self, value):
    163         return '%s%s%s' % (value, self.sep, self.signature(value))
     163        return force_str('%s%s%s' % (value, self.sep, self.signature(value)))
    164164
    165165    def unsign(self, signed_value):
     166        signed_value = force_text(signed_value)
    166167        if not self.sep in signed_value:
    167168            raise BadSignature('No "%s" found in value' % self.sep)
    168169        value, sig = signed_value.rsplit(self.sep, 1)
Back to Top