Replaced many smart_bytes by force_bytes

In all those occurrences, we didn't care about preserving the
lazy status of the strings, but we really wanted to obtain a
real bytestring.
This commit is contained in:
Claude Paroz 2012-08-28 20:59:56 +02:00
parent 9eafb6592e
commit ebc773ada3
32 changed files with 74 additions and 77 deletions

View File

@ -12,7 +12,7 @@ from django.utils import formats
from django.utils.html import format_html from django.utils.html import format_html
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils import timezone from django.utils import timezone
from django.utils.encoding import force_text, smart_text, smart_bytes from django.utils.encoding import force_str, force_text, smart_text
from django.utils import six from django.utils import six
from django.utils.translation import ungettext from django.utils.translation import ungettext
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
@ -277,7 +277,7 @@ def label_for_field(name, model, model_admin=None, return_attr=False):
label = force_text(model._meta.verbose_name) label = force_text(model._meta.verbose_name)
attr = six.text_type attr = six.text_type
elif name == "__str__": elif name == "__str__":
label = smart_bytes(model._meta.verbose_name) label = force_str(model._meta.verbose_name)
attr = bytes attr = bytes
else: else:
if callable(name): if callable(name):

View File

@ -6,7 +6,7 @@ from django.core.paginator import InvalidPage
from django.db import models from django.db import models
from django.db.models.fields import FieldDoesNotExist from django.db.models.fields import FieldDoesNotExist
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.encoding import force_text, smart_bytes from django.utils.encoding import force_str, force_text
from django.utils.translation import ugettext, ugettext_lazy from django.utils.translation import ugettext, ugettext_lazy
from django.utils.http import urlencode from django.utils.http import urlencode
@ -94,7 +94,7 @@ class ChangeList(object):
# 'key' will be used as a keyword argument later, so Python # 'key' will be used as a keyword argument later, so Python
# requires it to be a string. # requires it to be a string.
del lookup_params[key] del lookup_params[key]
lookup_params[smart_bytes(key)] = value lookup_params[force_str(key)] = value
if not self.model_admin.lookup_allowed(key, value): if not self.model_admin.lookup_allowed(key, value):
raise SuspiciousOperation("Filtering by %s not allowed" % key) raise SuspiciousOperation("Filtering by %s not allowed" % key)

View File

@ -6,7 +6,7 @@ from email.errors import HeaderParseError
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
try: try:
import docutils.core import docutils.core
import docutils.nodes import docutils.nodes
@ -66,7 +66,7 @@ def parse_rst(text, default_reference_context, thing_being_parsed=None):
"link_base" : reverse('django-admindocs-docroot').rstrip('/') "link_base" : reverse('django-admindocs-docroot').rstrip('/')
} }
if thing_being_parsed: if thing_being_parsed:
thing_being_parsed = smart_bytes("<%s>" % thing_being_parsed) thing_being_parsed = force_bytes("<%s>" % thing_being_parsed)
parts = docutils.core.publish_parts(text, source_path=thing_being_parsed, parts = docutils.core.publish_parts(text, source_path=thing_being_parsed,
destination_path=None, writer_name='html', destination_path=None, writer_name='html',
settings_overrides=overrides) settings_overrides=overrides)

View File

@ -8,7 +8,7 @@ from django.conf import settings
from django.test.signals import setting_changed from django.test.signals import setting_changed
from django.utils import importlib from django.utils import importlib
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.utils.crypto import ( from django.utils.crypto import (
pbkdf2, constant_time_compare, get_random_string) pbkdf2, constant_time_compare, get_random_string)
@ -299,7 +299,7 @@ class SHA1PasswordHasher(BasePasswordHasher):
def encode(self, password, salt): def encode(self, password, salt):
assert password assert password
assert salt and '$' not in salt assert salt and '$' not in salt
hash = hashlib.sha1(smart_bytes(salt + password)).hexdigest() hash = hashlib.sha1(force_bytes(salt + password)).hexdigest()
return "%s$%s$%s" % (self.algorithm, salt, hash) return "%s$%s$%s" % (self.algorithm, salt, hash)
def verify(self, password, encoded): def verify(self, password, encoded):
@ -327,7 +327,7 @@ class MD5PasswordHasher(BasePasswordHasher):
def encode(self, password, salt): def encode(self, password, salt):
assert password assert password
assert salt and '$' not in salt assert salt and '$' not in salt
hash = hashlib.md5(smart_bytes(salt + password)).hexdigest() hash = hashlib.md5(force_bytes(salt + password)).hexdigest()
return "%s$%s$%s" % (self.algorithm, salt, hash) return "%s$%s$%s" % (self.algorithm, salt, hash)
def verify(self, password, encoded): def verify(self, password, encoded):
@ -361,7 +361,7 @@ class UnsaltedMD5PasswordHasher(BasePasswordHasher):
return '' return ''
def encode(self, password, salt): def encode(self, password, salt):
return hashlib.md5(smart_bytes(password)).hexdigest() return hashlib.md5(force_bytes(password)).hexdigest()
def verify(self, password, encoded): def verify(self, password, encoded):
encoded_2 = self.encode(password, '') encoded_2 = self.encode(password, '')

