From c5ef65bcf324f4c90b53be90f4aec069a68e8c59 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 21 Jul 2012 10:00:10 +0200 Subject: [PATCH] [py3] Ported django.utils.encoding. * Renamed smart_unicode to smart_text (but kept the old name under Python 2 for backwards compatibility). * Renamed smart_str to smart_bytes. * Re-introduced smart_str as an alias for smart_text under Python 3 and smart_bytes under Python 2 (which is backwards compatible). Thus smart_str always returns a str objects. * Used the new smart_str in a few places where both Python 2 and 3 want a str. --- django/contrib/admin/actions.py | 8 +-- django/contrib/admin/filters.py | 8 +-- django/contrib/admin/helpers.py | 8 +-- django/contrib/admin/models.py | 6 +- django/contrib/admin/options.py | 58 ++++++++-------- .../contrib/admin/templatetags/admin_list.py | 12 ++-- django/contrib/admin/util.py | 18 ++--- django/contrib/admin/views/main.py | 6 +- django/contrib/admin/widgets.py | 8 +-- django/contrib/admindocs/utils.py | 4 +- django/contrib/auth/hashers.py | 8 +-- django/contrib/auth/tests/forms.py | 28 ++++---- django/contrib/auth/tests/views.py | 4 +- django/contrib/comments/forms.py | 4 +- django/contrib/comments/managers.py | 4 +- .../contrib/comments/templatetags/comments.py | 4 +- django/contrib/contenttypes/generic.py | 4 +- django/contrib/contenttypes/management.py | 4 +- django/contrib/contenttypes/models.py | 10 +-- django/contrib/databrowse/datastructures.py | 16 ++--- .../contrib/databrowse/plugins/calendars.py | 4 +- .../databrowse/plugins/fieldchoices.py | 4 +- .../contrib/formtools/wizard/storage/base.py | 4 +- django/contrib/gis/sitemaps/views.py | 4 +- .../contrib/humanize/templatetags/humanize.py | 4 +- django/contrib/localflavor/au/forms.py | 4 +- django/contrib/localflavor/br/forms.py | 8 +-- django/contrib/localflavor/ca/forms.py | 4 +- django/contrib/localflavor/ch/forms.py | 4 +- django/contrib/localflavor/cl/forms.py | 4 +- django/contrib/localflavor/fr/forms.py | 4 +- django/contrib/localflavor/hk/forms.py | 4 +- django/contrib/localflavor/hr/forms.py | 6 +- django/contrib/localflavor/id/forms.py | 10 +-- django/contrib/localflavor/in_/forms.py | 6 +- django/contrib/localflavor/is_/forms.py | 4 +- django/contrib/localflavor/it/forms.py | 4 +- django/contrib/localflavor/it/util.py | 6 +- django/contrib/localflavor/nl/forms.py | 4 +- django/contrib/localflavor/pt/forms.py | 6 +- django/contrib/localflavor/tr/forms.py | 4 +- django/contrib/localflavor/us/forms.py | 4 +- django/contrib/markup/templatetags/markup.py | 20 +++--- django/contrib/messages/storage/base.py | 14 ++-- django/contrib/sessions/backends/db.py | 4 +- .../management/commands/collectstatic.py | 6 +- .../management/commands/findstatic.py | 6 +- django/contrib/staticfiles/storage.py | 10 +-- django/contrib/syndication/views.py | 12 ++-- django/core/cache/backends/base.py | 6 +- django/core/context_processors.py | 4 +- django/core/exceptions.py | 6 +- django/core/files/base.py | 6 +- django/core/files/storage.py | 4 +- django/core/files/uploadedfile.py | 4 +- django/core/handlers/base.py | 8 +-- django/core/handlers/wsgi.py | 6 +- django/core/mail/message.py | 12 ++-- .../management/commands/createcachetable.py | 4 +- django/core/management/commands/loaddata.py | 4 +- django/core/serializers/base.py | 2 +- django/core/serializers/json.py | 2 +- django/core/serializers/python.py | 14 ++-- django/core/serializers/pyyaml.py | 2 +- django/core/serializers/xml_serializer.py | 18 ++--- django/core/signing.py | 12 ++-- django/core/urlresolvers.py | 12 ++-- django/core/validators.py | 6 +- django/db/backends/__init__.py | 10 +-- django/db/backends/oracle/base.py | 18 ++--- django/db/models/base.py | 10 +-- django/db/models/fields/__init__.py | 14 ++-- django/db/models/fields/files.py | 4 +- django/db/models/fields/related.py | 6 +- django/db/models/options.py | 6 +- django/db/models/related.py | 6 +- django/db/models/sql/query.py | 4 +- django/db/models/sql/subqueries.py | 6 +- django/forms/fields.py | 20 +++--- django/forms/forms.py | 20 +++--- django/forms/models.py | 10 +-- django/forms/util.py | 12 ++-- django/forms/widgets.py | 42 ++++++------ django/http/__init__.py | 24 +++---- django/http/multipartparser.py | 10 +-- django/template/base.py | 12 ++-- django/template/debug.py | 4 +- django/template/defaultfilters.py | 18 ++--- django/template/defaulttags.py | 6 +- django/templatetags/l10n.py | 6 +- django/test/client.py | 10 +-- django/test/html.py | 4 +- django/test/testcases.py | 8 +-- django/utils/_os.py | 6 +- django/utils/cache.py | 4 +- django/utils/crypto.py | 8 +-- django/utils/dateformat.py | 6 +- django/utils/encoding.py | 66 ++++++++++++------- django/utils/feedgenerator.py | 12 ++-- django/utils/html.py | 22 +++---- django/utils/http.py | 10 +-- django/utils/text.py | 24 +++---- django/utils/translation/__init__.py | 4 +- django/utils/translation/trans_null.py | 6 +- django/utils/tzinfo.py | 6 +- django/views/debug.py | 8 +-- django/views/generic/dates.py | 6 +- django/views/i18n.py | 6 +- docs/howto/custom-model-fields.txt | 2 +- docs/ref/databases.txt | 2 +- docs/ref/models/instances.txt | 4 +- docs/ref/settings.txt | 2 +- docs/ref/unicode.txt | 24 +++---- docs/ref/utils.txt | 50 ++++++++++---- docs/releases/1.5.txt | 2 +- docs/topics/cache.txt | 2 +- docs/topics/serialization.txt | 4 +- tests/modeltests/field_subclassing/fields.py | 8 +-- tests/modeltests/field_subclassing/models.py | 4 +- tests/regressiontests/admin_filters/tests.py | 60 ++++++++--------- tests/regressiontests/admin_views/admin.py | 2 +- tests/regressiontests/cache/tests.py | 6 +- tests/regressiontests/forms/tests/extra.py | 14 ++-- tests/regressiontests/signing/tests.py | 4 +- .../staticfiles_tests/tests.py | 6 +- 125 files changed, 629 insertions(+), 583 deletions(-) diff --git a/django/contrib/admin/actions.py b/django/contrib/admin/actions.py index 5b56402428..201101736e 100644 --- a/django/contrib/admin/actions.py +++ b/django/contrib/admin/actions.py @@ -7,7 +7,7 @@ from django.contrib.admin import helpers from django.contrib.admin.util import get_deleted_objects, model_ngettext from django.db import router from django.template.response import TemplateResponse -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.translation import ugettext_lazy, ugettext as _ def delete_selected(modeladmin, request, queryset): @@ -42,7 +42,7 @@ def delete_selected(modeladmin, request, queryset): n = queryset.count() if n: for obj in queryset: - obj_display = force_unicode(obj) + obj_display = force_text(obj) modeladmin.log_deletion(request, obj, obj_display) queryset.delete() modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % { @@ -52,9 +52,9 @@ def delete_selected(modeladmin, request, queryset): return None if len(queryset) == 1: - objects_name = force_unicode(opts.verbose_name) + objects_name = force_text(opts.verbose_name) else: - objects_name = force_unicode(opts.verbose_name_plural) + objects_name = force_text(opts.verbose_name_plural) if perms_needed or protected: title = _("Cannot delete %(name)s") % {"name": objects_name} diff --git a/django/contrib/admin/filters.py b/django/contrib/admin/filters.py index 538bf54df9..cecae216c0 100644 --- a/django/contrib/admin/filters.py +++ b/django/contrib/admin/filters.py @@ -9,7 +9,7 @@ import datetime from django.db import models from django.core.exceptions import ImproperlyConfigured -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ from django.utils import timezone @@ -195,7 +195,7 @@ class RelatedFieldListFilter(FieldListFilter): } for pk_val, val in self.lookup_choices: yield { - 'selected': self.lookup_val == smart_unicode(pk_val), + 'selected': self.lookup_val == smart_text(pk_val), 'query_string': cl.get_query_string({ self.lookup_kwarg: pk_val, }, [self.lookup_kwarg_isnull]), @@ -272,7 +272,7 @@ class ChoicesFieldListFilter(FieldListFilter): } for lookup, title in self.field.flatchoices: yield { - 'selected': smart_unicode(lookup) == self.lookup_val, + 'selected': smart_text(lookup) == self.lookup_val, 'query_string': cl.get_query_string({ self.lookup_kwarg: lookup}), 'display': title, @@ -381,7 +381,7 @@ class AllValuesFieldListFilter(FieldListFilter): if val is None: include_none = True continue - val = smart_unicode(val) + val = smart_text(val) yield { 'selected': self.lookup_val == val, 'query_string': cl.get_query_string({ diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index 4fc78784f4..aeacd234a3 100644 --- a/django/contrib/admin/helpers.py +++ b/django/contrib/admin/helpers.py @@ -9,7 +9,7 @@ from django.core.exceptions import ObjectDoesNotExist from django.db.models.fields.related import ManyToManyRel from django.forms.util import flatatt from django.template.defaultfilters import capfirst -from django.utils.encoding import force_unicode, smart_unicode +from django.utils.encoding import force_text, smart_text from django.utils.html import conditional_escape, format_html from django.utils.safestring import mark_safe from django.utils import six @@ -122,7 +122,7 @@ class AdminField(object): def label_tag(self): classes = [] - contents = conditional_escape(force_unicode(self.field.label)) + contents = conditional_escape(force_text(self.field.label)) if self.is_checkbox: classes.append('vCheckboxLabel') else: @@ -166,7 +166,7 @@ class AdminReadonlyField(object): label = self.field['label'] return format_html('{1}:', flatatt(attrs), - capfirst(force_unicode(label))) + capfirst(force_text(label))) def contents(self): from django.contrib.admin.templatetags.admin_list import _boolean_icon @@ -182,7 +182,7 @@ class AdminReadonlyField(object): if boolean: result_repr = _boolean_icon(value) else: - result_repr = smart_unicode(value) + result_repr = smart_text(value) if getattr(attr, "allow_tags", False): result_repr = mark_safe(result_repr) else: diff --git a/django/contrib/admin/models.py b/django/contrib/admin/models.py index 58bbbabfdf..e31c6d84ed 100644 --- a/django/contrib/admin/models.py +++ b/django/contrib/admin/models.py @@ -5,7 +5,7 @@ from django.contrib.contenttypes.models import ContentType from django.contrib.auth.models import User from django.contrib.admin.util import quote from django.utils.translation import ugettext_lazy as _ -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text ADDITION = 1 CHANGE = 2 @@ -13,7 +13,7 @@ DELETION = 3 class LogEntryManager(models.Manager): def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''): - e = self.model(None, None, user_id, content_type_id, smart_unicode(object_id), object_repr[:200], action_flag, change_message) + e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message) e.save() class LogEntry(models.Model): @@ -34,7 +34,7 @@ class LogEntry(models.Model): ordering = ('-action_time',) def __repr__(self): - return smart_unicode(self.action_time) + return smart_text(self.action_time) def __unicode__(self): if self.action_flag == ADDITION: diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index ea28125a5f..7708050e6f 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -28,7 +28,7 @@ from django.utils import six from django.utils.text import capfirst, get_text_list from django.utils.translation import ugettext as _ from django.utils.translation import ungettext -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text HORIZONTAL, VERTICAL = 1, 2 # returns the ', flatatt(self.attrs), format_html_join('\n', '
  • {0}
  • ', - ((force_unicode(w),) for w in self))) + ((force_text(w),) for w in self))) class AdminRadioSelect(forms.RadioSelect): renderer = AdminRadioFieldRenderer @@ -197,7 +197,7 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget): # The related object is registered with the same AdminSite attrs['class'] = 'vManyToManyRawIdAdminField' if value: - value = ','.join([force_unicode(v) for v in value]) + value = ','.join([force_text(v) for v in value]) else: value = '' return super(ManyToManyRawIdWidget, self).render(name, value, attrs) @@ -221,7 +221,7 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget): if len(initial) != len(data): return True for pk1, pk2 in zip(initial, data): - if force_unicode(pk1) != force_unicode(pk2): + if force_text(pk1) != force_text(pk2): return True return False diff --git a/django/contrib/admindocs/utils.py b/django/contrib/admindocs/utils.py index 4bf1250ac6..0e10eb4fa3 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_str +from django.utils.encoding import smart_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_str("<%s>" % thing_being_parsed) + thing_being_parsed = smart_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 96ec40ba60..c676cf84db 100644 --- a/django/contrib/auth/hashers.py +++ b/django/contrib/auth/hashers.py @@ -7,7 +7,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_str +from django.utils.encoding import smart_bytes from django.core.exceptions import ImproperlyConfigured from django.utils.crypto import ( pbkdf2, constant_time_compare, get_random_string) @@ -298,7 +298,7 @@ class SHA1PasswordHasher(BasePasswordHasher): def encode(self, password, salt): assert password assert salt and '$' not in salt - hash = hashlib.sha1(smart_str(salt + password)).hexdigest() + hash = hashlib.sha1(smart_bytes(salt + password)).hexdigest() return "%s$%s$%s" % (self.algorithm, salt, hash) def verify(self, password, encoded): @@ -326,7 +326,7 @@ class MD5PasswordHasher(BasePasswordHasher): def encode(self, password, salt): assert password assert salt and '$' not in salt - hash = hashlib.md5(smart_str(salt + password)).hexdigest() + hash = hashlib.md5(smart_bytes(salt + password)).hexdigest() return "%s$%s$%s" % (self.algorithm, salt, hash) def verify(self, password, encoded): @@ -360,7 +360,7 @@ class UnsaltedMD5PasswordHasher(BasePasswordHasher): return '' def encode(self, password, salt): - return hashlib.md5(smart_str(password)).hexdigest() + return hashlib.md5(smart_bytes(password)).hexdigest() def verify(self, password, encoded): encoded_2 = self.encode(password, '') diff --git a/django/contrib/auth/tests/forms.py b/django/contrib/auth/tests/forms.py index 2bfe35ac88..f917ea2601 100644 --- a/django/contrib/auth/tests/forms.py +++ b/django/contrib/auth/tests/forms.py @@ -8,7 +8,7 @@ from django.core import mail from django.forms.fields import Field, EmailField from django.test import TestCase from django.test.utils import override_settings -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils import six from django.utils import translation from django.utils.translation import ugettext as _ @@ -28,7 +28,7 @@ class UserCreationFormTest(TestCase): form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form["username"].errors, - [force_unicode(form.error_messages['duplicate_username'])]) + [force_text(form.error_messages['duplicate_username'])]) def test_invalid_data(self): data = { @@ -39,7 +39,7 @@ class UserCreationFormTest(TestCase): form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form["username"].errors, - [force_unicode(form.fields['username'].error_messages['invalid'])]) + [force_text(form.fields['username'].error_messages['invalid'])]) def test_password_verification(self): # The verification password is incorrect. @@ -51,13 +51,13 @@ class UserCreationFormTest(TestCase): form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form["password2"].errors, - [force_unicode(form.error_messages['password_mismatch'])]) + [force_text(form.error_messages['password_mismatch'])]) def test_both_passwords(self): # One (or both) passwords weren't given data = {'username': 'jsmith'} form = UserCreationForm(data) - required_error = [force_unicode(Field.default_error_messages['required'])] + required_error = [force_text(Field.default_error_messages['required'])] self.assertFalse(form.is_valid()) self.assertEqual(form['password1'].errors, required_error) self.assertEqual(form['password2'].errors, required_error) @@ -96,7 +96,7 @@ class AuthenticationFormTest(TestCase): form = AuthenticationForm(None, data) self.assertFalse(form.is_valid()) self.assertEqual(form.non_field_errors(), - [force_unicode(form.error_messages['invalid_login'])]) + [force_text(form.error_messages['invalid_login'])]) def test_inactive_user(self): # The user is inactive. @@ -107,7 +107,7 @@ class AuthenticationFormTest(TestCase): form = AuthenticationForm(None, data) self.assertFalse(form.is_valid()) self.assertEqual(form.non_field_errors(), - [force_unicode(form.error_messages['inactive'])]) + [force_text(form.error_messages['inactive'])]) def test_inactive_user_i18n(self): with self.settings(USE_I18N=True): @@ -120,7 +120,7 @@ class AuthenticationFormTest(TestCase): form = AuthenticationForm(None, data) self.assertFalse(form.is_valid()) self.assertEqual(form.non_field_errors(), - [force_unicode(form.error_messages['inactive'])]) + [force_text(form.error_messages['inactive'])]) def test_success(self): # The success case @@ -148,7 +148,7 @@ class SetPasswordFormTest(TestCase): form = SetPasswordForm(user, data) self.assertFalse(form.is_valid()) self.assertEqual(form["new_password2"].errors, - [force_unicode(form.error_messages['password_mismatch'])]) + [force_text(form.error_messages['password_mismatch'])]) def test_success(self): user = User.objects.get(username='testclient') @@ -175,7 +175,7 @@ class PasswordChangeFormTest(TestCase): form = PasswordChangeForm(user, data) self.assertFalse(form.is_valid()) self.assertEqual(form["old_password"].errors, - [force_unicode(form.error_messages['password_incorrect'])]) + [force_text(form.error_messages['password_incorrect'])]) def test_password_verification(self): # The two new passwords do not match. @@ -188,7 +188,7 @@ class PasswordChangeFormTest(TestCase): form = PasswordChangeForm(user, data) self.assertFalse(form.is_valid()) self.assertEqual(form["new_password2"].errors, - [force_unicode(form.error_messages['password_mismatch'])]) + [force_text(form.error_messages['password_mismatch'])]) def test_success(self): # The success case. @@ -219,7 +219,7 @@ class UserChangeFormTest(TestCase): form = UserChangeForm(data, instance=user) self.assertFalse(form.is_valid()) self.assertEqual(form['username'].errors, - [force_unicode(form.fields['username'].error_messages['invalid'])]) + [force_text(form.fields['username'].error_messages['invalid'])]) def test_bug_14242(self): # A regression test, introduce by adding an optimization for the @@ -274,7 +274,7 @@ class PasswordResetFormTest(TestCase): form = PasswordResetForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form['email'].errors, - [force_unicode(EmailField.default_error_messages['invalid'])]) + [force_text(EmailField.default_error_messages['invalid'])]) def test_nonexistant_email(self): # Test nonexistant email address @@ -282,7 +282,7 @@ class PasswordResetFormTest(TestCase): form = PasswordResetForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form.errors, - {'email': [force_unicode(form.error_messages['unknown'])]}) + {'email': [force_text(form.error_messages['unknown'])]}) def test_cleaned_data(self): # Regression test diff --git a/django/contrib/auth/tests/views.py b/django/contrib/auth/tests/views.py index e76e7dd10f..3c847f456a 100644 --- a/django/contrib/auth/tests/views.py +++ b/django/contrib/auth/tests/views.py @@ -7,7 +7,7 @@ from django.contrib.auth.models import User from django.core import mail from django.core.urlresolvers import reverse, NoReverseMatch from django.http import QueryDict -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.html import escape from django.utils.http import urlquote from django.test import TestCase @@ -46,7 +46,7 @@ class AuthViewsTestCase(TestCase): self.assertTrue(SESSION_KEY in self.client.session) def assertContainsEscaped(self, response, text, **kwargs): - return self.assertContains(response, escape(force_unicode(text)), **kwargs) + return self.assertContains(response, escape(force_text(text)), **kwargs) class AuthViewNamedURLTests(AuthViewsTestCase): diff --git a/django/contrib/comments/forms.py b/django/contrib/comments/forms.py index 830e24bca9..bd254d2733 100644 --- a/django/contrib/comments/forms.py +++ b/django/contrib/comments/forms.py @@ -5,7 +5,7 @@ from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.contrib.comments.models import Comment from django.utils.crypto import salted_hmac, constant_time_compare -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.text import get_text_list from django.utils import timezone from django.utils.translation import ungettext, ugettext, ugettext_lazy as _ @@ -133,7 +133,7 @@ class CommentDetailsForm(CommentSecurityForm): """ return dict( content_type = ContentType.objects.get_for_model(self.target_object), - object_pk = force_unicode(self.target_object._get_pk_val()), + object_pk = force_text(self.target_object._get_pk_val()), user_name = self.cleaned_data["name"], user_email = self.cleaned_data["email"], user_url = self.cleaned_data["url"], diff --git a/django/contrib/comments/managers.py b/django/contrib/comments/managers.py index 499feee6c3..bc0fc5f332 100644 --- a/django/contrib/comments/managers.py +++ b/django/contrib/comments/managers.py @@ -1,6 +1,6 @@ from django.db import models from django.contrib.contenttypes.models import ContentType -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text class CommentManager(models.Manager): @@ -18,5 +18,5 @@ class CommentManager(models.Manager): ct = ContentType.objects.get_for_model(model) qs = self.get_query_set().filter(content_type=ct) if isinstance(model, models.Model): - qs = qs.filter(object_pk=force_unicode(model._get_pk_val())) + qs = qs.filter(object_pk=force_text(model._get_pk_val())) return qs diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py index ce1825e529..4d4eb2322f 100644 --- a/django/contrib/comments/templatetags/comments.py +++ b/django/contrib/comments/templatetags/comments.py @@ -3,7 +3,7 @@ from django.template.loader import render_to_string from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.contrib import comments -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text register = template.Library() @@ -75,7 +75,7 @@ class BaseCommentNode(template.Node): qs = self.comment_model.objects.filter( content_type = ctype, - object_pk = smart_unicode(object_pk), + object_pk = smart_text(object_pk), site__pk = settings.SITE_ID, ) diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py index d5062cabf3..29e93eefe7 100644 --- a/django/contrib/contenttypes/generic.py +++ b/django/contrib/contenttypes/generic.py @@ -17,7 +17,7 @@ from django.forms import ModelForm from django.forms.models import BaseModelFormSet, modelformset_factory, save_instance from django.contrib.admin.options import InlineModelAdmin, flatten_fieldsets from django.contrib.contenttypes.models import ContentType -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text class GenericForeignKey(object): """ @@ -169,7 +169,7 @@ class GenericRelation(RelatedField, Field): def value_to_string(self, obj): qs = getattr(obj, self.name).all() - return smart_unicode([instance._get_pk_val() for instance in qs]) + return smart_text([instance._get_pk_val() for instance in qs]) def m2m_db_table(self): return self.rel.to._meta.db_table diff --git a/django/contrib/contenttypes/management.py b/django/contrib/contenttypes/management.py index 1b1c9c8562..11ca7e4763 100644 --- a/django/contrib/contenttypes/management.py +++ b/django/contrib/contenttypes/management.py @@ -1,6 +1,6 @@ from django.contrib.contenttypes.models import ContentType from django.db.models import get_apps, get_models, signals -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils import six def update_contenttypes(app, created_models, verbosity=2, **kwargs): @@ -31,7 +31,7 @@ def update_contenttypes(app, created_models, verbosity=2, **kwargs): cts = ContentType.objects.bulk_create([ ContentType( - name=smart_unicode(model._meta.verbose_name_raw), + name=smart_text(model._meta.verbose_name_raw), app_label=app_label, model=model_name, ) diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 867351f6c8..e6d547a491 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -1,6 +1,6 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ -from django.utils.encoding import smart_unicode, force_unicode +from django.utils.encoding import smart_text, force_text class ContentTypeManager(models.Manager): @@ -37,13 +37,13 @@ class ContentTypeManager(models.Manager): try: ct = self._get_from_cache(opts) except KeyError: - # Load or create the ContentType entry. The smart_unicode() is + # Load or create the ContentType entry. The smart_text() is # needed around opts.verbose_name_raw because name_raw might be a # django.utils.functional.__proxy__ object. ct, created = self.get_or_create( app_label = opts.app_label, model = opts.object_name.lower(), - defaults = {'name': smart_unicode(opts.verbose_name_raw)}, + defaults = {'name': smart_text(opts.verbose_name_raw)}, ) self._add_to_cache(self.db, ct) @@ -86,7 +86,7 @@ class ContentTypeManager(models.Manager): ct = self.create( app_label=opts.app_label, model=opts.object_name.lower(), - name=smart_unicode(opts.verbose_name_raw), + name=smart_text(opts.verbose_name_raw), ) self._add_to_cache(self.db, ct) results[ct.model_class()] = ct @@ -147,7 +147,7 @@ class ContentType(models.Model): if not model or self.name != model._meta.verbose_name_raw: return self.name else: - return force_unicode(model._meta.verbose_name) + return force_text(model._meta.verbose_name) def model_class(self): "Returns the Python model class for this type of content." diff --git a/django/contrib/databrowse/datastructures.py b/django/contrib/databrowse/datastructures.py index 95d347cac0..810e039894 100644 --- a/django/contrib/databrowse/datastructures.py +++ b/django/contrib/databrowse/datastructures.py @@ -7,7 +7,7 @@ from __future__ import unicode_literals from django.db import models from django.utils import formats from django.utils.text import capfirst -from django.utils.encoding import smart_unicode, smart_str, iri_to_uri +from django.utils.encoding import smart_text, smart_bytes, iri_to_uri from django.db.models.query import QuerySet EMPTY_VALUE = '(None)' @@ -22,7 +22,7 @@ class EasyModel(object): self.verbose_name_plural = model._meta.verbose_name_plural def __repr__(self): - return '' % smart_str(self.model._meta.object_name) + return '' % smart_bytes(self.model._meta.object_name) def model_databrowse(self): "Returns the ModelDatabrowse class for this model." @@ -61,7 +61,7 @@ class EasyField(object): self.model, self.field = easy_model, field def __repr__(self): - return smart_str('' % (self.model.model._meta.object_name, self.field.name)) + return smart_bytes('' % (self.model.model._meta.object_name, self.field.name)) def choices(self): for value, label in self.field.choices: @@ -79,7 +79,7 @@ class EasyChoice(object): self.value, self.label = value, label def __repr__(self): - return smart_str('' % (self.model.model._meta.object_name, self.field.name)) + return smart_bytes('' % (self.model.model._meta.object_name, self.field.name)) def url(self): return '%s%s/%s/%s/%s/' % (self.model.site.root_url, self.model.model._meta.app_label, self.model.model._meta.module_name, self.field.field.name, iri_to_uri(self.value)) @@ -89,10 +89,10 @@ class EasyInstance(object): self.model, self.instance = easy_model, instance def __repr__(self): - return smart_str('' % (self.model.model._meta.object_name, self.instance._get_pk_val())) + return smart_bytes('' % (self.model.model._meta.object_name, self.instance._get_pk_val())) def __unicode__(self): - val = smart_unicode(self.instance) + val = smart_text(self.instance) if len(val) > DISPLAY_SIZE: return val[:DISPLAY_SIZE] + '...' return val @@ -136,7 +136,7 @@ class EasyInstanceField(object): self.raw_value = getattr(instance.instance, field.name) def __repr__(self): - return smart_str('' % (self.model.model._meta.object_name, self.field.name)) + return smart_bytes('' % (self.model.model._meta.object_name, self.field.name)) def values(self): """ @@ -185,7 +185,7 @@ class EasyInstanceField(object): if value is None: continue url = '%s%s/%s/objects/%s/' % (self.model.site.root_url, m.model._meta.app_label, m.model._meta.module_name, iri_to_uri(value._get_pk_val())) - lst.append((smart_unicode(value), url)) + lst.append((smart_text(value), url)) else: lst = [(value, None) for value in self.values()] elif self.field.choices: diff --git a/django/contrib/databrowse/plugins/calendars.py b/django/contrib/databrowse/plugins/calendars.py index 923adb90f6..a548c33c8f 100644 --- a/django/contrib/databrowse/plugins/calendars.py +++ b/django/contrib/databrowse/plugins/calendars.py @@ -7,7 +7,7 @@ from django.contrib.databrowse.sites import DatabrowsePlugin from django.shortcuts import render_to_response from django.utils.html import format_html, format_html_join from django.utils.text import capfirst -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.views.generic import dates from django.utils import datetime_safe @@ -66,7 +66,7 @@ class CalendarPlugin(DatabrowsePlugin): return '' return format_html('

    View calendar by: {0}

    ', format_html_join(', ', '{1}', - ((f.name, force_unicode(capfirst(f.verbose_name))) for f in fields.values()))) + ((f.name, force_text(capfirst(f.verbose_name))) for f in fields.values()))) def urls(self, plugin_name, easy_instance_field): if isinstance(easy_instance_field.field, models.DateField): diff --git a/django/contrib/databrowse/plugins/fieldchoices.py b/django/contrib/databrowse/plugins/fieldchoices.py index 73298b8d56..dc5e9aef14 100644 --- a/django/contrib/databrowse/plugins/fieldchoices.py +++ b/django/contrib/databrowse/plugins/fieldchoices.py @@ -8,7 +8,7 @@ from django.shortcuts import render_to_response from django.utils.html import format_html, format_html_join from django.utils.http import urlquote from django.utils.text import capfirst -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text class FieldChoicePlugin(DatabrowsePlugin): @@ -35,7 +35,7 @@ class FieldChoicePlugin(DatabrowsePlugin): return '' return format_html('

    View by: {0}

    ', format_html_join(', ', '{1}', - ((f.name, force_unicode(capfirst(f.verbose_name))) for f in fields.values()))) + ((f.name, force_text(capfirst(f.verbose_name))) for f in fields.values()))) def urls(self, plugin_name, easy_instance_field): if easy_instance_field.field in self.field_dict(easy_instance_field.model.model).values(): diff --git a/django/contrib/formtools/wizard/storage/base.py b/django/contrib/formtools/wizard/storage/base.py index 7c802712c1..05c9f6f121 100644 --- a/django/contrib/formtools/wizard/storage/base.py +++ b/django/contrib/formtools/wizard/storage/base.py @@ -1,6 +1,6 @@ from django.core.files.uploadedfile import UploadedFile from django.utils.datastructures import MultiValueDict -from django.utils.encoding import smart_str +from django.utils.encoding import smart_bytes from django.utils.functional import lazy_property from django.utils import six @@ -74,7 +74,7 @@ class BaseStorage(object): files = {} for field, field_dict in six.iteritems(wizard_files): - field_dict = dict((smart_str(k), v) + field_dict = dict((smart_bytes(k), v) for k, v in six.iteritems(field_dict)) tmp_name = field_dict.pop('tmp_name') files[field] = UploadedFile( diff --git a/django/contrib/gis/sitemaps/views.py b/django/contrib/gis/sitemaps/views.py index 6753b9e34a..8bcdba1b44 100644 --- a/django/contrib/gis/sitemaps/views.py +++ b/django/contrib/gis/sitemaps/views.py @@ -8,7 +8,7 @@ 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_str +from django.utils.encoding import smart_bytes from django.utils import six from django.utils.translation import ugettext as _ @@ -61,7 +61,7 @@ def sitemap(request, sitemaps, section=None): raise Http404(_("Page %s empty") % page) except PageNotAnInteger: raise Http404(_("No page '%s'") % page) - xml = smart_str(loader.render_to_string('gis/sitemaps/geo_sitemap.xml', {'urlset': urls})) + xml = smart_bytes(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/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py index 8f6c2602c9..7e8f163174 100644 --- a/django/contrib/humanize/templatetags/humanize.py +++ b/django/contrib/humanize/templatetags/humanize.py @@ -5,7 +5,7 @@ from datetime import date, datetime from django import template from django.conf import settings from django.template import defaultfilters -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.formats import number_format from django.utils.translation import pgettext, ungettext, ugettext as _ from django.utils.timezone import is_aware, utc @@ -41,7 +41,7 @@ def intcomma(value, use_l10n=True): return intcomma(value, False) else: return number_format(value, force_grouping=True) - orig = force_unicode(value) + orig = force_text(value) new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig) if orig == new: return new diff --git a/django/contrib/localflavor/au/forms.py b/django/contrib/localflavor/au/forms.py index 34170fabc8..d3a00e200c 100644 --- a/django/contrib/localflavor/au/forms.py +++ b/django/contrib/localflavor/au/forms.py @@ -10,7 +10,7 @@ from django.contrib.localflavor.au.au_states import STATE_CHOICES from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, RegexField, Select -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -44,7 +44,7 @@ class AUPhoneNumberField(Field): super(AUPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: return '' - value = re.sub('(\(|\)|\s+|-)', '', smart_unicode(value)) + value = re.sub('(\(|\)|\s+|-)', '', smart_text(value)) phone_match = PHONE_DIGITS_RE.search(value) if phone_match: return '%s' % phone_match.group(1) diff --git a/django/contrib/localflavor/br/forms.py b/django/contrib/localflavor/br/forms.py index f287d46a9a..4d0d6815ba 100644 --- a/django/contrib/localflavor/br/forms.py +++ b/django/contrib/localflavor/br/forms.py @@ -11,7 +11,7 @@ from django.contrib.localflavor.br.br_states import STATE_CHOICES from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, RegexField, CharField, Select -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -35,7 +35,7 @@ class BRPhoneNumberField(Field): super(BRPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: return '' - value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) + value = re.sub('(\(|\)|\s+)', '', smart_text(value)) m = phone_digits_re.search(value) if m: return '%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) @@ -68,10 +68,10 @@ class BRStateChoiceField(Field): value = super(BRStateChoiceField, self).clean(value) if value in EMPTY_VALUES: value = '' - value = smart_unicode(value) + value = smart_text(value) if value == '': return value - valid_values = set([smart_unicode(k) for k, v in self.widget.choices]) + valid_values = set([smart_text(k) for k, v in self.widget.choices]) if value not in valid_values: raise ValidationError(self.error_messages['invalid']) return value diff --git a/django/contrib/localflavor/ca/forms.py b/django/contrib/localflavor/ca/forms.py index daa40044f9..4ebfb06c2b 100644 --- a/django/contrib/localflavor/ca/forms.py +++ b/django/contrib/localflavor/ca/forms.py @@ -9,7 +9,7 @@ import re from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, CharField, Select -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -53,7 +53,7 @@ class CAPhoneNumberField(Field): super(CAPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: return '' - value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) + value = re.sub('(\(|\)|\s+)', '', smart_text(value)) m = phone_digits_re.search(value) if m: return '%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) diff --git a/django/contrib/localflavor/ch/forms.py b/django/contrib/localflavor/ch/forms.py index e844a3c57c..bf71eeea32 100644 --- a/django/contrib/localflavor/ch/forms.py +++ b/django/contrib/localflavor/ch/forms.py @@ -10,7 +10,7 @@ from django.contrib.localflavor.ch.ch_states import STATE_CHOICES from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, RegexField, Select -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -41,7 +41,7 @@ class CHPhoneNumberField(Field): super(CHPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: return '' - value = re.sub('(\.|\s|/|-)', '', smart_unicode(value)) + value = re.sub('(\.|\s|/|-)', '', smart_text(value)) m = phone_digits_re.search(value) if m: return '%s %s %s %s' % (value[0:3], value[3:6], value[6:8], value[8:10]) diff --git a/django/contrib/localflavor/cl/forms.py b/django/contrib/localflavor/cl/forms.py index 5991176382..a5340141ce 100644 --- a/django/contrib/localflavor/cl/forms.py +++ b/django/contrib/localflavor/cl/forms.py @@ -8,7 +8,7 @@ from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import RegexField, Select from django.utils.translation import ugettext_lazy as _ -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from .cl_regions import REGION_CHOICES @@ -75,7 +75,7 @@ class CLRutField(RegexField): Turns the RUT into one normalized format. Returns a (rut, verifier) tuple. """ - rut = smart_unicode(rut).replace(' ', '').replace('.', '').replace('-', '') + rut = smart_text(rut).replace(' ', '').replace('.', '').replace('-', '') return rut[:-1], rut[-1].upper() def _format(self, code, verifier=None): diff --git a/django/contrib/localflavor/fr/forms.py b/django/contrib/localflavor/fr/forms.py index d836dd6397..8b841fff5f 100644 --- a/django/contrib/localflavor/fr/forms.py +++ b/django/contrib/localflavor/fr/forms.py @@ -9,7 +9,7 @@ from django.contrib.localflavor.fr.fr_department import DEPARTMENT_CHOICES from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import CharField, RegexField, Select -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -43,7 +43,7 @@ class FRPhoneNumberField(CharField): super(FRPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: return '' - value = re.sub('(\.|\s)', '', smart_unicode(value)) + value = re.sub('(\.|\s)', '', smart_text(value)) m = phone_digits_re.search(value) if m: return '%s %s %s %s %s' % (value[0:2], value[2:4], value[4:6], value[6:8], value[8:10]) diff --git a/django/contrib/localflavor/hk/forms.py b/django/contrib/localflavor/hk/forms.py index 8cf9360e19..ab4f70f193 100644 --- a/django/contrib/localflavor/hk/forms.py +++ b/django/contrib/localflavor/hk/forms.py @@ -8,7 +8,7 @@ import re from django.core.validators import EMPTY_VALUES from django.forms import CharField from django.forms import ValidationError -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -53,7 +53,7 @@ class HKPhoneNumberField(CharField): if value in EMPTY_VALUES: return '' - value = re.sub('(\(|\)|\s+|\+)', '', smart_unicode(value)) + value = re.sub('(\(|\)|\s+|\+)', '', smart_text(value)) m = hk_phone_digits_re.search(value) if not m: raise ValidationError(self.error_messages['invalid']) diff --git a/django/contrib/localflavor/hr/forms.py b/django/contrib/localflavor/hr/forms.py index eb4436a78c..b935fd8a3a 100644 --- a/django/contrib/localflavor/hr/forms.py +++ b/django/contrib/localflavor/hr/forms.py @@ -12,7 +12,7 @@ from django.contrib.localflavor.hr.hr_choices import ( from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, Select, RegexField -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -159,7 +159,7 @@ class HRLicensePlateField(Field): if value in EMPTY_VALUES: return '' - value = re.sub(r'[\s\-]+', '', smart_unicode(value.strip())).upper() + value = re.sub(r'[\s\-]+', '', smart_text(value.strip())).upper() matches = plate_re.search(value) if matches is None: @@ -225,7 +225,7 @@ class HRPhoneNumberField(Field): if value in EMPTY_VALUES: return '' - value = re.sub(r'[\-\s\(\)]', '', smart_unicode(value)) + value = re.sub(r'[\-\s\(\)]', '', smart_text(value)) matches = phone_re.search(value) if matches is None: diff --git a/django/contrib/localflavor/id/forms.py b/django/contrib/localflavor/id/forms.py index f22b06134e..2005dbc75c 100644 --- a/django/contrib/localflavor/id/forms.py +++ b/django/contrib/localflavor/id/forms.py @@ -11,7 +11,7 @@ from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, Select from django.utils.translation import ugettext_lazy as _ -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text postcode_re = re.compile(r'^[1-9]\d{4}$') @@ -77,10 +77,10 @@ class IDPhoneNumberField(Field): if value in EMPTY_VALUES: return '' - phone_number = re.sub(r'[\-\s\(\)]', '', smart_unicode(value)) + phone_number = re.sub(r'[\-\s\(\)]', '', smart_text(value)) if phone_re.search(phone_number): - return smart_unicode(value) + return smart_text(value) raise ValidationError(self.error_messages['invalid']) @@ -120,7 +120,7 @@ class IDLicensePlateField(Field): return '' plate_number = re.sub(r'\s+', ' ', - smart_unicode(value.strip())).upper() + smart_text(value.strip())).upper() matches = plate_re.search(plate_number) if matches is None: @@ -181,7 +181,7 @@ class IDNationalIdentityNumberField(Field): if value in EMPTY_VALUES: return '' - value = re.sub(r'[\s.]', '', smart_unicode(value)) + value = re.sub(r'[\s.]', '', smart_text(value)) if not nik_re.search(value): raise ValidationError(self.error_messages['invalid']) diff --git a/django/contrib/localflavor/in_/forms.py b/django/contrib/localflavor/in_/forms.py index b62ec7bdb2..5c1d009ef4 100644 --- a/django/contrib/localflavor/in_/forms.py +++ b/django/contrib/localflavor/in_/forms.py @@ -10,7 +10,7 @@ from django.contrib.localflavor.in_.in_states import STATES_NORMALIZED, STATE_CH from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, RegexField, CharField, Select -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -74,7 +74,7 @@ class INStateField(Field): pass else: try: - return smart_unicode(STATES_NORMALIZED[value.strip().lower()]) + return smart_text(STATES_NORMALIZED[value.strip().lower()]) except KeyError: pass raise ValidationError(self.error_messages['invalid']) @@ -107,7 +107,7 @@ class INPhoneNumberField(CharField): super(INPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: return '' - value = smart_unicode(value) + value = smart_text(value) m = phone_digits_re.match(value) if m: return '%s' % (value) diff --git a/django/contrib/localflavor/is_/forms.py b/django/contrib/localflavor/is_/forms.py index 7af9f51cfb..1ae3e012a1 100644 --- a/django/contrib/localflavor/is_/forms.py +++ b/django/contrib/localflavor/is_/forms.py @@ -9,7 +9,7 @@ from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import RegexField from django.forms.widgets import Select -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -58,7 +58,7 @@ class ISIdNumberField(RegexField): Takes in the value in canonical form and returns it in the common display format. """ - return smart_unicode(value[:6]+'-'+value[6:]) + return smart_text(value[:6]+'-'+value[6:]) class ISPhoneNumberField(RegexField): """ diff --git a/django/contrib/localflavor/it/forms.py b/django/contrib/localflavor/it/forms.py index 60b1eff951..916ce9bb3d 100644 --- a/django/contrib/localflavor/it/forms.py +++ b/django/contrib/localflavor/it/forms.py @@ -13,7 +13,7 @@ from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, RegexField, Select from django.utils.translation import ugettext_lazy as _ -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text class ITZipCodeField(RegexField): @@ -85,4 +85,4 @@ class ITVatNumberField(Field): check_digit = vat_number_check_digit(vat_number[0:10]) if not vat_number[10] == check_digit: raise ValidationError(self.error_messages['invalid']) - return smart_unicode(vat_number) + return smart_text(vat_number) diff --git a/django/contrib/localflavor/it/util.py b/django/contrib/localflavor/it/util.py index ec1b7e3f83..e1aa9c0419 100644 --- a/django/contrib/localflavor/it/util.py +++ b/django/contrib/localflavor/it/util.py @@ -1,4 +1,4 @@ -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text def ssn_check_digit(value): "Calculate Italian social security number check digit." @@ -34,11 +34,11 @@ def ssn_check_digit(value): def vat_number_check_digit(vat_number): "Calculate Italian VAT number check digit." - normalized_vat_number = smart_unicode(vat_number).zfill(10) + normalized_vat_number = smart_text(vat_number).zfill(10) total = 0 for i in range(0, 10, 2): total += int(normalized_vat_number[i]) for i in range(1, 11, 2): quotient , remainder = divmod(int(normalized_vat_number[i]) * 2, 10) total += quotient + remainder - return smart_unicode((10 - total % 10) % 10) + return smart_text((10 - total % 10) % 10) diff --git a/django/contrib/localflavor/nl/forms.py b/django/contrib/localflavor/nl/forms.py index bdd769bd39..a05dd38f7f 100644 --- a/django/contrib/localflavor/nl/forms.py +++ b/django/contrib/localflavor/nl/forms.py @@ -10,7 +10,7 @@ from django.contrib.localflavor.nl.nl_provinces import PROVINCE_CHOICES from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, Select -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -61,7 +61,7 @@ class NLPhoneNumberField(Field): if value in EMPTY_VALUES: return '' - phone_nr = re.sub('[\-\s\(\)]', '', smart_unicode(value)) + phone_nr = re.sub('[\-\s\(\)]', '', smart_text(value)) if len(phone_nr) == 10 and numeric_re.search(phone_nr): return value diff --git a/django/contrib/localflavor/pt/forms.py b/django/contrib/localflavor/pt/forms.py index b27fb577bd..01cdd101b2 100644 --- a/django/contrib/localflavor/pt/forms.py +++ b/django/contrib/localflavor/pt/forms.py @@ -8,7 +8,7 @@ import re from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, RegexField -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ phone_digits_re = re.compile(r'^(\d{9}|(00|\+)\d*)$') @@ -29,7 +29,7 @@ class PTZipCodeField(RegexField): return '%s-%s' % (cleaned[:4],cleaned[4:]) else: return cleaned - + class PTPhoneNumberField(Field): """ Validate local Portuguese phone number (including international ones) @@ -43,7 +43,7 @@ class PTPhoneNumberField(Field): super(PTPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: return '' - value = re.sub('(\.|\s)', '', smart_unicode(value)) + value = re.sub('(\.|\s)', '', smart_text(value)) m = phone_digits_re.search(value) if m: return '%s' % value diff --git a/django/contrib/localflavor/tr/forms.py b/django/contrib/localflavor/tr/forms.py index 15bc1f99bb..c4f928e670 100644 --- a/django/contrib/localflavor/tr/forms.py +++ b/django/contrib/localflavor/tr/forms.py @@ -10,7 +10,7 @@ from django.contrib.localflavor.tr.tr_provinces import PROVINCE_CHOICES from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, RegexField, Select, CharField -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -46,7 +46,7 @@ class TRPhoneNumberField(CharField): super(TRPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: return '' - value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) + value = re.sub('(\(|\)|\s+)', '', smart_text(value)) m = phone_digits_re.search(value) if m: return '%s%s' % (m.group(2), m.group(4)) diff --git a/django/contrib/localflavor/us/forms.py b/django/contrib/localflavor/us/forms.py index ef565163cd..437bb7c466 100644 --- a/django/contrib/localflavor/us/forms.py +++ b/django/contrib/localflavor/us/forms.py @@ -9,7 +9,7 @@ import re from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, RegexField, Select, CharField -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ @@ -34,7 +34,7 @@ class USPhoneNumberField(CharField): super(USPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: return '' - value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) + value = re.sub('(\(|\)|\s+)', '', smart_text(value)) m = phone_digits_re.search(value) if m: return '%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) diff --git a/django/contrib/markup/templatetags/markup.py b/django/contrib/markup/templatetags/markup.py index 84251cf30a..af9c842f42 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_str, force_unicode +from django.utils.encoding import smart_bytes, force_text from django.utils.safestring import mark_safe register = template.Library() @@ -25,9 +25,9 @@ def textile(value): except ImportError: if settings.DEBUG: raise template.TemplateSyntaxError("Error in 'textile' filter: The Python textile library isn't installed.") - return force_unicode(value) + return force_text(value) else: - return mark_safe(force_unicode(textile.textile(smart_str(value), encoding='utf-8', output='utf-8'))) + return mark_safe(force_text(textile.textile(smart_bytes(value), encoding='utf-8', output='utf-8'))) @register.filter(is_safe=True) def markdown(value, arg=''): @@ -52,23 +52,23 @@ def markdown(value, arg=''): except ImportError: if settings.DEBUG: raise template.TemplateSyntaxError("Error in 'markdown' filter: The Python markdown library isn't installed.") - return force_unicode(value) + return force_text(value) else: markdown_vers = getattr(markdown, "version_info", 0) if markdown_vers < (2, 1): if settings.DEBUG: raise template.TemplateSyntaxError( "Error in 'markdown' filter: Django does not support versions of the Python markdown library < 2.1.") - return force_unicode(value) + return force_text(value) else: extensions = [e for e in arg.split(",") if e] if extensions and extensions[0] == "safe": extensions = extensions[1:] return mark_safe(markdown.markdown( - force_unicode(value), extensions, safe_mode=True, enable_attributes=False)) + force_text(value), extensions, safe_mode=True, enable_attributes=False)) else: return mark_safe(markdown.markdown( - force_unicode(value), extensions, safe_mode=False)) + force_text(value), extensions, safe_mode=False)) @register.filter(is_safe=True) def restructuredtext(value): @@ -77,8 +77,8 @@ def restructuredtext(value): except ImportError: if settings.DEBUG: raise template.TemplateSyntaxError("Error in 'restructuredtext' filter: The Python docutils library isn't installed.") - return force_unicode(value) + return force_text(value) else: docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {}) - parts = publish_parts(source=smart_str(value), writer_name="html4css1", settings_overrides=docutils_settings) - return mark_safe(force_unicode(parts["fragment"])) + parts = publish_parts(source=smart_bytes(value), writer_name="html4css1", settings_overrides=docutils_settings) + return mark_safe(force_text(parts["fragment"])) diff --git a/django/contrib/messages/storage/base.py b/django/contrib/messages/storage/base.py index e80818e84e..5433bbff28 100644 --- a/django/contrib/messages/storage/base.py +++ b/django/contrib/messages/storage/base.py @@ -1,7 +1,7 @@ from __future__ import unicode_literals from django.conf import settings -from django.utils.encoding import force_unicode, StrAndUnicode +from django.utils.encoding import force_text, StrAndUnicode from django.contrib.messages import constants, utils @@ -26,22 +26,22 @@ class Message(StrAndUnicode): and ``extra_tags`` to unicode in case they are lazy translations. Known "safe" types (None, int, etc.) are not converted (see Django's - ``force_unicode`` implementation for details). + ``force_text`` implementation for details). """ - self.message = force_unicode(self.message, strings_only=True) - self.extra_tags = force_unicode(self.extra_tags, strings_only=True) + self.message = force_text(self.message, strings_only=True) + self.extra_tags = force_text(self.extra_tags, strings_only=True) def __eq__(self, other): return isinstance(other, Message) and self.level == other.level and \ self.message == other.message def __unicode__(self): - return force_unicode(self.message) + return force_text(self.message) def _get_tags(self): - label_tag = force_unicode(LEVEL_TAGS.get(self.level, ''), + label_tag = force_text(LEVEL_TAGS.get(self.level, ''), strings_only=True) - extra_tags = force_unicode(self.extra_tags, strings_only=True) + extra_tags = force_text(self.extra_tags, strings_only=True) if extra_tags and label_tag: return ' '.join([extra_tags, label_tag]) elif extra_tags: diff --git a/django/contrib/sessions/backends/db.py b/django/contrib/sessions/backends/db.py index 3dd0d9516c..0cc17b44c3 100644 --- a/django/contrib/sessions/backends/db.py +++ b/django/contrib/sessions/backends/db.py @@ -1,7 +1,7 @@ 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_unicode +from django.utils.encoding import force_text from django.utils import timezone @@ -18,7 +18,7 @@ class SessionStore(SessionBase): session_key = self.session_key, expire_date__gt=timezone.now() ) - return self.decode(force_unicode(s.session_data)) + return self.decode(force_text(s.session_data)) except (Session.DoesNotExist, SuspiciousOperation): self.create() return {} diff --git a/django/contrib/staticfiles/management/commands/collectstatic.py b/django/contrib/staticfiles/management/commands/collectstatic.py index d3977213a9..45c5ecfe1f 100644 --- a/django/contrib/staticfiles/management/commands/collectstatic.py +++ b/django/contrib/staticfiles/management/commands/collectstatic.py @@ -6,7 +6,7 @@ from optparse import make_option from django.core.files.storage import FileSystemStorage from django.core.management.base import CommandError, NoArgsCommand -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.datastructures import SortedDict from django.contrib.staticfiles import finders, storage @@ -198,9 +198,9 @@ Type 'yes' to continue, or 'no' to cancel: """ fpath = os.path.join(path, f) if self.dry_run: self.log("Pretending to delete '%s'" % - smart_unicode(fpath), level=1) + smart_text(fpath), level=1) else: - self.log("Deleting '%s'" % smart_unicode(fpath), level=1) + self.log("Deleting '%s'" % smart_text(fpath), level=1) self.storage.delete(fpath) for d in dirs: self.clear_dir(os.path.join(path, d)) diff --git a/django/contrib/staticfiles/management/commands/findstatic.py b/django/contrib/staticfiles/management/commands/findstatic.py index 772220b342..dc1e88d778 100644 --- a/django/contrib/staticfiles/management/commands/findstatic.py +++ b/django/contrib/staticfiles/management/commands/findstatic.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import os from optparse import make_option from django.core.management.base import LabelCommand -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.contrib.staticfiles import finders @@ -19,12 +19,12 @@ class Command(LabelCommand): def handle_label(self, path, **options): verbosity = int(options.get('verbosity', 1)) result = finders.find(path, all=options['all']) - path = smart_unicode(path) + path = smart_text(path) if result: if not isinstance(result, (list, tuple)): result = [result] output = '\n '.join( - (smart_unicode(os.path.realpath(path)) for path in result)) + (smart_text(os.path.realpath(path)) for path in result)) self.stdout.write("Found '%s' here:\n %s" % (path, output)) else: if verbosity >= 1: diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py index a0133e1c6a..2ca54dde71 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_unicode, smart_str +from django.utils.encoding import force_text, smart_bytes from django.utils.functional import LazyObject from django.utils.importlib import import_module @@ -112,7 +112,7 @@ class CachedFilesMixin(object): return urlunsplit(unparsed_name) def cache_key(self, name): - return 'staticfiles:%s' % hashlib.md5(smart_str(name)).hexdigest() + return 'staticfiles:%s' % hashlib.md5(smart_bytes(name)).hexdigest() def url(self, name, force=False): """ @@ -248,9 +248,9 @@ class CachedFilesMixin(object): if hashed_file_exists: self.delete(hashed_name) # then save the processed result - content_file = ContentFile(smart_str(content)) + content_file = ContentFile(smart_bytes(content)) saved_name = self._save(hashed_name, content_file) - hashed_name = force_unicode(saved_name.replace('\\', '/')) + hashed_name = force_text(saved_name.replace('\\', '/')) processed = True else: # or handle the case in which neither processing nor @@ -258,7 +258,7 @@ class CachedFilesMixin(object): if not hashed_file_exists: processed = True saved_name = self._save(hashed_name, original_file) - hashed_name = force_unicode(saved_name.replace('\\', '/')) + hashed_name = force_text(saved_name.replace('\\', '/')) # and then set the cache accordingly hashed_paths[self.cache_key(name)] = hashed_name diff --git a/django/contrib/syndication/views.py b/django/contrib/syndication/views.py index 3c84f1f60c..bce7ef7cfb 100644 --- a/django/contrib/syndication/views.py +++ b/django/contrib/syndication/views.py @@ -6,7 +6,7 @@ from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist from django.http import HttpResponse, Http404 from django.template import loader, TemplateDoesNotExist, RequestContext from django.utils import feedgenerator, tzinfo -from django.utils.encoding import force_unicode, iri_to_uri, smart_unicode +from django.utils.encoding import force_text, iri_to_uri, smart_text from django.utils.html import escape from django.utils.timezone import is_naive @@ -43,10 +43,10 @@ class Feed(object): def item_title(self, item): # Titles should be double escaped by default (see #6533) - return escape(force_unicode(item)) + return escape(force_text(item)) def item_description(self, item): - return force_unicode(item) + return force_text(item) def item_link(self, item): try: @@ -154,9 +154,9 @@ class Feed(object): enc_url = self.__get_dynamic_attr('item_enclosure_url', item) if enc_url: enc = feedgenerator.Enclosure( - url = smart_unicode(enc_url), - length = smart_unicode(self.__get_dynamic_attr('item_enclosure_length', item)), - mime_type = smart_unicode(self.__get_dynamic_attr('item_enclosure_mime_type', item)) + url = smart_text(enc_url), + length = smart_text(self.__get_dynamic_attr('item_enclosure_length', item)), + mime_type = smart_text(self.__get_dynamic_attr('item_enclosure_mime_type', item)) ) author_name = self.__get_dynamic_attr('item_author_name', item) if author_name is not None: diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py index f7573b2e31..d527e44d8b 100644 --- a/django/core/cache/backends/base.py +++ b/django/core/cache/backends/base.py @@ -3,7 +3,7 @@ import warnings from django.core.exceptions import ImproperlyConfigured, DjangoRuntimeWarning -from django.utils.encoding import smart_str +from django.utils.encoding import smart_bytes from django.utils.importlib import import_module class InvalidCacheBackendError(ImproperlyConfigured): @@ -23,7 +23,7 @@ def default_key_func(key, key_prefix, version): the `key_prefix'. KEY_FUNCTION can be used to specify an alternate function with custom key making behavior. """ - return ':'.join([key_prefix, str(version), smart_str(key)]) + return ':'.join([key_prefix, str(version), smart_bytes(key)]) def get_key_func(key_func): """ @@ -62,7 +62,7 @@ class BaseCache(object): except (ValueError, TypeError): self._cull_frequency = 3 - self.key_prefix = smart_str(params.get('KEY_PREFIX', '')) + self.key_prefix = smart_bytes(params.get('KEY_PREFIX', '')) self.version = params.get('VERSION', 1) self.key_func = get_key_func(params.get('KEY_FUNCTION', None)) diff --git a/django/core/context_processors.py b/django/core/context_processors.py index 325f64d224..a503270cf4 100644 --- a/django/core/context_processors.py +++ b/django/core/context_processors.py @@ -9,7 +9,7 @@ RequestContext. from django.conf import settings from django.middleware.csrf import get_token -from django.utils.encoding import smart_str +from django.utils.encoding import smart_bytes from django.utils.functional import lazy def csrf(request): @@ -25,7 +25,7 @@ def csrf(request): # instead of returning an empty dict. return b'NOTPROVIDED' else: - return smart_str(token) + return smart_bytes(token) _get_val = lazy(_get_val, str) return {'csrf_token': _get_val() } diff --git a/django/core/exceptions.py b/django/core/exceptions.py index e3d1dc9c7e..f0f14cffda 100644 --- a/django/core/exceptions.py +++ b/django/core/exceptions.py @@ -43,7 +43,7 @@ class ValidationError(Exception): """An error while validating data.""" def __init__(self, message, code=None, params=None): import operator - from django.utils.encoding import force_unicode + from django.utils.encoding import force_text """ ValidationError can be passed any object that can be printed (usually a string), a list of objects or a dictionary. @@ -54,11 +54,11 @@ class ValidationError(Exception): message = reduce(operator.add, message.values()) if isinstance(message, list): - self.messages = [force_unicode(msg) for msg in message] + self.messages = [force_text(msg) for msg in message] else: self.code = code self.params = params - message = force_unicode(message) + message = force_text(message) self.messages = [message] def __str__(self): diff --git a/django/core/files/base.py b/django/core/files/base.py index 04853fad0c..37b1be89b3 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 -from django.utils.encoding import smart_str, smart_unicode +from django.utils.encoding import smart_bytes, smart_text from django.core.files.utils import FileProxyMixin class File(FileProxyMixin): @@ -18,10 +18,10 @@ class File(FileProxyMixin): self.mode = file.mode def __str__(self): - return smart_str(self.name or '') + return smart_bytes(self.name or '') def __unicode__(self): - return smart_unicode(self.name or '') + return smart_text(self.name or '') def __repr__(self): return "<%s: %s>" % (self.__class__.__name__, self or "None") diff --git a/django/core/files/storage.py b/django/core/files/storage.py index 5179980513..7542dcda46 100644 --- a/django/core/files/storage.py +++ b/django/core/files/storage.py @@ -11,7 +11,7 @@ from django.conf import settings from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation from django.core.files import locks, File from django.core.files.move import file_move_safe -from django.utils.encoding import force_unicode, filepath_to_uri +from django.utils.encoding import force_text, filepath_to_uri from django.utils.functional import LazyObject from django.utils.importlib import import_module from django.utils.text import get_valid_filename @@ -48,7 +48,7 @@ class Storage(object): name = self._save(name, content) # Store filenames with forward slashes, even on Windows - return force_unicode(name.replace('\\', '/')) + return force_text(name.replace('\\', '/')) # These methods are part of the public API, with default implementations. diff --git a/django/core/files/uploadedfile.py b/django/core/files/uploadedfile.py index 97d53482e4..3a6c632975 100644 --- a/django/core/files/uploadedfile.py +++ b/django/core/files/uploadedfile.py @@ -8,7 +8,7 @@ from io import BytesIO from django.conf import settings from django.core.files.base import File from django.core.files import temp as tempfile -from django.utils.encoding import smart_str +from django.utils.encoding import smart_bytes __all__ = ('UploadedFile', 'TemporaryUploadedFile', 'InMemoryUploadedFile', 'SimpleUploadedFile') @@ -30,7 +30,7 @@ class UploadedFile(File): self.charset = charset def __repr__(self): - return smart_str("<%s: %s (%s)>" % ( + return smart_bytes("<%s: %s (%s)>" % ( self.__class__.__name__, self.name, self.content_type)) def _get_name(self): diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index 7fd7d19c4a..5a6825f0a7 100644 --- a/django/core/handlers/base.py +++ b/django/core/handlers/base.py @@ -4,7 +4,7 @@ import sys from django import http from django.core import signals -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.importlib import import_module from django.utils.log import getLogger from django.utils import six @@ -250,7 +250,7 @@ def get_script_name(environ): """ from django.conf import settings if settings.FORCE_SCRIPT_NAME is not None: - return force_unicode(settings.FORCE_SCRIPT_NAME) + return force_text(settings.FORCE_SCRIPT_NAME) # If Apache's mod_rewrite had a whack at the URL, Apache set either # SCRIPT_URL or REDIRECT_URL to the full resource URL before applying any @@ -261,5 +261,5 @@ def get_script_name(environ): if not script_url: script_url = environ.get('REDIRECT_URL', '') if script_url: - return force_unicode(script_url[:-len(environ.get('PATH_INFO', ''))]) - return force_unicode(environ.get('SCRIPT_NAME', '')) + return force_text(script_url[:-len(environ.get('PATH_INFO', ''))]) + return force_text(environ.get('SCRIPT_NAME', '')) diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py index 51ad2be002..70b23f8515 100644 --- a/django/core/handlers/wsgi.py +++ b/django/core/handlers/wsgi.py @@ -9,7 +9,7 @@ from django.core import signals from django.core.handlers import base from django.core.urlresolvers import set_script_prefix from django.utils import datastructures -from django.utils.encoding import force_unicode, smart_str, iri_to_uri +from django.utils.encoding import force_text, smart_bytes, iri_to_uri from django.utils.log import getLogger logger = getLogger('django.request') @@ -127,7 +127,7 @@ class LimitedStream(object): class WSGIRequest(http.HttpRequest): def __init__(self, environ): script_name = base.get_script_name(environ) - path_info = force_unicode(environ.get('PATH_INFO', '/')) + path_info = force_text(environ.get('PATH_INFO', '/')) if not path_info or path_info == script_name: # Sometimes PATH_INFO exists, but is empty (e.g. accessing # the SCRIPT_NAME URL without a trailing slash). We really need to @@ -246,5 +246,5 @@ class WSGIHandler(base.BaseHandler): response_headers = [(str(k), str(v)) for k, v in response.items()] for c in response.cookies.values(): response_headers.append((str('Set-Cookie'), str(c.output(header='')))) - start_response(smart_str(status), response_headers) + start_response(smart_bytes(status), response_headers) return response diff --git a/django/core/mail/message.py b/django/core/mail/message.py index 629ad464f9..8f589ae33d 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -15,7 +15,7 @@ from io import BytesIO from django.conf import settings from django.core.mail.utils import DNS_NAME -from django.utils.encoding import smart_str, force_unicode +from django.utils.encoding import smart_bytes, force_text from django.utils import six @@ -79,7 +79,7 @@ ADDRESS_HEADERS = set([ def forbid_multi_line_headers(name, val, encoding): """Forbids multi-line headers, to prevent header injection.""" encoding = encoding or settings.DEFAULT_CHARSET - val = force_unicode(val) + val = force_text(val) if '\n' in val or '\r' in val: raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name)) try: @@ -93,12 +93,12 @@ def forbid_multi_line_headers(name, val, encoding): else: if name.lower() == 'subject': val = Header(val) - return smart_str(name), val + return smart_bytes(name), val def sanitize_address(addr, encoding): if isinstance(addr, six.string_types): - addr = parseaddr(force_unicode(addr)) + addr = parseaddr(force_text(addr)) nm, addr = addr nm = str(Header(nm, encoding)) try: @@ -210,7 +210,7 @@ class EmailMessage(object): def message(self): encoding = self.encoding or settings.DEFAULT_CHARSET - msg = SafeMIMEText(smart_str(self.body, encoding), + msg = SafeMIMEText(smart_bytes(self.body, encoding), self.content_subtype, encoding) msg = self._create_message(msg) msg['Subject'] = self.subject @@ -293,7 +293,7 @@ class EmailMessage(object): basetype, subtype = mimetype.split('/', 1) if basetype == 'text': encoding = self.encoding or settings.DEFAULT_CHARSET - attachment = SafeMIMEText(smart_str(content, encoding), subtype, encoding) + attachment = SafeMIMEText(smart_bytes(content, encoding), subtype, encoding) else: # Encode non-text attachments with base64. attachment = MIMEBase(basetype, subtype) diff --git a/django/core/management/commands/createcachetable.py b/django/core/management/commands/createcachetable.py index fd6dbbbd2c..411042ee76 100644 --- a/django/core/management/commands/createcachetable.py +++ b/django/core/management/commands/createcachetable.py @@ -4,7 +4,7 @@ from django.core.cache.backends.db import BaseDatabaseCache from django.core.management.base import LabelCommand, CommandError from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS from django.db.utils import DatabaseError -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text class Command(LabelCommand): @@ -60,7 +60,7 @@ class Command(LabelCommand): transaction.rollback_unless_managed(using=db) raise CommandError( "Cache table '%s' could not be created.\nThe error was: %s." % - (tablename, force_unicode(e))) + (tablename, force_text(e))) for statement in index_output: curs.execute(statement) transaction.commit_unless_managed(using=db) diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index 34f8041d33..1896e53cee 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -14,7 +14,7 @@ from django.core.management.color import no_style from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS, IntegrityError, DatabaseError) from django.db.models import get_apps -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from itertools import product try: @@ -189,7 +189,7 @@ class Command(BaseCommand): 'app_label': obj.object._meta.app_label, 'object_name': obj.object._meta.object_name, 'pk': obj.object.pk, - 'error_msg': force_unicode(e) + 'error_msg': force_text(e) },) raise diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py index 19886f7d53..78a01c7098 100644 --- a/django/core/serializers/base.py +++ b/django/core/serializers/base.py @@ -5,7 +5,7 @@ Module for abstract serializer/unserializer base classes. from io import BytesIO from django.db import models -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils import six class SerializerDoesNotExist(KeyError): diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py index 8b56d0e7b8..3bac24d33a 100644 --- a/django/core/serializers/json.py +++ b/django/core/serializers/json.py @@ -12,7 +12,7 @@ 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_str +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/python.py b/django/core/serializers/python.py index 83c6eb6739..348ff1dada 100644 --- a/django/core/serializers/python.py +++ b/django/core/serializers/python.py @@ -8,7 +8,7 @@ from __future__ import unicode_literals from django.conf import settings from django.core.serializers import base from django.db import models, DEFAULT_DB_ALIAS -from django.utils.encoding import smart_unicode, is_protected_type +from django.utils.encoding import smart_text, is_protected_type from django.utils import six class Serializer(base.Serializer): @@ -34,8 +34,8 @@ class Serializer(base.Serializer): def get_dump_object(self, obj): return { - "pk": smart_unicode(obj._get_pk_val(), strings_only=True), - "model": smart_unicode(obj._meta), + "pk": smart_text(obj._get_pk_val(), strings_only=True), + "model": smart_text(obj._meta), "fields": self._current } @@ -65,7 +65,7 @@ class Serializer(base.Serializer): if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'): m2m_value = lambda value: value.natural_key() else: - m2m_value = lambda value: smart_unicode(value._get_pk_val(), strings_only=True) + m2m_value = lambda value: smart_text(value._get_pk_val(), strings_only=True) self._current[field.name] = [m2m_value(related) for related in getattr(obj, field.name).iterator()] @@ -90,7 +90,7 @@ def Deserializer(object_list, **options): # Handle each field for (field_name, field_value) in six.iteritems(d["fields"]): if isinstance(field_value, str): - field_value = smart_unicode(field_value, options.get("encoding", settings.DEFAULT_CHARSET), strings_only=True) + field_value = smart_text(field_value, options.get("encoding", settings.DEFAULT_CHARSET), strings_only=True) field = Model._meta.get_field(field_name) @@ -101,9 +101,9 @@ def Deserializer(object_list, **options): if hasattr(value, '__iter__'): return field.rel.to._default_manager.db_manager(db).get_by_natural_key(*value).pk else: - return smart_unicode(field.rel.to._meta.pk.to_python(value)) + return smart_text(field.rel.to._meta.pk.to_python(value)) else: - m2m_convert = lambda v: smart_unicode(field.rel.to._meta.pk.to_python(v)) + m2m_convert = lambda v: smart_text(field.rel.to._meta.pk.to_python(v)) m2m_data[field.name] = [m2m_convert(pk) for pk in field_value] # Handle FK fields diff --git a/django/core/serializers/pyyaml.py b/django/core/serializers/pyyaml.py index ac0e6cf82d..9be1ea4492 100644 --- a/django/core/serializers/pyyaml.py +++ b/django/core/serializers/pyyaml.py @@ -12,7 +12,7 @@ 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_str +from django.utils.encoding import smart_bytes from django.utils import six diff --git a/django/core/serializers/xml_serializer.py b/django/core/serializers/xml_serializer.py index 9d9c023b64..c4e4dd189e 100644 --- a/django/core/serializers/xml_serializer.py +++ b/django/core/serializers/xml_serializer.py @@ -8,7 +8,7 @@ from django.conf import settings from django.core.serializers import base from django.db import models, DEFAULT_DB_ALIAS from django.utils.xmlutils import SimplerXMLGenerator -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from xml.dom import pulldom class Serializer(base.Serializer): @@ -46,11 +46,11 @@ class Serializer(base.Serializer): self.indent(1) obj_pk = obj._get_pk_val() if obj_pk is None: - attrs = {"model": smart_unicode(obj._meta),} + attrs = {"model": smart_text(obj._meta),} else: attrs = { - "pk": smart_unicode(obj._get_pk_val()), - "model": smart_unicode(obj._meta), + "pk": smart_text(obj._get_pk_val()), + "model": smart_text(obj._meta), } self.xml.startElement("object", attrs) @@ -96,10 +96,10 @@ class Serializer(base.Serializer): # Iterable natural keys are rolled out as subelements for key_value in related: self.xml.startElement("natural", {}) - self.xml.characters(smart_unicode(key_value)) + self.xml.characters(smart_text(key_value)) self.xml.endElement("natural") else: - self.xml.characters(smart_unicode(related_att)) + self.xml.characters(smart_text(related_att)) else: self.xml.addQuickElement("None") self.xml.endElement("field") @@ -120,13 +120,13 @@ class Serializer(base.Serializer): self.xml.startElement("object", {}) for key_value in natural: self.xml.startElement("natural", {}) - self.xml.characters(smart_unicode(key_value)) + self.xml.characters(smart_text(key_value)) self.xml.endElement("natural") self.xml.endElement("object") else: def handle_m2m(value): self.xml.addQuickElement("object", attrs={ - 'pk' : smart_unicode(value._get_pk_val()) + 'pk' : smart_text(value._get_pk_val()) }) for relobj in getattr(obj, field.name).iterator(): handle_m2m(relobj) @@ -141,7 +141,7 @@ class Serializer(base.Serializer): self.xml.startElement("field", { "name" : field.name, "rel" : field.rel.__class__.__name__, - "to" : smart_unicode(field.rel.to._meta), + "to" : smart_text(field.rel.to._meta), }) class Deserializer(base.Deserializer): diff --git a/django/core/signing.py b/django/core/signing.py index cd9759e536..9ab8c5b8b0 100644 --- a/django/core/signing.py +++ b/django/core/signing.py @@ -41,7 +41,7 @@ from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.utils import baseconv from django.utils.crypto import constant_time_compare, salted_hmac -from django.utils.encoding import force_unicode, smart_str +from django.utils.encoding import force_text, smart_bytes from django.utils.importlib import import_module @@ -135,7 +135,7 @@ def loads(s, key=None, salt='django.core.signing', serializer=JSONSerializer, ma """ Reverse of dumps(), raises BadSignature if signature fails """ - base64d = smart_str( + base64d = smart_bytes( TimestampSigner(key, salt=salt).unsign(s, max_age=max_age)) decompress = False if base64d[0] == '.': @@ -159,16 +159,16 @@ class Signer(object): return base64_hmac(self.salt + 'signer', value, self.key) def sign(self, value): - value = smart_str(value) + value = smart_bytes(value) return '%s%s%s' % (value, self.sep, self.signature(value)) def unsign(self, signed_value): - signed_value = smart_str(signed_value) + signed_value = smart_bytes(signed_value) if not self.sep in signed_value: raise BadSignature('No "%s" found in value' % self.sep) value, sig = signed_value.rsplit(self.sep, 1) if constant_time_compare(sig, self.signature(value)): - return force_unicode(value) + return force_text(value) raise BadSignature('Signature "%s" does not match' % sig) @@ -178,7 +178,7 @@ class TimestampSigner(Signer): return baseconv.base62.encode(int(time.time())) def sign(self, value): - value = smart_str('%s%s%s' % (value, self.sep, self.timestamp())) + value = smart_bytes('%s%s%s' % (value, self.sep, self.timestamp())) return '%s%s%s' % (value, self.sep, self.signature(value)) def unsign(self, value, max_age=None): diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index c17168f8cb..2fe744e8eb 100644 --- a/django/core/urlresolvers.py +++ b/django/core/urlresolvers.py @@ -14,7 +14,7 @@ from threading import local from django.http import Http404 from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist from django.utils.datastructures import MultiValueDict -from django.utils.encoding import iri_to_uri, force_unicode, smart_str +from django.utils.encoding import iri_to_uri, force_text, smart_bytes from django.utils.functional import memoize, lazy from django.utils.importlib import import_module from django.utils.module_loading import module_has_submodule @@ -163,7 +163,7 @@ class LocaleRegexProvider(object): if isinstance(self._regex, six.string_types): regex = self._regex else: - regex = force_unicode(self._regex) + regex = force_text(self._regex) try: compiled_regex = re.compile(regex, re.UNICODE) except re.error as e: @@ -190,7 +190,7 @@ class RegexURLPattern(LocaleRegexProvider): self.name = name def __repr__(self): - return smart_str('<%s %s %s>' % (self.__class__.__name__, self.name, self.regex.pattern)) + return smart_bytes('<%s %s %s>' % (self.__class__.__name__, self.name, self.regex.pattern)) def add_prefix(self, prefix): """ @@ -240,7 +240,7 @@ class RegexURLResolver(LocaleRegexProvider): self._app_dict = {} def __repr__(self): - return smart_str('<%s %s (%s:%s) %s>' % (self.__class__.__name__, self.urlconf_name, self.app_name, self.namespace, self.regex.pattern)) + return smart_bytes('<%s %s (%s:%s) %s>' % (self.__class__.__name__, self.urlconf_name, self.app_name, self.namespace, self.regex.pattern)) def _populate(self): lookups = MultiValueDict() @@ -373,7 +373,7 @@ class RegexURLResolver(LocaleRegexProvider): if args: if len(args) != len(params) + len(prefix_args): continue - unicode_args = [force_unicode(val) for val in args] + unicode_args = [force_text(val) for val in args] candidate = (prefix_norm + result) % dict(zip(prefix_args + params, unicode_args)) else: if set(kwargs.keys()) | set(defaults.keys()) != set(params) | set(defaults.keys()) | set(prefix_args): @@ -385,7 +385,7 @@ class RegexURLResolver(LocaleRegexProvider): break if not matches: continue - unicode_kwargs = dict([(k, force_unicode(v)) for (k, v) in kwargs.items()]) + unicode_kwargs = dict([(k, force_text(v)) for (k, v) in kwargs.items()]) candidate = (prefix_norm + result) % unicode_kwargs if re.search('^%s%s' % (_prefix, pattern), candidate, re.UNICODE): return candidate diff --git a/django/core/validators.py b/django/core/validators.py index 91d6f62dcf..fd5dfa28d6 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -8,7 +8,7 @@ except ImportError: # Python 2 from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.ipv6 import is_valid_ipv6_address from django.utils import six @@ -36,7 +36,7 @@ class RegexValidator(object): """ Validates that the input matches the regular expression. """ - if not self.regex.search(smart_unicode(value)): + if not self.regex.search(smart_text(value)): raise ValidationError(self.message, code=self.code) class URLValidator(RegexValidator): @@ -54,7 +54,7 @@ class URLValidator(RegexValidator): except ValidationError as e: # Trivial case failed. Try for possible IDN domain if value: - value = smart_unicode(value) + value = smart_text(value) scheme, netloc, path, query, fragment = urlsplit(value) try: netloc = netloc.encode('idna') # IDN -> ACE diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 6e23ad5bb5..9606245162 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -607,16 +607,16 @@ class BaseDatabaseOperations(object): exists for database backends to provide a better implementation according to their own quoting schemes. """ - from django.utils.encoding import smart_unicode, force_unicode + from django.utils.encoding import smart_text, force_text # Convert params to contain Unicode values. - to_unicode = lambda s: force_unicode(s, strings_only=True, errors='replace') + to_unicode = lambda s: force_text(s, strings_only=True, errors='replace') if isinstance(params, (list, tuple)): u_params = tuple([to_unicode(val) for val in params]) else: u_params = dict([(to_unicode(k), to_unicode(v)) for k, v in params.items()]) - return smart_unicode(sql) % u_params + return smart_text(sql) % u_params def last_insert_id(self, cursor, table_name, pk_name): """ @@ -800,8 +800,8 @@ class BaseDatabaseOperations(object): def prep_for_like_query(self, x): """Prepares a value for use in a LIKE query.""" - from django.utils.encoding import smart_unicode - return smart_unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_") + from django.utils.encoding import smart_text + return smart_text(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_") # Same as prep_for_like_query(), but called for "iexact" matches, which # need not necessarily be implemented using "LIKE" in the backend. diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index b08113fed7..0f16130477 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -53,7 +53,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_str, force_unicode +from django.utils.encoding import smart_bytes, force_text from django.utils import six from django.utils import timezone @@ -64,9 +64,9 @@ IntegrityError = Database.IntegrityError # Check whether cx_Oracle was compiled with the WITH_UNICODE option. This will # also be True in Python 3.0. if int(Database.version.split('.', 1)[0]) >= 5 and not hasattr(Database, 'UNICODE'): - convert_unicode = force_unicode + convert_unicode = force_text else: - convert_unicode = smart_str + convert_unicode = smart_bytes class DatabaseFeatures(BaseDatabaseFeatures): @@ -162,7 +162,7 @@ WHEN (new.%(col_name)s IS NULL) if isinstance(value, Database.LOB): value = value.read() if field and field.get_internal_type() == 'TextField': - value = force_unicode(value) + value = force_text(value) # Oracle stores empty strings as null. We need to undo this in # order to adhere to the Django convention of using the empty @@ -245,7 +245,7 @@ WHEN (new.%(col_name)s IS NULL) def process_clob(self, value): if value is None: return '' - return force_unicode(value.read()) + return force_text(value.read()) def quote_name(self, name): # SQL92 requires delimited (quoted) names to be case-sensitive. When @@ -595,9 +595,9 @@ class OracleParam(object): param = param.astimezone(timezone.utc).replace(tzinfo=None) if hasattr(param, 'bind_parameter'): - self.smart_str = param.bind_parameter(cursor) + self.smart_bytes = param.bind_parameter(cursor) else: - self.smart_str = convert_unicode(param, cursor.charset, + self.smart_bytes = convert_unicode(param, cursor.charset, strings_only) if hasattr(param, 'input_size'): # If parameter has `input_size` attribute, use that. @@ -676,7 +676,7 @@ class FormatStylePlaceholderCursor(object): self.setinputsizes(*sizes) def _param_generator(self, params): - return [p.smart_str for p in params] + return [p.smart_bytes for p in params] def execute(self, query, params=None): if params is None: @@ -831,7 +831,7 @@ def to_unicode(s): unchanged). """ if isinstance(s, six.string_types): - return force_unicode(s) + return force_text(s) return s diff --git a/django/db/models/base.py b/django/db/models/base.py index a25c106290..4568430bfa 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -23,7 +23,7 @@ from django.db.models import signals from django.db.models.loading import register_models, get_model from django.utils.translation import ugettext_lazy as _ from django.utils.functional import curry -from django.utils.encoding import smart_str, force_unicode +from django.utils.encoding import smart_bytes, force_text from django.utils import six from django.utils.text import get_text_list, capfirst @@ -380,11 +380,11 @@ class Model(six.with_metaclass(ModelBase, object)): u = six.text_type(self) except (UnicodeEncodeError, UnicodeDecodeError): u = '[Bad Unicode data]' - return smart_str('<%s: %s>' % (self.__class__.__name__, u)) + return smart_bytes('<%s: %s>' % (self.__class__.__name__, u)) def __str__(self): if hasattr(self, '__unicode__'): - return force_unicode(self).encode('utf-8') + return force_text(self).encode('utf-8') return '%s object' % self.__class__.__name__ def __eq__(self, other): @@ -605,14 +605,14 @@ class Model(six.with_metaclass(ModelBase, object)): def _get_FIELD_display(self, field): value = getattr(self, field.attname) - return force_unicode(dict(field.flatchoices).get(value, value), strings_only=True) + return force_text(dict(field.flatchoices).get(value, value), strings_only=True) def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs): if not self.pk: raise ValueError("get_next/get_previous cannot be used on unsaved objects.") op = is_next and 'gt' or 'lt' order = not is_next and '-' or '' - param = smart_str(getattr(self, field.attname)) + param = smart_bytes(getattr(self, field.attname)) q = Q(**{'%s__%s' % (field.name, op): param}) q = q|Q(**{field.name: param, 'pk__%s' % op: self.pk}) qs = self.__class__._default_manager.using(self._state.db).filter(**kwargs).filter(q).order_by('%s%s' % (order, field.name), '%spk' % order) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index de24a24ed1..2c738d6a20 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -19,7 +19,7 @@ from django.utils.functional import curry, total_ordering from django.utils.text import capfirst from django.utils import timezone from django.utils.translation import ugettext_lazy as _ -from django.utils.encoding import smart_unicode, force_unicode +from django.utils.encoding import smart_text, force_text from django.utils.ipv6 import clean_ipv6_address from django.utils import six @@ -386,7 +386,7 @@ class Field(object): if self.has_default(): if callable(self.default): return self.default() - return force_unicode(self.default, strings_only=True) + return force_text(self.default, strings_only=True) if (not self.empty_strings_allowed or (self.null and not connection.features.interprets_empty_strings_as_nulls)): return None @@ -404,11 +404,11 @@ class Field(object): rel_model = self.rel.to if hasattr(self.rel, 'get_related_field'): lst = [(getattr(x, self.rel.get_related_field().attname), - smart_unicode(x)) + smart_text(x)) for x in rel_model._default_manager.complex_filter( self.rel.limit_choices_to)] else: - lst = [(x._get_pk_val(), smart_unicode(x)) + lst = [(x._get_pk_val(), smart_text(x)) for x in rel_model._default_manager.complex_filter( self.rel.limit_choices_to)] return first_choice + lst @@ -435,7 +435,7 @@ class Field(object): Returns a string value of this field from the passed obj. This is used by the serialization framework. """ - return smart_unicode(self._get_val_from_obj(obj)) + return smart_text(self._get_val_from_obj(obj)) def bind(self, fieldmapping, original, bound_field_class): return bound_field_class(self, fieldmapping, original) @@ -629,7 +629,7 @@ class CharField(Field): def to_python(self, value): if isinstance(value, six.string_types) or value is None: return value - return smart_unicode(value) + return smart_text(value) def get_prep_value(self, value): return self.to_python(value) @@ -1189,7 +1189,7 @@ class TextField(Field): def get_prep_value(self, value): if isinstance(value, six.string_types) or value is None: return value - return smart_unicode(value) + return smart_text(value) def formfield(self, **kwargs): defaults = {'widget': forms.Textarea} diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py index b51ef1d5d6..ad4c36ca0d 100644 --- a/django/db/models/fields/files.py +++ b/django/db/models/fields/files.py @@ -8,7 +8,7 @@ from django.core.files.base import File from django.core.files.storage import default_storage from django.core.files.images import ImageFile from django.db.models import signals -from django.utils.encoding import force_unicode, smart_str +from django.utils.encoding import force_text, smart_bytes from django.utils import six from django.utils.translation import ugettext_lazy as _ @@ -280,7 +280,7 @@ class FileField(Field): setattr(cls, self.name, self.descriptor_class(self)) def get_directory_name(self): - return os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to)))) + return os.path.normpath(force_text(datetime.datetime.now().strftime(smart_bytes(self.upload_to)))) def get_filename(self, filename): return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename))) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index bfa8feee9f..eaa62c6061 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -9,7 +9,7 @@ from django.db.models.related import RelatedObject from django.db.models.query import QuerySet from django.db.models.query_utils import QueryWrapper from django.db.models.deletion import CASCADE -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils import six from django.utils.translation import ugettext_lazy as _, string_concat from django.utils.functional import curry, cached_property @@ -999,7 +999,7 @@ class ForeignKey(RelatedField, Field): if not self.blank and self.choices: choice_list = self.get_choices_default() if len(choice_list) == 2: - return smart_unicode(choice_list[1][0]) + return smart_text(choice_list[1][0]) return Field.value_to_string(self, obj) def contribute_to_class(self, cls, name): @@ -1205,7 +1205,7 @@ class ManyToManyField(RelatedField, Field): choices_list = self.get_choices_default() if len(choices_list) == 1: data = [choices_list[0][0]] - return smart_unicode(data) + return smart_text(data) def contribute_to_class(self, cls, name): # To support multiple relations to self, it's useful to have a non-None diff --git a/django/db/models/options.py b/django/db/models/options.py index 9e8d4120e9..239ad30b06 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -8,7 +8,7 @@ from django.db.models.fields import AutoField, FieldDoesNotExist from django.db.models.fields.proxy import OrderWrt from django.db.models.loading import get_models, app_cache_ready from django.utils.translation import activate, deactivate_all, get_language, string_concat -from django.utils.encoding import force_unicode, smart_str +from django.utils.encoding import force_text, smart_bytes from django.utils.datastructures import SortedDict from django.utils import six @@ -199,7 +199,7 @@ class Options(object): return '' % self.object_name def __str__(self): - return "%s.%s" % (smart_str(self.app_label), smart_str(self.module_name)) + return "%s.%s" % (smart_bytes(self.app_label), smart_bytes(self.module_name)) def verbose_name_raw(self): """ @@ -209,7 +209,7 @@ class Options(object): """ lang = get_language() deactivate_all() - raw = force_unicode(self.verbose_name) + raw = force_text(self.verbose_name) activate(lang) return raw verbose_name_raw = property(verbose_name_raw) diff --git a/django/db/models/related.py b/django/db/models/related.py index 90995d749f..a0dcec7132 100644 --- a/django/db/models/related.py +++ b/django/db/models/related.py @@ -1,4 +1,4 @@ -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.db.models.fields import BLANK_CHOICE_DASH class BoundRelatedObject(object): @@ -34,9 +34,9 @@ class RelatedObject(object): if limit_to_currently_related: queryset = queryset.complex_filter( {'%s__isnull' % self.parent_model._meta.module_name: False}) - lst = [(x._get_pk_val(), smart_unicode(x)) for x in queryset] + lst = [(x._get_pk_val(), smart_text(x)) for x in queryset] return first_choice + lst - + def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False): # Defer to the actual field definition for db prep return self.field.get_db_prep_lookup(lookup_type, value, diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 9cf732f263..69dda228bd 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -10,7 +10,7 @@ all about the internals of models in order to get the information it needs. import copy from django.utils.datastructures import SortedDict -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.tree import Node from django.utils import six from django.db import connections, DEFAULT_DB_ALIAS @@ -1776,7 +1776,7 @@ class Query(object): else: param_iter = iter([]) for name, entry in select.items(): - entry = force_unicode(entry) + entry = force_text(entry) entry_params = [] pos = entry.find("%s") while pos != -1: diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py index cc7da0eeaf..937505b9b0 100644 --- a/django/db/models/sql/subqueries.py +++ b/django/db/models/sql/subqueries.py @@ -10,7 +10,7 @@ from django.db.models.sql.query import Query from django.db.models.sql.where import AND, Constraint from django.utils.datastructures import SortedDict from django.utils.functional import Promise -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils import six @@ -105,7 +105,7 @@ class UpdateQuery(Query): saving models. """ # Check that no Promise object passes to the query. Refs #10498. - values_seq = [(value[0], value[1], force_unicode(value[2])) + values_seq = [(value[0], value[1], force_text(value[2])) if isinstance(value[2], Promise) else value for value in values_seq] self.values.extend(values_seq) @@ -171,7 +171,7 @@ class InsertQuery(Query): for obj in objs: value = getattr(obj, field.attname) if isinstance(value, Promise): - setattr(obj, field.attname, force_unicode(value)) + setattr(obj, field.attname, force_text(value)) self.objs = objs self.raw = raw diff --git a/django/forms/fields.py b/django/forms/fields.py index cdb1d7be67..7f0d26d1aa 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -23,7 +23,7 @@ from django.forms.widgets import (TextInput, PasswordInput, HiddenInput, NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput, SplitDateTimeWidget, SplitHiddenDateTimeWidget, FILE_INPUT_CONTRADICTION) from django.utils import formats -from django.utils.encoding import smart_unicode, force_unicode +from django.utils.encoding import smart_text, force_text from django.utils.ipv6 import clean_ipv6_address from django.utils import six from django.utils.translation import ugettext_lazy as _ @@ -78,13 +78,13 @@ class Field(object): # validators -- List of addtional validators to use # localize -- Boolean that specifies if the field should be localized. if label is not None: - label = smart_unicode(label) + label = smart_text(label) self.required, self.label, self.initial = required, label, initial self.show_hidden_initial = show_hidden_initial if help_text is None: self.help_text = '' else: - self.help_text = smart_unicode(help_text) + self.help_text = smart_text(help_text) widget = widget or self.widget if isinstance(widget, type): widget = widget() @@ -195,7 +195,7 @@ class CharField(Field): "Returns a Unicode object." if value in validators.EMPTY_VALUES: return '' - return smart_unicode(value) + return smart_text(value) def widget_attrs(self, widget): attrs = super(CharField, self).widget_attrs(widget) @@ -288,7 +288,7 @@ class DecimalField(Field): return None if self.localize: value = formats.sanitize_separators(value) - value = smart_unicode(value).strip() + value = smart_text(value).strip() try: value = Decimal(value) except DecimalException: @@ -333,7 +333,7 @@ class BaseTemporalField(Field): def to_python(self, value): # Try to coerce the value to unicode. - unicode_value = force_unicode(value, strings_only=True) + unicode_value = force_text(value, strings_only=True) if isinstance(unicode_value, six.text_type): value = unicode_value.strip() # If unicode, try to strptime against each input format. @@ -692,7 +692,7 @@ class ChoiceField(Field): "Returns a Unicode object." if value in validators.EMPTY_VALUES: return '' - return smart_unicode(value) + return smart_text(value) def validate(self, value): """ @@ -708,10 +708,10 @@ class ChoiceField(Field): if isinstance(v, (list, tuple)): # This is an optgroup, so look inside the group for options for k2, v2 in v: - if value == smart_unicode(k2): + if value == smart_text(k2): return True else: - if value == smart_unicode(k): + if value == smart_text(k): return True return False @@ -752,7 +752,7 @@ class MultipleChoiceField(ChoiceField): return [] elif not isinstance(value, (list, tuple)): raise ValidationError(self.error_messages['invalid_list']) - return [smart_unicode(val) for val in value] + return [smart_text(val) for val in value] def validate(self, value): """ diff --git a/django/forms/forms.py b/django/forms/forms.py index 0f3fdb2e40..45b758202a 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -12,7 +12,7 @@ from django.forms.util import flatatt, ErrorDict, ErrorList from django.forms.widgets import Media, media_property, TextInput, Textarea from django.utils.datastructures import SortedDict from django.utils.html import conditional_escape, format_html -from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode +from django.utils.encoding import StrAndUnicode, smart_text, force_text from django.utils.safestring import mark_safe from django.utils import six @@ -150,7 +150,7 @@ class BaseForm(StrAndUnicode): bf_errors = self.error_class([conditional_escape(error) for error in bf.errors]) # Escape and cache in local variable. if bf.is_hidden: if bf_errors: - top_errors.extend(['(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors]) + top_errors.extend(['(Hidden field %s) %s' % (name, force_text(e)) for e in bf_errors]) hidden_fields.append(six.text_type(bf)) else: # Create a 'class="..."' atribute if the row should have any @@ -160,10 +160,10 @@ class BaseForm(StrAndUnicode): html_class_attr = ' class="%s"' % css_classes if errors_on_separate_row and bf_errors: - output.append(error_row % force_unicode(bf_errors)) + output.append(error_row % force_text(bf_errors)) if bf.label: - label = conditional_escape(force_unicode(bf.label)) + label = conditional_escape(force_text(bf.label)) # Only add the suffix if the label does not end in # punctuation. if self.label_suffix: @@ -174,20 +174,20 @@ class BaseForm(StrAndUnicode): label = '' if field.help_text: - help_text = help_text_html % force_unicode(field.help_text) + help_text = help_text_html % force_text(field.help_text) else: help_text = '' output.append(normal_row % { - 'errors': force_unicode(bf_errors), - 'label': force_unicode(label), + 'errors': force_text(bf_errors), + 'label': force_text(label), 'field': six.text_type(bf), 'help_text': help_text, 'html_class_attr': html_class_attr }) if top_errors: - output.insert(0, error_row % force_unicode(top_errors)) + output.insert(0, error_row % force_text(top_errors)) if hidden_fields: # Insert any hidden fields in the last row. str_hidden = ''.join(hidden_fields) @@ -535,8 +535,8 @@ class BoundField(StrAndUnicode): associated Form has specified auto_id. Returns an empty string otherwise. """ auto_id = self.form.auto_id - if auto_id and '%s' in smart_unicode(auto_id): - return smart_unicode(auto_id) % self.html_name + if auto_id and '%s' in smart_text(auto_id): + return smart_text(auto_id) % self.html_name elif auto_id: return self.html_name return '' diff --git a/django/forms/models.py b/django/forms/models.py index a2b5448b14..80d2a6536f 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -13,7 +13,7 @@ from django.forms.formsets import BaseFormSet, formset_factory from django.forms.util import ErrorList from django.forms.widgets import (SelectMultiple, HiddenInput, MultipleHiddenInput, media_property) -from django.utils.encoding import smart_unicode, force_unicode +from django.utils.encoding import smart_text, force_text from django.utils.datastructures import SortedDict from django.utils import six from django.utils.text import get_text_list, capfirst @@ -875,7 +875,7 @@ class InlineForeignKeyField(Field): orig = getattr(self.parent_instance, self.to_field) else: orig = self.parent_instance.pk - if force_unicode(value) != force_unicode(orig): + if force_text(value) != force_text(orig): raise ValidationError(self.error_messages['invalid_choice']) return self.parent_instance @@ -953,7 +953,7 @@ class ModelChoiceField(ChoiceField): generate the labels for the choices presented by this object. Subclasses can override this method to customize the display of the choices. """ - return smart_unicode(obj) + return smart_text(obj) def _get_choices(self): # If self._choices is set, then somebody must have manually set @@ -1025,9 +1025,9 @@ class ModelMultipleChoiceField(ModelChoiceField): except ValueError: raise ValidationError(self.error_messages['invalid_pk_value'] % pk) qs = self.queryset.filter(**{'%s__in' % key: value}) - pks = set([force_unicode(getattr(o, key)) for o in qs]) + pks = set([force_text(getattr(o, key)) for o in qs]) for val in value: - if force_unicode(val) not in pks: + if force_text(val) not in pks: raise ValidationError(self.error_messages['invalid_choice'] % val) # Since this overrides the inherited ModelChoiceField.clean # we run custom validators here diff --git a/django/forms/util.py b/django/forms/util.py index 8cf03d38af..cd6b52df6f 100644 --- a/django/forms/util.py +++ b/django/forms/util.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals from django.conf import settings from django.utils.html import format_html, format_html_join -from django.utils.encoding import StrAndUnicode, force_unicode +from django.utils.encoding import StrAndUnicode, force_text from django.utils.safestring import mark_safe from django.utils import timezone from django.utils.translation import ugettext_lazy as _ @@ -35,12 +35,12 @@ class ErrorDict(dict, StrAndUnicode): if not self: return '' return format_html('
      {0}
    ', format_html_join('', '
  • {0}{1}
  • ', - ((k, force_unicode(v)) + ((k, force_text(v)) for k, v in self.items()) )) def as_text(self): - return '\n'.join(['* %s\n%s' % (k, '\n'.join([' * %s' % force_unicode(i) for i in v])) for k, v in self.items()]) + return '\n'.join(['* %s\n%s' % (k, '\n'.join([' * %s' % force_text(i) for i in v])) for k, v in self.items()]) class ErrorList(list, StrAndUnicode): """ @@ -53,16 +53,16 @@ class ErrorList(list, StrAndUnicode): if not self: return '' return format_html('
      {0}
    ', format_html_join('', '
  • {0}
  • ', - ((force_unicode(e),) for e in self) + ((force_text(e),) for e in self) ) ) def as_text(self): if not self: return '' - return '\n'.join(['* %s' % force_unicode(e) for e in self]) + return '\n'.join(['* %s' % force_text(e) for e in self]) def __repr__(self): - return repr([force_unicode(e) for e in self]) + return repr([force_text(e) for e in self]) # Utilities for time zone support in DateTimeField et al. diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 13b7d8e7f6..be9ac8eb8f 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -17,7 +17,7 @@ from django.forms.util import flatatt, to_current_timezone from django.utils.datastructures import MultiValueDict, MergeDict from django.utils.html import conditional_escape, format_html, format_html_join from django.utils.translation import ugettext, ugettext_lazy -from django.utils.encoding import StrAndUnicode, force_unicode +from django.utils.encoding import StrAndUnicode, force_text from django.utils.safestring import mark_safe from django.utils import six from django.utils import datetime_safe, formats @@ -223,7 +223,7 @@ class Widget(six.with_metaclass(MediaDefiningClass)): initial_value = '' else: initial_value = initial - if force_unicode(initial_value) != force_unicode(data_value): + if force_text(initial_value) != force_text(data_value): return True return False @@ -257,7 +257,7 @@ class Input(Widget): final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) if value != '': # Only add the 'value' attribute if a value is non-empty. - final_attrs['value'] = force_unicode(self._format_value(value)) + final_attrs['value'] = force_text(self._format_value(value)) return format_html('', flatatt(final_attrs)) class TextInput(Input): @@ -294,7 +294,7 @@ class MultipleHiddenInput(HiddenInput): id_ = final_attrs.get('id', None) inputs = [] for i, v in enumerate(value): - input_attrs = dict(value=force_unicode(v), **final_attrs) + input_attrs = dict(value=force_text(v), **final_attrs) if id_: # An ID attribute was given. Add a numeric index as a suffix # so that the inputs don't all have the same ID attribute. @@ -361,7 +361,7 @@ class ClearableFileInput(FileInput): template = self.template_with_initial substitutions['initial'] = format_html('{1}', value.url, - force_unicode(value)) + force_text(value)) if not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) @@ -398,7 +398,7 @@ class Textarea(Widget): final_attrs = self.build_attrs(attrs, name=name) return format_html('{1}', flatatt(final_attrs), - force_unicode(value)) + force_text(value)) class DateInput(Input): input_type = 'text' @@ -515,7 +515,7 @@ class CheckboxInput(Widget): final_attrs['checked'] = 'checked' if not (value is True or value is False or value is None or value == ''): # Only add the 'value' attribute if a value is non-empty. - final_attrs['value'] = force_unicode(value) + final_attrs['value'] = force_text(value) return format_html('', flatatt(final_attrs)) def value_from_datadict(self, data, files, name): @@ -556,7 +556,7 @@ class Select(Widget): return mark_safe('\n'.join(output)) def render_option(self, selected_choices, option_value, option_label): - option_value = force_unicode(option_value) + option_value = force_text(option_value) if option_value in selected_choices: selected_html = mark_safe(' selected="selected"') if not self.allow_multiple_selected: @@ -567,15 +567,15 @@ class Select(Widget): return format_html('', option_value, selected_html, - force_unicode(option_label)) + force_text(option_label)) def render_options(self, choices, selected_choices): # Normalize to strings. - selected_choices = set(force_unicode(v) for v in selected_choices) + selected_choices = set(force_text(v) for v in selected_choices) output = [] for option_value, option_label in chain(self.choices, choices): if isinstance(option_label, (list, tuple)): - output.append(format_html('', force_unicode(option_value))) + output.append(format_html('', force_text(option_value))) for option in option_label: output.append(self.render_option(selected_choices, *option)) output.append('') @@ -643,8 +643,8 @@ class SelectMultiple(Select): data = [] if len(initial) != len(data): return True - initial_set = set([force_unicode(value) for value in initial]) - data_set = set([force_unicode(value) for value in data]) + initial_set = set([force_text(value) for value in initial]) + data_set = set([force_text(value) for value in data]) return data_set != initial_set class RadioInput(SubWidget): @@ -656,8 +656,8 @@ class RadioInput(SubWidget): def __init__(self, name, value, attrs, choice, index): self.name, self.value = name, value self.attrs = attrs - self.choice_value = force_unicode(choice[0]) - self.choice_label = force_unicode(choice[1]) + self.choice_value = force_text(choice[0]) + self.choice_label = force_text(choice[1]) self.index = index def __unicode__(self): @@ -671,7 +671,7 @@ class RadioInput(SubWidget): label_for = format_html(' for="{0}_{1}"', self.attrs['id'], self.index) else: label_for = '' - choice_label = force_unicode(self.choice_label) + choice_label = force_text(self.choice_label) return format_html('{1} {2}', label_for, self.tag(), choice_label) def is_checked(self): @@ -709,7 +709,7 @@ class RadioFieldRenderer(StrAndUnicode): """Outputs a
      for this set of radio fields.""" return format_html('
        \n{0}\n
      ', format_html_join('\n', '
    • {0}
    • ', - [(force_unicode(w),) for w in self] + [(force_text(w),) for w in self] )) class RadioSelect(Select): @@ -729,7 +729,7 @@ class RadioSelect(Select): def get_renderer(self, name, value, attrs=None, choices=()): """Returns an instance of the renderer.""" if value is None: value = '' - str_value = force_unicode(value) # Normalize to string. + str_value = force_text(value) # Normalize to string. final_attrs = self.build_attrs(attrs) choices = list(chain(self.choices, choices)) return self.renderer(name, str_value, final_attrs, choices) @@ -753,7 +753,7 @@ class CheckboxSelectMultiple(SelectMultiple): final_attrs = self.build_attrs(attrs, name=name) output = ['
        '] # Normalize to strings - str_values = set([force_unicode(v) for v in value]) + str_values = set([force_text(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. @@ -764,9 +764,9 @@ class CheckboxSelectMultiple(SelectMultiple): label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) - option_value = force_unicode(option_value) + option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) - option_label = force_unicode(option_label) + option_label = force_text(option_label) output.append(format_html('
      • {1} {2}
      • ', label_for, rendered_cb, option_label)) output.append('
      ') diff --git a/django/http/__init__.py b/django/http/__init__.py index 4c2db74890..b23304f346 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -61,14 +61,14 @@ else: if not _cookie_allows_colon_in_names: def load(self, rawdata): self.bad_cookies = set() - super(SimpleCookie, self).load(smart_str(rawdata)) + super(SimpleCookie, self).load(smart_bytes(rawdata)) for key in self.bad_cookies: del self[key] # override private __set() method: # (needed for using our Morsel, and for laxness with CookieError def _BaseCookie__set(self, key, real_value, coded_value): - key = smart_str(key) + key = smart_bytes(key) try: M = self.get(key, Morsel()) M.set(key, real_value, coded_value) @@ -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_str, iri_to_uri, force_unicode +from django.utils.encoding import smart_bytes, iri_to_uri, force_text from django.utils.http import cookie_date from django.utils import six from django.utils import timezone @@ -137,7 +137,7 @@ def build_request_repr(request, path_override=None, GET_override=None, except: meta = '' path = path_override if path_override is not None else request.path - return smart_str('<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % + return smart_bytes('<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % (request.__class__.__name__, path, six.text_type(get), @@ -385,8 +385,8 @@ class QueryDict(MultiValueDict): encoding = settings.DEFAULT_CHARSET self.encoding = encoding for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True - self.appendlist(force_unicode(key, encoding, errors='replace'), - force_unicode(value, encoding, errors='replace')) + self.appendlist(force_text(key, encoding, errors='replace'), + force_text(value, encoding, errors='replace')) self._mutable = mutable def _get_encoding(self): @@ -481,13 +481,13 @@ class QueryDict(MultiValueDict): """ output = [] if safe: - safe = smart_str(safe, self.encoding) + safe = smart_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_str(k, self.encoding) - output.extend([encode(k, smart_str(v, self.encoding)) + k = smart_bytes(k, self.encoding) + output.extend([encode(k, smart_bytes(v, self.encoding)) for v in list_]) return '&'.join(output) @@ -648,7 +648,7 @@ class HttpResponse(object): def _get_content(self): if self.has_header('Content-Encoding'): return b''.join([str(e) for e in self._container]) - return b''.join([smart_str(e, self._charset) for e in self._container]) + return b''.join([smart_bytes(e, self._charset) for e in self._container]) def _set_content(self, value): if hasattr(value, '__iter__'): @@ -698,7 +698,7 @@ class HttpResponseRedirectBase(HttpResponse): raise SuspiciousOperation("Unsafe redirect to URL with protocol '%s'" % parsed.scheme) super(HttpResponseRedirectBase, self).__init__() self['Location'] = iri_to_uri(redirect_to) - + class HttpResponseRedirect(HttpResponseRedirectBase): status_code = 302 @@ -735,7 +735,7 @@ def get_host(request): return request.get_host() # It's neither necessary nor appropriate to use -# django.utils.encoding.smart_unicode for parsing URLs and form inputs. Thus, +# django.utils.encoding.smart_text for parsing URLs and form inputs. Thus, # this slightly more restricted function. def str_to_unicode(s, encoding): """ diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py index 0e28a55c3a..1987ee53bc 100644 --- a/django/http/multipartparser.py +++ b/django/http/multipartparser.py @@ -10,7 +10,7 @@ import cgi from django.conf import settings from django.core.exceptions import SuspiciousOperation from django.utils.datastructures import MultiValueDict -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils import six from django.utils.text import unescape_entities from django.core.files.uploadhandler import StopUpload, SkipFile, StopFutureHandlers @@ -151,7 +151,7 @@ class MultiPartParser(object): transfer_encoding = meta_data.get('content-transfer-encoding') if transfer_encoding is not None: transfer_encoding = transfer_encoding[0].strip() - field_name = force_unicode(field_name, encoding, errors='replace') + field_name = force_text(field_name, encoding, errors='replace') if item_type == FIELD: # This is a post field, we can just set it in the post @@ -165,13 +165,13 @@ class MultiPartParser(object): data = field_stream.read() self._post.appendlist(field_name, - force_unicode(data, encoding, errors='replace')) + force_text(data, encoding, errors='replace')) elif item_type == FILE: # This is a file, use the handler... file_name = disposition.get('filename') if not file_name: continue - file_name = force_unicode(file_name, encoding, errors='replace') + file_name = force_text(file_name, encoding, errors='replace') file_name = self.IE_sanitize(unescape_entities(file_name)) content_type = meta_data.get('content-type', ('',))[0].strip() @@ -245,7 +245,7 @@ class MultiPartParser(object): file_obj = handler.file_complete(counters[i]) if file_obj: # If it returns a file object, then set the files dict. - self._files.appendlist(force_unicode(old_field_name, + self._files.appendlist(force_text(old_field_name, self._encoding, errors='replace'), file_obj) diff --git a/django/template/base.py b/django/template/base.py index d5c2438500..661d8c092a 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -11,7 +11,7 @@ from django.utils.importlib import import_module from django.utils.itercompat import is_iterable from django.utils.text import (smart_split, unescape_string_literal, get_text_list) -from django.utils.encoding import smart_unicode, force_unicode, smart_str +from django.utils.encoding import smart_text, force_text, smart_bytes from django.utils.translation import ugettext_lazy, pgettext_lazy from django.utils.safestring import (SafeData, EscapeData, mark_safe, mark_for_escaping) @@ -89,7 +89,7 @@ class VariableDoesNotExist(Exception): return six.text_type(self).encode('utf-8') def __unicode__(self): - return self.msg % tuple([force_unicode(p, errors='replace') + return self.msg % tuple([force_text(p, errors='replace') for p in self.params]) class InvalidTemplateLibrary(Exception): @@ -117,7 +117,7 @@ class Template(object): def __init__(self, template_string, origin=None, name=''): try: - template_string = smart_unicode(template_string) + template_string = smart_text(template_string) except UnicodeDecodeError: raise TemplateEncodingError("Templates can only be constructed " "from unicode or UTF-8 strings.") @@ -831,7 +831,7 @@ class NodeList(list): bit = self.render_node(node, context) else: bit = node - bits.append(force_unicode(bit)) + bits.append(force_text(bit)) return mark_safe(''.join(bits)) def get_nodes_by_type(self, nodetype): @@ -849,7 +849,7 @@ class TextNode(Node): self.s = s def __repr__(self): - return "" % smart_str(self.s[:25], 'ascii', + return "" % smart_bytes(self.s[:25], 'ascii', errors='replace') def render(self, context): @@ -863,7 +863,7 @@ def _render_value_in_context(value, context): """ value = template_localtime(value, use_tz=context.use_tz) value = localize(value, use_l10n=context.use_l10n) - value = force_unicode(value) + value = force_text(value) if ((context.autoescape and not isinstance(value, SafeData)) or isinstance(value, EscapeData)): return escape(value) diff --git a/django/template/debug.py b/django/template/debug.py index 61674034d6..c7ac007b48 100644 --- a/django/template/debug.py +++ b/django/template/debug.py @@ -1,5 +1,5 @@ from django.template.base import Lexer, Parser, tag_re, NodeList, VariableNode, TemplateSyntaxError -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.html import escape from django.utils.safestring import SafeData, EscapeData from django.utils.formats import localize @@ -84,7 +84,7 @@ class DebugVariableNode(VariableNode): output = self.filter_expression.resolve(context) output = template_localtime(output, use_tz=context.use_tz) output = localize(output, use_l10n=context.use_l10n) - output = force_unicode(output) + output = force_text(output) except UnicodeDecodeError: return '' except Exception as e: diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index fa799cd46f..16801fd573 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -12,7 +12,7 @@ from django.template.base import Variable, Library, VariableDoesNotExist from django.conf import settings from django.utils import formats from django.utils.dateformat import format, time_format -from django.utils.encoding import force_unicode, iri_to_uri +from django.utils.encoding import force_text, iri_to_uri from django.utils.html import (conditional_escape, escapejs, fix_ampersands, escape, urlize as urlize_impl, linebreaks, strip_tags) from django.utils.http import urlquote @@ -38,7 +38,7 @@ def stringfilter(func): def _dec(*args, **kwargs): if args: args = list(args) - args[0] = force_unicode(args[0]) + args[0] = force_text(args[0]) if (isinstance(args[0], SafeData) and getattr(_dec._decorated_function, 'is_safe', False)): return mark_safe(func(*args, **kwargs)) @@ -139,7 +139,7 @@ def floatformat(text, arg=-1): """ try: - input_val = force_unicode(text) + input_val = force_text(text) d = Decimal(input_val) except UnicodeEncodeError: return '' @@ -147,7 +147,7 @@ def floatformat(text, arg=-1): if input_val in special_floats: return input_val try: - d = Decimal(force_unicode(float(text))) + d = Decimal(force_text(float(text))) except (ValueError, InvalidOperation, TypeError, UnicodeEncodeError): return '' try: @@ -192,7 +192,7 @@ def floatformat(text, arg=-1): @stringfilter def iriencode(value): """Escapes an IRI value for use in a URL.""" - return force_unicode(iri_to_uri(value)) + return force_text(iri_to_uri(value)) @register.filter(is_safe=True, needs_autoescape=True) @stringfilter @@ -462,7 +462,7 @@ def safeseq(value): individually, as safe, after converting them to unicode. Returns a list with the results. """ - return [mark_safe(force_unicode(obj)) for obj in value] + return [mark_safe(force_text(obj)) for obj in value] @register.filter(is_safe=True) @stringfilter @@ -521,7 +521,7 @@ def join(value, arg, autoescape=None): """ Joins a list with a string, like Python's ``str.join(list)``. """ - value = map(force_unicode, value) + value = map(force_text, value) if autoescape: value = [conditional_escape(v) for v in value] try: @@ -661,7 +661,7 @@ def unordered_list(value, autoescape=None): sublist = '\n%s
        \n%s\n%s
      \n%s' % (indent, sublist, indent, indent) output.append('%s
    • %s%s
    • ' % (indent, - escaper(force_unicode(title)), sublist)) + escaper(force_text(title)), sublist)) i += 1 return '\n'.join(output) value, converted = convert_old_style_list(value) @@ -901,4 +901,4 @@ def pprint(value): try: return pformat(value) except Exception as e: - return "Error in formatting: %s" % force_unicode(e, errors="replace") + return "Error in formatting: %s" % force_text(e, errors="replace") diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 7a00d60361..dca47a3da8 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -14,7 +14,7 @@ from django.template.base import (Node, NodeList, Template, Context, Library, VARIABLE_ATTRIBUTE_SEPARATOR, get_library, token_kwargs, kwarg_re) from django.template.smartif import IfParser, Literal from django.template.defaultfilters import date -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.safestring import mark_safe from django.utils.html import format_html from django.utils import six @@ -104,7 +104,7 @@ class FirstOfNode(Node): for var in self.vars: value = var.resolve(context, True) if value: - return smart_unicode(value) + return smart_text(value) return '' class ForNode(Node): @@ -393,7 +393,7 @@ class URLNode(Node): def render(self, context): from django.core.urlresolvers import reverse, NoReverseMatch args = [arg.resolve(context) for arg in self.args] - kwargs = dict([(smart_unicode(k, 'ascii'), v.resolve(context)) + kwargs = dict([(smart_text(k, 'ascii'), v.resolve(context)) for k, v in self.kwargs.items()]) view_name = self.view_name.resolve(context) diff --git a/django/templatetags/l10n.py b/django/templatetags/l10n.py index 1eac1de071..667de2470e 100644 --- a/django/templatetags/l10n.py +++ b/django/templatetags/l10n.py @@ -1,7 +1,7 @@ from django.template import Node from django.template import TemplateSyntaxError, Library from django.utils import formats -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text register = Library() @@ -11,7 +11,7 @@ def localize(value): Forces a value to be rendered as a localized value, regardless of the value of ``settings.USE_L10N``. """ - return force_unicode(formats.localize(value, use_l10n=True)) + return force_text(formats.localize(value, use_l10n=True)) @register.filter(is_safe=False) def unlocalize(value): @@ -19,7 +19,7 @@ def unlocalize(value): Forces a value to be rendered as a non-localized value, regardless of the value of ``settings.USE_L10N``. """ - return force_unicode(value) + return force_text(value) class LocalizeNode(Node): def __init__(self, nodelist, use_l10n): diff --git a/django/test/client.py b/django/test/client.py index a18b7f8853..ef80a7129a 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -19,7 +19,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_str +from django.utils.encoding import smart_bytes from django.utils.http import urlencode from django.utils.importlib import import_module from django.utils.itercompat import is_iterable @@ -108,7 +108,7 @@ def encode_multipart(boundary, data): as an application/octet-stream; otherwise, str(value) will be sent. """ lines = [] - to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET) + to_str = lambda s: smart_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) @@ -145,7 +145,7 @@ def encode_multipart(boundary, data): return '\r\n'.join(lines) def encode_file(boundary, key, file): - to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET) + to_str = lambda s: smart_bytes(s, settings.DEFAULT_CHARSET) content_type = mimetypes.guess_type(file.name)[0] if content_type is None: content_type = 'application/octet-stream' @@ -220,7 +220,7 @@ class RequestFactory(object): charset = match.group(1) else: charset = settings.DEFAULT_CHARSET - return smart_str(data, encoding=charset) + return smart_bytes(data, encoding=charset) def _get_path(self, parsed): # If there are parameters, add them @@ -291,7 +291,7 @@ class RequestFactory(object): def generic(self, method, path, data='', content_type='application/octet-stream', **extra): parsed = urlparse(path) - data = smart_str(data, settings.DEFAULT_CHARSET) + data = smart_bytes(data, settings.DEFAULT_CHARSET) r = { 'PATH_INFO': self._get_path(parsed), 'QUERY_STRING': parsed[4], diff --git a/django/test/html.py b/django/test/html.py index 2a1421bf17..143c3728be 100644 --- a/django/test/html.py +++ b/django/test/html.py @@ -5,7 +5,7 @@ Comparing two html documents. from __future__ import unicode_literals import re -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.html_parser import HTMLParser, HTMLParseError from django.utils import six @@ -25,7 +25,7 @@ class Element(object): def append(self, element): if isinstance(element, six.string_types): - element = force_unicode(element) + element = force_text(element) element = normalize_whitespace(element) if self.children: if isinstance(self.children[-1], six.string_types): diff --git a/django/test/testcases.py b/django/test/testcases.py index b60188bf30..3a0dc760c6 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_str, force_unicode +from django.utils.encoding import smart_bytes, force_text from django.utils import six from django.utils.unittest.util import safe_repr from django.views.static import serve @@ -398,7 +398,7 @@ class SimpleTestCase(ut2.TestCase): optional.clean(input) self.assertEqual(context_manager.exception.messages, errors) # test required inputs - error_required = [force_unicode(required.error_messages['required'])] + error_required = [force_text(required.error_messages['required'])] for e in EMPTY_VALUES: with self.assertRaises(ValidationError) as context_manager: required.clean(e) @@ -647,7 +647,7 @@ class TransactionTestCase(SimpleTestCase): self.assertEqual(response.status_code, status_code, msg_prefix + "Couldn't retrieve content: Response code was %d" " (expected %d)" % (response.status_code, status_code)) - enc_text = smart_str(text, response._charset) + enc_text = smart_bytes(text, response._charset) content = response.content if html: content = assert_and_parse_html(self, content, None, @@ -683,7 +683,7 @@ class TransactionTestCase(SimpleTestCase): self.assertEqual(response.status_code, status_code, msg_prefix + "Couldn't retrieve content: Response code was %d" " (expected %d)" % (response.status_code, status_code)) - enc_text = smart_str(text, response._charset) + enc_text = smart_bytes(text, response._charset) content = response.content if html: content = assert_and_parse_html(self, content, None, diff --git a/django/utils/_os.py b/django/utils/_os.py index 884689fefc..82dccd0efe 100644 --- a/django/utils/_os.py +++ b/django/utils/_os.py @@ -1,7 +1,7 @@ import os import stat from os.path import join, normcase, normpath, abspath, isabs, sep -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text try: WindowsError = WindowsError @@ -37,8 +37,8 @@ def safe_join(base, *paths): The final path must be located inside of the base path component (otherwise a ValueError is raised). """ - base = force_unicode(base) - paths = [force_unicode(p) for p in paths] + base = force_text(base) + paths = [force_text(p) for p in paths] final_path = abspathu(join(base, *paths)) base_path = abspathu(base) base_path_len = len(base_path) diff --git a/django/utils/cache.py b/django/utils/cache.py index 8b81a2ffa2..42b0de4ce6 100644 --- a/django/utils/cache.py +++ b/django/utils/cache.py @@ -23,7 +23,7 @@ import time from django.conf import settings from django.core.cache import get_cache -from django.utils.encoding import iri_to_uri, force_unicode +from django.utils.encoding import iri_to_uri, 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 @@ -169,7 +169,7 @@ def _i18n_cache_key_suffix(request, cache_key): # Windows is known to use non-standard, locale-dependant names. # User-defined tzinfo classes may return absolutely anything. # Hence this paranoid conversion to create a valid cache key. - tz_name = force_unicode(get_current_timezone_name(), errors='ignore') + tz_name = force_text(get_current_timezone_name(), errors='ignore') cache_key += '.%s' % tz_name.encode('ascii', 'ignore').replace(' ', '_') return cache_key diff --git a/django/utils/crypto.py b/django/utils/crypto.py index 9d46bdd793..1edbb43eb3 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_str +from django.utils.encoding import smart_bytes from django.utils.six.moves import xrange @@ -115,7 +115,7 @@ def _fast_hmac(key, msg, digest): A trimmed down version of Python's HMAC implementation """ dig1, dig2 = digest(), digest() - key = smart_str(key) + key = smart_bytes(key) if len(key) > dig1.block_size: key = digest(key).digest() key += chr(0) * (dig1.block_size - len(key)) @@ -141,8 +141,8 @@ def pbkdf2(password, salt, iterations, dklen=0, digest=None): assert iterations > 0 if not digest: digest = hashlib.sha256 - password = smart_str(password) - salt = smart_str(salt) + password = smart_bytes(password) + salt = smart_bytes(salt) hlen = digest().digest_size if not dklen: dklen = hlen diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py index c9a6138aed..6a91a370e5 100644 --- a/django/utils/dateformat.py +++ b/django/utils/dateformat.py @@ -20,7 +20,7 @@ import datetime from django.utils.dates import MONTHS, MONTHS_3, MONTHS_ALT, MONTHS_AP, WEEKDAYS, WEEKDAYS_ABBR from django.utils.tzinfo import LocalTimezone from django.utils.translation import ugettext as _ -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils import six from django.utils.timezone import is_aware, is_naive @@ -30,9 +30,9 @@ re_escaped = re.compile(r'\\(.)') class Formatter(object): def format(self, formatstr): pieces = [] - for i, piece in enumerate(re_formatchars.split(force_unicode(formatstr))): + for i, piece in enumerate(re_formatchars.split(force_text(formatstr))): if i % 2: - pieces.append(force_unicode(getattr(self, piece)())) + pieces.append(force_text(getattr(self, piece)())) elif piece: pieces.append(re_escaped.sub(r'\1', piece)) return ''.join(pieces) diff --git a/django/utils/encoding.py b/django/utils/encoding.py index b5f4b76507..eb60cfde8b 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -24,9 +24,13 @@ class DjangoUnicodeDecodeError(UnicodeDecodeError): class StrAndUnicode(object): """ - A class whose __str__ returns its __unicode__ as a UTF-8 bytestring. + A class that derives __str__ from __unicode__. - Useful as a mix-in. + On Python 2, __str__ returns the output of __unicode__ encoded as a UTF-8 + bytestring. On Python 3, __str__ returns the output of __unicode__. + + Useful as a mix-in. If you support Python 2 and 3 with a single code base, + you can inherit this mix-in and just define __unicode__. """ if six.PY3: def __str__(self): @@ -35,37 +39,36 @@ class StrAndUnicode(object): def __str__(self): return self.__unicode__().encode('utf-8') -def smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): +def smart_text(s, encoding='utf-8', strings_only=False, errors='strict'): """ - Returns a unicode object representing 's'. Treats bytestrings using the - 'encoding' codec. + Returns a text object representing 's' -- unicode on Python 2 and str on + Python 3. Treats bytestrings using the 'encoding' codec. If strings_only is True, don't convert (some) non-string-like objects. """ if isinstance(s, Promise): # The input is the result of a gettext_lazy() call. return s - return force_unicode(s, encoding, strings_only, errors) + return force_text(s, encoding, strings_only, errors) def is_protected_type(obj): """Determine if the object instance is of a protected type. Objects of protected types are preserved as-is when passed to - force_unicode(strings_only=True). + force_text(strings_only=True). """ return isinstance(obj, six.integer_types + (type(None), float, Decimal, datetime.datetime, datetime.date, datetime.time)) -def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): +def force_text(s, encoding='utf-8', strings_only=False, errors='strict'): """ - Similar to smart_unicode, except that lazy instances are resolved to + Similar to smart_text, except that lazy instances are resolved to strings, rather than kept as lazy objects. If strings_only is True, don't convert (some) non-string-like objects. """ - # Handle the common case first, saves 30-40% in performance when s - # is an instance of unicode. This function gets called often in that - # setting. + # Handle the common case first, saves 30-40% when s is an instance of + # six.text_type. This function gets called often in that setting. if isinstance(s, six.text_type): return s if strings_only and is_protected_type(s): @@ -92,7 +95,7 @@ def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): # without raising a further exception. We do an # approximation to what the Exception's standard str() # output should be. - s = ' '.join([force_unicode(arg, encoding, strings_only, + s = ' '.join([force_text(arg, encoding, strings_only, errors) for arg in s]) else: # Note: We use .decode() here, instead of six.text_type(s, encoding, @@ -108,21 +111,26 @@ def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): # working unicode method. Try to handle this without raising a # further exception by individually forcing the exception args # to unicode. - s = ' '.join([force_unicode(arg, encoding, strings_only, + s = ' '.join([force_text(arg, encoding, strings_only, errors) for arg in s]) return s -def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'): +def smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict'): """ Returns a bytestring version of 's', encoded as specified in 'encoding'. If strings_only is True, don't convert (some) non-string-like objects. """ + if isinstance(s, bytes): + if encoding == 'utf-8': + return s + else: + return s.decode('utf-8', errors).encode(encoding, errors) if strings_only and (s is None or isinstance(s, int)): return s if isinstance(s, Promise): return six.text_type(s).encode(encoding, errors) - elif not isinstance(s, six.string_types): + if not isinstance(s, six.string_types): try: if six.PY3: return six.text_type(s).encode(encoding) @@ -133,15 +141,25 @@ def smart_str(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_str(arg, encoding, strings_only, + return ' '.join([smart_bytes(arg, encoding, strings_only, errors) for arg in s]) return six.text_type(s).encode(encoding, errors) - elif isinstance(s, six.text_type): - return s.encode(encoding, errors) - elif s and encoding != 'utf-8': - return s.decode('utf-8', errors).encode(encoding, errors) else: - return s + return s.encode(encoding, errors) + +if six.PY3: + smart_str = smart_text +else: + smart_str = smart_bytes + # backwards compatibility for Python 2 + smart_unicode = smart_text + force_unicode = force_text + +smart_str.__doc__ = """\ +Apply smart_text in Python 3 and smart_bytes in Python 2. + +This is suitable for writing to sys.stdout (for instance). +""" def iri_to_uri(iri): """ @@ -168,7 +186,7 @@ def iri_to_uri(iri): # converted. if iri is None: return iri - return quote(smart_str(iri), safe=b"/#%[]=:;$&()+,!?*@'~") + return quote(smart_bytes(iri), safe=b"/#%[]=:;$&()+,!?*@'~") def filepath_to_uri(path): """Convert an file system path to a URI portion that is suitable for @@ -187,7 +205,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_str(path).replace("\\", "/"), safe=b"/~!*()'") + return quote(smart_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/feedgenerator.py b/django/utils/feedgenerator.py index 6498aaf57c..1bf43bf0a7 100644 --- a/django/utils/feedgenerator.py +++ b/django/utils/feedgenerator.py @@ -29,7 +29,7 @@ try: except ImportError: # Python 2 from urlparse import urlparse from django.utils.xmlutils import SimplerXMLGenerator -from django.utils.encoding import force_unicode, iri_to_uri +from django.utils.encoding import force_text, iri_to_uri from django.utils import datetime_safe from django.utils.timezone import is_aware @@ -81,12 +81,12 @@ class SyndicationFeed(object): def __init__(self, title, link, description, language=None, author_email=None, author_name=None, author_link=None, subtitle=None, categories=None, feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs): - to_unicode = lambda s: force_unicode(s, strings_only=True) + to_unicode = lambda s: force_text(s, strings_only=True) if categories: - categories = [force_unicode(c) for c in categories] + categories = [force_text(c) for c in categories] if ttl is not None: # Force ints to unicode - ttl = force_unicode(ttl) + ttl = force_text(ttl) self.feed = { 'title': to_unicode(title), 'link': iri_to_uri(link), @@ -114,12 +114,12 @@ class SyndicationFeed(object): objects except pubdate, which is a datetime.datetime object, and enclosure, which is an instance of the Enclosure class. """ - to_unicode = lambda s: force_unicode(s, strings_only=True) + to_unicode = lambda s: force_text(s, strings_only=True) if categories: categories = [to_unicode(c) for c in categories] if ttl is not None: # Force ints to unicode - ttl = force_unicode(ttl) + ttl = force_text(ttl) item = { 'title': to_unicode(title), 'link': iri_to_uri(link), diff --git a/django/utils/html.py b/django/utils/html.py index 4e888fc59b..7dd53a1353 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_str, force_unicode +from django.utils.encoding import smart_bytes, force_text from django.utils.functional import allow_lazy from django.utils import six from django.utils.text import normalize_newlines @@ -39,7 +39,7 @@ def escape(text): """ Returns the given text with ampersands, quotes and angle brackets encoded for use in HTML. """ - return mark_safe(force_unicode(text).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')) + return mark_safe(force_text(text).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')) escape = allow_lazy(escape, six.text_type) _base_js_escapes = ( @@ -63,7 +63,7 @@ _js_escapes = (_base_js_escapes + def escapejs(value): """Hex encodes characters for use in JavaScript strings.""" for bad, good in _js_escapes: - value = mark_safe(force_unicode(value).replace(bad, good)) + value = mark_safe(force_text(value).replace(bad, good)) return value escapejs = allow_lazy(escapejs, six.text_type) @@ -120,22 +120,22 @@ linebreaks = allow_lazy(linebreaks, six.text_type) def strip_tags(value): """Returns the given HTML with all tags stripped.""" - return re.sub(r'<[^>]*?>', '', force_unicode(value)) + return re.sub(r'<[^>]*?>', '', force_text(value)) strip_tags = allow_lazy(strip_tags) def strip_spaces_between_tags(value): """Returns the given HTML with spaces between tags removed.""" - return re.sub(r'>\s+<', '><', force_unicode(value)) + return re.sub(r'>\s+<', '><', force_text(value)) strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, six.text_type) def strip_entities(value): """Returns the given HTML with all entities (&something;) stripped.""" - return re.sub(r'&(?:\w+|#\d+);', '', force_unicode(value)) + return re.sub(r'&(?:\w+|#\d+);', '', force_text(value)) strip_entities = allow_lazy(strip_entities, six.text_type) def fix_ampersands(value): """Returns the given HTML with all unencoded ampersands encoded correctly.""" - return unencoded_ampersands_re.sub('&', force_unicode(value)) + return unencoded_ampersands_re.sub('&', force_text(value)) fix_ampersands = allow_lazy(fix_ampersands, six.text_type) def smart_urlquote(url): @@ -153,9 +153,9 @@ 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_str(url), safe=b'!*\'();:@&=+$,/?#[]~') + url = quote(smart_bytes(url), safe=b'!*\'();:@&=+$,/?#[]~') - return force_unicode(url) + return force_text(url) def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False): """ @@ -176,7 +176,7 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False): """ trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x safe_input = isinstance(text, SafeData) - words = word_split_re.split(force_unicode(text)) + words = word_split_re.split(force_text(text)) for i, word in enumerate(words): match = None if '.' in word or '@' in word or ':' in word: @@ -245,7 +245,7 @@ def clean_html(text): bottom of the text. """ from django.utils.text import normalize_newlines - text = normalize_newlines(force_unicode(text)) + text = normalize_newlines(force_text(text)) text = re.sub(r'<(/?)\s*b\s*>', '<\\1strong>', text) text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text) text = fix_ampersands(text) diff --git a/django/utils/http.py b/django/utils/http.py index 272e73f190..22e81a33d7 100644 --- a/django/utils/http.py +++ b/django/utils/http.py @@ -13,7 +13,7 @@ except ImportError: # Python 2 from email.utils import formatdate from django.utils.datastructures import MultiValueDict -from django.utils.encoding import smart_str, force_unicode +from django.utils.encoding import force_text, smart_str from django.utils.functional import allow_lazy from django.utils import six @@ -37,7 +37,7 @@ def urlquote(url, safe='/'): can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ - return force_unicode(urllib_parse.quote(smart_str(url), smart_str(safe))) + return force_text(urllib_parse.quote(smart_str(url), smart_str(safe))) urlquote = allow_lazy(urlquote, six.text_type) def urlquote_plus(url, safe=''): @@ -47,7 +47,7 @@ def urlquote_plus(url, safe=''): returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ - return force_unicode(urllib_parse.quote_plus(smart_str(url), smart_str(safe))) + return force_text(urllib_parse.quote_plus(smart_str(url), smart_str(safe))) urlquote_plus = allow_lazy(urlquote_plus, six.text_type) def urlunquote(quoted_url): @@ -55,7 +55,7 @@ def urlunquote(quoted_url): A wrapper for Python's urllib.unquote() function that can operate on the result of django.utils.http.urlquote(). """ - return force_unicode(urllib_parse.unquote(smart_str(quoted_url))) + return force_text(urllib_parse.unquote(smart_str(quoted_url))) urlunquote = allow_lazy(urlunquote, six.text_type) def urlunquote_plus(quoted_url): @@ -63,7 +63,7 @@ def urlunquote_plus(quoted_url): A wrapper for Python's urllib.unquote_plus() function that can operate on the result of django.utils.http.urlquote_plus(). """ - return force_unicode(urllib_parse.unquote_plus(smart_str(quoted_url))) + return force_text(urllib_parse.unquote_plus(smart_str(quoted_url))) urlunquote_plus = allow_lazy(urlunquote_plus, six.text_type) def urlencode(query, doseq=0): diff --git a/django/utils/text.py b/django/utils/text.py index 43056aa634..0838d79c65 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -7,13 +7,13 @@ from gzip import GzipFile from django.utils.six.moves import html_entities from io import BytesIO -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.functional import allow_lazy, SimpleLazyObject from django.utils import six from django.utils.translation import ugettext_lazy, ugettext as _, pgettext # Capitalizes the first letter of a string. -capfirst = lambda x: x and force_unicode(x)[0].upper() + force_unicode(x)[1:] +capfirst = lambda x: x and force_text(x)[0].upper() + force_text(x)[1:] capfirst = allow_lazy(capfirst, six.text_type) # Set up regular expressions @@ -26,7 +26,7 @@ def wrap(text, width): A word-wrap function that preserves existing line breaks and most spaces in the text. Expects that existing line breaks are posix newlines. """ - text = force_unicode(text) + text = force_text(text) def _generator(): it = iter(text.split(' ')) word = next(it) @@ -55,14 +55,14 @@ class Truncator(SimpleLazyObject): An object used to truncate text, either by characters or words. """ def __init__(self, text): - super(Truncator, self).__init__(lambda: force_unicode(text)) + super(Truncator, self).__init__(lambda: force_text(text)) def add_truncation_text(self, text, truncate=None): if truncate is None: truncate = pgettext( 'String to return when truncating text', '%(truncated_text)s...') - truncate = force_unicode(truncate) + truncate = force_text(truncate) if '%(truncated_text)s' in truncate: return truncate % {'truncated_text': text} # The truncation text didn't contain the %(truncated_text)s string @@ -226,7 +226,7 @@ def get_valid_filename(s): >>> get_valid_filename("john's portrait in 2004.jpg") 'johns_portrait_in_2004.jpg' """ - s = force_unicode(s).strip().replace(' ', '_') + s = force_text(s).strip().replace(' ', '_') return re.sub(r'(?u)[^-\w.]', '', s) get_valid_filename = allow_lazy(get_valid_filename, six.text_type) @@ -244,20 +244,20 @@ def get_text_list(list_, last_word=ugettext_lazy('or')): '' """ if len(list_) == 0: return '' - if len(list_) == 1: return force_unicode(list_[0]) + if len(list_) == 1: return force_text(list_[0]) return '%s %s %s' % ( # Translators: This string is used as a separator between list elements - _(', ').join([force_unicode(i) for i in list_][:-1]), - force_unicode(last_word), force_unicode(list_[-1])) + _(', ').join([force_text(i) for i in list_][:-1]), + force_text(last_word), force_text(list_[-1])) get_text_list = allow_lazy(get_text_list, six.text_type) def normalize_newlines(text): - return force_unicode(re.sub(r'\r\n|\r|\n', '\n', text)) + return force_text(re.sub(r'\r\n|\r|\n', '\n', text)) normalize_newlines = allow_lazy(normalize_newlines, six.text_type) def recapitalize(text): "Recapitalizes text, placing caps after end-of-sentence punctuation." - text = force_unicode(text).lower() + text = force_text(text).lower() capsRE = re.compile(r'(?:^|(?<=[\.\?\!] ))([a-z])') text = capsRE.sub(lambda x: x.group(1).upper(), text) return text @@ -330,7 +330,7 @@ def smart_split(text): >>> list(smart_split(r'A "\"funky\" style" test.')) ['A', '"\\"funky\\" style"', 'test.'] """ - text = force_unicode(text) + text = force_text(text) for bit in smart_split_re.finditer(text): yield bit.group(0) smart_split = allow_lazy(smart_split, six.text_type) diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index d31a7aebf1..febde404a5 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -3,7 +3,7 @@ Internationalization support. """ from __future__ import unicode_literals -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.functional import lazy from django.utils import six @@ -139,7 +139,7 @@ def _string_concat(*strings): Lazy variant of string concatenation, needed for translations that are constructed from multiple parts. """ - return ''.join([force_unicode(s) for s in strings]) + return ''.join([force_text(s) for s in strings]) string_concat = lazy(_string_concat, six.text_type) def get_language_info(lang_code): diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py index 0fabc707ce..5c51468204 100644 --- a/django/utils/translation/trans_null.py +++ b/django/utils/translation/trans_null.py @@ -3,7 +3,7 @@ # settings.USE_I18N = False can use this module rather than trans_real.py. from django.conf import settings -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.safestring import mark_safe, SafeData def ngettext(singular, plural, number): @@ -12,7 +12,7 @@ def ngettext(singular, plural, number): ngettext_lazy = ngettext def ungettext(singular, plural, number): - return force_unicode(ngettext(singular, plural, number)) + return force_text(ngettext(singular, plural, number)) def pgettext(context, message): return ugettext(message) @@ -44,7 +44,7 @@ def gettext(message): return result def ugettext(message): - return force_unicode(gettext(message)) + return force_text(gettext(message)) gettext_noop = gettext_lazy = _ = gettext diff --git a/django/utils/tzinfo.py b/django/utils/tzinfo.py index 05f4aa6d2f..c40b430411 100644 --- a/django/utils/tzinfo.py +++ b/django/utils/tzinfo.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals import time from datetime import timedelta, tzinfo -from django.utils.encoding import smart_unicode, smart_str, DEFAULT_LOCALE_ENCODING +from django.utils.encoding import smart_text, smart_bytes, DEFAULT_LOCALE_ENCODING # Python's doc say: "A tzinfo subclass must have an __init__() method that can # be called with no arguments". FixedOffset and LocalTimezone don't honor this @@ -53,7 +53,7 @@ class LocalTimezone(tzinfo): self._tzname = self.tzname(dt) def __repr__(self): - return smart_str(self._tzname) + return smart_bytes(self._tzname) def __getinitargs__(self): return self.__dt, @@ -72,7 +72,7 @@ class LocalTimezone(tzinfo): def tzname(self, dt): try: - return smart_unicode(time.tzname[self._isdst(dt)], + return smart_text(time.tzname[self._isdst(dt)], DEFAULT_LOCALE_ENCODING) except UnicodeDecodeError: return None diff --git a/django/views/debug.py b/django/views/debug.py index 08341fe145..b275ef9e73 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_unicode, smart_str +from django.utils.encoding import smart_text, smart_bytes from django.utils import six HIDDEN_SETTINGS = re.compile('API|TOKEN|KEY|SECRET|PASS|PROFANITIES_LIST|SIGNATURE') @@ -256,7 +256,7 @@ class ExceptionReporter(object): end = getattr(self.exc_value, 'end', None) if start is not None and end is not None: unicode_str = self.exc_value.args[1] - unicode_hint = smart_unicode(unicode_str[max(start-5, 0):min(end+5, len(unicode_str))], 'ascii', errors='replace') + unicode_hint = smart_text(unicode_str[max(start-5, 0):min(end+5, len(unicode_str))], 'ascii', errors='replace') from django import get_version c = { 'is_email': self.is_email, @@ -278,7 +278,7 @@ class ExceptionReporter(object): if self.exc_type: c['exception_type'] = self.exc_type.__name__ if self.exc_value: - c['exception_value'] = smart_unicode(self.exc_value, errors='replace') + c['exception_value'] = smart_text(self.exc_value, errors='replace') if frames: c['lastframe'] = frames[-1] return c @@ -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_str(exception, errors='replace'), + 'reason': smart_bytes(exception, errors='replace'), 'request': request, 'settings': get_safe_settings(), }) diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py index 22a72bc433..08b7318738 100644 --- a/django/views/generic/dates.py +++ b/django/views/generic/dates.py @@ -5,7 +5,7 @@ from django.conf import settings from django.db import models from django.core.exceptions import ImproperlyConfigured from django.http import Http404 -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.functional import cached_property from django.utils.translation import ugettext as _ from django.utils import timezone @@ -365,7 +365,7 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View): is_empty = len(qs) == 0 if paginate_by is None else not qs.exists() if is_empty: raise Http404(_("No %(verbose_name_plural)s available") % { - 'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural) + 'verbose_name_plural': force_text(qs.model._meta.verbose_name_plural) }) return qs @@ -380,7 +380,7 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View): date_list = queryset.dates(date_field, date_type)[::-1] if date_list is not None and not date_list and not allow_empty: - name = force_unicode(queryset.model._meta.verbose_name_plural) + name = force_text(queryset.model._meta.verbose_name_plural) raise Http404(_("No %(verbose_name_plural)s available") % {'verbose_name_plural': name}) diff --git a/django/views/i18n.py b/django/views/i18n.py index b0f64f4902..00ef224254 100644 --- a/django/views/i18n.py +++ b/django/views/i18n.py @@ -6,7 +6,7 @@ from django.conf import settings from django.utils import importlib from django.utils.translation import check_for_language, activate, to_locale, get_language from django.utils.text import javascript_quote -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.formats import get_format_modules, get_format from django.utils import six @@ -54,9 +54,9 @@ def get_formats(): src = [] for k, v in result.items(): if isinstance(v, (six.string_types, int)): - src.append("formats['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(smart_unicode(v)))) + src.append("formats['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(smart_text(v)))) elif isinstance(v, (tuple, list)): - v = [javascript_quote(smart_unicode(value)) for value in v] + v = [javascript_quote(smart_text(value)) for value in v] src.append("formats['%s'] = ['%s'];\n" % (javascript_quote(k), "', '".join(v))) return ''.join(src) diff --git a/docs/howto/custom-model-fields.txt b/docs/howto/custom-model-fields.txt index 706cc25129..e73ef9aa42 100644 --- a/docs/howto/custom-model-fields.txt +++ b/docs/howto/custom-model-fields.txt @@ -688,7 +688,7 @@ smoothly: 2. Put a :meth:`__str__` or :meth:`__unicode__` method on the class you're wrapping up as a field. There are a lot of places where the default behavior of the field code is to call - :func:`~django.utils.encoding.force_unicode` on the value. (In our + :func:`~django.utils.encoding.force_text` on the value. (In our examples in this document, ``value`` would be a ``Hand`` instance, not a ``HandField``). So if your :meth:`__unicode__` method automatically converts to the string form of your Python object, you can save yourself diff --git a/docs/ref/databases.txt b/docs/ref/databases.txt index 74e6b48f07..92b5665bea 100644 --- a/docs/ref/databases.txt +++ b/docs/ref/databases.txt @@ -238,7 +238,7 @@ to you, the developer, to handle the fact that you will receive bytestrings if you configure your table(s) to use ``utf8_bin`` collation. Django itself should mostly work smoothly with such columns (except for the ``contrib.sessions`` ``Session`` and ``contrib.admin`` ``LogEntry`` tables described below), but -your code must be prepared to call ``django.utils.encoding.smart_unicode()`` at +your code must be prepared to call ``django.utils.encoding.smart_text()`` at times if it really wants to work with consistent data -- Django will not do this for you (the database backend layer and the model population layer are separated internally so the database layer doesn't know it needs to make this diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt index 509ea9d30e..14541ad0d1 100644 --- a/docs/ref/models/instances.txt +++ b/docs/ref/models/instances.txt @@ -453,9 +453,9 @@ using ``__str__()`` like this:: last_name = models.CharField(max_length=50) def __str__(self): - # Note use of django.utils.encoding.smart_str() here because + # Note use of django.utils.encoding.smart_bytes() here because # first_name and last_name will be unicode strings. - return smart_str('%s %s' % (self.first_name, self.last_name)) + return smart_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 72d60453c3..531ff33da2 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_str(key)]) + return ':'.join([key_prefix, str(version), smart_bytes(key)]) You may use any key function you want, as long as it has the same argument signature. diff --git a/docs/ref/unicode.txt b/docs/ref/unicode.txt index b9253e70b3..ffab647379 100644 --- a/docs/ref/unicode.txt +++ b/docs/ref/unicode.txt @@ -129,7 +129,7 @@ Conversion functions The ``django.utils.encoding`` module contains a few functions that are handy for converting back and forth between Unicode and bytestrings. -* ``smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict')`` +* ``smart_text(s, encoding='utf-8', strings_only=False, errors='strict')`` converts its input to a Unicode string. The ``encoding`` parameter specifies the input encoding. (For example, Django uses this internally when processing form input data, which might not be UTF-8 encoded.) The @@ -139,27 +139,27 @@ for converting back and forth between Unicode and bytestrings. that are accepted by Python's ``unicode()`` function for its error handling. - If you pass ``smart_unicode()`` an object that has a ``__unicode__`` + If you pass ``smart_text()`` an object that has a ``__unicode__`` method, it will use that method to do the conversion. -* ``force_unicode(s, encoding='utf-8', strings_only=False, - errors='strict')`` is identical to ``smart_unicode()`` in almost all +* ``force_text(s, encoding='utf-8', strings_only=False, + errors='strict')`` is identical to ``smart_text()`` in almost all cases. The difference is when the first argument is a :ref:`lazy - translation ` instance. While ``smart_unicode()`` - preserves lazy translations, ``force_unicode()`` forces those objects to a + translation ` instance. While ``smart_text()`` + preserves lazy translations, ``force_text()`` forces those objects to a Unicode string (causing the translation to occur). Normally, you'll want - to use ``smart_unicode()``. However, ``force_unicode()`` is useful in + to use ``smart_text()``. However, ``force_text()`` is useful in template tags and filters that absolutely *must* have a string to work with, not just something that can be converted to a string. -* ``smart_str(s, encoding='utf-8', strings_only=False, errors='strict')`` - is essentially the opposite of ``smart_unicode()``. It forces the first +* ``smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict')`` + is essentially the opposite of ``smart_text()``. It forces the first argument to a bytestring. The ``strings_only`` parameter has the same - behavior as for ``smart_unicode()`` and ``force_unicode()``. This is + behavior as for ``smart_text()`` and ``force_text()``. This is slightly different semantics from Python's builtin ``str()`` function, but the difference is needed in a few places within Django's internals. -Normally, you'll only need to use ``smart_unicode()``. Call it as early as +Normally, you'll only need to use ``smart_text()``. Call it as early as possible on any input data that might be either Unicode or a bytestring, and from then on, you can treat the result as always being Unicode. @@ -324,7 +324,7 @@ A couple of tips to remember when writing your own template tags and filters: * Always return Unicode strings from a template tag's ``render()`` method and from template filters. -* Use ``force_unicode()`` in preference to ``smart_unicode()`` in these +* Use ``force_text()`` in preference to ``smart_text()`` in these places. Tag rendering and filter calls occur as the template is being rendered, so there is no advantage to postponing the conversion of lazy translation objects into strings. It's easier to work solely with Unicode diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt index 5157c399da..b6cb1035d3 100644 --- a/docs/ref/utils.txt +++ b/docs/ref/utils.txt @@ -178,33 +178,53 @@ The functions defined in this module share the following properties: .. class:: StrAndUnicode - A class whose ``__str__`` returns its ``__unicode__`` as a UTF-8 - bytestring. Useful as a mix-in. + A class that derives ``__str__`` from ``__unicode__``. -.. function:: smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict') + On Python 2, ``__str__`` returns the output of ``__unicode__`` encoded as + a UTF-8 bytestring. On Python 3, ``__str__`` returns the output of + ``__unicode__``. - Returns a ``unicode`` object representing ``s``. Treats bytestrings using - the 'encoding' codec. + Useful as a mix-in. If you support Python 2 and 3 with a single code base, + you can inherit this mix-in and just define ``__unicode__``. + +.. function:: smart_text(s, encoding='utf-8', strings_only=False, errors='strict') + + .. versionadded:: 1.5 + + Returns a text object representing ``s`` -- ``unicode`` on Python 2 and + ``str`` on Python 3. Treats bytestrings using the ``encoding`` codec. If ``strings_only`` is ``True``, don't convert (some) non-string-like objects. +.. function:: smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict') + + Historical name of :func:`smart_text`. Only available under Python 2. + .. function:: is_protected_type(obj) Determine if the object instance is of a protected type. Objects of protected types are preserved as-is when passed to - ``force_unicode(strings_only=True)``. + ``force_text(strings_only=True)``. -.. function:: force_unicode(s, encoding='utf-8', strings_only=False, errors='strict') +.. function:: force_text(s, encoding='utf-8', strings_only=False, errors='strict') - Similar to ``smart_unicode``, except that lazy instances are resolved to + .. versionadded:: 1.5 + + Similar to ``smart_text``, except that lazy instances are resolved to strings, rather than kept as lazy objects. If ``strings_only`` is ``True``, don't convert (some) non-string-like objects. -.. function:: smart_str(s, encoding='utf-8', strings_only=False, errors='strict') +.. function:: force_unicode(s, encoding='utf-8', strings_only=False, errors='strict') + + Historical name of :func:`force_text`. Only available under Python 2. + +.. function:: smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict') + + .. versionadded:: 1.5 Returns a bytestring version of ``s``, encoded as specified in ``encoding``. @@ -212,6 +232,14 @@ The functions defined in this module share the following properties: If ``strings_only`` is ``True``, don't convert (some) non-string-like objects. +.. function:: smart_str(s, encoding='utf-8', strings_only=False, errors='strict') + + Alias of :func:`smart_bytes` on Python 2 and :func:`smart_text` on Python + 3. This function always returns a :class:`str`. + + For instance, this is suitable for writing to :attr:`sys.stdout` on + Python 2 and 3. + .. function:: iri_to_uri(iri) Convert an Internationalized Resource Identifier (IRI) portion to a URI @@ -406,7 +434,7 @@ escaping HTML. Returns the given text with ampersands, quotes and angle brackets encoded for use in HTML. The input is first passed through - :func:`~django.utils.encoding.force_unicode` and the output has + :func:`~django.utils.encoding.force_text` and the output has :func:`~django.utils.safestring.mark_safe` applied. .. function:: conditional_escape(text) @@ -448,7 +476,7 @@ escaping HTML. interpolation, some of the formatting options provided by `str.format`_ (e.g. number formatting) will not work, since all arguments are passed through :func:`conditional_escape` which (ultimately) calls - :func:`~django.utils.encoding.force_unicode` on the values. + :func:`~django.utils.encoding.force_text` on the values. .. _str.format: http://docs.python.org/library/stdtypes.html#str.format diff --git a/docs/releases/1.5.txt b/docs/releases/1.5.txt index d58d3cadf4..f789ebde40 100644 --- a/docs/releases/1.5.txt +++ b/docs/releases/1.5.txt @@ -161,7 +161,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_str` utility to encode the strings. +:func:`~django.utils.encoding.smart_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 d0bd9f6992..219b6c7795 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_str(key)]) + return ':'.join([key_prefix, str(version), smart_bytes(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/docs/topics/serialization.txt b/docs/topics/serialization.txt index 505ac17f09..ac1a77ed98 100644 --- a/docs/topics/serialization.txt +++ b/docs/topics/serialization.txt @@ -173,12 +173,12 @@ In particular, :ref:`lazy translation objects ` need a import json from django.utils.functional import Promise - from django.utils.encoding import force_unicode + from django.utils.encoding import force_text class LazyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, Promise): - return force_unicode(obj) + return force_text(obj) return super(LazyEncoder, self).default(obj) .. _special encoder: http://docs.python.org/library/json.html#encoders-and-decoders diff --git a/tests/modeltests/field_subclassing/fields.py b/tests/modeltests/field_subclassing/fields.py index a21085de9d..0d4ff98aa7 100644 --- a/tests/modeltests/field_subclassing/fields.py +++ b/tests/modeltests/field_subclassing/fields.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import json from django.db import models -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils import six @@ -16,7 +16,7 @@ class Small(object): self.first, self.second = first, second def __unicode__(self): - return '%s%s' % (force_unicode(self.first), force_unicode(self.second)) + return '%s%s' % (force_text(self.first), force_text(self.second)) def __str__(self): return six.text_type(self).encode('utf-8') @@ -46,9 +46,9 @@ class SmallField(models.Field): def get_prep_lookup(self, lookup_type, value): if lookup_type == 'exact': - return force_unicode(value) + return force_text(value) if lookup_type == 'in': - return [force_unicode(v) for v in value] + return [force_text(v) for v in value] if lookup_type == 'isnull': return [] raise TypeError('Invalid lookup type: %r' % lookup_type) diff --git a/tests/modeltests/field_subclassing/models.py b/tests/modeltests/field_subclassing/models.py index 5e3c376976..2df9664cdc 100644 --- a/tests/modeltests/field_subclassing/models.py +++ b/tests/modeltests/field_subclassing/models.py @@ -5,7 +5,7 @@ Tests for field subclassing. from __future__ import absolute_import from django.db import models -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from .fields import SmallField, SmallerField, JSONField @@ -15,7 +15,7 @@ class MyModel(models.Model): data = SmallField('small field') def __unicode__(self): - return force_unicode(self.name) + return force_text(self.name) class OtherModel(models.Model): data = SmallerField() diff --git a/tests/regressiontests/admin_filters/tests.py b/tests/regressiontests/admin_filters/tests.py index 72cc5d7ee5..b92c2f4c8b 100644 --- a/tests/regressiontests/admin_filters/tests.py +++ b/tests/regressiontests/admin_filters/tests.py @@ -10,7 +10,7 @@ from django.contrib.auth.models import User from django.core.exceptions import ImproperlyConfigured from django.test import TestCase, RequestFactory from django.test.utils import override_settings -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from .models import Book, Department, Employee @@ -160,7 +160,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][4] - self.assertEqual(force_unicode(filterspec.title), 'date registered') + self.assertEqual(force_text(filterspec.title), 'date registered') choice = select_by(filterspec.choices(changelist), "display", "Today") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?date_registered__gte=%s' @@ -181,7 +181,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][4] - self.assertEqual(force_unicode(filterspec.title), 'date registered') + self.assertEqual(force_text(filterspec.title), 'date registered') choice = select_by(filterspec.choices(changelist), "display", "This month") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?date_registered__gte=%s' @@ -202,7 +202,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][4] - self.assertEqual(force_unicode(filterspec.title), 'date registered') + self.assertEqual(force_text(filterspec.title), 'date registered') choice = select_by(filterspec.choices(changelist), "display", "This year") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?date_registered__gte=%s' @@ -219,7 +219,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][4] - self.assertEqual(force_unicode(filterspec.title), 'date registered') + self.assertEqual(force_text(filterspec.title), 'date registered') choice = select_by(filterspec.choices(changelist), "display", "Past 7 days") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?date_registered__gte=%s' @@ -243,7 +243,7 @@ class ListFiltersTests(TestCase): # Make sure the last choice is None and is selected filterspec = changelist.get_filters(request)[0][0] - self.assertEqual(force_unicode(filterspec.title), 'year') + self.assertEqual(force_text(filterspec.title), 'year') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[-1]['selected'], True) self.assertEqual(choices[-1]['query_string'], '?year__isnull=True') @@ -253,7 +253,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][0] - self.assertEqual(force_unicode(filterspec.title), 'year') + self.assertEqual(force_text(filterspec.title), 'year') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[2]['selected'], True) self.assertEqual(choices[2]['query_string'], '?year=2002') @@ -270,7 +270,7 @@ class ListFiltersTests(TestCase): # Make sure the last choice is None and is selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), 'Verbose Author') + self.assertEqual(force_text(filterspec.title), 'Verbose Author') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[-1]['selected'], True) self.assertEqual(choices[-1]['query_string'], '?author__isnull=True') @@ -280,7 +280,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), 'Verbose Author') + self.assertEqual(force_text(filterspec.title), 'Verbose Author') # order of choices depends on User model, which has no order choice = select_by(filterspec.choices(changelist), "display", "alfred") self.assertEqual(choice['selected'], True) @@ -298,7 +298,7 @@ class ListFiltersTests(TestCase): # Make sure the last choice is None and is selected filterspec = changelist.get_filters(request)[0][2] - self.assertEqual(force_unicode(filterspec.title), 'Verbose Contributors') + self.assertEqual(force_text(filterspec.title), 'Verbose Contributors') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[-1]['selected'], True) self.assertEqual(choices[-1]['query_string'], '?contributors__isnull=True') @@ -308,7 +308,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][2] - self.assertEqual(force_unicode(filterspec.title), 'Verbose Contributors') + self.assertEqual(force_text(filterspec.title), 'Verbose Contributors') choice = select_by(filterspec.choices(changelist), "display", "bob") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?contributors__id__exact=%d' % self.bob.pk) @@ -326,7 +326,7 @@ class ListFiltersTests(TestCase): # Make sure the last choice is None and is selected filterspec = changelist.get_filters(request)[0][0] - self.assertEqual(force_unicode(filterspec.title), 'book') + self.assertEqual(force_text(filterspec.title), 'book') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[-1]['selected'], True) self.assertEqual(choices[-1]['query_string'], '?books_authored__isnull=True') @@ -336,7 +336,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][0] - self.assertEqual(force_unicode(filterspec.title), 'book') + self.assertEqual(force_text(filterspec.title), 'book') choice = select_by(filterspec.choices(changelist), "display", self.bio_book.title) self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?books_authored__id__exact=%d' % self.bio_book.pk) @@ -351,7 +351,7 @@ class ListFiltersTests(TestCase): # Make sure the last choice is None and is selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), 'book') + self.assertEqual(force_text(filterspec.title), 'book') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[-1]['selected'], True) self.assertEqual(choices[-1]['query_string'], '?books_contributed__isnull=True') @@ -361,7 +361,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), 'book') + self.assertEqual(force_text(filterspec.title), 'book') choice = select_by(filterspec.choices(changelist), "display", self.django_book.title) self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?books_contributed__id__exact=%d' % self.django_book.pk) @@ -387,7 +387,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][3] - self.assertEqual(force_unicode(filterspec.title), 'is best seller') + self.assertEqual(force_text(filterspec.title), 'is best seller') choice = select_by(filterspec.choices(changelist), "display", "No") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?is_best_seller__exact=0') @@ -401,7 +401,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][3] - self.assertEqual(force_unicode(filterspec.title), 'is best seller') + self.assertEqual(force_text(filterspec.title), 'is best seller') choice = select_by(filterspec.choices(changelist), "display", "Yes") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?is_best_seller__exact=1') @@ -415,7 +415,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][3] - self.assertEqual(force_unicode(filterspec.title), 'is best seller') + self.assertEqual(force_text(filterspec.title), 'is best seller') choice = select_by(filterspec.choices(changelist), "display", "Unknown") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?is_best_seller__isnull=True') @@ -434,7 +434,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), 'publication decade') + self.assertEqual(force_text(filterspec.title), 'publication decade') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[0]['display'], 'All') self.assertEqual(choices[0]['selected'], True) @@ -451,7 +451,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), 'publication decade') + self.assertEqual(force_text(filterspec.title), 'publication decade') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[1]['display'], 'the 1980\'s') self.assertEqual(choices[1]['selected'], True) @@ -468,7 +468,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), 'publication decade') + self.assertEqual(force_text(filterspec.title), 'publication decade') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[2]['display'], 'the 1990\'s') self.assertEqual(choices[2]['selected'], True) @@ -485,7 +485,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), 'publication decade') + self.assertEqual(force_text(filterspec.title), 'publication decade') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[3]['display'], 'the 2000\'s') self.assertEqual(choices[3]['selected'], True) @@ -502,14 +502,14 @@ class ListFiltersTests(TestCase): # Make sure the correct choices are selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), 'publication decade') + self.assertEqual(force_text(filterspec.title), 'publication decade') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[3]['display'], 'the 2000\'s') self.assertEqual(choices[3]['selected'], True) self.assertEqual(choices[3]['query_string'], '?publication-decade=the+00s&author__id__exact=%s' % self.alfred.pk) filterspec = changelist.get_filters(request)[0][0] - self.assertEqual(force_unicode(filterspec.title), 'Verbose Author') + self.assertEqual(force_text(filterspec.title), 'Verbose Author') choice = select_by(filterspec.choices(changelist), "display", "alfred") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?publication-decade=the+00s&author__id__exact=%s' % self.alfred.pk) @@ -561,7 +561,7 @@ class ListFiltersTests(TestCase): changelist = self.get_changelist(request, Book, modeladmin) filterspec = changelist.get_filters(request)[0][0] - self.assertEqual(force_unicode(filterspec.title), 'publication decade') + self.assertEqual(force_text(filterspec.title), 'publication decade') choices = list(filterspec.choices(changelist)) self.assertEqual(len(choices), 3) @@ -591,7 +591,7 @@ class ListFiltersTests(TestCase): self.assertEqual(list(queryset), [self.bio_book]) filterspec = changelist.get_filters(request)[0][-1] - self.assertEqual(force_unicode(filterspec.title), 'number') + self.assertEqual(force_text(filterspec.title), 'number') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[2]['selected'], True) self.assertEqual(choices[2]['query_string'], '?no=207') @@ -614,7 +614,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][0] - self.assertEqual(force_unicode(filterspec.title), 'publication decade') + self.assertEqual(force_text(filterspec.title), 'publication decade') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[2]['display'], 'the 1990\'s') self.assertEqual(choices[2]['selected'], True) @@ -631,7 +631,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][0] - self.assertEqual(force_unicode(filterspec.title), 'publication decade') + self.assertEqual(force_text(filterspec.title), 'publication decade') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[2]['display'], 'the 1990\'s') self.assertEqual(choices[2]['selected'], True) @@ -657,7 +657,7 @@ class ListFiltersTests(TestCase): self.assertEqual(list(queryset), [jack, john]) filterspec = changelist.get_filters(request)[0][-1] - self.assertEqual(force_unicode(filterspec.title), 'department') + self.assertEqual(force_text(filterspec.title), 'department') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[0]['display'], 'All') @@ -682,7 +682,7 @@ class ListFiltersTests(TestCase): self.assertEqual(list(queryset), [john]) filterspec = changelist.get_filters(request)[0][-1] - self.assertEqual(force_unicode(filterspec.title), 'department') + self.assertEqual(force_text(filterspec.title), 'department') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[0]['display'], 'All') diff --git a/tests/regressiontests/admin_views/admin.py b/tests/regressiontests/admin_views/admin.py index 6ec933f89b..293ddfebf6 100644 --- a/tests/regressiontests/admin_views/admin.py +++ b/tests/regressiontests/admin_views/admin.py @@ -630,7 +630,7 @@ site.register(UndeletableObject, UndeletableObjectAdmin) # related OneToOne object registered in admin # related OneToOne object not registered in admin # when deleting Book so as exercise all four troublesome (w.r.t escaping -# and calling force_unicode to avoid problems on Python 2.3) paths through +# and calling force_text to avoid problems on Python 2.3) paths through # contrib.admin.util's get_deleted_objects function. site.register(Book, inlines=[ChapterInline]) site.register(Promo) diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index 264ef74abd..8fc749aaa2 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -28,7 +28,7 @@ from django.test.utils import (get_warnings_state, restore_warnings_state, from django.utils import timezone, translation, unittest from django.utils.cache import (patch_vary_headers, get_cache_key, learn_cache_key, patch_cache_control, patch_response_headers) -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.views.decorators.cache import cache_page from .models import Poll, expensive_calculation @@ -1307,7 +1307,7 @@ class CacheI18nTest(TestCase): request = self._get_request() # This is tightly coupled to the implementation, # but it's the most straightforward way to test the key. - tz = force_unicode(timezone.get_current_timezone_name(), errors='ignore') + tz = force_text(timezone.get_current_timezone_name(), errors='ignore') tz = tz.encode('ascii', 'ignore').replace(' ', '_') response = HttpResponse() key = learn_cache_key(request, response) @@ -1319,7 +1319,7 @@ class CacheI18nTest(TestCase): def test_cache_key_no_i18n (self): request = self._get_request() lang = translation.get_language() - tz = force_unicode(timezone.get_current_timezone_name(), errors='ignore') + tz = force_text(timezone.get_current_timezone_name(), errors='ignore') tz = tz.encode('ascii', 'ignore').replace(' ', '_') response = HttpResponse() key = learn_cache_key(request, response) diff --git a/tests/regressiontests/forms/tests/extra.py b/tests/regressiontests/forms/tests/extra.py index 28b6c12453..e0e62858d0 100644 --- a/tests/regressiontests/forms/tests/extra.py +++ b/tests/regressiontests/forms/tests/extra.py @@ -9,7 +9,7 @@ from django.forms.extras import SelectDateWidget from django.forms.util import ErrorList from django.test import TestCase from django.utils import translation -from django.utils.encoding import force_unicode, smart_unicode +from django.utils.encoding import force_text, smart_text from .error_messages import AssertFormErrorsMixin @@ -551,7 +551,7 @@ class FormsExtraTestCase(TestCase, AssertFormErrorsMixin): f = GenericIPAddressField(unpack_ipv4=True) self.assertEqual(f.clean('::ffff:0a0a:0a0a'), '10.10.10.10') - def test_smart_unicode(self): + def test_smart_text(self): class Test: def __str__(self): return 'ŠĐĆŽćžšđ'.encode('utf-8') @@ -562,10 +562,10 @@ class FormsExtraTestCase(TestCase, AssertFormErrorsMixin): def __unicode__(self): return '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111' - self.assertEqual(smart_unicode(Test()), '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111') - self.assertEqual(smart_unicode(TestU()), '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111') - self.assertEqual(smart_unicode(1), '1') - self.assertEqual(smart_unicode('foo'), 'foo') + self.assertEqual(smart_text(Test()), '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111') + self.assertEqual(smart_text(TestU()), '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111') + self.assertEqual(smart_text(1), '1') + self.assertEqual(smart_text('foo'), 'foo') def test_accessing_clean(self): class UserForm(Form): @@ -591,7 +591,7 @@ class FormsExtraTestCase(TestCase, AssertFormErrorsMixin): def as_divs(self): if not self: return '' - return '
      %s
      ' % ''.join(['
      %s
      ' % force_unicode(e) for e in self]) + return '
      %s
      ' % ''.join(['
      %s
      ' % force_text(e) for e in self]) class CommentForm(Form): name = CharField(max_length=50, required=False) diff --git a/tests/regressiontests/signing/tests.py b/tests/regressiontests/signing/tests.py index f3fe5f3ec7..2368405060 100644 --- a/tests/regressiontests/signing/tests.py +++ b/tests/regressiontests/signing/tests.py @@ -4,7 +4,7 @@ import time from django.core import signing from django.test import TestCase -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text class TestSigner(TestCase): @@ -48,7 +48,7 @@ class TestSigner(TestCase): ) for example in examples: self.assertNotEqual( - force_unicode(example), force_unicode(signer.sign(example))) + force_text(example), force_text(signer.sign(example))) self.assertEqual(example, signer.unsign(signer.sign(example))) def unsign_detects_tampering(self): diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py index 2c038e1713..19951f100b 100644 --- a/tests/regressiontests/staticfiles_tests/tests.py +++ b/tests/regressiontests/staticfiles_tests/tests.py @@ -17,7 +17,7 @@ from django.core.files.storage import default_storage from django.core.management import call_command from django.test import TestCase from django.test.utils import override_settings -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_text from django.utils.functional import empty from django.utils._os import rmtree_errorhandler from django.utils import six @@ -80,7 +80,7 @@ class BaseStaticFilesTestCase(object): os.unlink(self._backup_filepath) def assertFileContains(self, filepath, text): - self.assertIn(text, self._get_file(smart_unicode(filepath)), + self.assertIn(text, self._get_file(smart_text(filepath)), "'%s' not in '%s'" % (text, filepath)) def assertFileNotFound(self, filepath): @@ -199,7 +199,7 @@ class TestFindStatic(CollectionTestCase, TestDefaults): out.seek(0) lines = [l.strip() for l in out.readlines()] contents = codecs.open( - smart_unicode(lines[1].strip()), "r", "utf-8").read() + smart_text(lines[1].strip()), "r", "utf-8").read() return contents def test_all_files(self):