[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.
This commit is contained in:
Aymeric Augustin 2012-07-21 10:00:10 +02:00
parent ee191715ea
commit c5ef65bcf3
125 changed files with 629 additions and 583 deletions

View File

@ -7,7 +7,7 @@ from django.contrib.admin import helpers
from django.contrib.admin.util import get_deleted_objects, model_ngettext from django.contrib.admin.util import get_deleted_objects, model_ngettext
from django.db import router from django.db import router
from django.template.response import TemplateResponse 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 _ from django.utils.translation import ugettext_lazy, ugettext as _
def delete_selected(modeladmin, request, queryset): def delete_selected(modeladmin, request, queryset):
@ -42,7 +42,7 @@ def delete_selected(modeladmin, request, queryset):
n = queryset.count() n = queryset.count()
if n: if n:
for obj in queryset: for obj in queryset:
obj_display = force_unicode(obj) obj_display = force_text(obj)
modeladmin.log_deletion(request, obj, obj_display) modeladmin.log_deletion(request, obj, obj_display)
queryset.delete() queryset.delete()
modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % { modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % {
@ -52,9 +52,9 @@ def delete_selected(modeladmin, request, queryset):
return None return None
if len(queryset) == 1: if len(queryset) == 1:
objects_name = force_unicode(opts.verbose_name) objects_name = force_text(opts.verbose_name)
else: else:
objects_name = force_unicode(opts.verbose_name_plural) objects_name = force_text(opts.verbose_name_plural)
if perms_needed or protected: if perms_needed or protected:
title = _("Cannot delete %(name)s") % {"name": objects_name} title = _("Cannot delete %(name)s") % {"name": objects_name}

View File

@ -9,7 +9,7 @@ import datetime
from django.db import models from django.db import models
from django.core.exceptions import ImproperlyConfigured 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.translation import ugettext_lazy as _
from django.utils import timezone from django.utils import timezone
@ -195,7 +195,7 @@ class RelatedFieldListFilter(FieldListFilter):
} }
for pk_val, val in self.lookup_choices: for pk_val, val in self.lookup_choices:
yield { yield {
'selected': self.lookup_val == smart_unicode(pk_val), 'selected': self.lookup_val == smart_text(pk_val),
'query_string': cl.get_query_string({ 'query_string': cl.get_query_string({
self.lookup_kwarg: pk_val, self.lookup_kwarg: pk_val,
}, [self.lookup_kwarg_isnull]), }, [self.lookup_kwarg_isnull]),
@ -272,7 +272,7 @@ class ChoicesFieldListFilter(FieldListFilter):
} }
for lookup, title in self.field.flatchoices: for lookup, title in self.field.flatchoices:
yield { yield {
'selected': smart_unicode(lookup) == self.lookup_val, 'selected': smart_text(lookup) == self.lookup_val,
'query_string': cl.get_query_string({ 'query_string': cl.get_query_string({
self.lookup_kwarg: lookup}), self.lookup_kwarg: lookup}),
'display': title, 'display': title,
@ -381,7 +381,7 @@ class AllValuesFieldListFilter(FieldListFilter):
if val is None: if val is None:
include_none = True include_none = True
continue continue
val = smart_unicode(val) val = smart_text(val)
yield { yield {
'selected': self.lookup_val == val, 'selected': self.lookup_val == val,
'query_string': cl.get_query_string({ 'query_string': cl.get_query_string({

View File

@ -9,7 +9,7 @@ from django.core.exceptions import ObjectDoesNotExist
from django.db.models.fields.related import ManyToManyRel from django.db.models.fields.related import ManyToManyRel
from django.forms.util import flatatt from django.forms.util import flatatt
from django.template.defaultfilters import capfirst 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.html import conditional_escape, format_html
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils import six from django.utils import six
@ -122,7 +122,7 @@ class AdminField(object):
def label_tag(self): def label_tag(self):
classes = [] classes = []
contents = conditional_escape(force_unicode(self.field.label)) contents = conditional_escape(force_text(self.field.label))
if self.is_checkbox: if self.is_checkbox:
classes.append('vCheckboxLabel') classes.append('vCheckboxLabel')
else: else:
@ -166,7 +166,7 @@ class AdminReadonlyField(object):
label = self.field['label'] label = self.field['label']
return format_html('<label{0}>{1}:</label>', return format_html('<label{0}>{1}:</label>',
flatatt(attrs), flatatt(attrs),
capfirst(force_unicode(label))) capfirst(force_text(label)))
def contents(self): def contents(self):
from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.templatetags.admin_list import _boolean_icon
@ -182,7 +182,7 @@ class AdminReadonlyField(object):
if boolean: if boolean:
result_repr = _boolean_icon(value) result_repr = _boolean_icon(value)
else: else:
result_repr = smart_unicode(value) result_repr = smart_text(value)
if getattr(attr, "allow_tags", False): if getattr(attr, "allow_tags", False):
result_repr = mark_safe(result_repr) result_repr = mark_safe(result_repr)
else: else:

View File

@ -5,7 +5,7 @@ from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.admin.util import quote from django.contrib.admin.util import quote
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_unicode from django.utils.encoding import smart_text
ADDITION = 1 ADDITION = 1
CHANGE = 2 CHANGE = 2
@ -13,7 +13,7 @@ DELETION = 3
class LogEntryManager(models.Manager): class LogEntryManager(models.Manager):
def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''): 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() e.save()
class LogEntry(models.Model): class LogEntry(models.Model):
@ -34,7 +34,7 @@ class LogEntry(models.Model):
ordering = ('-action_time',) ordering = ('-action_time',)
def __repr__(self): def __repr__(self):
return smart_unicode(self.action_time) return smart_text(self.action_time)
def __unicode__(self): def __unicode__(self):
if self.action_flag == ADDITION: if self.action_flag == ADDITION:

View File

@ -28,7 +28,7 @@ from django.utils import six
from django.utils.text import capfirst, get_text_list from django.utils.text import capfirst, get_text_list
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.translation import ungettext from django.utils.translation import ungettext
from django.utils.encoding import force_unicode from django.utils.encoding import force_text
HORIZONTAL, VERTICAL = 1, 2 HORIZONTAL, VERTICAL = 1, 2
# returns the <ul> class for a given radio_admin field # returns the <ul> class for a given radio_admin field
@ -520,7 +520,7 @@ class ModelAdmin(BaseModelAdmin):
user_id = request.user.pk, user_id = request.user.pk,
content_type_id = ContentType.objects.get_for_model(object).pk, content_type_id = ContentType.objects.get_for_model(object).pk,
object_id = object.pk, object_id = object.pk,
object_repr = force_unicode(object), object_repr = force_text(object),
action_flag = ADDITION action_flag = ADDITION
) )
@ -535,7 +535,7 @@ class ModelAdmin(BaseModelAdmin):
user_id = request.user.pk, user_id = request.user.pk,
content_type_id = ContentType.objects.get_for_model(object).pk, content_type_id = ContentType.objects.get_for_model(object).pk,
object_id = object.pk, object_id = object.pk,
object_repr = force_unicode(object), object_repr = force_text(object),
action_flag = CHANGE, action_flag = CHANGE,
change_message = message change_message = message
) )
@ -560,7 +560,7 @@ class ModelAdmin(BaseModelAdmin):
""" """
A list_display column containing a checkbox widget. A list_display column containing a checkbox widget.
""" """
return helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, force_unicode(obj.pk)) return helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, force_text(obj.pk))
action_checkbox.short_description = mark_safe('<input type="checkbox" id="action-toggle" />') action_checkbox.short_description = mark_safe('<input type="checkbox" id="action-toggle" />')
action_checkbox.allow_tags = True action_checkbox.allow_tags = True
@ -674,17 +674,17 @@ class ModelAdmin(BaseModelAdmin):
for formset in formsets: for formset in formsets:
for added_object in formset.new_objects: for added_object in formset.new_objects:
change_message.append(_('Added %(name)s "%(object)s".') change_message.append(_('Added %(name)s "%(object)s".')
% {'name': force_unicode(added_object._meta.verbose_name), % {'name': force_text(added_object._meta.verbose_name),
'object': force_unicode(added_object)}) 'object': force_text(added_object)})
for changed_object, changed_fields in formset.changed_objects: for changed_object, changed_fields in formset.changed_objects:
change_message.append(_('Changed %(list)s for %(name)s "%(object)s".') change_message.append(_('Changed %(list)s for %(name)s "%(object)s".')
% {'list': get_text_list(changed_fields, _('and')), % {'list': get_text_list(changed_fields, _('and')),
'name': force_unicode(changed_object._meta.verbose_name), 'name': force_text(changed_object._meta.verbose_name),
'object': force_unicode(changed_object)}) 'object': force_text(changed_object)})
for deleted_object in formset.deleted_objects: for deleted_object in formset.deleted_objects:
change_message.append(_('Deleted %(name)s "%(object)s".') change_message.append(_('Deleted %(name)s "%(object)s".')
% {'name': force_unicode(deleted_object._meta.verbose_name), % {'name': force_text(deleted_object._meta.verbose_name),
'object': force_unicode(deleted_object)}) 'object': force_text(deleted_object)})
change_message = ' '.join(change_message) change_message = ' '.join(change_message)
return change_message or _('No fields changed.') return change_message or _('No fields changed.')
@ -769,7 +769,7 @@ class ModelAdmin(BaseModelAdmin):
opts = obj._meta opts = obj._meta
pk_value = obj._get_pk_val() pk_value = obj._get_pk_val()
msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)} msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': force_text(opts.verbose_name), 'obj': force_text(obj)}
# Here, we distinguish between different save types by checking for # Here, we distinguish between different save types by checking for
# the presence of keys in request.POST. # the presence of keys in request.POST.
if "_continue" in request.POST: if "_continue" in request.POST:
@ -782,10 +782,10 @@ class ModelAdmin(BaseModelAdmin):
return HttpResponse( return HttpResponse(
'<!DOCTYPE html><html><head><title></title></head><body>' '<!DOCTYPE html><html><head><title></title></head><body>'
'<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script></body></html>' % \ '<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script></body></html>' % \
# escape() calls force_unicode. # escape() calls force_text.
(escape(pk_value), escapejs(obj))) (escape(pk_value), escapejs(obj)))
elif "_addanother" in request.POST: elif "_addanother" in request.POST:
self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name))) self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_text(opts.verbose_name)))
return HttpResponseRedirect(request.path) return HttpResponseRedirect(request.path)
else: else:
self.message_user(request, msg) self.message_user(request, msg)
@ -819,7 +819,7 @@ class ModelAdmin(BaseModelAdmin):
pk_value = obj._get_pk_val() pk_value = obj._get_pk_val()
msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(verbose_name), 'obj': force_unicode(obj)} msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_text(verbose_name), 'obj': force_text(obj)}
if "_continue" in request.POST: if "_continue" in request.POST:
self.message_user(request, msg + ' ' + _("You may edit it again below.")) self.message_user(request, msg + ' ' + _("You may edit it again below."))
if "_popup" in request.REQUEST: if "_popup" in request.REQUEST:
@ -827,14 +827,14 @@ class ModelAdmin(BaseModelAdmin):
else: else:
return HttpResponseRedirect(request.path) return HttpResponseRedirect(request.path)
elif "_saveasnew" in request.POST: elif "_saveasnew" in request.POST:
msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(verbose_name), 'obj': obj} msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_text(verbose_name), 'obj': obj}
self.message_user(request, msg) self.message_user(request, msg)
return HttpResponseRedirect(reverse('admin:%s_%s_change' % return HttpResponseRedirect(reverse('admin:%s_%s_change' %
(opts.app_label, module_name), (opts.app_label, module_name),
args=(pk_value,), args=(pk_value,),
current_app=self.admin_site.name)) current_app=self.admin_site.name))
elif "_addanother" in request.POST: elif "_addanother" in request.POST:
self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(verbose_name))) self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_text(verbose_name)))
return HttpResponseRedirect(reverse('admin:%s_%s_add' % return HttpResponseRedirect(reverse('admin:%s_%s_add' %
(opts.app_label, module_name), (opts.app_label, module_name),
current_app=self.admin_site.name)) current_app=self.admin_site.name))
@ -995,7 +995,7 @@ class ModelAdmin(BaseModelAdmin):
media = media + inline_admin_formset.media media = media + inline_admin_formset.media
context = { context = {
'title': _('Add %s') % force_unicode(opts.verbose_name), 'title': _('Add %s') % force_text(opts.verbose_name),
'adminform': adminForm, 'adminform': adminForm,
'is_popup': "_popup" in request.REQUEST, 'is_popup': "_popup" in request.REQUEST,
'media': media, 'media': media,
@ -1019,7 +1019,7 @@ class ModelAdmin(BaseModelAdmin):
raise PermissionDenied raise PermissionDenied
if obj is None: if obj is None:
raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)}) raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_text(opts.verbose_name), 'key': escape(object_id)})
if request.method == 'POST' and "_saveasnew" in request.POST: if request.method == 'POST' and "_saveasnew" in request.POST:
return self.add_view(request, form_url=reverse('admin:%s_%s_add' % return self.add_view(request, form_url=reverse('admin:%s_%s_add' %
@ -1085,7 +1085,7 @@ class ModelAdmin(BaseModelAdmin):
media = media + inline_admin_formset.media media = media + inline_admin_formset.media
context = { context = {
'title': _('Change %s') % force_unicode(opts.verbose_name), 'title': _('Change %s') % force_text(opts.verbose_name),
'adminform': adminForm, 'adminform': adminForm,
'object_id': object_id, 'object_id': object_id,
'original': obj, 'original': obj,
@ -1194,14 +1194,14 @@ class ModelAdmin(BaseModelAdmin):
if changecount: if changecount:
if changecount == 1: if changecount == 1:
name = force_unicode(opts.verbose_name) name = force_text(opts.verbose_name)
else: else:
name = force_unicode(opts.verbose_name_plural) name = force_text(opts.verbose_name_plural)
msg = ungettext("%(count)s %(name)s was changed successfully.", msg = ungettext("%(count)s %(name)s was changed successfully.",
"%(count)s %(name)s were changed successfully.", "%(count)s %(name)s were changed successfully.",
changecount) % {'count': changecount, changecount) % {'count': changecount,
'name': name, 'name': name,
'obj': force_unicode(obj)} 'obj': force_text(obj)}
self.message_user(request, msg) self.message_user(request, msg)
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
@ -1228,7 +1228,7 @@ class ModelAdmin(BaseModelAdmin):
'All %(total_count)s selected', cl.result_count) 'All %(total_count)s selected', cl.result_count)
context = { context = {
'module_name': force_unicode(opts.verbose_name_plural), 'module_name': force_text(opts.verbose_name_plural),
'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)}, 'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
'selection_note_all': selection_note_all % {'total_count': cl.result_count}, 'selection_note_all': selection_note_all % {'total_count': cl.result_count},
'title': cl.title, 'title': cl.title,
@ -1263,7 +1263,7 @@ class ModelAdmin(BaseModelAdmin):
raise PermissionDenied raise PermissionDenied
if obj is None: if obj is None:
raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)}) raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_text(opts.verbose_name), 'key': escape(object_id)})
using = router.db_for_write(self.model) using = router.db_for_write(self.model)
@ -1275,11 +1275,11 @@ class ModelAdmin(BaseModelAdmin):
if request.POST: # The user has already confirmed the deletion. if request.POST: # The user has already confirmed the deletion.
if perms_needed: if perms_needed:
raise PermissionDenied raise PermissionDenied
obj_display = force_unicode(obj) obj_display = force_text(obj)
self.log_deletion(request, obj, obj_display) self.log_deletion(request, obj, obj_display)
self.delete_model(request, obj) self.delete_model(request, obj)
self.message_user(request, _('The %(name)s "%(obj)s" was deleted successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj_display)}) self.message_user(request, _('The %(name)s "%(obj)s" was deleted successfully.') % {'name': force_text(opts.verbose_name), 'obj': force_text(obj_display)})
if not self.has_change_permission(request, None): if not self.has_change_permission(request, None):
return HttpResponseRedirect(reverse('admin:index', return HttpResponseRedirect(reverse('admin:index',
@ -1288,7 +1288,7 @@ class ModelAdmin(BaseModelAdmin):
(opts.app_label, opts.module_name), (opts.app_label, opts.module_name),
current_app=self.admin_site.name)) current_app=self.admin_site.name))
object_name = force_unicode(opts.verbose_name) object_name = force_text(opts.verbose_name)
if perms_needed or protected: if perms_needed or protected:
title = _("Cannot delete %(name)s") % {"name": object_name} title = _("Cannot delete %(name)s") % {"name": object_name}
@ -1326,9 +1326,9 @@ class ModelAdmin(BaseModelAdmin):
# If no history was found, see whether this object even exists. # If no history was found, see whether this object even exists.
obj = get_object_or_404(model, pk=unquote(object_id)) obj = get_object_or_404(model, pk=unquote(object_id))
context = { context = {
'title': _('Change history: %s') % force_unicode(obj), 'title': _('Change history: %s') % force_text(obj),
'action_list': action_list, 'action_list': action_list,
'module_name': capfirst(force_unicode(opts.verbose_name_plural)), 'module_name': capfirst(force_text(opts.verbose_name_plural)),
'object': obj, 'object': obj,
'app_label': app_label, 'app_label': app_label,
'opts': opts, 'opts': opts,

View File

@ -15,7 +15,7 @@ from django.utils.safestring import mark_safe
from django.utils import six from django.utils import six
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.encoding import smart_unicode, force_unicode from django.utils.encoding import smart_text, force_text
from django.template import Library from django.template import Library
from django.template.loader import get_template from django.template.loader import get_template
from django.template.context import Context from django.template.context import Context
@ -210,7 +210,7 @@ def items_for_result(cl, result, form):
result_repr = display_for_field(value, f) result_repr = display_for_field(value, f)
if isinstance(f, (models.DateField, models.TimeField, models.ForeignKey)): if isinstance(f, (models.DateField, models.TimeField, models.ForeignKey)):
row_class = mark_safe(' class="nowrap"') row_class = mark_safe(' class="nowrap"')
if force_unicode(result_repr) == '': if force_text(result_repr) == '':
result_repr = mark_safe('&nbsp;') result_repr = mark_safe('&nbsp;')
# If list_display_links not defined, add the link tag to the first field # If list_display_links not defined, add the link tag to the first field
if (first and not cl.list_display_links) or field_name in cl.list_display_links: if (first and not cl.list_display_links) or field_name in cl.list_display_links:
@ -224,7 +224,7 @@ def items_for_result(cl, result, form):
else: else:
attr = pk attr = pk
value = result.serializable_value(attr) value = result.serializable_value(attr)
result_id = repr(force_unicode(value))[1:] result_id = repr(force_text(value))[1:]
yield format_html('<{0}{1}><a href="{2}"{3}>{4}</a></{5}>', yield format_html('<{0}{1}><a href="{2}"{3}>{4}</a></{5}>',
table_tag, table_tag,
row_class, row_class,
@ -241,10 +241,10 @@ def items_for_result(cl, result, form):
field_name == cl.model._meta.pk.name and field_name == cl.model._meta.pk.name and
form[cl.model._meta.pk.name].is_hidden)): form[cl.model._meta.pk.name].is_hidden)):
bf = form[field_name] bf = form[field_name]
result_repr = mark_safe(force_unicode(bf.errors) + force_unicode(bf)) result_repr = mark_safe(force_text(bf.errors) + force_text(bf))
yield format_html('<td{0}>{1}</td>', row_class, result_repr) yield format_html('<td{0}>{1}</td>', row_class, result_repr)
if form and not form[cl.model._meta.pk.name].is_hidden: if form and not form[cl.model._meta.pk.name].is_hidden:
yield format_html('<td>{0}</td>', force_unicode(form[cl.model._meta.pk.name])) yield format_html('<td>{0}</td>', force_text(form[cl.model._meta.pk.name]))
class ResultList(list): class ResultList(list):
# Wrapper class used to return items in a list_editable # Wrapper class used to return items in a list_editable
@ -267,7 +267,7 @@ def result_hidden_fields(cl):
if cl.formset: if cl.formset:
for res, form in zip(cl.result_list, cl.formset.forms): for res, form in zip(cl.result_list, cl.formset.forms):
if form[cl.model._meta.pk.name].is_hidden: if form[cl.model._meta.pk.name].is_hidden:
yield mark_safe(force_unicode(form[cl.model._meta.pk.name])) yield mark_safe(force_text(form[cl.model._meta.pk.name]))
@register.inclusion_tag("admin/change_list_results.html") @register.inclusion_tag("admin/change_list_results.html")
def result_list(cl): def result_list(cl):

View File

@ -12,7 +12,7 @@ from django.utils import formats
from django.utils.html import format_html from django.utils.html import format_html
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils import timezone from django.utils import timezone
from django.utils.encoding import force_unicode, smart_unicode, smart_str from django.utils.encoding import force_text, smart_text, smart_bytes
from django.utils import six from django.utils import six
from django.utils.translation import ungettext from django.utils.translation import ungettext
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
@ -132,7 +132,7 @@ def get_deleted_objects(objs, opts, user, admin_site, using):
# Don't display link to edit, because it either has no # Don't display link to edit, because it either has no
# admin or is edited inline. # admin or is edited inline.
return '%s: %s' % (capfirst(opts.verbose_name), return '%s: %s' % (capfirst(opts.verbose_name),
force_unicode(obj)) force_text(obj))
to_delete = collector.nested(format_callback) to_delete = collector.nested(format_callback)
@ -207,8 +207,8 @@ def model_format_dict(obj):
else: else:
opts = obj opts = obj
return { return {
'verbose_name': force_unicode(opts.verbose_name), 'verbose_name': force_text(opts.verbose_name),
'verbose_name_plural': force_unicode(opts.verbose_name_plural) 'verbose_name_plural': force_text(opts.verbose_name_plural)
} }
@ -274,10 +274,10 @@ def label_for_field(name, model, model_admin=None, return_attr=False):
label = field.verbose_name label = field.verbose_name
except models.FieldDoesNotExist: except models.FieldDoesNotExist:
if name == "__unicode__": if name == "__unicode__":
label = force_unicode(model._meta.verbose_name) label = force_text(model._meta.verbose_name)
attr = six.text_type attr = six.text_type
elif name == "__str__": elif name == "__str__":
label = smart_str(model._meta.verbose_name) label = smart_bytes(model._meta.verbose_name)
attr = bytes attr = bytes
else: else:
if callable(name): if callable(name):
@ -311,7 +311,7 @@ def help_text_for_field(name, model):
help_text = model._meta.get_field_by_name(name)[0].help_text help_text = model._meta.get_field_by_name(name)[0].help_text
except models.FieldDoesNotExist: except models.FieldDoesNotExist:
help_text = "" help_text = ""
return smart_unicode(help_text) return smart_text(help_text)
def display_for_field(value, field): def display_for_field(value, field):
@ -335,7 +335,7 @@ def display_for_field(value, field):
elif isinstance(field, models.FloatField): elif isinstance(field, models.FloatField):
return formats.number_format(value) return formats.number_format(value)
else: else:
return smart_unicode(value) return smart_text(value)
def display_for_value(value, boolean=False): def display_for_value(value, boolean=False):
@ -353,7 +353,7 @@ def display_for_value(value, boolean=False):
elif isinstance(value, six.integer_types + (decimal.Decimal, float)): elif isinstance(value, six.integer_types + (decimal.Decimal, float)):
return formats.number_format(value) return formats.number_format(value)
else: else:
return smart_unicode(value) return smart_text(value)
class NotRelationField(Exception): class NotRelationField(Exception):

View File

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

View File

@ -14,7 +14,7 @@ from django.utils.html import escape, format_html, format_html_join
from django.utils.text import Truncator from django.utils.text import Truncator
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode from django.utils.encoding import force_text
from django.utils import six from django.utils import six
@ -96,7 +96,7 @@ class AdminRadioFieldRenderer(RadioFieldRenderer):
return format_html('<ul{0}>\n{1}\n</ul>', return format_html('<ul{0}>\n{1}\n</ul>',
flatatt(self.attrs), flatatt(self.attrs),
format_html_join('\n', '<li>{0}</li>', format_html_join('\n', '<li>{0}</li>',
((force_unicode(w),) for w in self))) ((force_text(w),) for w in self)))
class AdminRadioSelect(forms.RadioSelect): class AdminRadioSelect(forms.RadioSelect):
renderer = AdminRadioFieldRenderer renderer = AdminRadioFieldRenderer
@ -197,7 +197,7 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
# The related object is registered with the same AdminSite # The related object is registered with the same AdminSite
attrs['class'] = 'vManyToManyRawIdAdminField' attrs['class'] = 'vManyToManyRawIdAdminField'
if value: if value:
value = ','.join([force_unicode(v) for v in value]) value = ','.join([force_text(v) for v in value])
else: else:
value = '' value = ''
return super(ManyToManyRawIdWidget, self).render(name, value, attrs) return super(ManyToManyRawIdWidget, self).render(name, value, attrs)
@ -221,7 +221,7 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
if len(initial) != len(data): if len(initial) != len(data):
return True return True
for pk1, pk2 in zip(initial, data): for pk1, pk2 in zip(initial, data):
if force_unicode(pk1) != force_unicode(pk2): if force_text(pk1) != force_text(pk2):
return True return True
return False return False

View File

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

View File

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

View File

@ -8,7 +8,7 @@ from django.core import mail
from django.forms.fields import Field, EmailField from django.forms.fields import Field, EmailField
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings 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 six
from django.utils import translation from django.utils import translation
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -28,7 +28,7 @@ class UserCreationFormTest(TestCase):
form = UserCreationForm(data) form = UserCreationForm(data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form["username"].errors, self.assertEqual(form["username"].errors,
[force_unicode(form.error_messages['duplicate_username'])]) [force_text(form.error_messages['duplicate_username'])])
def test_invalid_data(self): def test_invalid_data(self):
data = { data = {
@ -39,7 +39,7 @@ class UserCreationFormTest(TestCase):
form = UserCreationForm(data) form = UserCreationForm(data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form["username"].errors, 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): def test_password_verification(self):
# The verification password is incorrect. # The verification password is incorrect.
@ -51,13 +51,13 @@ class UserCreationFormTest(TestCase):
form = UserCreationForm(data) form = UserCreationForm(data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form["password2"].errors, self.assertEqual(form["password2"].errors,
[force_unicode(form.error_messages['password_mismatch'])]) [force_text(form.error_messages['password_mismatch'])])
def test_both_passwords(self): def test_both_passwords(self):
# One (or both) passwords weren't given # One (or both) passwords weren't given
data = {'username': 'jsmith'} data = {'username': 'jsmith'}
form = UserCreationForm(data) 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.assertFalse(form.is_valid())
self.assertEqual(form['password1'].errors, required_error) self.assertEqual(form['password1'].errors, required_error)
self.assertEqual(form['password2'].errors, required_error) self.assertEqual(form['password2'].errors, required_error)
@ -96,7 +96,7 @@ class AuthenticationFormTest(TestCase):
form = AuthenticationForm(None, data) form = AuthenticationForm(None, data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form.non_field_errors(), 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): def test_inactive_user(self):
# The user is inactive. # The user is inactive.
@ -107,7 +107,7 @@ class AuthenticationFormTest(TestCase):
form = AuthenticationForm(None, data) form = AuthenticationForm(None, data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form.non_field_errors(), self.assertEqual(form.non_field_errors(),
[force_unicode(form.error_messages['inactive'])]) [force_text(form.error_messages['inactive'])])
def test_inactive_user_i18n(self): def test_inactive_user_i18n(self):
with self.settings(USE_I18N=True): with self.settings(USE_I18N=True):
@ -120,7 +120,7 @@ class AuthenticationFormTest(TestCase):
form = AuthenticationForm(None, data) form = AuthenticationForm(None, data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form.non_field_errors(), self.assertEqual(form.non_field_errors(),
[force_unicode(form.error_messages['inactive'])]) [force_text(form.error_messages['inactive'])])
def test_success(self): def test_success(self):
# The success case # The success case
@ -148,7 +148,7 @@ class SetPasswordFormTest(TestCase):
form = SetPasswordForm(user, data) form = SetPasswordForm(user, data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form["new_password2"].errors, self.assertEqual(form["new_password2"].errors,
[force_unicode(form.error_messages['password_mismatch'])]) [force_text(form.error_messages['password_mismatch'])])
def test_success(self): def test_success(self):
user = User.objects.get(username='testclient') user = User.objects.get(username='testclient')
@ -175,7 +175,7 @@ class PasswordChangeFormTest(TestCase):
form = PasswordChangeForm(user, data) form = PasswordChangeForm(user, data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form["old_password"].errors, 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): def test_password_verification(self):
# The two new passwords do not match. # The two new passwords do not match.
@ -188,7 +188,7 @@ class PasswordChangeFormTest(TestCase):
form = PasswordChangeForm(user, data) form = PasswordChangeForm(user, data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form["new_password2"].errors, self.assertEqual(form["new_password2"].errors,
[force_unicode(form.error_messages['password_mismatch'])]) [force_text(form.error_messages['password_mismatch'])])
def test_success(self): def test_success(self):
# The success case. # The success case.
@ -219,7 +219,7 @@ class UserChangeFormTest(TestCase):
form = UserChangeForm(data, instance=user) form = UserChangeForm(data, instance=user)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form['username'].errors, 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): def test_bug_14242(self):
# A regression test, introduce by adding an optimization for the # A regression test, introduce by adding an optimization for the
@ -274,7 +274,7 @@ class PasswordResetFormTest(TestCase):
form = PasswordResetForm(data) form = PasswordResetForm(data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form['email'].errors, self.assertEqual(form['email'].errors,
[force_unicode(EmailField.default_error_messages['invalid'])]) [force_text(EmailField.default_error_messages['invalid'])])
def test_nonexistant_email(self): def test_nonexistant_email(self):
# Test nonexistant email address # Test nonexistant email address
@ -282,7 +282,7 @@ class PasswordResetFormTest(TestCase):
form = PasswordResetForm(data) form = PasswordResetForm(data)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
self.assertEqual(form.errors, self.assertEqual(form.errors,
{'email': [force_unicode(form.error_messages['unknown'])]}) {'email': [force_text(form.error_messages['unknown'])]})
def test_cleaned_data(self): def test_cleaned_data(self):
# Regression test # Regression test

View File

@ -7,7 +7,7 @@ from django.contrib.auth.models import User
from django.core import mail from django.core import mail
from django.core.urlresolvers import reverse, NoReverseMatch from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import QueryDict 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.html import escape
from django.utils.http import urlquote from django.utils.http import urlquote
from django.test import TestCase from django.test import TestCase
@ -46,7 +46,7 @@ class AuthViewsTestCase(TestCase):
self.assertTrue(SESSION_KEY in self.client.session) self.assertTrue(SESSION_KEY in self.client.session)
def assertContainsEscaped(self, response, text, **kwargs): 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): class AuthViewNamedURLTests(AuthViewsTestCase):

View File

@ -5,7 +5,7 @@ from django.conf import settings
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.comments.models import Comment from django.contrib.comments.models import Comment
from django.utils.crypto import salted_hmac, constant_time_compare 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.text import get_text_list
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ungettext, ugettext, ugettext_lazy as _ from django.utils.translation import ungettext, ugettext, ugettext_lazy as _
@ -133,7 +133,7 @@ class CommentDetailsForm(CommentSecurityForm):
""" """
return dict( return dict(
content_type = ContentType.objects.get_for_model(self.target_object), 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_name = self.cleaned_data["name"],
user_email = self.cleaned_data["email"], user_email = self.cleaned_data["email"],
user_url = self.cleaned_data["url"], user_url = self.cleaned_data["url"],

View File

@ -1,6 +1,6 @@
from django.db import models from django.db import models
from django.contrib.contenttypes.models import ContentType 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): class CommentManager(models.Manager):
@ -18,5 +18,5 @@ class CommentManager(models.Manager):
ct = ContentType.objects.get_for_model(model) ct = ContentType.objects.get_for_model(model)
qs = self.get_query_set().filter(content_type=ct) qs = self.get_query_set().filter(content_type=ct)
if isinstance(model, models.Model): 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 return qs

View File

@ -3,7 +3,7 @@ from django.template.loader import render_to_string
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib import comments from django.contrib import comments
from django.utils.encoding import smart_unicode from django.utils.encoding import smart_text
register = template.Library() register = template.Library()
@ -75,7 +75,7 @@ class BaseCommentNode(template.Node):
qs = self.comment_model.objects.filter( qs = self.comment_model.objects.filter(
content_type = ctype, content_type = ctype,
object_pk = smart_unicode(object_pk), object_pk = smart_text(object_pk),
site__pk = settings.SITE_ID, site__pk = settings.SITE_ID,
) )

View File

@ -17,7 +17,7 @@ from django.forms import ModelForm
from django.forms.models import BaseModelFormSet, modelformset_factory, save_instance from django.forms.models import BaseModelFormSet, modelformset_factory, save_instance
from django.contrib.admin.options import InlineModelAdmin, flatten_fieldsets from django.contrib.admin.options import InlineModelAdmin, flatten_fieldsets
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_unicode from django.utils.encoding import smart_text
class GenericForeignKey(object): class GenericForeignKey(object):
""" """
@ -169,7 +169,7 @@ class GenericRelation(RelatedField, Field):
def value_to_string(self, obj): def value_to_string(self, obj):
qs = getattr(obj, self.name).all() 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): def m2m_db_table(self):
return self.rel.to._meta.db_table return self.rel.to._meta.db_table

View File

@ -1,6 +1,6 @@
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db.models import get_apps, get_models, signals 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 from django.utils import six
def update_contenttypes(app, created_models, verbosity=2, **kwargs): 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([ cts = ContentType.objects.bulk_create([
ContentType( ContentType(
name=smart_unicode(model._meta.verbose_name_raw), name=smart_text(model._meta.verbose_name_raw),
app_label=app_label, app_label=app_label,
model=model_name, model=model_name,
) )

View File

@ -1,6 +1,6 @@
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ 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): class ContentTypeManager(models.Manager):
@ -37,13 +37,13 @@ class ContentTypeManager(models.Manager):
try: try:
ct = self._get_from_cache(opts) ct = self._get_from_cache(opts)
except KeyError: 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 # needed around opts.verbose_name_raw because name_raw might be a
# django.utils.functional.__proxy__ object. # django.utils.functional.__proxy__ object.
ct, created = self.get_or_create( ct, created = self.get_or_create(
app_label = opts.app_label, app_label = opts.app_label,
model = opts.object_name.lower(), 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) self._add_to_cache(self.db, ct)
@ -86,7 +86,7 @@ class ContentTypeManager(models.Manager):
ct = self.create( ct = self.create(
app_label=opts.app_label, app_label=opts.app_label,
model=opts.object_name.lower(), 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) self._add_to_cache(self.db, ct)
results[ct.model_class()] = 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: if not model or self.name != model._meta.verbose_name_raw:
return self.name return self.name
else: else:
return force_unicode(model._meta.verbose_name) return force_text(model._meta.verbose_name)
def model_class(self): def model_class(self):
"Returns the Python model class for this type of content." "Returns the Python model class for this type of content."

View File

@ -7,7 +7,7 @@ from __future__ import unicode_literals
from django.db import models from django.db import models
from django.utils import formats from django.utils import formats
from django.utils.text import capfirst 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 from django.db.models.query import QuerySet
EMPTY_VALUE = '(None)' EMPTY_VALUE = '(None)'
@ -22,7 +22,7 @@ class EasyModel(object):
self.verbose_name_plural = model._meta.verbose_name_plural self.verbose_name_plural = model._meta.verbose_name_plural
def __repr__(self): def __repr__(self):
return '<EasyModel for %s>' % smart_str(self.model._meta.object_name) return '<EasyModel for %s>' % smart_bytes(self.model._meta.object_name)
def model_databrowse(self): def model_databrowse(self):
"Returns the ModelDatabrowse class for this model." "Returns the ModelDatabrowse class for this model."
@ -61,7 +61,7 @@ class EasyField(object):
self.model, self.field = easy_model, field self.model, self.field = easy_model, field
def __repr__(self): def __repr__(self):
return smart_str('<EasyField for %s.%s>' % (self.model.model._meta.object_name, self.field.name)) return smart_bytes('<EasyField for %s.%s>' % (self.model.model._meta.object_name, self.field.name))
def choices(self): def choices(self):
for value, label in self.field.choices: for value, label in self.field.choices:
@ -79,7 +79,7 @@ class EasyChoice(object):
self.value, self.label = value, label self.value, self.label = value, label
def __repr__(self): def __repr__(self):
return smart_str('<EasyChoice for %s.%s>' % (self.model.model._meta.object_name, self.field.name)) return smart_bytes('<EasyChoice for %s.%s>' % (self.model.model._meta.object_name, self.field.name))
def url(self): 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)) 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 self.model, self.instance = easy_model, instance
def __repr__(self): def __repr__(self):
return smart_str('<EasyInstance for %s (%s)>' % (self.model.model._meta.object_name, self.instance._get_pk_val())) return smart_bytes('<EasyInstance for %s (%s)>' % (self.model.model._meta.object_name, self.instance._get_pk_val()))
def __unicode__(self): def __unicode__(self):
val = smart_unicode(self.instance) val = smart_text(self.instance)
if len(val) > DISPLAY_SIZE: if len(val) > DISPLAY_SIZE:
return val[:DISPLAY_SIZE] + '...' return val[:DISPLAY_SIZE] + '...'
return val return val
@ -136,7 +136,7 @@ class EasyInstanceField(object):
self.raw_value = getattr(instance.instance, field.name) self.raw_value = getattr(instance.instance, field.name)
def __repr__(self): def __repr__(self):
return smart_str('<EasyInstanceField for %s.%s>' % (self.model.model._meta.object_name, self.field.name)) return smart_bytes('<EasyInstanceField for %s.%s>' % (self.model.model._meta.object_name, self.field.name))
def values(self): def values(self):
""" """
@ -185,7 +185,7 @@ class EasyInstanceField(object):
if value is None: if value is None:
continue 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())) 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: else:
lst = [(value, None) for value in self.values()] lst = [(value, None) for value in self.values()]
elif self.field.choices: elif self.field.choices:

View File

@ -7,7 +7,7 @@ from django.contrib.databrowse.sites import DatabrowsePlugin
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.utils.html import format_html, format_html_join from django.utils.html import format_html, format_html_join
from django.utils.text import capfirst 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.views.generic import dates
from django.utils import datetime_safe from django.utils import datetime_safe
@ -66,7 +66,7 @@ class CalendarPlugin(DatabrowsePlugin):
return '' return ''
return format_html('<p class="filter"><strong>View calendar by:</strong> {0}</p>', return format_html('<p class="filter"><strong>View calendar by:</strong> {0}</p>',
format_html_join(', ', '<a href="calendars/{0}/">{1}</a>', format_html_join(', ', '<a href="calendars/{0}/">{1}</a>',
((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): def urls(self, plugin_name, easy_instance_field):
if isinstance(easy_instance_field.field, models.DateField): if isinstance(easy_instance_field.field, models.DateField):

View File

@ -8,7 +8,7 @@ from django.shortcuts import render_to_response
from django.utils.html import format_html, format_html_join from django.utils.html import format_html, format_html_join
from django.utils.http import urlquote from django.utils.http import urlquote
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils.encoding import force_unicode from django.utils.encoding import force_text
class FieldChoicePlugin(DatabrowsePlugin): class FieldChoicePlugin(DatabrowsePlugin):
@ -35,7 +35,7 @@ class FieldChoicePlugin(DatabrowsePlugin):
return '' return ''
return format_html('<p class="filter"><strong>View by:</strong> {0}</p>', return format_html('<p class="filter"><strong>View by:</strong> {0}</p>',
format_html_join(', ', '<a href="fields/{0}/">{1}</a>', format_html_join(', ', '<a href="fields/{0}/">{1}</a>',
((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): def urls(self, plugin_name, easy_instance_field):
if easy_instance_field.field in self.field_dict(easy_instance_field.model.model).values(): if easy_instance_field.field in self.field_dict(easy_instance_field.model.model).values():

View File

@ -1,6 +1,6 @@
from django.core.files.uploadedfile import UploadedFile from django.core.files.uploadedfile import UploadedFile
from django.utils.datastructures import MultiValueDict 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.functional import lazy_property
from django.utils import six from django.utils import six
@ -74,7 +74,7 @@ class BaseStorage(object):
files = {} files = {}
for field, field_dict in six.iteritems(wizard_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)) for k, v in six.iteritems(field_dict))
tmp_name = field_dict.pop('tmp_name') tmp_name = field_dict.pop('tmp_name')
files[field] = UploadedFile( files[field] = UploadedFile(

View File

@ -8,7 +8,7 @@ from django.core.paginator import EmptyPage, PageNotAnInteger
from django.contrib.gis.db.models.fields import GeometryField from django.contrib.gis.db.models.fields import GeometryField
from django.db import connections, DEFAULT_DB_ALIAS from django.db import connections, DEFAULT_DB_ALIAS
from django.db.models import get_model from django.db.models import get_model
from django.utils.encoding import smart_str from django.utils.encoding import smart_bytes
from django.utils import six from django.utils import six
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -61,7 +61,7 @@ def sitemap(request, sitemaps, section=None):
raise Http404(_("Page %s empty") % page) raise Http404(_("Page %s empty") % page)
except PageNotAnInteger: except PageNotAnInteger:
raise Http404(_("No page '%s'") % page) raise Http404(_("No page '%s'") % page)
xml = smart_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') return HttpResponse(xml, content_type='application/xml')
def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS): def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS):

View File

@ -5,7 +5,7 @@ from datetime import date, datetime
from django import template from django import template
from django.conf import settings from django.conf import settings
from django.template import defaultfilters 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.formats import number_format
from django.utils.translation import pgettext, ungettext, ugettext as _ from django.utils.translation import pgettext, ungettext, ugettext as _
from django.utils.timezone import is_aware, utc from django.utils.timezone import is_aware, utc
@ -41,7 +41,7 @@ def intcomma(value, use_l10n=True):
return intcomma(value, False) return intcomma(value, False)
else: else:
return number_format(value, force_grouping=True) 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) new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig)
if orig == new: if orig == new:
return new return new

View File

@ -10,7 +10,7 @@ from django.contrib.localflavor.au.au_states import STATE_CHOICES
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, RegexField, Select 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 _ from django.utils.translation import ugettext_lazy as _
@ -44,7 +44,7 @@ class AUPhoneNumberField(Field):
super(AUPhoneNumberField, self).clean(value) super(AUPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub('(\(|\)|\s+|-)', '', smart_unicode(value)) value = re.sub('(\(|\)|\s+|-)', '', smart_text(value))
phone_match = PHONE_DIGITS_RE.search(value) phone_match = PHONE_DIGITS_RE.search(value)
if phone_match: if phone_match:
return '%s' % phone_match.group(1) return '%s' % phone_match.group(1)

View File

@ -11,7 +11,7 @@ from django.contrib.localflavor.br.br_states import STATE_CHOICES
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, RegexField, CharField, Select 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 _ from django.utils.translation import ugettext_lazy as _
@ -35,7 +35,7 @@ class BRPhoneNumberField(Field):
super(BRPhoneNumberField, self).clean(value) super(BRPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) value = re.sub('(\(|\)|\s+)', '', smart_text(value))
m = phone_digits_re.search(value) m = phone_digits_re.search(value)
if m: if m:
return '%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) 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) value = super(BRStateChoiceField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
value = '' value = ''
value = smart_unicode(value) value = smart_text(value)
if value == '': if value == '':
return 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: if value not in valid_values:
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])
return value return value

View File

@ -9,7 +9,7 @@ import re
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, CharField, Select 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 _ from django.utils.translation import ugettext_lazy as _
@ -53,7 +53,7 @@ class CAPhoneNumberField(Field):
super(CAPhoneNumberField, self).clean(value) super(CAPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) value = re.sub('(\(|\)|\s+)', '', smart_text(value))
m = phone_digits_re.search(value) m = phone_digits_re.search(value)
if m: if m:
return '%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) return '%s-%s-%s' % (m.group(1), m.group(2), m.group(3))

View File

@ -10,7 +10,7 @@ from django.contrib.localflavor.ch.ch_states import STATE_CHOICES
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, RegexField, Select 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 _ from django.utils.translation import ugettext_lazy as _
@ -41,7 +41,7 @@ class CHPhoneNumberField(Field):
super(CHPhoneNumberField, self).clean(value) super(CHPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub('(\.|\s|/|-)', '', smart_unicode(value)) value = re.sub('(\.|\s|/|-)', '', smart_text(value))
m = phone_digits_re.search(value) m = phone_digits_re.search(value)
if m: if m:
return '%s %s %s %s' % (value[0:3], value[3:6], value[6:8], value[8:10]) return '%s %s %s %s' % (value[0:3], value[3:6], value[6:8], value[8:10])

View File

@ -8,7 +8,7 @@ from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import RegexField, Select from django.forms.fields import RegexField, Select
from django.utils.translation import ugettext_lazy as _ 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 from .cl_regions import REGION_CHOICES
@ -75,7 +75,7 @@ class CLRutField(RegexField):
Turns the RUT into one normalized format. Returns a (rut, verifier) Turns the RUT into one normalized format. Returns a (rut, verifier)
tuple. tuple.
""" """
rut = smart_unicode(rut).replace(' ', '').replace('.', '').replace('-', '') rut = smart_text(rut).replace(' ', '').replace('.', '').replace('-', '')
return rut[:-1], rut[-1].upper() return rut[:-1], rut[-1].upper()
def _format(self, code, verifier=None): def _format(self, code, verifier=None):

View File

@ -9,7 +9,7 @@ from django.contrib.localflavor.fr.fr_department import DEPARTMENT_CHOICES
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import CharField, RegexField, Select 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 _ from django.utils.translation import ugettext_lazy as _
@ -43,7 +43,7 @@ class FRPhoneNumberField(CharField):
super(FRPhoneNumberField, self).clean(value) super(FRPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub('(\.|\s)', '', smart_unicode(value)) value = re.sub('(\.|\s)', '', smart_text(value))
m = phone_digits_re.search(value) m = phone_digits_re.search(value)
if m: if m:
return '%s %s %s %s %s' % (value[0:2], value[2:4], value[4:6], value[6:8], value[8:10]) return '%s %s %s %s %s' % (value[0:2], value[2:4], value[4:6], value[6:8], value[8:10])

View File

@ -8,7 +8,7 @@ import re
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import CharField from django.forms import CharField
from django.forms import ValidationError 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 _ from django.utils.translation import ugettext_lazy as _
@ -53,7 +53,7 @@ class HKPhoneNumberField(CharField):
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub('(\(|\)|\s+|\+)', '', smart_unicode(value)) value = re.sub('(\(|\)|\s+|\+)', '', smart_text(value))
m = hk_phone_digits_re.search(value) m = hk_phone_digits_re.search(value)
if not m: if not m:
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])

View File

@ -12,7 +12,7 @@ from django.contrib.localflavor.hr.hr_choices import (
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, Select, RegexField 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 _ from django.utils.translation import ugettext_lazy as _
@ -159,7 +159,7 @@ class HRLicensePlateField(Field):
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' 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) matches = plate_re.search(value)
if matches is None: if matches is None:
@ -225,7 +225,7 @@ class HRPhoneNumberField(Field):
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub(r'[\-\s\(\)]', '', smart_unicode(value)) value = re.sub(r'[\-\s\(\)]', '', smart_text(value))
matches = phone_re.search(value) matches = phone_re.search(value)
if matches is None: if matches is None:

View File

@ -11,7 +11,7 @@ from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, Select from django.forms.fields import Field, Select
from django.utils.translation import ugettext_lazy as _ 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}$') postcode_re = re.compile(r'^[1-9]\d{4}$')
@ -77,10 +77,10 @@ class IDPhoneNumberField(Field):
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' 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): if phone_re.search(phone_number):
return smart_unicode(value) return smart_text(value)
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])
@ -120,7 +120,7 @@ class IDLicensePlateField(Field):
return '' return ''
plate_number = re.sub(r'\s+', ' ', plate_number = re.sub(r'\s+', ' ',
smart_unicode(value.strip())).upper() smart_text(value.strip())).upper()
matches = plate_re.search(plate_number) matches = plate_re.search(plate_number)
if matches is None: if matches is None:
@ -181,7 +181,7 @@ class IDNationalIdentityNumberField(Field):
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub(r'[\s.]', '', smart_unicode(value)) value = re.sub(r'[\s.]', '', smart_text(value))
if not nik_re.search(value): if not nik_re.search(value):
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])

View File

@ -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.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, RegexField, CharField, Select 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 _ from django.utils.translation import ugettext_lazy as _
@ -74,7 +74,7 @@ class INStateField(Field):
pass pass
else: else:
try: try:
return smart_unicode(STATES_NORMALIZED[value.strip().lower()]) return smart_text(STATES_NORMALIZED[value.strip().lower()])
except KeyError: except KeyError:
pass pass
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])
@ -107,7 +107,7 @@ class INPhoneNumberField(CharField):
super(INPhoneNumberField, self).clean(value) super(INPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = smart_unicode(value) value = smart_text(value)
m = phone_digits_re.match(value) m = phone_digits_re.match(value)
if m: if m:
return '%s' % (value) return '%s' % (value)

View File

@ -9,7 +9,7 @@ from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import RegexField from django.forms.fields import RegexField
from django.forms.widgets import Select 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 _ 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 Takes in the value in canonical form and returns it in the common
display format. display format.
""" """
return smart_unicode(value[:6]+'-'+value[6:]) return smart_text(value[:6]+'-'+value[6:])
class ISPhoneNumberField(RegexField): class ISPhoneNumberField(RegexField):
""" """

View File

@ -13,7 +13,7 @@ from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, RegexField, Select from django.forms.fields import Field, RegexField, Select
from django.utils.translation import ugettext_lazy as _ 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): class ITZipCodeField(RegexField):
@ -85,4 +85,4 @@ class ITVatNumberField(Field):
check_digit = vat_number_check_digit(vat_number[0:10]) check_digit = vat_number_check_digit(vat_number[0:10])
if not vat_number[10] == check_digit: if not vat_number[10] == check_digit:
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])
return smart_unicode(vat_number) return smart_text(vat_number)

View File

@ -1,4 +1,4 @@
from django.utils.encoding import smart_unicode from django.utils.encoding import smart_text
def ssn_check_digit(value): def ssn_check_digit(value):
"Calculate Italian social security number check digit." "Calculate Italian social security number check digit."
@ -34,11 +34,11 @@ def ssn_check_digit(value):
def vat_number_check_digit(vat_number): def vat_number_check_digit(vat_number):
"Calculate Italian VAT number check digit." "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 total = 0
for i in range(0, 10, 2): for i in range(0, 10, 2):
total += int(normalized_vat_number[i]) total += int(normalized_vat_number[i])
for i in range(1, 11, 2): for i in range(1, 11, 2):
quotient , remainder = divmod(int(normalized_vat_number[i]) * 2, 10) quotient , remainder = divmod(int(normalized_vat_number[i]) * 2, 10)
total += quotient + remainder total += quotient + remainder
return smart_unicode((10 - total % 10) % 10) return smart_text((10 - total % 10) % 10)

View File

@ -10,7 +10,7 @@ from django.contrib.localflavor.nl.nl_provinces import PROVINCE_CHOICES
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, Select 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 _ from django.utils.translation import ugettext_lazy as _
@ -61,7 +61,7 @@ class NLPhoneNumberField(Field):
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' 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): if len(phone_nr) == 10 and numeric_re.search(phone_nr):
return value return value

View File

@ -8,7 +8,7 @@ import re
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, RegexField 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 _ from django.utils.translation import ugettext_lazy as _
phone_digits_re = re.compile(r'^(\d{9}|(00|\+)\d*)$') phone_digits_re = re.compile(r'^(\d{9}|(00|\+)\d*)$')
@ -29,7 +29,7 @@ class PTZipCodeField(RegexField):
return '%s-%s' % (cleaned[:4],cleaned[4:]) return '%s-%s' % (cleaned[:4],cleaned[4:])
else: else:
return cleaned return cleaned
class PTPhoneNumberField(Field): class PTPhoneNumberField(Field):
""" """
Validate local Portuguese phone number (including international ones) Validate local Portuguese phone number (including international ones)
@ -43,7 +43,7 @@ class PTPhoneNumberField(Field):
super(PTPhoneNumberField, self).clean(value) super(PTPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub('(\.|\s)', '', smart_unicode(value)) value = re.sub('(\.|\s)', '', smart_text(value))
m = phone_digits_re.search(value) m = phone_digits_re.search(value)
if m: if m:
return '%s' % value return '%s' % value

View File

@ -10,7 +10,7 @@ from django.contrib.localflavor.tr.tr_provinces import PROVINCE_CHOICES
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, RegexField, Select, CharField 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 _ from django.utils.translation import ugettext_lazy as _
@ -46,7 +46,7 @@ class TRPhoneNumberField(CharField):
super(TRPhoneNumberField, self).clean(value) super(TRPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) value = re.sub('(\(|\)|\s+)', '', smart_text(value))
m = phone_digits_re.search(value) m = phone_digits_re.search(value)
if m: if m:
return '%s%s' % (m.group(2), m.group(4)) return '%s%s' % (m.group(2), m.group(4))

View File

@ -9,7 +9,7 @@ import re
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, RegexField, Select, CharField 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 _ from django.utils.translation import ugettext_lazy as _
@ -34,7 +34,7 @@ class USPhoneNumberField(CharField):
super(USPhoneNumberField, self).clean(value) super(USPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
return '' return ''
value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) value = re.sub('(\(|\)|\s+)', '', smart_text(value))
m = phone_digits_re.search(value) m = phone_digits_re.search(value)
if m: if m:
return '%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) return '%s-%s-%s' % (m.group(1), m.group(2), m.group(3))

View File

@ -13,7 +13,7 @@ markup syntaxes to HTML; currently there is support for:
from django import template from django import template
from django.conf import settings from django.conf import settings
from django.utils.encoding import smart_str, force_unicode from django.utils.encoding import smart_bytes, force_text
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
register = template.Library() register = template.Library()
@ -25,9 +25,9 @@ def textile(value):
except ImportError: except ImportError:
if settings.DEBUG: if settings.DEBUG:
raise template.TemplateSyntaxError("Error in 'textile' filter: The Python textile library isn't installed.") raise template.TemplateSyntaxError("Error in 'textile' filter: The Python textile library isn't installed.")
return force_unicode(value) return force_text(value)
else: 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) @register.filter(is_safe=True)
def markdown(value, arg=''): def markdown(value, arg=''):
@ -52,23 +52,23 @@ def markdown(value, arg=''):
except ImportError: except ImportError:
if settings.DEBUG: if settings.DEBUG:
raise template.TemplateSyntaxError("Error in 'markdown' filter: The Python markdown library isn't installed.") raise template.TemplateSyntaxError("Error in 'markdown' filter: The Python markdown library isn't installed.")
return force_unicode(value) return force_text(value)
else: else:
markdown_vers = getattr(markdown, "version_info", 0) markdown_vers = getattr(markdown, "version_info", 0)
if markdown_vers < (2, 1): if markdown_vers < (2, 1):
if settings.DEBUG: if settings.DEBUG:
raise template.TemplateSyntaxError( raise template.TemplateSyntaxError(
"Error in 'markdown' filter: Django does not support versions of the Python markdown library < 2.1.") "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: else:
extensions = [e for e in arg.split(",") if e] extensions = [e for e in arg.split(",") if e]
if extensions and extensions[0] == "safe": if extensions and extensions[0] == "safe":
extensions = extensions[1:] extensions = extensions[1:]
return mark_safe(markdown.markdown( 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: else:
return mark_safe(markdown.markdown( return mark_safe(markdown.markdown(
force_unicode(value), extensions, safe_mode=False)) force_text(value), extensions, safe_mode=False))
@register.filter(is_safe=True) @register.filter(is_safe=True)
def restructuredtext(value): def restructuredtext(value):
@ -77,8 +77,8 @@ def restructuredtext(value):
except ImportError: except ImportError:
if settings.DEBUG: if settings.DEBUG:
raise template.TemplateSyntaxError("Error in 'restructuredtext' filter: The Python docutils library isn't installed.") raise template.TemplateSyntaxError("Error in 'restructuredtext' filter: The Python docutils library isn't installed.")
return force_unicode(value) return force_text(value)
else: else:
docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {}) docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
parts = publish_parts(source=smart_str(value), writer_name="html4css1", settings_overrides=docutils_settings) parts = publish_parts(source=smart_bytes(value), writer_name="html4css1", settings_overrides=docutils_settings)
return mark_safe(force_unicode(parts["fragment"])) return mark_safe(force_text(parts["fragment"]))

View File

@ -1,7 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.conf import settings 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 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. and ``extra_tags`` to unicode in case they are lazy translations.
Known "safe" types (None, int, etc.) are not converted (see Django's 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.message = force_text(self.message, strings_only=True)
self.extra_tags = force_unicode(self.extra_tags, strings_only=True) self.extra_tags = force_text(self.extra_tags, strings_only=True)
def __eq__(self, other): def __eq__(self, other):
return isinstance(other, Message) and self.level == other.level and \ return isinstance(other, Message) and self.level == other.level and \
self.message == other.message self.message == other.message
def __unicode__(self): def __unicode__(self):
return force_unicode(self.message) return force_text(self.message)
def _get_tags(self): 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) 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: if extra_tags and label_tag:
return ' '.join([extra_tags, label_tag]) return ' '.join([extra_tags, label_tag])
elif extra_tags: elif extra_tags:

View File

@ -1,7 +1,7 @@
from django.contrib.sessions.backends.base import SessionBase, CreateError from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.core.exceptions import SuspiciousOperation from django.core.exceptions import SuspiciousOperation
from django.db import IntegrityError, transaction, router 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 from django.utils import timezone
@ -18,7 +18,7 @@ class SessionStore(SessionBase):
session_key = self.session_key, session_key = self.session_key,
expire_date__gt=timezone.now() 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): except (Session.DoesNotExist, SuspiciousOperation):
self.create() self.create()
return {} return {}

View File

@ -6,7 +6,7 @@ from optparse import make_option
from django.core.files.storage import FileSystemStorage from django.core.files.storage import FileSystemStorage
from django.core.management.base import CommandError, NoArgsCommand 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.utils.datastructures import SortedDict
from django.contrib.staticfiles import finders, storage 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) fpath = os.path.join(path, f)
if self.dry_run: if self.dry_run:
self.log("Pretending to delete '%s'" % self.log("Pretending to delete '%s'" %
smart_unicode(fpath), level=1) smart_text(fpath), level=1)
else: else:
self.log("Deleting '%s'" % smart_unicode(fpath), level=1) self.log("Deleting '%s'" % smart_text(fpath), level=1)
self.storage.delete(fpath) self.storage.delete(fpath)
for d in dirs: for d in dirs:
self.clear_dir(os.path.join(path, d)) self.clear_dir(os.path.join(path, d))

View File

@ -3,7 +3,7 @@ from __future__ import unicode_literals
import os import os
from optparse import make_option from optparse import make_option
from django.core.management.base import LabelCommand 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 from django.contrib.staticfiles import finders
@ -19,12 +19,12 @@ class Command(LabelCommand):
def handle_label(self, path, **options): def handle_label(self, path, **options):
verbosity = int(options.get('verbosity', 1)) verbosity = int(options.get('verbosity', 1))
result = finders.find(path, all=options['all']) result = finders.find(path, all=options['all'])
path = smart_unicode(path) path = smart_text(path)
if result: if result:
if not isinstance(result, (list, tuple)): if not isinstance(result, (list, tuple)):
result = [result] result = [result]
output = '\n '.join( 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)) self.stdout.write("Found '%s' here:\n %s" % (path, output))
else: else:
if verbosity >= 1: if verbosity >= 1:

View File

@ -16,7 +16,7 @@ from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.core.files.storage import FileSystemStorage, get_storage_class from django.core.files.storage import FileSystemStorage, get_storage_class
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.encoding import force_unicode, smart_str from django.utils.encoding import force_text, smart_bytes
from django.utils.functional import LazyObject from django.utils.functional import LazyObject
from django.utils.importlib import import_module from django.utils.importlib import import_module
@ -112,7 +112,7 @@ class CachedFilesMixin(object):
return urlunsplit(unparsed_name) return urlunsplit(unparsed_name)
def cache_key(self, name): def cache_key(self, name):
return 'staticfiles:%s' % hashlib.md5(smart_str(name)).hexdigest() return 'staticfiles:%s' % hashlib.md5(smart_bytes(name)).hexdigest()
def url(self, name, force=False): def url(self, name, force=False):
""" """
@ -248,9 +248,9 @@ class CachedFilesMixin(object):
if hashed_file_exists: if hashed_file_exists:
self.delete(hashed_name) self.delete(hashed_name)
# then save the processed result # then save the processed result
content_file = ContentFile(smart_str(content)) content_file = ContentFile(smart_bytes(content))
saved_name = self._save(hashed_name, content_file) saved_name = self._save(hashed_name, content_file)
hashed_name = force_unicode(saved_name.replace('\\', '/')) hashed_name = force_text(saved_name.replace('\\', '/'))
processed = True processed = True
else: else:
# or handle the case in which neither processing nor # or handle the case in which neither processing nor
@ -258,7 +258,7 @@ class CachedFilesMixin(object):
if not hashed_file_exists: if not hashed_file_exists:
processed = True processed = True
saved_name = self._save(hashed_name, original_file) 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 # and then set the cache accordingly
hashed_paths[self.cache_key(name)] = hashed_name hashed_paths[self.cache_key(name)] = hashed_name