View File

@ -8,7 +8,6 @@ from django.core.paginator import EmptyPage, PageNotAnInteger
from django.contrib.gis.db.models.fields import GeometryField from django.contrib.gis.db.models.fields import GeometryField
from django.db import connections, DEFAULT_DB_ALIAS from django.db import connections, DEFAULT_DB_ALIAS
from django.db.models import get_model from django.db.models import get_model
from django.utils.encoding import smart_bytes
from django.utils import six from django.utils import six
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -61,7 +60,7 @@ def sitemap(request, sitemaps, section=None):
raise Http404(_("Page %s empty") % page) raise Http404(_("Page %s empty") % page)
except PageNotAnInteger: except PageNotAnInteger:
raise Http404(_("No page '%s'") % page) raise Http404(_("No page '%s'") % page)
xml = smart_bytes(loader.render_to_string('gis/sitemaps/geo_sitemap.xml', {'urlset': urls})) xml = loader.render_to_string('gis/sitemaps/geo_sitemap.xml', {'urlset': urls})
return HttpResponse(xml, content_type='application/xml') return HttpResponse(xml, content_type='application/xml')
def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS): def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS):

View File

@ -13,7 +13,7 @@ markup syntaxes to HTML; currently there is support for:
from django import template from django import template
from django.conf import settings from django.conf import settings
from django.utils.encoding import smart_bytes, force_text from django.utils.encoding import force_bytes, force_text
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
register = template.Library() register = template.Library()
@ -27,7 +27,7 @@ def textile(value):
raise template.TemplateSyntaxError("Error in 'textile' filter: The Python textile library isn't installed.") raise template.TemplateSyntaxError("Error in 'textile' filter: The Python textile library isn't installed.")
return force_text(value) return force_text(value)
else: else:
return mark_safe(force_text(textile.textile(smart_bytes(value), encoding='utf-8', output='utf-8'))) return mark_safe(force_text(textile.textile(force_bytes(value), encoding='utf-8', output='utf-8')))
@register.filter(is_safe=True) @register.filter(is_safe=True)
def markdown(value, arg=''): def markdown(value, arg=''):
@ -80,5 +80,5 @@ def restructuredtext(value):
return force_text(value) return force_text(value)
else: else:
docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {}) docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
parts = publish_parts(source=smart_bytes(value), writer_name="html4css1", settings_overrides=docutils_settings) parts = publish_parts(source=force_bytes(value), writer_name="html4css1", settings_overrides=docutils_settings)
return mark_safe(force_text(parts["fragment"])) return mark_safe(force_text(parts["fragment"]))

View File

@ -14,7 +14,7 @@ from django.utils.crypto import constant_time_compare
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
from django.utils.crypto import salted_hmac from django.utils.crypto import salted_hmac
from django.utils import timezone from django.utils import timezone
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
class CreateError(Exception): class CreateError(Exception):
""" """
@ -84,7 +84,7 @@ class SessionBase(object):
return base64.b64encode(hash.encode() + b":" + pickled).decode('ascii') return base64.b64encode(hash.encode() + b":" + pickled).decode('ascii')
def decode(self, session_data): def decode(self, session_data):
encoded_data = base64.b64decode(smart_bytes(session_data)) encoded_data = base64.b64decode(force_bytes(session_data))
try: try:
# could produce ValueError if there is no ':' # could produce ValueError if there is no ':'
hash, pickled = encoded_data.split(b':', 1) hash, pickled = encoded_data.split(b':', 1)

View File

