From ebc773ada3e4f40cf5084268387b873d7fe22e8b Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Tue, 28 Aug 2012 20:59:56 +0200 Subject: [PATCH] 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. --- django/contrib/admin/util.py | 4 ++-- django/contrib/admin/views/main.py | 4 ++-- django/contrib/admindocs/utils.py | 4 ++-- django/contrib/auth/hashers.py | 8 ++++---- django/contrib/gis/sitemaps/views.py | 3 +-- django/contrib/markup/templatetags/markup.py | 6 +++--- django/contrib/sessions/backends/base.py | 4 ++-- django/contrib/staticfiles/storage.py | 6 +++--- django/core/cache/backends/db.py | 4 ++-- django/core/cache/backends/filebased.py | 4 ++-- django/core/files/base.py | 2 +- django/core/serializers/json.py | 1 - django/core/serializers/pyyaml.py | 1 - django/db/backends/oracle/base.py | 10 +++++----- django/db/backends/util.py | 4 ++-- django/http/__init__.py | 10 +++++----- django/templatetags/cache.py | 6 +++--- django/test/client.py | 10 +++++----- django/test/testcases.py | 2 +- django/utils/cache.py | 6 +++--- django/utils/crypto.py | 8 ++++---- django/utils/encoding.py | 6 +++--- django/utils/html.py | 4 ++-- django/views/debug.py | 4 ++-- docs/ref/models/instances.txt | 4 ++-- docs/ref/settings.txt | 2 +- docs/releases/1.5.txt | 2 +- docs/topics/cache.txt | 2 +- tests/regressiontests/admin_util/tests.py | 2 +- tests/regressiontests/admin_views/tests.py | 8 ++++---- tests/regressiontests/file_uploads/tests.py | 6 +++--- tests/regressiontests/file_uploads/views.py | 4 ++-- 32 files changed, 74 insertions(+), 77 deletions(-) diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py index ff90e1d0079..889f692ac36 100644 --- a/django/contrib/admin/util.py +++ b/django/contrib/admin/util.py @@ -12,7 +12,7 @@ from django.utils import formats from django.utils.html import format_html from django.utils.text import capfirst 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.translation import ungettext 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) attr = six.text_type elif name == "__str__": - label = smart_bytes(model._meta.verbose_name) + label = force_str(model._meta.verbose_name) attr = bytes else: if callable(name): diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index 30074f16487..74ef095b4bc 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -6,7 +6,7 @@ from django.core.paginator import InvalidPage from django.db import models from django.db.models.fields import FieldDoesNotExist 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.http import urlencode @@ -94,7 +94,7 @@ class ChangeList(object): # 'key' will be used as a keyword argument later, so Python # requires it to be a string. 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): raise SuspiciousOperation("Filtering by %s not allowed" % key) diff --git a/django/contrib/admindocs/utils.py b/django/contrib/admindocs/utils.py index 0e10eb4fa3a..9be0093dfcc 100644 --- a/django/contrib/admindocs/utils.py +++ b/django/contrib/admindocs/utils.py @@ -6,7 +6,7 @@ from email.errors import HeaderParseError from django.utils.safestring import mark_safe from django.core.urlresolvers import reverse -from django.utils.encoding import smart_bytes +from django.utils.encoding import force_bytes try: import docutils.core 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('/') } 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, destination_path=None, writer_name='html', settings_overrides=overrides) diff --git a/django/contrib/auth/hashers.py b/django/contrib/auth/hashers.py index cdaf75636fd..bd0c6778c92 100644 --- a/django/contrib/auth/hashers.py +++ b/django/contrib/auth/hashers.py @@ -8,7 +8,7 @@ from django.conf import settings from django.test.signals import setting_changed from django.utils import importlib 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.utils.crypto import ( pbkdf2, constant_time_compare, get_random_string) @@ -299,7 +299,7 @@ class SHA1PasswordHasher(BasePasswordHasher): def encode(self, password, salt): assert password 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) def verify(self, password, encoded): @@ -327,7 +327,7 @@ class MD5PasswordHasher(BasePasswordHasher): def encode(self, password, salt): assert password 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) def verify(self, password, encoded): @@ -361,7 +361,7 @@ class UnsaltedMD5PasswordHasher(BasePasswordHasher): return '' def encode(self, password, salt): - return hashlib.md5(smart_bytes(password)).hexdigest() + return hashlib.md5(force_bytes(password)).hexdigest() def verify(self, password, encoded): encoded_2 = self.encode(password, '') diff --git a/django/contrib/gis/sitemaps/views.py b/django/contrib/gis/sitemaps/views.py index 8bcdba1b44d..36e48f7d209 100644 --- a/django/contrib/gis/sitemaps/views.py +++ b/django/contrib/gis/sitemaps/views.py @@ -8,7 +8,6 @@ from django.core.paginator import EmptyPage, PageNotAnInteger from django.contrib.gis.db.models.fields import GeometryField from django.db import connections, DEFAULT_DB_ALIAS from django.db.models import get_model -from django.utils.encoding import smart_bytes from django.utils import six from django.utils.translation import ugettext as _ @@ -61,7 +60,7 @@ def sitemap(request, sitemaps, section=None): raise Http404(_("Page %s empty") % page) except PageNotAnInteger: 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') def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS): diff --git a/django/contrib/markup/templatetags/markup.py b/django/contrib/markup/templatetags/markup.py index af9c842f423..18b7475ca7f 100644 --- a/django/contrib/markup/templatetags/markup.py +++ b/django/contrib/markup/templatetags/markup.py @@ -13,7 +13,7 @@ markup syntaxes to HTML; currently there is support for: from django import template 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 register = template.Library() @@ -27,7 +27,7 @@ def textile(value): raise template.TemplateSyntaxError("Error in 'textile' filter: The Python textile library isn't installed.") return force_text(value) 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) def markdown(value, arg=''): @@ -80,5 +80,5 @@ def restructuredtext(value): return force_text(value) else: 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"])) diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py index 2fb7991b498..c8393f23c61 100644 --- a/django/contrib/sessions/backends/base.py +++ b/django/contrib/sessions/backends/base.py @@ -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 salted_hmac from django.utils import timezone -from django.utils.encoding import smart_bytes +from django.utils.encoding import force_bytes class CreateError(Exception): """ @@ -84,7 +84,7 @@ class SessionBase(object): return base64.b64encode(hash.encode() + b":" + pickled).decode('ascii') def decode(self, session_data): - encoded_data = base64.b64decode(smart_bytes(session_data)) + encoded_data = base64.b64decode(force_bytes(session_data)) try: # could produce ValueError if there is no ':' hash, pickled = encoded_data.split(b':', 1) diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py index 4be7540c6ea..9691b7849d4 100644 --- a/django/contrib/staticfiles/storage.py +++ b/django/contrib/staticfiles/storage.py @@ -16,7 +16,7 @@ from django.core.exceptions import ImproperlyConfigured from django.core.files.base import ContentFile from django.core.files.storage import FileSystemStorage, get_storage_class 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.importlib import import_module @@ -118,7 +118,7 @@ class CachedFilesMixin(object): return urlunsplit(unparsed_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): """ @@ -254,7 +254,7 @@ class CachedFilesMixin(object): if hashed_file_exists: self.delete(hashed_name) # 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) hashed_name = force_text(saved_name.replace('\\', '/')) processed = True diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py index 52db4d1b1da..348b03f7337 100644 --- a/django/core/cache/backends/db.py +++ b/django/core/cache/backends/db.py @@ -12,7 +12,7 @@ from django.conf import settings from django.core.cache.backends.base import BaseCache from django.db import connections, router, transaction, DatabaseError from django.utils import timezone -from django.utils.encoding import smart_bytes +from django.utils.encoding import force_bytes class Options(object): @@ -73,7 +73,7 @@ class DatabaseCache(BaseDatabaseCache): transaction.commit_unless_managed(using=db) return default 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): key = self.make_key(key, version=version) diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py index c54e8d280f9..96194d458fd 100644 --- a/django/core/cache/backends/filebased.py +++ b/django/core/cache/backends/filebased.py @@ -10,7 +10,7 @@ except ImportError: import pickle 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): def __init__(self, dir, params): @@ -137,7 +137,7 @@ class FileBasedCache(BaseCache): Thus, a cache key of "foo" gets turnned into a file named ``{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:]) return os.path.join(self._dir, path) diff --git a/django/core/files/base.py b/django/core/files/base.py index 4a422be90db..4f40606c533 100644 --- a/django/core/files/base.py +++ b/django/core/files/base.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import os 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.utils.encoding import python_2_unicode_compatible diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py index 4ba0d7fd799..ed65f2922c3 100644 --- a/django/core/serializers/json.py +++ b/django/core/serializers/json.py @@ -12,7 +12,6 @@ import json from django.core.serializers.base import DeserializationError from django.core.serializers.python import Serializer as PythonSerializer from django.core.serializers.python import Deserializer as PythonDeserializer -from django.utils.encoding import smart_bytes from django.utils import six from django.utils.timezone import is_aware diff --git a/django/core/serializers/pyyaml.py b/django/core/serializers/pyyaml.py index 9be1ea44925..4c11626badb 100644 --- a/django/core/serializers/pyyaml.py +++ b/django/core/serializers/pyyaml.py @@ -12,7 +12,6 @@ from django.db import models from django.core.serializers.base import DeserializationError from django.core.serializers.python import Serializer as PythonSerializer from django.core.serializers.python import Deserializer as PythonDeserializer -from django.utils.encoding import smart_bytes from django.utils import six diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index 59132502062..6bf2e815a73 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -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.creation import DatabaseCreation 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 timezone @@ -64,7 +64,7 @@ IntegrityError = Database.IntegrityError if int(Database.version.split('.', 1)[0]) >= 5 and not hasattr(Database, 'UNICODE'): convert_unicode = force_text else: - convert_unicode = smart_bytes + convert_unicode = force_bytes class DatabaseFeatures(BaseDatabaseFeatures): @@ -602,9 +602,9 @@ class OracleParam(object): elif param is False: param = "0" if hasattr(param, 'bind_parameter'): - self.smart_bytes = param.bind_parameter(cursor) + self.force_bytes = param.bind_parameter(cursor) else: - self.smart_bytes = convert_unicode(param, cursor.charset, + self.force_bytes = convert_unicode(param, cursor.charset, strings_only) if hasattr(param, 'input_size'): # If parameter has `input_size` attribute, use that. @@ -683,7 +683,7 @@ class FormatStylePlaceholderCursor(object): self.setinputsizes(*sizes) 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): if params is None: diff --git a/django/db/backends/util.py b/django/db/backends/util.py index 9d70248ebf6..75d4d07a665 100644 --- a/django/db/backends/util.py +++ b/django/db/backends/util.py @@ -6,7 +6,7 @@ import hashlib from time import time 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.timezone import utc @@ -138,7 +138,7 @@ def truncate_name(name, length=None, hash_len=4): if length is None or len(name) <= length: 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) def format_number(value, max_digits, decimal_places): diff --git a/django/http/__init__.py b/django/http/__init__.py index c6f104695d0..b458fe6c2ed 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -85,7 +85,7 @@ from django.core.files import uploadhandler from django.http.multipartparser import MultiPartParser from django.http.utils import * 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 import six from django.utils import timezone @@ -489,13 +489,13 @@ class QueryDict(MultiValueDict): """ output = [] 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))) else: encode = lambda k, v: urlencode({k: v}) for k, list_ in self.lists(): - k = smart_bytes(k, self.encoding) - output.extend([encode(k, smart_bytes(v, self.encoding)) + k = force_bytes(k, self.encoding) + output.extend([encode(k, force_bytes(v, self.encoding)) for v in list_]) return '&'.join(output) @@ -680,7 +680,7 @@ class HttpResponse(object): # force conversion to bytes in case chunk is a subclass return bytes(value) 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 def content(self, value): diff --git a/django/templatetags/cache.py b/django/templatetags/cache.py index e431f99d0d7..36db4807c79 100644 --- a/django/templatetags/cache.py +++ b/django/templatetags/cache.py @@ -4,7 +4,7 @@ import hashlib from django.template import Library, Node, TemplateSyntaxError, Variable, VariableDoesNotExist from django.template import resolve_variable 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 register = Library() @@ -26,8 +26,8 @@ class CacheNode(Node): except (ValueError, TypeError): raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time) # 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])) - args = hashlib.md5(key) + key = ':'.join([urlquote(resolve_variable(var, context)) for var in self.vary_on]) + args = hashlib.md5(force_bytes(key)) cache_key = 'template.cache.%s.%s' % (self.fragment_name, args.hexdigest()) value = cache.get(cache_key) if value is None: diff --git a/django/test/client.py b/django/test/client.py index 2b61c51ce17..8fd765ec9aa 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -21,7 +21,7 @@ from django.http import SimpleCookie, HttpRequest, QueryDict from django.template import TemplateDoesNotExist from django.test import signals 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.importlib import import_module 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. """ 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. 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) 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] if content_type is None: content_type = 'application/octet-stream' @@ -222,7 +222,7 @@ class RequestFactory(object): charset = match.group(1) else: charset = settings.DEFAULT_CHARSET - return smart_bytes(data, encoding=charset) + return force_bytes(data, encoding=charset) def _get_path(self, parsed): # If there are parameters, add them @@ -293,7 +293,7 @@ class RequestFactory(object): def generic(self, method, path, data='', content_type='application/octet-stream', **extra): parsed = urlparse(path) - data = smart_bytes(data, settings.DEFAULT_CHARSET) + data = force_bytes(data, settings.DEFAULT_CHARSET) r = { 'PATH_INFO': self._get_path(parsed), 'QUERY_STRING': parsed[4], diff --git a/django/test/testcases.py b/django/test/testcases.py index e0f2655ee8c..f12c431d3a5 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -41,7 +41,7 @@ from django.test.utils import (get_warnings_state, restore_warnings_state, override_settings) from django.test.utils import ContextList 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.unittest.util import safe_repr from django.views.static import serve diff --git a/django/utils/cache.py b/django/utils/cache.py index 3e99833aa66..91c47969888 100644 --- a/django/utils/cache.py +++ b/django/utils/cache.py @@ -24,7 +24,7 @@ import time from django.conf import settings 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.timezone import get_current_timezone_name 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) if value is not None: 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' % ( key_prefix, method, path.hexdigest(), ctx.hexdigest()) return _i18n_cache_key_suffix(request, cache_key) def _generate_cache_header_key(key_prefix, request): """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' % ( key_prefix, path.hexdigest()) return _i18n_cache_key_suffix(request, cache_key) diff --git a/django/utils/crypto.py b/django/utils/crypto.py index 1fdcc3082e1..57bc60dc4f8 100644 --- a/django/utils/crypto.py +++ b/django/utils/crypto.py @@ -23,7 +23,7 @@ except NotImplementedError: using_sysrandom = False 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.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 # the hmac module does the same thing for keys longer than the block size. # 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, @@ -147,8 +147,8 @@ def pbkdf2(password, salt, iterations, dklen=0, digest=None): assert iterations > 0 if not digest: digest = hashlib.sha256 - password = smart_bytes(password) - salt = smart_bytes(salt) + password = force_bytes(password) + salt = force_bytes(salt) hlen = digest().digest_size if not dklen: dklen = hlen diff --git a/django/utils/encoding.py b/django/utils/encoding.py index 7027b82a61d..998cf4f3f77 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -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 # know how to print itself properly. We shouldn't raise a # further exception. - return ' '.join([smart_bytes(arg, encoding, strings_only, + return ' '.join([force_bytes(arg, encoding, strings_only, errors) for arg in s]) return six.text_type(s).encode(encoding, errors) else: @@ -225,7 +225,7 @@ def iri_to_uri(iri): # converted. if iri is None: return iri - return quote(smart_bytes(iri), safe=b"/#%[]=:;$&()+,!?*@'~") + return quote(force_bytes(iri), safe=b"/#%[]=:;$&()+,!?*@'~") def filepath_to_uri(path): """Convert an file system path to a URI portion that is suitable for @@ -244,7 +244,7 @@ def filepath_to_uri(path): return path # I know about `os.sep` and `os.altsep` but I want to leave # 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 # given fallback encoding if the encoding is unsupported by python or could diff --git a/django/utils/html.py b/django/utils/html.py index 0ee789ebb5d..2b669cc8ecc 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -11,7 +11,7 @@ except ImportError: # Python 2 from urlparse import urlsplit, urlunsplit 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 import six 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. if '%' not in url or unquoted_percents_re.search(url): # See http://bugs.python.org/issue2637 - url = quote(smart_bytes(url), safe=b'!*\'();:@&=+$,/?#[]~') + url = quote(force_bytes(url), safe=b'!*\'();:@&=+$,/?#[]~') return force_text(url) diff --git a/django/views/debug.py b/django/views/debug.py index b275ef9e73d..ed99d8dfe64 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -14,7 +14,7 @@ from django.template import Template, Context, TemplateDoesNotExist from django.template.defaultfilters import force_escape, pprint from django.utils.html import escape 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 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, 'request_path': request.path_info[1:], # Trim leading slash 'urlpatterns': tried, - 'reason': smart_bytes(exception, errors='replace'), + 'reason': force_bytes(exception, errors='replace'), 'request': request, 'settings': get_safe_settings(), }) diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt index b7ae3890cff..b01abc1936b 100644 --- a/docs/ref/models/instances.txt +++ b/docs/ref/models/instances.txt @@ -459,9 +459,9 @@ using ``__str__()`` like this:: last_name = models.CharField(max_length=50) 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. - 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`` -------------------- diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index 32ba5f0ced2..4729a2b6f19 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -183,7 +183,7 @@ compose a prefix, version and key into a final cache key. The default implementation is equivalent to the function:: 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 argument signature. diff --git a/docs/releases/1.5.txt b/docs/releases/1.5.txt index 6042569cea1..5728d8559a8 100644 --- a/docs/releases/1.5.txt +++ b/docs/releases/1.5.txt @@ -227,7 +227,7 @@ If you have written a :ref:`custom password hasher `, your ``encode()``, ``verify()`` or ``safe_summary()`` methods should accept Unicode parameters (``password``, ``salt`` or ``encoded``). If any of 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt index 219b6c77957..f13238e3427 100644 --- a/docs/topics/cache.txt +++ b/docs/topics/cache.txt @@ -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:: 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 processing to the final key (e.g., taking a hash digest of the key diff --git a/tests/regressiontests/admin_util/tests.py b/tests/regressiontests/admin_util/tests.py index 6b6dad4336d..d04740ce951 100644 --- a/tests/regressiontests/admin_util/tests.py +++ b/tests/regressiontests/admin_util/tests.py @@ -171,7 +171,7 @@ class UtilTests(unittest.TestCase): ) self.assertEqual( label_for_field("__str__", Article), - b"article" + str("article") ) self.assertRaises( diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 679e3a03463..cf7d4855fb3 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -30,7 +30,7 @@ from django.template.response import TemplateResponse from django.test import TestCase from django.utils import formats, translation, unittest 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.http import urlencode from django.utils import six @@ -84,7 +84,7 @@ class AdminViewBasicTest(TestCase): content. """ 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 ) @@ -1378,9 +1378,9 @@ class AdminViewStringPrimaryKeyTest(TestCase): logentry.content_type = None 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/') - 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, counted_presence_after) diff --git a/tests/regressiontests/file_uploads/tests.py b/tests/regressiontests/file_uploads/tests.py index 19af992a36d..a545ed649e5 100644 --- a/tests/regressiontests/file_uploads/tests.py +++ b/tests/regressiontests/file_uploads/tests.py @@ -12,7 +12,7 @@ from django.core.files import temp as tempfile from django.core.files.uploadedfile import SimpleUploadedFile from django.http.multipartparser import MultiPartParser 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 import unittest @@ -54,7 +54,7 @@ class FileUploadTests(TestCase): post_data[key + '_hash'] = hashlib.sha1(post_data[key].read()).hexdigest() post_data[key].seek(0) 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) @@ -68,7 +68,7 @@ class FileUploadTests(TestCase): 'Content-Type: application/octet-stream', 'Content-Transfer-Encoding: base64', '', - base64.b64encode(smart_bytes(test_string)).decode('ascii'), + base64.b64encode(force_bytes(test_string)).decode('ascii'), '--' + client.BOUNDARY + '--', '', ]).encode('utf-8') diff --git a/tests/regressiontests/file_uploads/views.py b/tests/regressiontests/file_uploads/views.py index 95b2124fd99..fcf32ceceae 100644 --- a/tests/regressiontests/file_uploads/views.py +++ b/tests/regressiontests/file_uploads/views.py @@ -7,7 +7,7 @@ import os from django.core.files.uploadedfile import UploadedFile from django.http import HttpResponse, HttpResponseServerError 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 .tests import UNICODE_FILENAME @@ -46,7 +46,7 @@ def file_upload_view_verify(request): if isinstance(value, UploadedFile): new_hash = hashlib.sha1(value.read()).hexdigest() else: - new_hash = hashlib.sha1(smart_bytes(value)).hexdigest() + new_hash = hashlib.sha1(force_bytes(value)).hexdigest() if new_hash != submitted_hash: return HttpResponseServerError()