View File

@ -6,7 +6,7 @@ from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from django.template import loader, TemplateDoesNotExist, RequestContext from django.template import loader, TemplateDoesNotExist, RequestContext
from django.utils import feedgenerator, tzinfo 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.html import escape
from django.utils.timezone import is_naive from django.utils.timezone import is_naive
@ -43,10 +43,10 @@ class Feed(object):
def item_title(self, item): def item_title(self, item):
# Titles should be double escaped by default (see #6533) # Titles should be double escaped by default (see #6533)
return escape(force_unicode(item)) return escape(force_text(item))
def item_description(self, item): def item_description(self, item):
return force_unicode(item) return force_text(item)
def item_link(self, item): def item_link(self, item):
try: try:
@ -154,9 +154,9 @@ class Feed(object):
enc_url = self.__get_dynamic_attr('item_enclosure_url', item) enc_url = self.__get_dynamic_attr('item_enclosure_url', item)
if enc_url: if enc_url:
enc = feedgenerator.Enclosure( enc = feedgenerator.Enclosure(
url = smart_unicode(enc_url), url = smart_text(enc_url),
length = smart_unicode(self.__get_dynamic_attr('item_enclosure_length', item)), length = smart_text(self.__get_dynamic_attr('item_enclosure_length', item)),
mime_type = smart_unicode(self.__get_dynamic_attr('item_enclosure_mime_type', item)) mime_type = smart_text(self.__get_dynamic_attr('item_enclosure_mime_type', item))
) )
author_name = self.__get_dynamic_attr('item_author_name', item) author_name = self.__get_dynamic_attr('item_author_name', item)
if author_name is not None: if author_name is not None:

View File

@ -3,7 +3,7 @@
import warnings import warnings
from django.core.exceptions import ImproperlyConfigured, DjangoRuntimeWarning 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 from django.utils.importlib import import_module
class InvalidCacheBackendError(ImproperlyConfigured): 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 the `key_prefix'. KEY_FUNCTION can be used to specify an alternate
function with custom key making behavior. 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): def get_key_func(key_func):
""" """
@ -62,7 +62,7 @@ class BaseCache(object):
except (ValueError, TypeError): except (ValueError, TypeError):
self._cull_frequency = 3 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.version = params.get('VERSION', 1)
self.key_func = get_key_func(params.get('KEY_FUNCTION', None)) self.key_func = get_key_func(params.get('KEY_FUNCTION', None))

View File

@ -9,7 +9,7 @@ RequestContext.
from django.conf import settings from django.conf import settings
from django.middleware.csrf import get_token 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 from django.utils.functional import lazy
def csrf(request): def csrf(request):
@ -25,7 +25,7 @@ def csrf(request):
# instead of returning an empty dict. # instead of returning an empty dict.
return b'NOTPROVIDED' return b'NOTPROVIDED'
else: else:
return smart_str(token) return smart_bytes(token)
_get_val = lazy(_get_val, str) _get_val = lazy(_get_val, str)
return {'csrf_token': _get_val() } return {'csrf_token': _get_val() }

View File

@ -43,7 +43,7 @@ class ValidationError(Exception):
"""An error while validating data.""" """An error while validating data."""
def __init__(self, message, code=None, params=None): def __init__(self, message, code=None, params=None):
import operator 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 ValidationError can be passed any object that can be printed (usually
a string), a list of objects or a dictionary. a string), a list of objects or a dictionary.
@ -54,11 +54,11 @@ class ValidationError(Exception):
message = reduce(operator.add, message.values()) message = reduce(operator.add, message.values())
if isinstance(message, list): if isinstance(message, list):
self.messages = [force_unicode(msg) for msg in message] self.messages = [force_text(msg) for msg in message]
else: else:
self.code = code self.code = code
self.params = params self.params = params
message = force_unicode(message) message = force_text(message)
self.messages = [message] self.messages = [message]
def __str__(self): def __str__(self):

View File

@ -3,7 +3,7 @@ from __future__ import unicode_literals
import os import os
from io import BytesIO 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 from django.core.files.utils import FileProxyMixin
class File(FileProxyMixin): class File(FileProxyMixin):
@ -18,10 +18,10 @@ class File(FileProxyMixin):
self.mode = file.mode self.mode = file.mode
def __str__(self): def __str__(self):
return smart_str(self.name or '') return smart_bytes(self.name or '')
def __unicode__(self): def __unicode__(self):
return smart_unicode(self.name or '') return smart_text(self.name or '')
def __repr__(self): def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self or "None") return "<%s: %s>" % (self.__class__.__name__, self or "None")

View File

@ -11,7 +11,7 @@ from django.conf import settings
from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
from django.core.files import locks, File from django.core.files import locks, File
from django.core.files.move import file_move_safe 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.functional import LazyObject
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.text import get_valid_filename from django.utils.text import get_valid_filename
@ -48,7 +48,7 @@ class Storage(object):
name = self._save(name, content) name = self._save(name, content)
# Store filenames with forward slashes, even on Windows # 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. # These methods are part of the public API, with default implementations.

View File

@ -8,7 +8,7 @@ from io import BytesIO
from django.conf import settings from django.conf import settings
from django.core.files.base import File from django.core.files.base import File
from django.core.files import temp as tempfile 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', __all__ = ('UploadedFile', 'TemporaryUploadedFile', 'InMemoryUploadedFile',
'SimpleUploadedFile') 'SimpleUploadedFile')
@ -30,7 +30,7 @@ class UploadedFile(File):
self.charset = charset self.charset = charset
def __repr__(self): def __repr__(self):
return smart_str("<%s: %s (%s)>" % ( return smart_bytes("<%s: %s (%s)>" % (
self.__class__.__name__, self.name, self.content_type)) self.__class__.__name__, self.name, self.content_type))
def _get_name(self): def _get_name(self):

View File

@ -4,7 +4,7 @@ import sys
from django import http from django import http
from django.core import signals 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.importlib import import_module
from django.utils.log import getLogger from django.utils.log import getLogger
from django.utils import six from django.utils import six
@ -250,7 +250,7 @@ def get_script_name(environ):
""" """
from django.conf import settings from django.conf import settings
if settings.FORCE_SCRIPT_NAME is not None: 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 # 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 # 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: if not script_url:
script_url = environ.get('REDIRECT_URL', '') script_url = environ.get('REDIRECT_URL', '')
if script_url: if script_url:
return force_unicode(script_url[:-len(environ.get('PATH_INFO', ''))]) return force_text(script_url[:-len(environ.get('PATH_INFO', ''))])
return force_unicode(environ.get('SCRIPT_NAME', '')) return force_text(environ.get('SCRIPT_NAME', ''))

View File

@ -9,7 +9,7 @@ from django.core import signals
from django.core.handlers import base from django.core.handlers import base
from django.core.urlresolvers import set_script_prefix from django.core.urlresolvers import set_script_prefix
from django.utils import datastructures 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 from django.utils.log import getLogger
logger = getLogger('django.request') logger = getLogger('django.request')
@ -127,7 +127,7 @@ class LimitedStream(object):
class WSGIRequest(http.HttpRequest): class WSGIRequest(http.HttpRequest):
def __init__(self, environ): def __init__(self, environ):
script_name = base.get_script_name(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: if not path_info or path_info == script_name:
# Sometimes PATH_INFO exists, but is empty (e.g. accessing # Sometimes PATH_INFO exists, but is empty (e.g. accessing
# the SCRIPT_NAME URL without a trailing slash). We really need to # 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()] response_headers = [(str(k), str(v)) for k, v in response.items()]
for c in response.cookies.values(): for c in response.cookies.values():
response_headers.append((str('Set-Cookie'), str(c.output(header='')))) 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 return response

View File

@ -15,7 +15,7 @@ from io import BytesIO
from django.conf import settings from django.conf import settings
from django.core.mail.utils import DNS_NAME 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 from django.utils import six
@ -79,7 +79,7 @@ ADDRESS_HEADERS = set([
def forbid_multi_line_headers(name, val, encoding): def forbid_multi_line_headers(name, val, encoding):
"""Forbids multi-line headers, to prevent header injection.""" """Forbids multi-line headers, to prevent header injection."""
encoding = encoding or settings.DEFAULT_CHARSET encoding = encoding or settings.DEFAULT_CHARSET
val = force_unicode(val) val = force_text(val)
if '\n' in val or '\r' in val: if '\n' in val or '\r' in val:
raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name)) raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name))
try: try:
@ -93,12 +93,12 @@ def forbid_multi_line_headers(name, val, encoding):
else: else:
if name.lower() == 'subject': if name.lower() == 'subject':
val = Header(val) val = Header(val)
return smart_str(name), val return smart_bytes(name), val
def sanitize_address(addr, encoding): def sanitize_address(addr, encoding):
if isinstance(addr, six.string_types): if isinstance(addr, six.string_types):
addr = parseaddr(force_unicode(addr)) addr = parseaddr(force_text(addr))
nm, addr = addr nm, addr = addr
nm = str(Header(nm, encoding)) nm = str(Header(nm, encoding))
try: try:
@ -210,7 +210,7 @@ class EmailMessage(object):
def message(self): def message(self):
encoding = self.encoding or settings.DEFAULT_CHARSET 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) self.content_subtype, encoding)
msg = self._create_message(msg) msg = self._create_message(msg)
msg['Subject'] = self.subject msg['Subject'] = self.subject
@ -293,7 +293,7 @@ class EmailMessage(object):
basetype, subtype = mimetype.split('/', 1) basetype, subtype = mimetype.split('/', 1)
if basetype == 'text': if basetype == 'text':
encoding = self.encoding or settings.DEFAULT_CHARSET encoding = self.encoding or settings.DEFAULT_CHARSET
attachment = SafeMIMEText(smart_str(content, encoding), subtype, encoding) attachment = SafeMIMEText(smart_bytes(content, encoding), subtype, encoding)
else: else:
# Encode non-text attachments with base64. # Encode non-text attachments with base64.
attachment = MIMEBase(basetype, subtype) attachment = MIMEBase(basetype, subtype)

View File

@ -4,7 +4,7 @@ from django.core.cache.backends.db import BaseDatabaseCache
from django.core.management.base import LabelCommand, CommandError from django.core.management.base import LabelCommand, CommandError
from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
from django.db.utils import DatabaseError from django.db.utils import DatabaseError
from django.utils.encoding import force_unicode from django.utils.encoding import force_text
class Command(LabelCommand): class Command(LabelCommand):
@ -60,7 +60,7 @@ class Command(LabelCommand):
transaction.rollback_unless_managed(using=db) transaction.rollback_unless_managed(using=db)
raise CommandError( raise CommandError(
"Cache table '%s' could not be created.\nThe error was: %s." % "Cache table '%s' could not be created.\nThe error was: %s." %
(tablename, force_unicode(e))) (tablename, force_text(e)))
for statement in index_output: for statement in index_output:
curs.execute(statement) curs.execute(statement)
transaction.commit_unless_managed(using=db) transaction.commit_unless_managed(using=db)

View File

@ -14,7 +14,7 @@ from django.core.management.color import no_style
from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS, from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS,
IntegrityError, DatabaseError) IntegrityError, DatabaseError)
from django.db.models import get_apps 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 from itertools import product
try: try:
@ -189,7 +189,7 @@ class Command(BaseCommand):
'app_label': obj.object._meta.app_label, 'app_label': obj.object._meta.app_label,
'object_name': obj.object._meta.object_name, 'object_name': obj.object._meta.object_name,
'pk': obj.object.pk, 'pk': obj.object.pk,
'error_msg': force_unicode(e) 'error_msg': force_text(e)
},) },)
raise raise

View File

@ -5,7 +5,7 @@ Module for abstract serializer/unserializer base classes.
from io import BytesIO from io import BytesIO
from django.db import models from django.db import models
from django.utils.encoding import smart_unicode from django.utils.encoding import smart_text
from django.utils import six from django.utils import six
class SerializerDoesNotExist(KeyError): class SerializerDoesNotExist(KeyError):

View File

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

View File

@ -8,7 +8,7 @@ from __future__ import unicode_literals
from django.conf import settings from django.conf import settings
from django.core.serializers import base from django.core.serializers import base
from django.db import models, DEFAULT_DB_ALIAS 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 from django.utils import six
class Serializer(base.Serializer): class Serializer(base.Serializer):
@ -34,8 +34,8 @@ class Serializer(base.Serializer):
def get_dump_object(self, obj): def get_dump_object(self, obj):
return { return {
"pk": smart_unicode(obj._get_pk_val(), strings_only=True), "pk": smart_text(obj._get_pk_val(), strings_only=True),
"model": smart_unicode(obj._meta), "model": smart_text(obj._meta),
"fields": self._current "fields": self._current
} }
@ -65,7 +65,7 @@ class Serializer(base.Serializer):
if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'): if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'):
m2m_value = lambda value: value.natural_key() m2m_value = lambda value: value.natural_key()
else: 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) self._current[field.name] = [m2m_value(related)
for related in getattr(obj, field.name).iterator()] for related in getattr(obj, field.name).iterator()]
@ -90,7 +90,7 @@ def Deserializer(object_list, **options):
# Handle each field # Handle each field
for (field_name, field_value) in six.iteritems(d["fields"]): for (field_name, field_value) in six.iteritems(d["fields"]):
if isinstance(field_value, str): 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) field = Model._meta.get_field(field_name)
@ -101,9 +101,9 @@ def Deserializer(object_list, **options):
if hasattr(value, '__iter__'): if hasattr(value, '__iter__'):
return field.rel.to._default_manager.db_manager(db).get_by_natural_key(*value).pk return field.rel.to._default_manager.db_manager(db).get_by_natural_key(*value).pk
else: else:
return smart_unicode(field.rel.to._meta.pk.to_python(value)) return smart_text(field.rel.to._meta.pk.to_python(value))
else: 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] m2m_data[field.name] = [m2m_convert(pk) for pk in field_value]
# Handle FK fields # Handle FK fields

View File

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

View File

@ -8,7 +8,7 @@ from django.conf import settings
from django.core.serializers import base from django.core.serializers import base
from django.db import models, DEFAULT_DB_ALIAS from django.db import models, DEFAULT_DB_ALIAS
from django.utils.xmlutils import SimplerXMLGenerator 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 from xml.dom import pulldom
class Serializer(base.Serializer): class Serializer(base.Serializer):
@ -46,11 +46,11 @@ class Serializer(base.Serializer):
self.indent(1) self.indent(1)
obj_pk = obj._get_pk_val() obj_pk = obj._get_pk_val()
if obj_pk is None: if obj_pk is None:
attrs = {"model": smart_unicode(obj._meta),} attrs = {"model": smart_text(obj._meta),}
else: else:
attrs = { attrs = {
"pk": smart_unicode(obj._get_pk_val()), "pk": smart_text(obj._get_pk_val()),
"model": smart_unicode(obj._meta), "model": smart_text(obj._meta),
} }
self.xml.startElement("object", attrs) self.xml.startElement("object", attrs)
@ -96,10 +96,10 @@ class Serializer(base.Serializer):
# Iterable natural keys are rolled out as subelements # Iterable natural keys are rolled out as subelements
for key_value in related: for key_value in related:
self.xml.startElement("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("natural")
else: else:
self.xml.characters(smart_unicode(related_att)) self.xml.characters(smart_text(related_att))
else: else:
self.xml.addQuickElement("None") self.xml.addQuickElement("None")
self.xml.endElement("field") self.xml.endElement("field")
@ -120,13 +120,13 @@ class Serializer(base.Serializer):
self.xml.startElement("object", {}) self.xml.startElement("object", {})
for key_value in natural: for key_value in natural:
self.xml.startElement("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("natural")
self.xml.endElement("object") self.xml.endElement("object")
else: else:
def handle_m2m(value): def handle_m2m(value):
self.xml.addQuickElement("object", attrs={ 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(): for relobj in getattr(obj, field.name).iterator():
handle_m2m(relobj) handle_m2m(relobj)
@ -141,7 +141,7 @@ class Serializer(base.Serializer):
self.xml.startElement("field", { self.xml.startElement("field", {
"name" : field.name, "name" : field.name,
"rel" : field.rel.__class__.__name__, "rel" : field.rel.__class__.__name__,
"to" : smart_unicode(field.rel.to._meta), "to" : smart_text(field.rel.to._meta),
}) })
class Deserializer(base.Deserializer): class Deserializer(base.Deserializer):

View File

@ -41,7 +41,7 @@ from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.utils import baseconv from django.utils import baseconv
from django.utils.crypto import constant_time_compare, salted_hmac 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 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 Reverse of dumps(), raises BadSignature if signature fails
""" """
base64d = smart_str( base64d = smart_bytes(
TimestampSigner(key, salt=salt).unsign(s, max_age=max_age)) TimestampSigner(key, salt=salt).unsign(s, max_age=max_age))
decompress = False decompress = False
if base64d[0] == '.': if base64d[0] == '.':
@ -159,16 +159,16 @@ class Signer(object):
return base64_hmac(self.salt + 'signer', value, self.key) return base64_hmac(self.salt + 'signer', value, self.key)
def sign(self, value): def sign(self, value):
value = smart_str(value) value = smart_bytes(value)
return '%s%s%s' % (value, self.sep, self.signature(value)) return '%s%s%s' % (value, self.sep, self.signature(value))
def unsign(self, signed_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: if not self.sep in signed_value:
raise BadSignature('No "%s" found in value' % self.sep) raise BadSignature('No "%s" found in value' % self.sep)
value, sig = signed_value.rsplit(self.sep, 1) value, sig = signed_value.rsplit(self.sep, 1)
if constant_time_compare(sig, self.signature(value)): if constant_time_compare(sig, self.signature(value)):
return force_unicode(value) return force_text(value)
raise BadSignature('Signature "%s" does not match' % sig) raise BadSignature('Signature "%s" does not match' % sig)
@ -178,7 +178,7 @@ class TimestampSigner(Signer):
return baseconv.base62.encode(int(time.time())) return baseconv.base62.encode(int(time.time()))
def sign(self, value): 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)) return '%s%s%s' % (value, self.sep, self.signature(value))
def unsign(self, value, max_age=None): def unsign(self, value, max_age=None):

View File

@ -14,7 +14,7 @@ from threading import local
from django.http import Http404 from django.http import Http404
from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
from django.utils.datastructures import MultiValueDict 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.functional import memoize, lazy
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.module_loading import module_has_submodule from django.utils.module_loading import module_has_submodule
@ -163,7 +163,7 @@ class LocaleRegexProvider(object):
if isinstance(self._regex, six.string_types): if isinstance(self._regex, six.string_types):
regex = self._regex regex = self._regex
else: else:
regex = force_unicode(self._regex) regex = force_text(self._regex)
try: try:
compiled_regex = re.compile(regex, re.UNICODE) compiled_regex = re.compile(regex, re.UNICODE)
except re.error as e: except re.error as e:
@ -190,7 +190,7 @@ class RegexURLPattern(LocaleRegexProvider):
self.name = name self.name = name
def __repr__(self): 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): def add_prefix(self, prefix):
""" """
@ -240,7 +240,7 @@ class RegexURLResolver(LocaleRegexProvider):
self._app_dict = {} self._app_dict = {}
def __repr__(self): 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): def _populate(self):
lookups = MultiValueDict() lookups = MultiValueDict()
@ -373,7 +373,7 @@ class RegexURLResolver(LocaleRegexProvider):
if args: if args:
if len(args) != len(params) + len(prefix_args): if len(args) != len(params) + len(prefix_args):
continue 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)) candidate = (prefix_norm + result) % dict(zip(prefix_args + params, unicode_args))
else: else:
if set(kwargs.keys()) | set(defaults.keys()) != set(params) | set(defaults.keys()) | set(prefix_args): if set(kwargs.keys()) | set(defaults.keys()) != set(params) | set(defaults.keys()) | set(prefix_args):
@ -385,7 +385,7 @@ class RegexURLResolver(LocaleRegexProvider):
break break
if not matches: if not matches:
continue 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 candidate = (prefix_norm + result) % unicode_kwargs
if re.search('^%s%s' % (_prefix, pattern), candidate, re.UNICODE): if re.search('^%s%s' % (_prefix, pattern), candidate, re.UNICODE):
return candidate return candidate

View File

@ -8,7 +8,7 @@ except ImportError: # Python 2
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _ 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.ipv6 import is_valid_ipv6_address
from django.utils import six from django.utils import six
@ -36,7 +36,7 @@ class RegexValidator(object):
""" """
Validates that the input matches the regular expression. 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) raise ValidationError(self.message, code=self.code)
class URLValidator(RegexValidator): class URLValidator(RegexValidator):
@ -54,7 +54,7 @@ class URLValidator(RegexValidator):
except ValidationError as e: except ValidationError as e:
# Trivial case failed. Try for possible IDN domain # Trivial case failed. Try for possible IDN domain
if value: if value:
value = smart_unicode(value) value = smart_text(value)
scheme, netloc, path, query, fragment = urlsplit(value) scheme, netloc, path, query, fragment = urlsplit(value)
try: try:
netloc = netloc.encode('idna') # IDN -> ACE netloc = netloc.encode('idna') # IDN -> ACE

View File

@ -607,16 +607,16 @@ class BaseDatabaseOperations(object):
exists for database backends to provide a better implementation exists for database backends to provide a better implementation
according to their own quoting schemes. 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. # 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)): if isinstance(params, (list, tuple)):
u_params = tuple([to_unicode(val) for val in params]) u_params = tuple([to_unicode(val) for val in params])
else: else:
u_params = dict([(to_unicode(k), to_unicode(v)) for k, v in params.items()]) 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): def last_insert_id(self, cursor, table_name, pk_name):
""" """
@ -800,8 +800,8 @@ class BaseDatabaseOperations(object):
def prep_for_like_query(self, x): def prep_for_like_query(self, x):
"""Prepares a value for use in a LIKE query.""" """Prepares a value for use in a LIKE query."""
from django.utils.encoding import smart_unicode from django.utils.encoding import smart_text
return smart_unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_") return smart_text(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")
# Same as prep_for_like_query(), but called for "iexact" matches, which # Same as prep_for_like_query(), but called for "iexact" matches, which
# need not necessarily be implemented using "LIKE" in the backend. # need not necessarily be implemented using "LIKE" in the backend.

View File

@ -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.client import DatabaseClient
from django.db.backends.oracle.creation import DatabaseCreation from django.db.backends.oracle.creation import DatabaseCreation
from django.db.backends.oracle.introspection import DatabaseIntrospection from django.db.backends.oracle.introspection import DatabaseIntrospection
from django.utils.encoding import smart_str, force_unicode from django.utils.encoding import smart_bytes, force_text
from django.utils import six from django.utils import six
from django.utils import timezone 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 # Check whether cx_Oracle was compiled with the WITH_UNICODE option. This will
# also be True in Python 3.0. # also be True in Python 3.0.
if int(Database.version.split('.', 1)[0]) >= 5 and not hasattr(Database, 'UNICODE'): if int(Database.version.split('.', 1)[0]) >= 5 and not hasattr(Database, 'UNICODE'):
convert_unicode = force_unicode convert_unicode = force_text
else: else:
convert_unicode = smart_str convert_unicode = smart_bytes
class DatabaseFeatures(BaseDatabaseFeatures): class DatabaseFeatures(BaseDatabaseFeatures):
@ -162,7 +162,7 @@ WHEN (new.%(col_name)s IS NULL)
if isinstance(value, Database.LOB): if isinstance(value, Database.LOB):
value = value.read() value = value.read()
if field and field.get_internal_type() == 'TextField': 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 # Oracle stores empty strings as null. We need to undo this in
# order to adhere to the Django convention of using the empty # 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): def process_clob(self, value):
if value is None: if value is None:
return '' return ''
return force_unicode(value.read()) return force_text(value.read())
def quote_name(self, name): def quote_name(self, name):
# SQL92 requires delimited (quoted) names to be case-sensitive. When # 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) param = param.astimezone(timezone.utc).replace(tzinfo=None)
if hasattr(param, 'bind_parameter'): if hasattr(param, 'bind_parameter'):
self.smart_str = param.bind_parameter(cursor) self.smart_bytes = param.bind_parameter(cursor)
else: else:
self.smart_str = convert_unicode(param, cursor.charset, self.smart_bytes = convert_unicode(param, cursor.charset,
strings_only) strings_only)
if hasattr(param, 'input_size'): if hasattr(param, 'input_size'):
# If parameter has `input_size` attribute, use that. # If parameter has `input_size` attribute, use that.
@ -676,7 +676,7 @@ class FormatStylePlaceholderCursor(object):
self.setinputsizes(*sizes) self.setinputsizes(*sizes)
def _param_generator(self, params): 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): def execute(self, query, params=None):
if params is None: if params is None:
@ -831,7 +831,7 @@ def to_unicode(s):
unchanged). unchanged).
""" """
if isinstance(s, six.string_types): if isinstance(s, six.string_types):
return force_unicode(s) return force_text(s)
return s return s

View File

@ -23,7 +23,7 @@ from django.db.models import signals
from django.db.models.loading import register_models, get_model from django.db.models.loading import register_models, get_model
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils.functional import curry 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 import six
from django.utils.text import get_text_list, capfirst 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) u = six.text_type(self)
except (UnicodeEncodeError, UnicodeDecodeError): except (UnicodeEncodeError, UnicodeDecodeError):
u = '[Bad Unicode data]' 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): def __str__(self):
if hasattr(self, '__unicode__'): if hasattr(self, '__unicode__'):
return force_unicode(self).encode('utf-8') return force_text(self).encode('utf-8')
return '%s object' % self.__class__.__name__ return '%s object' % self.__class__.__name__
def __eq__(self, other): def __eq__(self, other):
@ -605,14 +605,14 @@ class Model(six.with_metaclass(ModelBase, object)):
def _get_FIELD_display(self, field): def _get_FIELD_display(self, field):
value = getattr(self, field.attname) 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): def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
if not self.pk: if not self.pk:
raise ValueError("get_next/get_previous cannot be used on unsaved objects.") raise ValueError("get_next/get_previous cannot be used on unsaved objects.")
op = is_next and 'gt' or 'lt' op = is_next and 'gt' or 'lt'
order = not is_next and '-' or '' 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(**{'%s__%s' % (field.name, op): param})
q = q|Q(**{field.name: param, 'pk__%s' % op: self.pk}) 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) qs = self.__class__._default_manager.using(self._state.db).filter(**kwargs).filter(q).order_by('%s%s' % (order, field.name), '%spk' % order)

View File

@ -19,7 +19,7 @@ from django.utils.functional import curry, total_ordering
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ 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.ipv6 import clean_ipv6_address
from django.utils import six from django.utils import six
@ -386,7 +386,7 @@ class Field(object):
if self.has_default(): if self.has_default():
if callable(self.default): if callable(self.default):
return 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 if (not self.empty_strings_allowed or (self.null and
not connection.features.interprets_empty_strings_as_nulls)): not connection.features.interprets_empty_strings_as_nulls)):
return None return None
@ -404,11 +404,11 @@ class Field(object):
rel_model = self.rel.to rel_model = self.rel.to
if hasattr(self.rel, 'get_related_field'): if hasattr(self.rel, 'get_related_field'):
lst = [(getattr(x, self.rel.get_related_field().attname), lst = [(getattr(x, self.rel.get_related_field().attname),
smart_unicode(x)) smart_text(x))
for x in rel_model._default_manager.complex_filter( for x in rel_model._default_manager.complex_filter(
self.rel.limit_choices_to)] self.rel.limit_choices_to)]
else: 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( for x in rel_model._default_manager.complex_filter(
self.rel.limit_choices_to)] self.rel.limit_choices_to)]
return first_choice + lst return first_choice + lst
@ -435,7 +435,7 @@ class Field(object):
Returns a string value of this field from the passed obj. Returns a string value of this field from the passed obj.
This is used by the serialization framework. 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): def bind(self, fieldmapping, original, bound_field_class):
return bound_field_class(self, fieldmapping, original) return bound_field_class(self, fieldmapping, original)
@ -629,7 +629,7 @@ class CharField(Field):
def to_python(self, value): def to_python(self, value):
if isinstance(value, six.string_types) or value is None: if isinstance(value, six.string_types) or value is None:
return value return value
return smart_unicode(value) return smart_text(value)
def get_prep_value(self, value): def get_prep_value(self, value):
return self.to_python(value) return self.to_python(value)
@ -1189,7 +1189,7 @@ class TextField(Field):
def get_prep_value(self, value): def get_prep_value(self, value):
if isinstance(value, six.string_types) or value is None: if isinstance(value, six.string_types) or value is None:
return value return value
return smart_unicode(value) return smart_text(value)
def formfield(self, **kwargs): def formfield(self, **kwargs):
defaults = {'widget': forms.Textarea} defaults = {'widget': forms.Textarea}

View File

@ -8,7 +8,7 @@ from django.core.files.base import File
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
from django.core.files.images import ImageFile from django.core.files.images import ImageFile
from django.db.models import signals 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 import six
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -280,7 +280,7 @@ class FileField(Field):
setattr(cls, self.name, self.descriptor_class(self)) setattr(cls, self.name, self.descriptor_class(self))
def get_directory_name(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): def get_filename(self, filename):
return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename))) return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename)))

View File

@ -9,7 +9,7 @@ from django.db.models.related import RelatedObject
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.db.models.query_utils import QueryWrapper from django.db.models.query_utils import QueryWrapper
from django.db.models.deletion import CASCADE 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 import six
from django.utils.translation import ugettext_lazy as _, string_concat from django.utils.translation import ugettext_lazy as _, string_concat
from django.utils.functional import curry, cached_property from django.utils.functional import curry, cached_property
@ -999,7 +999,7 @@ class ForeignKey(RelatedField, Field):
if not self.blank and self.choices: if not self.blank and self.choices:
choice_list = self.get_choices_default() choice_list = self.get_choices_default()
if len(choice_list) == 2: 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) return Field.value_to_string(self, obj)
def contribute_to_class(self, cls, name): def contribute_to_class(self, cls, name):
@ -1205,7 +1205,7 @@ class ManyToManyField(RelatedField, Field):
choices_list = self.get_choices_default() choices_list = self.get_choices_default()
if len(choices_list) == 1: if len(choices_list) == 1:
data = [choices_list[0][0]] data = [choices_list[0][0]]
return smart_unicode(data) return smart_text(data)
def contribute_to_class(self, cls, name): def contribute_to_class(self, cls, name):
# To support multiple relations to self, it's useful to have a non-None # To support multiple relations to self, it's useful to have a non-None

View File

@ -8,7 +8,7 @@ from django.db.models.fields import AutoField, FieldDoesNotExist
from django.db.models.fields.proxy import OrderWrt from django.db.models.fields.proxy import OrderWrt
from django.db.models.loading import get_models, app_cache_ready 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.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.datastructures import SortedDict
from django.utils import six from django.utils import six
@ -199,7 +199,7 @@ class Options(object):
return '<Options for %s>' % self.object_name return '<Options for %s>' % self.object_name
def __str__(self): 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): def verbose_name_raw(self):
""" """
@ -209,7 +209,7 @@ class Options(object):
""" """
lang = get_language() lang = get_language()
deactivate_all() deactivate_all()
raw = force_unicode(self.verbose_name) raw = force_text(self.verbose_name)
activate(lang) activate(lang)
return raw return raw
verbose_name_raw = property(verbose_name_raw) verbose_name_raw = property(verbose_name_raw)

View File

@ -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 from django.db.models.fields import BLANK_CHOICE_DASH
class BoundRelatedObject(object): class BoundRelatedObject(object):
@ -34,9 +34,9 @@ class RelatedObject(object):
if limit_to_currently_related: if limit_to_currently_related:
queryset = queryset.complex_filter( queryset = queryset.complex_filter(
{'%s__isnull' % self.parent_model._meta.module_name: False}) {'%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 return first_choice + lst
def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False): def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
# Defer to the actual field definition for db prep # Defer to the actual field definition for db prep
return self.field.get_db_prep_lookup(lookup_type, value, return self.field.get_db_prep_lookup(lookup_type, value,

View File

@ -10,7 +10,7 @@ all about the internals of models in order to get the information it needs.
import copy import copy
from django.utils.datastructures import SortedDict 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.tree import Node
from django.utils import six from django.utils import six
from django.db import connections, DEFAULT_DB_ALIAS from django.db import connections, DEFAULT_DB_ALIAS
@ -1776,7 +1776,7 @@ class Query(object):
else: else:
param_iter = iter([]) param_iter = iter([])
for name, entry in select.items(): for name, entry in select.items():
entry = force_unicode(entry) entry = force_text(entry)
entry_params = [] entry_params = []
pos = entry.find("%s") pos = entry.find("%s")
while pos != -1: while pos != -1:

View File

@ -10,7 +10,7 @@ from django.db.models.sql.query import Query
from django.db.models.sql.where import AND, Constraint from django.db.models.sql.where import AND, Constraint
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.functional import Promise 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 from django.utils import six
@ -105,7 +105,7 @@ class UpdateQuery(Query):
saving models. saving models.
""" """
# Check that no Promise object passes to the query. Refs #10498. # 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 if isinstance(value[2], Promise) else value
for value in values_seq] for value in values_seq]
self.values.extend(values_seq) self.values.extend(values_seq)
@ -171,7 +171,7 @@ class InsertQuery(Query):
for obj in objs: for obj in objs:
value = getattr(obj, field.attname) value = getattr(obj, field.attname)
if isinstance(value, Promise): if isinstance(value, Promise):
setattr(obj, field.attname, force_unicode(value)) setattr(obj, field.attname, force_text(value))
self.objs = objs self.objs = objs
self.raw = raw self.raw = raw

View File

@ -23,7 +23,7 @@ from django.forms.widgets import (TextInput, PasswordInput, HiddenInput,
NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput, NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput,
SplitDateTimeWidget, SplitHiddenDateTimeWidget, FILE_INPUT_CONTRADICTION) SplitDateTimeWidget, SplitHiddenDateTimeWidget, FILE_INPUT_CONTRADICTION)
from django.utils import formats 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.ipv6 import clean_ipv6_address
from django.utils import six from django.utils import six
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -78,13 +78,13 @@ class Field(object):
# validators -- List of addtional validators to use # validators -- List of addtional validators to use
# localize -- Boolean that specifies if the field should be localized. # localize -- Boolean that specifies if the field should be localized.
if label is not None: if label is not None:
label = smart_unicode(label) label = smart_text(label)
self.required, self.label, self.initial = required, label, initial self.required, self.label, self.initial = required, label, initial
self.show_hidden_initial = show_hidden_initial self.show_hidden_initial = show_hidden_initial
if help_text is None: if help_text is None:
self.help_text = '' self.help_text = ''
else: else:
self.help_text = smart_unicode(help_text) self.help_text = smart_text(help_text)
widget = widget or self.widget widget = widget or self.widget
if isinstance(widget, type): if isinstance(widget, type):
widget = widget() widget = widget()
@ -195,7 +195,7 @@ class CharField(Field):
"Returns a Unicode object." "Returns a Unicode object."
if value in validators.EMPTY_VALUES: if value in validators.EMPTY_VALUES:
return '' return ''
return smart_unicode(value) return smart_text(value)
def widget_attrs(self, widget): def widget_attrs(self, widget):
attrs = super(CharField, self).widget_attrs(widget) attrs = super(CharField, self).widget_attrs(widget)
@ -288,7 +288,7 @@ class DecimalField(Field):
return None return None
if self.localize: if self.localize:
value = formats.sanitize_separators(value) value = formats.sanitize_separators(value)
value = smart_unicode(value).strip() value = smart_text(value).strip()
try: try:
value = Decimal(value) value = Decimal(value)
except DecimalException: except DecimalException:
@ -333,7 +333,7 @@ class BaseTemporalField(Field):
def to_python(self, value): def to_python(self, value):
# Try to coerce the value to unicode. # 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): if isinstance(unicode_value, six.text_type):
value = unicode_value.strip() value = unicode_value.strip()
# If unicode, try to strptime against each input format. # If unicode, try to strptime against each input format.
@ -692,7 +692,7 @@ class ChoiceField(Field):
"Returns a Unicode object." "Returns a Unicode object."
if value in validators.EMPTY_VALUES: if value in validators.EMPTY_VALUES:
return '' return ''
return smart_unicode(value) return smart_text(value)
def validate(self, value): def validate(self, value):
""" """
@ -708,10 +708,10 @@ class ChoiceField(Field):
if isinstance(v, (list, tuple)): if isinstance(v, (list, tuple)):
# This is an optgroup, so look inside the group for options # This is an optgroup, so look inside the group for options
for k2, v2 in v: for k2, v2 in v:
if value == smart_unicode(k2): if value == smart_text(k2):
return True return True
else: else:
if value == smart_unicode(k): if value == smart_text(k):
return True return True
return False return False
@ -752,7 +752,7 @@ class MultipleChoiceField(ChoiceField):
return [] return []
elif not isinstance(value, (list, tuple)): elif not isinstance(value, (list, tuple)):
raise ValidationError(self.error_messages['invalid_list']) 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): def validate(self, value):
""" """

View File

@ -12,7 +12,7 @@ from django.forms.util import flatatt, ErrorDict, ErrorList
from django.forms.widgets import Media, media_property, TextInput, Textarea from django.forms.widgets import Media, media_property, TextInput, Textarea
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.html import conditional_escape, format_html 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.safestring import mark_safe
from django.utils import six 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. 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.is_hidden:
if bf_errors: 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)) hidden_fields.append(six.text_type(bf))
else: else:
# Create a 'class="..."' atribute if the row should have any # Create a 'class="..."' atribute if the row should have any
@ -160,10 +160,10 @@ class BaseForm(StrAndUnicode):
html_class_attr = ' class="%s"' % css_classes html_class_attr = ' class="%s"' % css_classes
if errors_on_separate_row and bf_errors: 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: 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 # Only add the suffix if the label does not end in
# punctuation. # punctuation.
if self.label_suffix: if self.label_suffix:
@ -174,20 +174,20 @@ class BaseForm(StrAndUnicode):
label = '' label = ''
if field.help_text: 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: else:
help_text = '' help_text = ''
output.append(normal_row % { output.append(normal_row % {
'errors': force_unicode(bf_errors), 'errors': force_text(bf_errors),
'label': force_unicode(label), 'label': force_text(label),
'field': six.text_type(bf), 'field': six.text_type(bf),
'help_text': help_text, 'help_text': help_text,
'html_class_attr': html_class_attr 'html_class_attr': html_class_attr
}) })
if top_errors: 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. if hidden_fields: # Insert any hidden fields in the last row.
str_hidden = ''.join(hidden_fields) str_hidden = ''.join(hidden_fields)
@ -535,8 +535,8 @@ class BoundField(StrAndUnicode):
associated Form has specified auto_id. Returns an empty string otherwise. associated Form has specified auto_id. Returns an empty string otherwise.
""" """
auto_id = self.form.auto_id auto_id = self.form.auto_id
if auto_id and '%s' in smart_unicode(auto_id): if auto_id and '%s' in smart_text(auto_id):
return smart_unicode(auto_id) % self.html_name return smart_text(auto_id) % self.html_name
elif auto_id: elif auto_id:
return self.html_name return self.html_name
return '' return ''

View File

@ -13,7 +13,7 @@ from django.forms.formsets import BaseFormSet, formset_factory
from django.forms.util import ErrorList from django.forms.util import ErrorList
from django.forms.widgets import (SelectMultiple, HiddenInput, from django.forms.widgets import (SelectMultiple, HiddenInput,
MultipleHiddenInput, media_property) 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.datastructures import SortedDict
from django.utils import six from django.utils import six
from django.utils.text import get_text_list, capfirst from django.utils.text import get_text_list, capfirst
@ -875,7 +875,7 @@ class InlineForeignKeyField(Field):
orig = getattr(self.parent_instance, self.to_field) orig = getattr(self.parent_instance, self.to_field)
else: else:
orig = self.parent_instance.pk 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']) raise ValidationError(self.error_messages['invalid_choice'])
return self.parent_instance return self.parent_instance
@ -953,7 +953,7 @@ class ModelChoiceField(ChoiceField):
generate the labels for the choices presented by this object. Subclasses generate the labels for the choices presented by this object. Subclasses
can override this method to customize the display of the choices. can override this method to customize the display of the choices.
""" """
return smart_unicode(obj) return smart_text(obj)
def _get_choices(self): def _get_choices(self):
# If self._choices is set, then somebody must have manually set # If self._choices is set, then somebody must have manually set
@ -1025,9 +1025,9 @@ class ModelMultipleChoiceField(ModelChoiceField):
except ValueError: except ValueError:
raise ValidationError(self.error_messages['invalid_pk_value'] % pk) raise ValidationError(self.error_messages['invalid_pk_value'] % pk)
qs = self.queryset.filter(**{'%s__in' % key: value}) 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: 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) raise ValidationError(self.error_messages['invalid_choice'] % val)
# Since this overrides the inherited ModelChoiceField.clean # Since this overrides the inherited ModelChoiceField.clean
# we run custom validators here # we run custom validators here

View File

@ -2,7 +2,7 @@ from __future__ import unicode_literals
from django.conf import settings from django.conf import settings
from django.utils.html import format_html, format_html_join 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.safestring import mark_safe
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -35,12 +35,12 @@ class ErrorDict(dict, StrAndUnicode):
if not self: return '' if not self: return ''
return format_html('<ul class="errorlist">{0}</ul>', return format_html('<ul class="errorlist">{0}</ul>',
format_html_join('', '<li>{0}{1}</li>', format_html_join('', '<li>{0}{1}</li>',
((k, force_unicode(v)) ((k, force_text(v))
for k, v in self.items()) for k, v in self.items())
)) ))
def as_text(self): 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): class ErrorList(list, StrAndUnicode):
""" """
@ -53,16 +53,16 @@ class ErrorList(list, StrAndUnicode):
if not self: return '' if not self: return ''
return format_html('<ul class="errorlist">{0}</ul>', return format_html('<ul class="errorlist">{0}</ul>',
format_html_join('', '<li>{0}</li>', format_html_join('', '<li>{0}</li>',
((force_unicode(e),) for e in self) ((force_text(e),) for e in self)
) )
) )
def as_text(self): def as_text(self):
if not self: return '' 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): 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. # Utilities for time zone support in DateTimeField et al.

View File

@ -17,7 +17,7 @@ from django.forms.util import flatatt, to_current_timezone
from django.utils.datastructures import MultiValueDict, MergeDict from django.utils.datastructures import MultiValueDict, MergeDict
from django.utils.html import conditional_escape, format_html, format_html_join from django.utils.html import conditional_escape, format_html, format_html_join
from django.utils.translation import ugettext, ugettext_lazy 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.safestring import mark_safe
from django.utils import six from django.utils import six
from django.utils import datetime_safe, formats from django.utils import datetime_safe, formats
@ -223,7 +223,7 @@ class Widget(six.with_metaclass(MediaDefiningClass)):
initial_value = '' initial_value = ''
else: else:
initial_value = initial initial_value = initial
if force_unicode(initial_value) != force_unicode(data_value): if force_text(initial_value) != force_text(data_value):
return True return True
return False return False
@ -257,7 +257,7 @@ class Input(Widget):
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
if value != '': if value != '':
# Only add the 'value' attribute if a value is non-empty. # 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('<input{0} />', flatatt(final_attrs)) return format_html('<input{0} />', flatatt(final_attrs))
class TextInput(Input): class TextInput(Input):
@ -294,7 +294,7 @@ class MultipleHiddenInput(HiddenInput):
id_ = final_attrs.get('id', None) id_ = final_attrs.get('id', None)
inputs = [] inputs = []
for i, v in enumerate(value): 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_: if id_:
# An ID attribute was given. Add a numeric index as a suffix # An ID attribute was given. Add a numeric index as a suffix
# so that the inputs don't all have the same ID attribute. # so that the inputs don't all have the same ID attribute.
@ -361,7 +361,7 @@ class ClearableFileInput(FileInput):
template = self.template_with_initial template = self.template_with_initial
substitutions['initial'] = format_html('<a href="{0}">{1}</a>', substitutions['initial'] = format_html('<a href="{0}">{1}</a>',
value.url, value.url,
force_unicode(value)) force_text(value))
if not self.is_required: if not self.is_required:
checkbox_name = self.clear_checkbox_name(name) checkbox_name = self.clear_checkbox_name(name)
checkbox_id = self.clear_checkbox_id(checkbox_name) checkbox_id = self.clear_checkbox_id(checkbox_name)
@ -398,7 +398,7 @@ class Textarea(Widget):
final_attrs = self.build_attrs(attrs, name=name) final_attrs = self.build_attrs(attrs, name=name)
return format_html('<textarea{0}>{1}</textarea>', return format_html('<textarea{0}>{1}</textarea>',
flatatt(final_attrs), flatatt(final_attrs),
force_unicode(value)) force_text(value))
class DateInput(Input): class DateInput(Input):
input_type = 'text' input_type = 'text'
@ -515,7 +515,7 @@ class CheckboxInput(Widget):
final_attrs['checked'] = 'checked' final_attrs['checked'] = 'checked'
if not (value is True or value is False or value is None or value == ''): 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. # 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('<input{0} />', flatatt(final_attrs)) return format_html('<input{0} />', flatatt(final_attrs))
def value_from_datadict(self, data, files, name): def value_from_datadict(self, data, files, name):
@ -556,7 +556,7 @@ class Select(Widget):
return mark_safe('\n'.join(output)) return mark_safe('\n'.join(output))
def render_option(self, selected_choices, option_value, option_label): 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: if option_value in selected_choices:
selected_html = mark_safe(' selected="selected"') selected_html = mark_safe(' selected="selected"')
if not self.allow_multiple_selected: if not self.allow_multiple_selected:
@ -567,15 +567,15 @@ class Select(Widget):
return format_html('<option value="{0}"{1}>{2}</option>', return format_html('<option value="{0}"{1}>{2}</option>',
option_value, option_value,
selected_html, selected_html,
force_unicode(option_label)) force_text(option_label))
def render_options(self, choices, selected_choices): def render_options(self, choices, selected_choices):
# Normalize to strings. # 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 = [] output = []
for option_value, option_label in chain(self.choices, choices): for option_value, option_label in chain(self.choices, choices):
if isinstance(option_label, (list, tuple)): if isinstance(option_label, (list, tuple)):
output.append(format_html('<optgroup label="{0}">', force_unicode(option_value))) output.append(format_html('<optgroup label="{0}">', force_text(option_value)))
for option in option_label: for option in option_label:
output.append(self.render_option(selected_choices, *option)) output.append(self.render_option(selected_choices, *option))
output.append('</optgroup>') output.append('</optgroup>')
@ -643,8 +643,8 @@ class SelectMultiple(Select):
data = [] data = []
if len(initial) != len(data): if len(initial) != len(data):
return True return True
initial_set = set([force_unicode(value) for value in initial]) initial_set = set([force_text(value) for value in initial])
data_set = set([force_unicode(value) for value in data]) data_set = set([force_text(value) for value in data])
return data_set != initial_set return data_set != initial_set
class RadioInput(SubWidget): class RadioInput(SubWidget):
@ -656,8 +656,8 @@ class RadioInput(SubWidget):
def __init__(self, name, value, attrs, choice, index): def __init__(self, name, value, attrs, choice, index):
self.name, self.value = name, value self.name, self.value = name, value
self.attrs = attrs self.attrs = attrs
self.choice_value = force_unicode(choice[0]) self.choice_value = force_text(choice[0])
self.choice_label = force_unicode(choice[1]) self.choice_label = force_text(choice[1])
self.index = index self.index = index
def __unicode__(self): def __unicode__(self):
@ -671,7 +671,7 @@ class RadioInput(SubWidget):
label_for = format_html(' for="{0}_{1}"', self.attrs['id'], self.index) label_for = format_html(' for="{0}_{1}"', self.attrs['id'], self.index)
else: else:
label_for = '' label_for = ''
choice_label = force_unicode(self.choice_label) choice_label = force_text(self.choice_label)
return format_html('<label{0}>{1} {2}</label>', label_for, self.tag(), choice_label) return format_html('<label{0}>{1} {2}</label>', label_for, self.tag(), choice_label)
def is_checked(self): def is_checked(self):
@ -709,7 +709,7 @@ class RadioFieldRenderer(StrAndUnicode):
"""Outputs a <ul> for this set of radio fields.""" """Outputs a <ul> for this set of radio fields."""
return format_html('<ul>\n{0}\n</ul>', return format_html('<ul>\n{0}\n</ul>',
format_html_join('\n', '<li>{0}</li>', format_html_join('\n', '<li>{0}</li>',
[(force_unicode(w),) for w in self] [(force_text(w),) for w in self]
)) ))
class RadioSelect(Select): class RadioSelect(Select):
@ -729,7 +729,7 @@ class RadioSelect(Select):
def get_renderer(self, name, value, attrs=None, choices=()): def get_renderer(self, name, value, attrs=None, choices=()):
"""Returns an instance of the renderer.""" """Returns an instance of the renderer."""
if value is None: value = '' 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) final_attrs = self.build_attrs(attrs)
choices = list(chain(self.choices, choices)) choices = list(chain(self.choices, choices))
return self.renderer(name, str_value, final_attrs, 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) final_attrs = self.build_attrs(attrs, name=name)
output = ['<ul>'] output = ['<ul>']
# Normalize to strings # 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)): 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, # 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. # so that the checkboxes don't all have the same ID attribute.
@ -764,9 +764,9 @@ class CheckboxSelectMultiple(SelectMultiple):
label_for = '' label_for = ''
cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) 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) rendered_cb = cb.render(name, option_value)
option_label = force_unicode(option_label) option_label = force_text(option_label)
output.append(format_html('<li><label{0}>{1} {2}</label></li>', output.append(format_html('<li><label{0}>{1} {2}</label></li>',
label_for, rendered_cb, option_label)) label_for, rendered_cb, option_label))
output.append('</ul>') output.append('</ul>')

View File

@ -61,14 +61,14 @@ else:
if not _cookie_allows_colon_in_names: if not _cookie_allows_colon_in_names:
def load(self, rawdata): def load(self, rawdata):
self.bad_cookies = set() self.bad_cookies = set()
super(SimpleCookie, self).load(smart_str(rawdata)) super(SimpleCookie, self).load(smart_bytes(rawdata))
for key in self.bad_cookies: for key in self.bad_cookies:
del self[key] del self[key]
# override private __set() method: # override private __set() method:
# (needed for using our Morsel, and for laxness with CookieError # (needed for using our Morsel, and for laxness with CookieError
def _BaseCookie__set(self, key, real_value, coded_value): def _BaseCookie__set(self, key, real_value, coded_value):
key = smart_str(key) key = smart_bytes(key)
try: try:
M = self.get(key, Morsel()) M = self.get(key, Morsel())
M.set(key, real_value, coded_value) 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.multipartparser import MultiPartParser
from django.http.utils import * from django.http.utils import *
from django.utils.datastructures import MultiValueDict, ImmutableList from django.utils.datastructures import MultiValueDict, ImmutableList
from django.utils.encoding import smart_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.http import cookie_date
from django.utils import six from django.utils import six
from django.utils import timezone from django.utils import timezone
@ -137,7 +137,7 @@ def build_request_repr(request, path_override=None, GET_override=None,
except: except:
meta = '<could not parse>' meta = '<could not parse>'
path = path_override if path_override is not None else request.path 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__, (request.__class__.__name__,
path, path,
six.text_type(get), six.text_type(get),
@ -385,8 +385,8 @@ class QueryDict(MultiValueDict):
encoding = settings.DEFAULT_CHARSET encoding = settings.DEFAULT_CHARSET
self.encoding = encoding self.encoding = encoding
for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
self.appendlist(force_unicode(key, encoding, errors='replace'), self.appendlist(force_text(key, encoding, errors='replace'),
force_unicode(value, encoding, errors='replace')) force_text(value, encoding, errors='replace'))
self._mutable = mutable self._mutable = mutable
def _get_encoding(self): def _get_encoding(self):
@ -481,13 +481,13 @@ class QueryDict(MultiValueDict):
""" """
output = [] output = []
if safe: 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))) encode = lambda k, v: '%s=%s' % ((quote(k, safe), quote(v, safe)))
else: else:
encode = lambda k, v: urlencode({k: v}) encode = lambda k, v: urlencode({k: v})
for k, list_ in self.lists(): for k, list_ in self.lists():
k = smart_str(k, self.encoding) k = smart_bytes(k, self.encoding)
output.extend([encode(k, smart_str(v, self.encoding)) output.extend([encode(k, smart_bytes(v, self.encoding))
for v in list_]) for v in list_])
return '&'.join(output) return '&'.join(output)
@ -648,7 +648,7 @@ class HttpResponse(object):
def _get_content(self): def _get_content(self):
if self.has_header('Content-Encoding'): if self.has_header('Content-Encoding'):
return b''.join([str(e) for e in self._container]) 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): def _set_content(self, value):
if hasattr(value, '__iter__'): if hasattr(value, '__iter__'):
@ -698,7 +698,7 @@ class HttpResponseRedirectBase(HttpResponse):
raise SuspiciousOperation("Unsafe redirect to URL with protocol '%s'" % parsed.scheme) raise SuspiciousOperation("Unsafe redirect to URL with protocol '%s'" % parsed.scheme)
super(HttpResponseRedirectBase, self).__init__() super(HttpResponseRedirectBase, self).__init__()
self['Location'] = iri_to_uri(redirect_to) self['Location'] = iri_to_uri(redirect_to)
class HttpResponseRedirect(HttpResponseRedirectBase): class HttpResponseRedirect(HttpResponseRedirectBase):
status_code = 302 status_code = 302
@ -735,7 +735,7 @@ def get_host(request):
return request.get_host() return request.get_host()
# It's neither necessary nor appropriate to use # 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. # this slightly more restricted function.
def str_to_unicode(s, encoding): def str_to_unicode(s, encoding):
""" """

View File

@ -10,7 +10,7 @@ import cgi
from django.conf import settings from django.conf import settings
from django.core.exceptions import SuspiciousOperation from django.core.exceptions import SuspiciousOperation
from django.utils.datastructures import MultiValueDict 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 import six
from django.utils.text import unescape_entities from django.utils.text import unescape_entities
from django.core.files.uploadhandler import StopUpload, SkipFile, StopFutureHandlers from django.core.files.uploadhandler import StopUpload, SkipFile, StopFutureHandlers
@ -151,7 +151,7 @@ class MultiPartParser(object):
transfer_encoding = meta_data.get('content-transfer-encoding') transfer_encoding = meta_data.get('content-transfer-encoding')
if transfer_encoding is not None: if transfer_encoding is not None:
transfer_encoding = transfer_encoding[0].strip() 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: if item_type == FIELD:
# This is a post field, we can just set it in the post # This is a post field, we can just set it in the post
@ -165,13 +165,13 @@ class MultiPartParser(object):
data = field_stream.read() data = field_stream.read()
self._post.appendlist(field_name, self._post.appendlist(field_name,
force_unicode(data, encoding, errors='replace')) force_text(data, encoding, errors='replace'))
elif item_type == FILE: elif item_type == FILE:
# This is a file, use the handler... # This is a file, use the handler...
file_name = disposition.get('filename') file_name = disposition.get('filename')
if not file_name: if not file_name:
continue 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)) file_name = self.IE_sanitize(unescape_entities(file_name))
content_type = meta_data.get('content-type', ('',))[0].strip() content_type = meta_data.get('content-type', ('',))[0].strip()
@ -245,7 +245,7 @@ class MultiPartParser(object):
file_obj = handler.file_complete(counters[i]) file_obj = handler.file_complete(counters[i])
if file_obj: if file_obj:
# If it returns a file object, then set the files dict. # 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, self._encoding,
errors='replace'), errors='replace'),
file_obj) file_obj)

View File

@ -11,7 +11,7 @@ from django.utils.importlib import import_module
from django.utils.itercompat import is_iterable from django.utils.itercompat import is_iterable
from django.utils.text import (smart_split, unescape_string_literal, from django.utils.text import (smart_split, unescape_string_literal,
get_text_list) 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.translation import ugettext_lazy, pgettext_lazy
from django.utils.safestring import (SafeData, EscapeData, mark_safe, from django.utils.safestring import (SafeData, EscapeData, mark_safe,
mark_for_escaping) mark_for_escaping)
@ -89,7 +89,7 @@ class VariableDoesNotExist(Exception):
return six.text_type(self).encode('utf-8') return six.text_type(self).encode('utf-8')
def __unicode__(self): 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]) for p in self.params])
class InvalidTemplateLibrary(Exception): class InvalidTemplateLibrary(Exception):
@ -117,7 +117,7 @@ class Template(object):
def __init__(self, template_string, origin=None, def __init__(self, template_string, origin=None,
name='<Unknown Template>'): name='<Unknown Template>'):
try: try:
template_string = smart_unicode(template_string) template_string = smart_text(template_string)
except UnicodeDecodeError: except UnicodeDecodeError:
raise TemplateEncodingError("Templates can only be constructed " raise TemplateEncodingError("Templates can only be constructed "
"from unicode or UTF-8 strings.") "from unicode or UTF-8 strings.")
@ -831,7 +831,7 @@ class NodeList(list):
bit = self.render_node(node, context) bit = self.render_node(node, context)
else: else:
bit = node bit = node
bits.append(force_unicode(bit)) bits.append(force_text(bit))
return mark_safe(''.join(bits)) return mark_safe(''.join(bits))
def get_nodes_by_type(self, nodetype): def get_nodes_by_type(self, nodetype):
@ -849,7 +849,7 @@ class TextNode(Node):
self.s = s self.s = s
def __repr__(self): def __repr__(self):
return "<Text Node: '%s'>" % smart_str(self.s[:25], 'ascii', return "<Text Node: '%s'>" % smart_bytes(self.s[:25], 'ascii',
errors='replace') errors='replace')
def render(self, context): 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 = template_localtime(value, use_tz=context.use_tz)
value = localize(value, use_l10n=context.use_l10n) 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 if ((context.autoescape and not isinstance(value, SafeData)) or
isinstance(value, EscapeData)): isinstance(value, EscapeData)):
return escape(value) return escape(value)

View File

@ -1,5 +1,5 @@
from django.template.base import Lexer, Parser, tag_re, NodeList, VariableNode, TemplateSyntaxError 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.html import escape
from django.utils.safestring import SafeData, EscapeData from django.utils.safestring import SafeData, EscapeData
from django.utils.formats import localize from django.utils.formats import localize
@ -84,7 +84,7 @@ class DebugVariableNode(VariableNode):
output = self.filter_expression.resolve(context) output = self.filter_expression.resolve(context)
output = template_localtime(output, use_tz=context.use_tz) output = template_localtime(output, use_tz=context.use_tz)
output = localize(output, use_l10n=context.use_l10n) output = localize(output, use_l10n=context.use_l10n)
output = force_unicode(output) output = force_text(output)
except UnicodeDecodeError: except UnicodeDecodeError:
return '' return ''
except Exception as e: except Exception as e:

View File

@ -12,7 +12,7 @@ from django.template.base import Variable, Library, VariableDoesNotExist
from django.conf import settings from django.conf import settings
from django.utils import formats from django.utils import formats
from django.utils.dateformat import format, time_format 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, from django.utils.html import (conditional_escape, escapejs, fix_ampersands,
escape, urlize as urlize_impl, linebreaks, strip_tags) escape, urlize as urlize_impl, linebreaks, strip_tags)
from django.utils.http import urlquote from django.utils.http import urlquote
@ -38,7 +38,7 @@ def stringfilter(func):
def _dec(*args, **kwargs): def _dec(*args, **kwargs):
if args: if args:
args = list(args) args = list(args)
args[0] = force_unicode(args[0]) args[0] = force_text(args[0])
if (isinstance(args[0], SafeData) and if (isinstance(args[0], SafeData) and
getattr(_dec._decorated_function, 'is_safe', False)): getattr(_dec._decorated_function, 'is_safe', False)):
return mark_safe(func(*args, **kwargs)) return mark_safe(func(*args, **kwargs))
@ -139,7 +139,7 @@ def floatformat(text, arg=-1):
""" """
try: try:
input_val = force_unicode(text) input_val = force_text(text)
d = Decimal(input_val) d = Decimal(input_val)
except UnicodeEncodeError: except UnicodeEncodeError:
return '' return ''
@ -147,7 +147,7 @@ def floatformat(text, arg=-1):
if input_val in special_floats: if input_val in special_floats:
return input_val return input_val
try: try:
d = Decimal(force_unicode(float(text))) d = Decimal(force_text(float(text)))
except (ValueError, InvalidOperation, TypeError, UnicodeEncodeError): except (ValueError, InvalidOperation, TypeError, UnicodeEncodeError):
return '' return ''
try: try:
@ -192,7 +192,7 @@ def floatformat(text, arg=-1):
@stringfilter @stringfilter
def iriencode(value): def iriencode(value):
"""Escapes an IRI value for use in a URL.""" """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) @register.filter(is_safe=True, needs_autoescape=True)
@stringfilter @stringfilter
@ -462,7 +462,7 @@ def safeseq(value):
individually, as safe, after converting them to unicode. Returns a list individually, as safe, after converting them to unicode. Returns a list
with the results. 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) @register.filter(is_safe=True)
@stringfilter @stringfilter
@ -521,7 +521,7 @@ def join(value, arg, autoescape=None):
""" """
Joins a list with a string, like Python's ``str.join(list)``. Joins a list with a string, like Python's ``str.join(list)``.
""" """
value = map(force_unicode, value) value = map(force_text, value)
if autoescape: if autoescape:
value = [conditional_escape(v) for v in value] value = [conditional_escape(v) for v in value]
try: try:
@ -661,7 +661,7 @@ def unordered_list(value, autoescape=None):
sublist = '\n%s<ul>\n%s\n%s</ul>\n%s' % (indent, sublist, sublist = '\n%s<ul>\n%s\n%s</ul>\n%s' % (indent, sublist,
indent, indent) indent, indent)
output.append('%s<li>%s%s</li>' % (indent, output.append('%s<li>%s%s</li>' % (indent,
escaper(force_unicode(title)), sublist)) escaper(force_text(title)), sublist))
i += 1 i += 1
return '\n'.join(output) return '\n'.join(output)
value, converted = convert_old_style_list(value) value, converted = convert_old_style_list(value)
@ -901,4 +901,4 @@ def pprint(value):
try: try:
return pformat(value) return pformat(value)
except Exception as e: except Exception as e:
return "Error in formatting: %s" % force_unicode(e, errors="replace") return "Error in formatting: %s" % force_text(e, errors="replace")

View File

@ -14,7 +14,7 @@ from django.template.base import (Node, NodeList, Template, Context, Library,
VARIABLE_ATTRIBUTE_SEPARATOR, get_library, token_kwargs, kwarg_re) VARIABLE_ATTRIBUTE_SEPARATOR, get_library, token_kwargs, kwarg_re)
from django.template.smartif import IfParser, Literal from django.template.smartif import IfParser, Literal
from django.template.defaultfilters import date 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.safestring import mark_safe
from django.utils.html import format_html from django.utils.html import format_html
from django.utils import six from django.utils import six
@ -104,7 +104,7 @@ class FirstOfNode(Node):
for var in self.vars: for var in self.vars:
value = var.resolve(context, True) value = var.resolve(context, True)
if value: if value:
return smart_unicode(value) return smart_text(value)
return '' return ''
class ForNode(Node): class ForNode(Node):
@ -393,7 +393,7 @@ class URLNode(Node):
def render(self, context): def render(self, context):
from django.core.urlresolvers import reverse, NoReverseMatch from django.core.urlresolvers import reverse, NoReverseMatch
args = [arg.resolve(context) for arg in self.args] 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()]) for k, v in self.kwargs.items()])
view_name = self.view_name.resolve(context) view_name = self.view_name.resolve(context)

View File

@ -1,7 +1,7 @@
from django.template import Node from django.template import Node
from django.template import TemplateSyntaxError, Library from django.template import TemplateSyntaxError, Library
from django.utils import formats from django.utils import formats
from django.utils.encoding import force_unicode from django.utils.encoding import force_text
register = Library() register = Library()
@ -11,7 +11,7 @@ def localize(value):
Forces a value to be rendered as a localized value, Forces a value to be rendered as a localized value,
regardless of the value of ``settings.USE_L10N``. 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) @register.filter(is_safe=False)
def unlocalize(value): def unlocalize(value):
@ -19,7 +19,7 @@ def unlocalize(value):
Forces a value to be rendered as a non-localized value, Forces a value to be rendered as a non-localized value,
regardless of the value of ``settings.USE_L10N``. regardless of the value of ``settings.USE_L10N``.
""" """
return force_unicode(value) return force_text(value)
class LocalizeNode(Node): class LocalizeNode(Node):
def __init__(self, nodelist, use_l10n): def __init__(self, nodelist, use_l10n):

View File

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

View File

@ -5,7 +5,7 @@ Comparing two html documents.
from __future__ import unicode_literals from __future__ import unicode_literals
import re 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.html_parser import HTMLParser, HTMLParseError
from django.utils import six from django.utils import six
@ -25,7 +25,7 @@ class Element(object):
def append(self, element): def append(self, element):
if isinstance(element, six.string_types): if isinstance(element, six.string_types):
element = force_unicode(element) element = force_text(element)
element = normalize_whitespace(element) element = normalize_whitespace(element)
if self.children: if self.children:
if isinstance(self.children[-1], six.string_types): if isinstance(self.children[-1], six.string_types):

View File

@ -41,7 +41,7 @@ from django.test.utils import (get_warnings_state, restore_warnings_state,
override_settings) override_settings)
from django.test.utils import ContextList from django.test.utils import ContextList
from django.utils import unittest as ut2 from django.utils import unittest as ut2
from django.utils.encoding import smart_str, force_unicode from django.utils.encoding import smart_bytes, force_text
from django.utils import six from django.utils import six
from django.utils.unittest.util import safe_repr from django.utils.unittest.util import safe_repr
from django.views.static import serve from django.views.static import serve
@ -398,7 +398,7 @@ class SimpleTestCase(ut2.TestCase):
optional.clean(input) optional.clean(input)
self.assertEqual(context_manager.exception.messages, errors) self.assertEqual(context_manager.exception.messages, errors)
# test required inputs # test required inputs
error_required = [force_unicode(required.error_messages['required'])] error_required = [force_text(required.error_messages['required'])]
for e in EMPTY_VALUES: for e in EMPTY_VALUES:
with self.assertRaises(ValidationError) as context_manager: with self.assertRaises(ValidationError) as context_manager:
required.clean(e) required.clean(e)
@ -647,7 +647,7 @@ class TransactionTestCase(SimpleTestCase):
self.assertEqual(response.status_code, status_code, self.assertEqual(response.status_code, status_code,
msg_prefix + "Couldn't retrieve content: Response code was %d" msg_prefix + "Couldn't retrieve content: Response code was %d"
" (expected %d)" % (response.status_code, status_code)) " (expected %d)" % (response.status_code, status_code))
enc_text = smart_str(text, response._charset) enc_text = smart_bytes(text, response._charset)
content = response.content content = response.content
if html: if html:
content = assert_and_parse_html(self, content, None, content = assert_and_parse_html(self, content, None,
@ -683,7 +683,7 @@ class TransactionTestCase(SimpleTestCase):
self.assertEqual(response.status_code, status_code, self.assertEqual(response.status_code, status_code,
msg_prefix + "Couldn't retrieve content: Response code was %d" msg_prefix + "Couldn't retrieve content: Response code was %d"
" (expected %d)" % (response.status_code, status_code)) " (expected %d)" % (response.status_code, status_code))
enc_text = smart_str(text, response._charset) enc_text = smart_bytes(text, response._charset)
content = response.content content = response.content
if html: if html:
content = assert_and_parse_html(self, content, None, content = assert_and_parse_html(self, content, None,

View File

@ -1,7 +1,7 @@
import os import os
import stat import stat
from os.path import join, normcase, normpath, abspath, isabs, sep 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: try:
WindowsError = WindowsError WindowsError = WindowsError
@ -37,8 +37,8 @@ def safe_join(base, *paths):
The final path must be located inside of the base path component (otherwise The final path must be located inside of the base path component (otherwise
a ValueError is raised). a ValueError is raised).
""" """
base = force_unicode(base) base = force_text(base)
paths = [force_unicode(p) for p in paths] paths = [force_text(p) for p in paths]
final_path = abspathu(join(base, *paths)) final_path = abspathu(join(base, *paths))
base_path = abspathu(base) base_path = abspathu(base)
base_path_len = len(base_path) base_path_len = len(base_path)

View File

@ -23,7 +23,7 @@ import time
from django.conf import settings from django.conf import settings
from django.core.cache import get_cache from django.core.cache import get_cache
from django.utils.encoding import iri_to_uri, force_unicode from django.utils.encoding import iri_to_uri, force_text
from django.utils.http import http_date from django.utils.http import http_date
from django.utils.timezone import get_current_timezone_name from django.utils.timezone import get_current_timezone_name
from django.utils.translation import get_language from django.utils.translation import get_language
@ -169,7 +169,7 @@ def _i18n_cache_key_suffix(request, cache_key):
# Windows is known to use non-standard, locale-dependant names. # Windows is known to use non-standard, locale-dependant names.
# User-defined tzinfo classes may return absolutely anything. # User-defined tzinfo classes may return absolutely anything.
# Hence this paranoid conversion to create a valid cache key. # 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(' ', '_') cache_key += '.%s' % tz_name.encode('ascii', 'ignore').replace(' ', '_')
return cache_key return cache_key

View File

@ -23,7 +23,7 @@ except NotImplementedError:
using_sysrandom = False using_sysrandom = False
from django.conf import settings from django.conf import settings
from django.utils.encoding import smart_str from django.utils.encoding import smart_bytes
from django.utils.six.moves import xrange 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 A trimmed down version of Python's HMAC implementation
""" """
dig1, dig2 = digest(), digest() dig1, dig2 = digest(), digest()
key = smart_str(key) key = smart_bytes(key)
if len(key) > dig1.block_size: if len(key) > dig1.block_size:
key = digest(key).digest() key = digest(key).digest()
key += chr(0) * (dig1.block_size - len(key)) key += chr(0) * (dig1.block_size - len(key))
@ -141,8 +141,8 @@ def pbkdf2(password, salt, iterations, dklen=0, digest=None):
assert iterations > 0 assert iterations > 0
if not digest: if not digest:
digest = hashlib.sha256 digest = hashlib.sha256
password = smart_str(password) password = smart_bytes(password)
salt = smart_str(salt) salt = smart_bytes(salt)
hlen = digest().digest_size hlen = digest().digest_size
if not dklen: if not dklen:
dklen = hlen dklen = hlen

View File

@ -20,7 +20,7 @@ import datetime
from django.utils.dates import MONTHS, MONTHS_3, MONTHS_ALT, MONTHS_AP, WEEKDAYS, WEEKDAYS_ABBR from django.utils.dates import MONTHS, MONTHS_3, MONTHS_ALT, MONTHS_AP, WEEKDAYS, WEEKDAYS_ABBR
from django.utils.tzinfo import LocalTimezone from django.utils.tzinfo import LocalTimezone
from django.utils.translation import ugettext as _ 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 import six
from django.utils.timezone import is_aware, is_naive from django.utils.timezone import is_aware, is_naive
@ -30,9 +30,9 @@ re_escaped = re.compile(r'\\(.)')
class Formatter(object): class Formatter(object):
def format(self, formatstr): def format(self, formatstr):
pieces = [] 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: if i % 2:
pieces.append(force_unicode(getattr(self, piece)())) pieces.append(force_text(getattr(self, piece)()))
elif piece: elif piece:
pieces.append(re_escaped.sub(r'\1', piece)) pieces.append(re_escaped.sub(r'\1', piece))
return ''.join(pieces) return ''.join(pieces)

View File

@ -24,9 +24,13 @@ class DjangoUnicodeDecodeError(UnicodeDecodeError):
class StrAndUnicode(object): 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: if six.PY3:
def __str__(self): def __str__(self):
@ -35,37 +39,36 @@ class StrAndUnicode(object):
def __str__(self): def __str__(self):
return self.__unicode__().encode('utf-8') 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 Returns a text object representing 's' -- unicode on Python 2 and str on
'encoding' codec. Python 3. Treats bytestrings using the 'encoding' codec.
If strings_only is True, don't convert (some) non-string-like objects. If strings_only is True, don't convert (some) non-string-like objects.
""" """
if isinstance(s, Promise): if isinstance(s, Promise):
# The input is the result of a gettext_lazy() call. # The input is the result of a gettext_lazy() call.
return s return s
return force_unicode(s, encoding, strings_only, errors) return force_text(s, encoding, strings_only, errors)
def is_protected_type(obj): def is_protected_type(obj):
"""Determine if the object instance is of a protected type. """Determine if the object instance is of a protected type.
Objects of protected types are preserved as-is when passed to 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, return isinstance(obj, six.integer_types + (type(None), float, Decimal,
datetime.datetime, datetime.date, datetime.time)) 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. strings, rather than kept as lazy objects.
If strings_only is True, don't convert (some) non-string-like 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 # Handle the common case first, saves 30-40% when s is an instance of
# is an instance of unicode. This function gets called often in that # six.text_type. This function gets called often in that setting.
# setting.
if isinstance(s, six.text_type): if isinstance(s, six.text_type):
return s return s
if strings_only and is_protected_type(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 # without raising a further exception. We do an
# approximation to what the Exception's standard str() # approximation to what the Exception's standard str()
# output should be. # output should be.
s = ' '.join([force_unicode(arg, encoding, strings_only, s = ' '.join([force_text(arg, encoding, strings_only,
errors) for arg in s]) errors) for arg in s])
else: else:
# Note: We use .decode() here, instead of six.text_type(s, encoding, # 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 # working unicode method. Try to handle this without raising a
# further exception by individually forcing the exception args # further exception by individually forcing the exception args
# to unicode. # to unicode.
s = ' '.join([force_unicode(arg, encoding, strings_only, s = ' '.join([force_text(arg, encoding, strings_only,
errors) for arg in s]) errors) for arg in s])
return 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'. 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 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)): if strings_only and (s is None or isinstance(s, int)):
return s return s
if isinstance(s, Promise): if isinstance(s, Promise):
return six.text_type(s).encode(encoding, errors) return six.text_type(s).encode(encoding, errors)
elif not isinstance(s, six.string_types): if not isinstance(s, six.string_types):
try: try:
if six.PY3: if six.PY3:
return six.text_type(s).encode(encoding) 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 # An Exception subclass containing non-ASCII data that doesn't
# know how to print itself properly. We shouldn't raise a # know how to print itself properly. We shouldn't raise a
# further exception. # further exception.
return ' '.join([smart_str(arg, encoding, strings_only, return ' '.join([smart_bytes(arg, encoding, strings_only,
errors) for arg in s]) errors) for arg in s])
return six.text_type(s).encode(encoding, errors) return six.text_type(s).encode(encoding, errors)
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: 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): def iri_to_uri(iri):
""" """
@ -168,7 +186,7 @@ def iri_to_uri(iri):
# converted. # converted.
if iri is None: if iri is None:
return iri return iri
return quote(smart_str(iri), safe=b"/#%[]=:;$&()+,!?*@'~") return quote(smart_bytes(iri), safe=b"/#%[]=:;$&()+,!?*@'~")
def filepath_to_uri(path): def filepath_to_uri(path):
"""Convert an file system path to a URI portion that is suitable for """Convert an file system path to a URI portion that is suitable for
@ -187,7 +205,7 @@ def filepath_to_uri(path):
return path return path
# I know about `os.sep` and `os.altsep` but I want to leave # I know about `os.sep` and `os.altsep` but I want to leave
# some flexibility for hardcoding separators. # some flexibility for hardcoding separators.
return quote(smart_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 # The encoding of the default system locale but falls back to the
# given fallback encoding if the encoding is unsupported by python or could # given fallback encoding if the encoding is unsupported by python or could

View File

@ -29,7 +29,7 @@ try:
except ImportError: # Python 2 except ImportError: # Python 2
from urlparse import urlparse from urlparse import urlparse
from django.utils.xmlutils import SimplerXMLGenerator 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 import datetime_safe
from django.utils.timezone import is_aware 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, def __init__(self, title, link, description, language=None, author_email=None,
author_name=None, author_link=None, subtitle=None, categories=None, author_name=None, author_link=None, subtitle=None, categories=None,
feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs): 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: if categories:
categories = [force_unicode(c) for c in categories] categories = [force_text(c) for c in categories]
if ttl is not None: if ttl is not None:
# Force ints to unicode # Force ints to unicode
ttl = force_unicode(ttl) ttl = force_text(ttl)
self.feed = { self.feed = {
'title': to_unicode(title), 'title': to_unicode(title),
'link': iri_to_uri(link), 'link': iri_to_uri(link),
@ -114,12 +114,12 @@ class SyndicationFeed(object):
objects except pubdate, which is a datetime.datetime object, and objects except pubdate, which is a datetime.datetime object, and
enclosure, which is an instance of the Enclosure class. 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: if categories:
categories = [to_unicode(c) for c in categories] categories = [to_unicode(c) for c in categories]
if ttl is not None: if ttl is not None:
# Force ints to unicode # Force ints to unicode
ttl = force_unicode(ttl) ttl = force_text(ttl)
item = { item = {
'title': to_unicode(title), 'title': to_unicode(title),
'link': iri_to_uri(link), 'link': iri_to_uri(link),

View File

@ -11,7 +11,7 @@ except ImportError: # Python 2
from urlparse import urlsplit, urlunsplit from urlparse import urlsplit, urlunsplit
from django.utils.safestring import SafeData, mark_safe from django.utils.safestring import SafeData, mark_safe
from django.utils.encoding import smart_str, force_unicode from django.utils.encoding import smart_bytes, force_text
from django.utils.functional import allow_lazy from django.utils.functional import allow_lazy
from django.utils import six from django.utils import six
from django.utils.text import normalize_newlines from django.utils.text import normalize_newlines
@ -39,7 +39,7 @@ def escape(text):
""" """
Returns the given text with ampersands, quotes and angle brackets encoded for use in HTML. Returns the given text with ampersands, quotes and angle brackets encoded for use in HTML.
""" """
return mark_safe(force_unicode(text).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')) return mark_safe(force_text(text).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;'))
escape = allow_lazy(escape, six.text_type) escape = allow_lazy(escape, six.text_type)
_base_js_escapes = ( _base_js_escapes = (
@ -63,7 +63,7 @@ _js_escapes = (_base_js_escapes +
def escapejs(value): def escapejs(value):
"""Hex encodes characters for use in JavaScript strings.""" """Hex encodes characters for use in JavaScript strings."""
for bad, good in _js_escapes: 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 return value
escapejs = allow_lazy(escapejs, six.text_type) escapejs = allow_lazy(escapejs, six.text_type)
@ -120,22 +120,22 @@ linebreaks = allow_lazy(linebreaks, six.text_type)
def strip_tags(value): def strip_tags(value):
"""Returns the given HTML with all tags stripped.""" """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) strip_tags = allow_lazy(strip_tags)
def strip_spaces_between_tags(value): def strip_spaces_between_tags(value):
"""Returns the given HTML with spaces between tags removed.""" """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) strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, six.text_type)
def strip_entities(value): def strip_entities(value):
"""Returns the given HTML with all entities (&something;) stripped.""" """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) strip_entities = allow_lazy(strip_entities, six.text_type)
def fix_ampersands(value): def fix_ampersands(value):
"""Returns the given HTML with all unencoded ampersands encoded correctly.""" """Returns the given HTML with all unencoded ampersands encoded correctly."""
return unencoded_ampersands_re.sub('&amp;', force_unicode(value)) return unencoded_ampersands_re.sub('&amp;', force_text(value))
fix_ampersands = allow_lazy(fix_ampersands, six.text_type) fix_ampersands = allow_lazy(fix_ampersands, six.text_type)
def smart_urlquote(url): def smart_urlquote(url):
@ -153,9 +153,9 @@ def smart_urlquote(url):
# contains a % not followed by two hexadecimal digits. See #9655. # contains a % not followed by two hexadecimal digits. See #9655.
if '%' not in url or unquoted_percents_re.search(url): if '%' not in url or unquoted_percents_re.search(url):
# See http://bugs.python.org/issue2637 # See http://bugs.python.org/issue2637
url = quote(smart_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): 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 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) 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): for i, word in enumerate(words):
match = None match = None
if '.' in word or '@' in word or ':' in word: if '.' in word or '@' in word or ':' in word:
@ -245,7 +245,7 @@ def clean_html(text):
bottom of the text. bottom of the text.
""" """
from django.utils.text import normalize_newlines 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*b\s*>', '<\\1strong>', text)
text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text) text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text)
text = fix_ampersands(text) text = fix_ampersands(text)

Some files were not shown because too many files have changed in this diff Show More