@ -16,7 +16,7 @@ from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.core.files.storage import FileSystemStorage, get_storage_class from django.core.files.storage import FileSystemStorage, get_storage_class
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.encoding import force_text, smart_bytes from django.utils.encoding import force_bytes, force_text
from django.utils.functional import LazyObject from django.utils.functional import LazyObject
from django.utils.importlib import import_module from django.utils.importlib import import_module
@ -118,7 +118,7 @@ class CachedFilesMixin(object):
return urlunsplit(unparsed_name) return urlunsplit(unparsed_name)
def cache_key(self, name): def cache_key(self, name):
return 'staticfiles:%s' % hashlib.md5(smart_bytes(name)).hexdigest() return 'staticfiles:%s' % hashlib.md5(force_bytes(name)).hexdigest()
def url(self, name, force=False): def url(self, name, force=False):
""" """
@ -254,7 +254,7 @@ class CachedFilesMixin(object):
if hashed_file_exists: if hashed_file_exists:
self.delete(hashed_name) self.delete(hashed_name)
# then save the processed result # then save the processed result
content_file = ContentFile(smart_bytes(content)) content_file = ContentFile(force_bytes(content))
saved_name = self._save(hashed_name, content_file) saved_name = self._save(hashed_name, content_file)
hashed_name = force_text(saved_name.replace('\\', '/')) hashed_name = force_text(saved_name.replace('\\', '/'))
processed = True processed = True

View File

@ -12,7 +12,7 @@ from django.conf import settings
from django.core.cache.backends.base import BaseCache from django.core.cache.backends.base import BaseCache
from django.db import connections, router, transaction, DatabaseError from django.db import connections, router, transaction, DatabaseError
from django.utils import timezone from django.utils import timezone
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
class Options(object): class Options(object):
@ -73,7 +73,7 @@ class DatabaseCache(BaseDatabaseCache):
transaction.commit_unless_managed(using=db) transaction.commit_unless_managed(using=db)
return default return default
value = connections[db].ops.process_clob(row[1]) value = connections[db].ops.process_clob(row[1])
return pickle.loads(base64.b64decode(smart_bytes(value))) return pickle.loads(base64.b64decode(force_bytes(value)))
def set(self, key, value, timeout=None, version=None): def set(self, key, value, timeout=None, version=None):
key = self.make_key(key, version=version) key = self.make_key(key, version=version)

View File

@ -10,7 +10,7 @@ except ImportError:
import pickle import pickle
from django.core.cache.backends.base import BaseCache from django.core.cache.backends.base import BaseCache
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
class FileBasedCache(BaseCache): class FileBasedCache(BaseCache):
def __init__(self, dir, params): def __init__(self, dir, params):
@ -137,7 +137,7 @@ class FileBasedCache(BaseCache):
Thus, a cache key of "foo" gets turnned into a file named Thus, a cache key of "foo" gets turnned into a file named
``{cache-dir}ac/bd/18db4cc2f85cedef654fccc4a4d8``. ``{cache-dir}ac/bd/18db4cc2f85cedef654fccc4a4d8``.
""" """
path = hashlib.md5(smart_bytes(key)).hexdigest() path = hashlib.md5(force_bytes(key)).hexdigest()
path = os.path.join(path[:2], path[2:4], path[4:]) path = os.path.join(path[:2], path[2:4], path[4:])
return os.path.join(self._dir, path) return os.path.join(self._dir, path)

View File

@ -3,7 +3,7 @@ from __future__ import unicode_literals
import os import os
from io import BytesIO, UnsupportedOperation from io import BytesIO, UnsupportedOperation
from django.utils.encoding import smart_bytes, smart_text from django.utils.encoding import smart_text
from django.core.files.utils import FileProxyMixin from django.core.files.utils import FileProxyMixin
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible

View File

@ -12,7 +12,6 @@ import json
from django.core.serializers.base import DeserializationError from django.core.serializers.base import DeserializationError
from django.core.serializers.python import Serializer as PythonSerializer from django.core.serializers.python import Serializer as PythonSerializer
from django.core.serializers.python import Deserializer as PythonDeserializer from django.core.serializers.python import Deserializer as PythonDeserializer
from django.utils.encoding import smart_bytes
from django.utils import six from django.utils import six
from django.utils.timezone import is_aware from django.utils.timezone import is_aware

View File

@ -12,7 +12,6 @@ from django.db import models
from django.core.serializers.base import DeserializationError from django.core.serializers.base import DeserializationError
from django.core.serializers.python import Serializer as PythonSerializer from django.core.serializers.python import Serializer as PythonSerializer
from django.core.serializers.python import Deserializer as PythonDeserializer from django.core.serializers.python import Deserializer as PythonDeserializer
from django.utils.encoding import smart_bytes
from django.utils import six from django.utils import six

View File

@ -51,7 +51,7 @@ from django.db.backends.signals import connection_created
from django.db.backends.oracle.client import DatabaseClient from django.db.backends.oracle.client import DatabaseClient
from django.db.backends.oracle.creation import DatabaseCreation from django.db.backends.oracle.creation import DatabaseCreation
from django.db.backends.oracle.introspection import DatabaseIntrospection from django.db.backends.oracle.introspection import DatabaseIntrospection
from django.utils.encoding import smart_bytes, force_text from django.utils.encoding import force_bytes, force_text
from django.utils import six from django.utils import six
from django.utils import timezone from django.utils import timezone
@ -64,7 +64,7 @@ IntegrityError = Database.IntegrityError
if int(Database.version.split('.', 1)[0]) >= 5 and not hasattr(Database, 'UNICODE'): if int(Database.version.split('.', 1)[0]) >= 5 and not hasattr(Database, 'UNICODE'):
convert_unicode = force_text convert_unicode = force_text
else: else:
convert_unicode = smart_bytes convert_unicode = force_bytes
class DatabaseFeatures(BaseDatabaseFeatures): class DatabaseFeatures(BaseDatabaseFeatures):
@ -602,9 +602,9 @@ class OracleParam(object):
elif param is False: elif param is False:
param = "0" param = "0"
if hasattr(param, 'bind_parameter'): if hasattr(param, 'bind_parameter'):
self.smart_bytes = param.bind_parameter(cursor) self.force_bytes = param.bind_parameter(cursor)
else: else:
self.smart_bytes = convert_unicode(param, cursor.charset, self.force_bytes = convert_unicode(param, cursor.charset,
strings_only) strings_only)
if hasattr(param, 'input_size'): if hasattr(param, 'input_size'):
# If parameter has `input_size` attribute, use that. # If parameter has `input_size` attribute, use that.
@ -683,7 +683,7 @@ class FormatStylePlaceholderCursor(object):
self.setinputsizes(*sizes) self.setinputsizes(*sizes)
def _param_generator(self, params): def _param_generator(self, params):
return [p.smart_bytes for p in params] return [p.force_bytes for p in params]
def execute(self, query, params=None): def execute(self, query, params=None):
if params is None: if params is None:

View File

@ -6,7 +6,7 @@ import hashlib
from time import time from time import time
from django.conf import settings from django.conf import settings
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
from django.utils.log import getLogger from django.utils.log import getLogger
from django.utils.timezone import utc from django.utils.timezone import utc
@ -138,7 +138,7 @@ def truncate_name(name, length=None, hash_len=4):
if length is None or len(name) <= length: if length is None or len(name) <= length:
return name return name
hsh = hashlib.md5(smart_bytes(name)).hexdigest()[:hash_len] hsh = hashlib.md5(force_bytes(name)).hexdigest()[:hash_len]
return '%s%s' % (name[:length-hash_len], hsh) return '%s%s' % (name[:length-hash_len], hsh)
def format_number(value, max_digits, decimal_places): def format_number(value, max_digits, decimal_places):

View File

@ -85,7 +85,7 @@ from django.core.files import uploadhandler
from django.http.multipartparser import MultiPartParser from django.http.multipartparser import MultiPartParser
from django.http.utils import * from django.http.utils import *
from django.utils.datastructures import MultiValueDict, ImmutableList from django.utils.datastructures import MultiValueDict, ImmutableList
from django.utils.encoding import smart_bytes, smart_str, iri_to_uri, force_text from django.utils.encoding import force_bytes, force_text, smart_str, iri_to_uri
from django.utils.http import cookie_date from django.utils.http import cookie_date
from django.utils import six from django.utils import six
from django.utils import timezone from django.utils import timezone
@ -489,13 +489,13 @@ class QueryDict(MultiValueDict):
""" """
output = [] output = []
if safe: if safe:
safe = smart_bytes(safe, self.encoding) safe = force_bytes(safe, self.encoding)
encode = lambda k, v: '%s=%s' % ((quote(k, safe), quote(v, safe))) encode = lambda k, v: '%s=%s' % ((quote(k, safe), quote(v, safe)))
else: else:
encode = lambda k, v: urlencode({k: v}) encode = lambda k, v: urlencode({k: v})
for k, list_ in self.lists(): for k, list_ in self.lists():
k = smart_bytes(k, self.encoding) k = force_bytes(k, self.encoding)
output.extend([encode(k, smart_bytes(v, self.encoding)) output.extend([encode(k, force_bytes(v, self.encoding))
for v in list_]) for v in list_])
return '&'.join(output) return '&'.join(output)
@ -680,7 +680,7 @@ class HttpResponse(object):
# force conversion to bytes in case chunk is a subclass # force conversion to bytes in case chunk is a subclass
return bytes(value) return bytes(value)
return b''.join(make_bytes(e) for e in self._container) return b''.join(make_bytes(e) for e in self._container)
return b''.join(smart_bytes(e, self._charset) for e in self._container) return b''.join(force_bytes(e, self._charset) for e in self._container)
@content.setter @content.setter
def content(self, value): def content(self, value):

View File

@ -4,7 +4,7 @@ import hashlib
from django.template import Library, Node, TemplateSyntaxError, Variable, VariableDoesNotExist from django.template import Library, Node, TemplateSyntaxError, Variable, VariableDoesNotExist
from django.template import resolve_variable from django.template import resolve_variable
from django.core.cache import cache from django.core.cache import cache
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
from django.utils.http import urlquote from django.utils.http import urlquote
register = Library() register = Library()
@ -26,8 +26,8 @@ class CacheNode(Node):
except (ValueError, TypeError): except (ValueError, TypeError):
raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time) raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time)
# Build a key for this fragment and all vary-on's. # Build a key for this fragment and all vary-on's.
key = smart_bytes(':'.join([urlquote(resolve_variable(var, context)) for var in self.vary_on])) key = ':'.join([urlquote(resolve_variable(var, context)) for var in self.vary_on])
args = hashlib.md5(key) args = hashlib.md5(force_bytes(key))
cache_key = 'template.cache.%s.%s' % (self.fragment_name, args.hexdigest()) cache_key = 'template.cache.%s.%s' % (self.fragment_name, args.hexdigest())
value = cache.get(cache_key) value = cache.get(cache_key)
if value is None: if value is None:

View File

@ -21,7 +21,7 @@ from django.http import SimpleCookie, HttpRequest, QueryDict
from django.template import TemplateDoesNotExist from django.template import TemplateDoesNotExist
from django.test import signals from django.test import signals
from django.utils.functional import curry from django.utils.functional import curry
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
from django.utils.http import urlencode from django.utils.http import urlencode
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.itercompat import is_iterable from django.utils.itercompat import is_iterable
@ -110,7 +110,7 @@ def encode_multipart(boundary, data):
as an application/octet-stream; otherwise, str(value) will be sent. as an application/octet-stream; otherwise, str(value) will be sent.
""" """
lines = [] lines = []
to_bytes = lambda s: smart_bytes(s, settings.DEFAULT_CHARSET) to_bytes = lambda s: force_bytes(s, settings.DEFAULT_CHARSET)
# Not by any means perfect, but good enough for our purposes. # Not by any means perfect, but good enough for our purposes.
is_file = lambda thing: hasattr(thing, "read") and callable(thing.read) is_file = lambda thing: hasattr(thing, "read") and callable(thing.read)
@ -147,7 +147,7 @@ def encode_multipart(boundary, data):
return b'\r\n'.join(lines) return b'\r\n'.join(lines)
def encode_file(boundary, key, file): def encode_file(boundary, key, file):
to_bytes = lambda s: smart_bytes(s, settings.DEFAULT_CHARSET) to_bytes = lambda s: force_bytes(s, settings.DEFAULT_CHARSET)
content_type = mimetypes.guess_type(file.name)[0] content_type = mimetypes.guess_type(file.name)[0]
if content_type is None: if content_type is None:
content_type = 'application/octet-stream' content_type = 'application/octet-stream'
@ -222,7 +222,7 @@ class RequestFactory(object):
charset = match.group(1) charset = match.group(1)
else: else:
charset = settings.DEFAULT_CHARSET charset = settings.DEFAULT_CHARSET
return smart_bytes(data, encoding=charset) return force_bytes(data, encoding=charset)
def _get_path(self, parsed): def _get_path(self, parsed):
# If there are parameters, add them # If there are parameters, add them
@ -293,7 +293,7 @@ class RequestFactory(object):
def generic(self, method, path, def generic(self, method, path,
data='', content_type='application/octet-stream', **extra): data='', content_type='application/octet-stream', **extra):
parsed = urlparse(path) parsed = urlparse(path)
data = smart_bytes(data, settings.DEFAULT_CHARSET) data = force_bytes(data, settings.DEFAULT_CHARSET)
r = { r = {
'PATH_INFO': self._get_path(parsed), 'PATH_INFO': self._get_path(parsed),
'QUERY_STRING': parsed[4], 'QUERY_STRING': parsed[4],

View File

@ -41,7 +41,7 @@ from django.test.utils import (get_warnings_state, restore_warnings_state,
override_settings) override_settings)
from django.test.utils import ContextList from django.test.utils import ContextList
from django.utils import unittest as ut2 from django.utils import unittest as ut2
from django.utils.encoding import smart_bytes, force_text from django.utils.encoding import force_text
from django.utils import six from django.utils import six
from django.utils.unittest.util import safe_repr from django.utils.unittest.util import safe_repr
from django.views.static import serve from django.views.static import serve

View File

@ -24,7 +24,7 @@ import time
from django.conf import settings from django.conf import settings
from django.core.cache import get_cache from django.core.cache import get_cache
from django.utils.encoding import iri_to_uri, force_text, smart_bytes from django.utils.encoding import iri_to_uri, force_bytes, force_text
from django.utils.http import http_date from django.utils.http import http_date
from django.utils.timezone import get_current_timezone_name from django.utils.timezone import get_current_timezone_name
from django.utils.translation import get_language from django.utils.translation import get_language
@ -181,14 +181,14 @@ def _generate_cache_key(request, method, headerlist, key_prefix):
value = request.META.get(header, None) value = request.META.get(header, None)
if value is not None: if value is not None:
ctx.update(value) ctx.update(value)
path = hashlib.md5(smart_bytes(iri_to_uri(request.get_full_path()))) path = hashlib.md5(force_bytes(iri_to_uri(request.get_full_path())))
cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % ( cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % (
key_prefix, method, path.hexdigest(), ctx.hexdigest()) key_prefix, method, path.hexdigest(), ctx.hexdigest())
return _i18n_cache_key_suffix(request, cache_key) return _i18n_cache_key_suffix(request, cache_key)
def _generate_cache_header_key(key_prefix, request): def _generate_cache_header_key(key_prefix, request):
"""Returns a cache key for the header cache.""" """Returns a cache key for the header cache."""
path = hashlib.md5(smart_bytes(iri_to_uri(request.get_full_path()))) path = hashlib.md5(force_bytes(iri_to_uri(request.get_full_path())))
cache_key = 'views.decorators.cache.cache_header.%s.%s' % ( cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
key_prefix, path.hexdigest()) key_prefix, path.hexdigest())
return _i18n_cache_key_suffix(request, cache_key) return _i18n_cache_key_suffix(request, cache_key)

View File

@ -23,7 +23,7 @@ except NotImplementedError:
using_sysrandom = False using_sysrandom = False
from django.conf import settings from django.conf import settings
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange from django.utils.six.moves import xrange
@ -51,7 +51,7 @@ def salted_hmac(key_salt, value, secret=None):
# line is redundant and could be replaced by key = key_salt + secret, since # line is redundant and could be replaced by key = key_salt + secret, since
# the hmac module does the same thing for keys longer than the block size. # the hmac module does the same thing for keys longer than the block size.
# However, we need to ensure that we *always* do this. # However, we need to ensure that we *always* do this.
return hmac.new(key, msg=smart_bytes(value), digestmod=hashlib.sha1) return hmac.new(key, msg=force_bytes(value), digestmod=hashlib.sha1)
def get_random_string(length=12, def get_random_string(length=12,
@ -147,8 +147,8 @@ def pbkdf2(password, salt, iterations, dklen=0, digest=None):
assert iterations > 0 assert iterations > 0
if not digest: if not digest:
digest = hashlib.sha256 digest = hashlib.sha256
password = smart_bytes(password) password = force_bytes(password)
salt = smart_bytes(salt) salt = force_bytes(salt)
hlen = digest().digest_size hlen = digest().digest_size
if not dklen: if not dklen:
dklen = hlen dklen = hlen

View File

@ -174,7 +174,7 @@ def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
# An Exception subclass containing non-ASCII data that doesn't # An Exception subclass containing non-ASCII data that doesn't
# know how to print itself properly. We shouldn't raise a # know how to print itself properly. We shouldn't raise a
# further exception. # further exception.
return ' '.join([smart_bytes(arg, encoding, strings_only, return ' '.join([force_bytes(arg, encoding, strings_only,
errors) for arg in s]) errors) for arg in s])
return six.text_type(s).encode(encoding, errors) return six.text_type(s).encode(encoding, errors)
else: else:
@ -225,7 +225,7 @@ def iri_to_uri(iri):
# converted. # converted.
if iri is None: if iri is None:
return iri return iri
return quote(smart_bytes(iri), safe=b"/#%[]=:;$&()+,!?*@'~") return quote(force_bytes(iri), safe=b"/#%[]=:;$&()+,!?*@'~")
def filepath_to_uri(path): def filepath_to_uri(path):
"""Convert an file system path to a URI portion that is suitable for """Convert an file system path to a URI portion that is suitable for
@ -244,7 +244,7 @@ def filepath_to_uri(path):
return path return path
# I know about `os.sep` and `os.altsep` but I want to leave # I know about `os.sep` and `os.altsep` but I want to leave
# some flexibility for hardcoding separators. # some flexibility for hardcoding separators.
return quote(smart_bytes(path.replace("\\", "/")), safe=b"/~!*()'") return quote(force_bytes(path.replace("\\", "/")), safe=b"/~!*()'")
# The encoding of the default system locale but falls back to the # The encoding of the default system locale but falls back to the
# given fallback encoding if the encoding is unsupported by python or could # given fallback encoding if the encoding is unsupported by python or could

View File

@ -11,7 +11,7 @@ except ImportError: # Python 2
from urlparse import urlsplit, urlunsplit from urlparse import urlsplit, urlunsplit
from django.utils.safestring import SafeData, mark_safe from django.utils.safestring import SafeData, mark_safe
from django.utils.encoding import smart_bytes, force_text from django.utils.encoding import force_bytes, force_text
from django.utils.functional import allow_lazy from django.utils.functional import allow_lazy
from django.utils import six from django.utils import six
from django.utils.text import normalize_newlines from django.utils.text import normalize_newlines
@ -164,7 +164,7 @@ def smart_urlquote(url):
# contains a % not followed by two hexadecimal digits. See #9655. # contains a % not followed by two hexadecimal digits. See #9655.
if '%' not in url or unquoted_percents_re.search(url): if '%' not in url or unquoted_percents_re.search(url):
# See http://bugs.python.org/issue2637 # See http://bugs.python.org/issue2637
url = quote(smart_bytes(url), safe=b'!*\'();:@&=+$,/?#[]~') url = quote(force_bytes(url), safe=b'!*\'();:@&=+$,/?#[]~')
return force_text(url) return force_text(url)

View File

@ -14,7 +14,7 @@ from django.template import Template, Context, TemplateDoesNotExist
from django.template.defaultfilters import force_escape, pprint from django.template.defaultfilters import force_escape, pprint
from django.utils.html import escape from django.utils.html import escape
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.encoding import smart_text, smart_bytes from django.utils.encoding import force_bytes, smart_text
from django.utils import six from django.utils import six
HIDDEN_SETTINGS = re.compile('API|TOKEN|KEY|SECRET|PASS|PROFANITIES_LIST|SIGNATURE') HIDDEN_SETTINGS = re.compile('API|TOKEN|KEY|SECRET|PASS|PROFANITIES_LIST|SIGNATURE')
@ -440,7 +440,7 @@ def technical_404_response(request, exception):
'root_urlconf': settings.ROOT_URLCONF, 'root_urlconf': settings.ROOT_URLCONF,
'request_path': request.path_info[1:], # Trim leading slash 'request_path': request.path_info[1:], # Trim leading slash
'urlpatterns': tried, 'urlpatterns': tried,
'reason': smart_bytes(exception, errors='replace'), 'reason': force_bytes(exception, errors='replace'),
'request': request, 'request': request,
'settings': get_safe_settings(), 'settings': get_safe_settings(),
}) })

View File

@ -459,9 +459,9 @@ using ``__str__()`` like this::
last_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50)
def __str__(self): def __str__(self):
# Note use of django.utils.encoding.smart_bytes() here because # Note use of django.utils.encoding.force_bytes() here because
# first_name and last_name will be unicode strings. # first_name and last_name will be unicode strings.
return smart_bytes('%s %s' % (self.first_name, self.last_name)) return force_bytes('%s %s' % (self.first_name, self.last_name))
``get_absolute_url`` ``get_absolute_url``
-------------------- --------------------

View File

@ -183,7 +183,7 @@ compose a prefix, version and key into a final cache key. The default
implementation is equivalent to the function:: implementation is equivalent to the function::
def make_key(key, key_prefix, version): def make_key(key, key_prefix, version):
return ':'.join([key_prefix, str(version), smart_bytes(key)]) return ':'.join([key_prefix, str(version), key])
You may use any key function you want, as long as it has the same You may use any key function you want, as long as it has the same
argument signature. argument signature.

View File

@ -227,7 +227,7 @@ If you have written a :ref:`custom password hasher <auth_password_storage>`,
your ``encode()``, ``verify()`` or ``safe_summary()`` methods should accept your ``encode()``, ``verify()`` or ``safe_summary()`` methods should accept
Unicode parameters (``password``, ``salt`` or ``encoded``). If any of the Unicode parameters (``password``, ``salt`` or ``encoded``). If any of the
hashing methods need byte strings, you can use the hashing methods need byte strings, you can use the
:func:`~django.utils.encoding.smart_bytes` utility to encode the strings. :func:`~django.utils.encoding.force_bytes` utility to encode the strings.
Validation of previous_page_number and next_page_number Validation of previous_page_number and next_page_number
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -864,7 +864,7 @@ key version to provide a final cache key. By default, the three parts
are joined using colons to produce a final string:: are joined using colons to produce a final string::
def make_key(key, key_prefix, version): def make_key(key, key_prefix, version):
return ':'.join([key_prefix, str(version), smart_bytes(key)]) return ':'.join([key_prefix, str(version), key])
If you want to combine the parts in different ways, or apply other If you want to combine the parts in different ways, or apply other
processing to the final key (e.g., taking a hash digest of the key processing to the final key (e.g., taking a hash digest of the key

View File

@ -171,7 +171,7 @@ class UtilTests(unittest.TestCase):
) )
self.assertEqual( self.assertEqual(
label_for_field("__str__", Article), label_for_field("__str__", Article),
b"article" str("article")
) )
self.assertRaises( self.assertRaises(

View File

@ -30,7 +30,7 @@ from django.template.response import TemplateResponse
from django.test import TestCase from django.test import TestCase
from django.utils import formats, translation, unittest from django.utils import formats, translation, unittest
from django.utils.cache import get_max_age from django.utils.cache import get_max_age
from django.utils.encoding import iri_to_uri, smart_bytes from django.utils.encoding import iri_to_uri, force_bytes
from django.utils.html import escape from django.utils.html import escape
from django.utils.http import urlencode from django.utils.http import urlencode
from django.utils import six from django.utils import six
@ -84,7 +84,7 @@ class AdminViewBasicTest(TestCase):
content. content.
""" """
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTrue(response.content.index(smart_bytes(text1)) < response.content.index(smart_bytes(text2)), self.assertTrue(response.content.index(force_bytes(text1)) < response.content.index(force_bytes(text2)),
failing_msg failing_msg
) )
@ -1378,9 +1378,9 @@ class AdminViewStringPrimaryKeyTest(TestCase):
logentry.content_type = None logentry.content_type = None
logentry.save() logentry.save()
counted_presence_before = response.content.count(smart_bytes(should_contain)) counted_presence_before = response.content.count(force_bytes(should_contain))
response = self.client.get('/test_admin/admin/') response = self.client.get('/test_admin/admin/')
counted_presence_after = response.content.count(smart_bytes(should_contain)) counted_presence_after = response.content.count(force_bytes(should_contain))
self.assertEqual(counted_presence_before - 1, self.assertEqual(counted_presence_before - 1,
counted_presence_after) counted_presence_after)

View File

@ -12,7 +12,7 @@ from django.core.files import temp as tempfile
from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files.uploadedfile import SimpleUploadedFile
from django.http.multipartparser import MultiPartParser from django.http.multipartparser import MultiPartParser
from django.test import TestCase, client from django.test import TestCase, client
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
from django.utils.six import StringIO from django.utils.six import StringIO
from django.utils import unittest from django.utils import unittest
@ -54,7 +54,7 @@ class FileUploadTests(TestCase):
post_data[key + '_hash'] = hashlib.sha1(post_data[key].read()).hexdigest() post_data[key + '_hash'] = hashlib.sha1(post_data[key].read()).hexdigest()
post_data[key].seek(0) post_data[key].seek(0)
except AttributeError: except AttributeError:
post_data[key + '_hash'] = hashlib.sha1(smart_bytes(post_data[key])).hexdigest() post_data[key + '_hash'] = hashlib.sha1(force_bytes(post_data[key])).hexdigest()
response = self.client.post('/file_uploads/verify/', post_data) response = self.client.post('/file_uploads/verify/', post_data)
@ -68,7 +68,7 @@ class FileUploadTests(TestCase):
'Content-Type: application/octet-stream', 'Content-Type: application/octet-stream',
'Content-Transfer-Encoding: base64', 'Content-Transfer-Encoding: base64',
'', '',
base64.b64encode(smart_bytes(test_string)).decode('ascii'), base64.b64encode(force_bytes(test_string)).decode('ascii'),
'--' + client.BOUNDARY + '--', '--' + client.BOUNDARY + '--',
'', '',
]).encode('utf-8') ]).encode('utf-8')

View File

@ -7,7 +7,7 @@ import os
from django.core.files.uploadedfile import UploadedFile from django.core.files.uploadedfile import UploadedFile
from django.http import HttpResponse, HttpResponseServerError from django.http import HttpResponse, HttpResponseServerError
from django.utils import six from django.utils import six
from django.utils.encoding import smart_bytes from django.utils.encoding import force_bytes
from .models import FileModel, UPLOAD_TO from .models import FileModel, UPLOAD_TO
from .tests import UNICODE_FILENAME from .tests import UNICODE_FILENAME
@ -46,7 +46,7 @@ def file_upload_view_verify(request):
if isinstance(value, UploadedFile): if isinstance(value, UploadedFile):
new_hash = hashlib.sha1(value.read()).hexdigest() new_hash = hashlib.sha1(value.read()).hexdigest()
else: else:
new_hash = hashlib.sha1(smart_bytes(value)).hexdigest() new_hash = hashlib.sha1(force_bytes(value)).hexdigest()
if new_hash != submitted_hash: if new_hash != submitted_hash:
return HttpResponseServerError() return HttpResponseServerError()