Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[py3] Replaced unicode/str by six.text_type/bytes.

  • Loading branch information...
commit bdca5ea345c548a82a80d198906818c9ccbef896 1 parent 3cb2457
@aaugustin aaugustin authored
Showing with 376 additions and 294 deletions.
  1. +1 −1  django/contrib/admin/helpers.py
  2. +2 −2 django/contrib/admin/util.py
  3. +2 −1  django/contrib/admin/widgets.py
  4. +5 −4 django/contrib/auth/models.py
  5. +3 −2 django/contrib/auth/tokens.py
  6. +2 −1  django/contrib/contenttypes/tests.py
  7. +3 −2 django/contrib/formtools/wizard/views.py
  8. +4 −3 django/contrib/gis/db/backends/base.py
  9. +1 −1  django/contrib/gis/db/backends/util.py
  10. +1 −1  django/contrib/gis/gdal/geometries.py
  11. +1 −1  django/contrib/gis/gdal/srs.py
  12. +1 −1  django/contrib/gis/geos/geometry.py
  13. +1 −1  django/contrib/gis/geos/tests/test_io.py
  14. +2 −1  django/contrib/gis/tests/geoapp/tests.py
  15. +1 −1  django/contrib/gis/utils/layermapping.py
  16. +3 −2 django/contrib/localflavor/mx/forms.py
  17. +3 −2 django/contrib/localflavor/se/utils.py
  18. +2 −1  django/core/management/commands/inspectdb.py
  19. +2 −2 django/core/management/validation.py
  20. +1 −1  django/core/urlresolvers.py
  21. +4 −3 django/db/backends/__init__.py
  22. +4 −4 django/db/backends/mysql/base.py
  23. +1 −1  django/db/backends/oracle/base.py
  24. +2 −2 django/db/backends/sqlite3/base.py
  25. +7 −7 django/db/models/base.py
  26. +1 −1  django/db/models/fields/files.py
  27. +2 −2 django/forms/fields.py
  28. +2 −2 django/forms/forms.py
  29. +4 −3 django/forms/formsets.py
  30. +2 −2 django/forms/models.py
  31. +8 −8 django/http/__init__.py
  32. +2 −1  django/http/multipartparser.py
  33. +1 −1  django/template/base.py
  34. +5 −4 django/template/defaultfilters.py
  35. +1 −1  django/test/_doctest.py
  36. +3 −3 django/test/html.py
  37. +3 −2 django/test/testcases.py
  38. +3 −2 django/utils/dateformat.py
  39. +25 −12 django/utils/encoding.py
  40. +2 −2 django/utils/formats.py
  41. +6 −5 django/utils/functional.py
  42. +9 −8 django/utils/html.py
  43. +5 −4 django/utils/http.py
  44. +3 −2 django/utils/numberformat.py
  45. +17 −16 django/utils/safestring.py
  46. +13 −12 django/utils/text.py
  47. +8 −7 django/utils/translation/__init__.py
  48. +1 −1  django/views/debug.py
  49. +5 −4 tests/modeltests/custom_columns/tests.py
  50. +2 −1  tests/modeltests/custom_managers/tests.py
  51. +3 −2 tests/modeltests/custom_pk/fields.py
  52. +8 −7 tests/modeltests/custom_pk/tests.py
  53. +2 −1  tests/modeltests/expressions/tests.py
  54. +2 −2 tests/modeltests/field_subclassing/fields.py
  55. +2 −1  tests/modeltests/lookup/models.py
  56. +2 −1  tests/modeltests/m2m_and_m2o/models.py
  57. +3 −2 tests/modeltests/m2m_intermediary/tests.py
  58. +2 −1  tests/modeltests/many_to_one/tests.py
  59. +2 −1  tests/modeltests/model_forms/models.py
  60. +17 −16 tests/modeltests/model_forms/tests.py
  61. +2 −1  tests/modeltests/model_formsets/models.py
  62. +11 −10 tests/modeltests/model_formsets/tests.py
  63. +3 −2 tests/modeltests/model_inheritance/tests.py
  64. +2 −1  tests/modeltests/order_with_respect_to/models.py
  65. +4 −3 tests/modeltests/pagination/tests.py
  66. +20 −19 tests/modeltests/prefetch_related/tests.py
  67. +2 −1  tests/modeltests/save_delete_hooks/tests.py
  68. +2 −1  tests/modeltests/serializers/models.py
  69. +1 −1  tests/modeltests/serializers/tests.py
  70. +2 −1  tests/modeltests/signals/tests.py
  71. +3 −2 tests/modeltests/update/models.py
  72. +5 −4 tests/regressiontests/admin_changelist/tests.py
  73. +2 −1  tests/regressiontests/admin_util/models.py
  74. +4 −3 tests/regressiontests/admin_util/tests.py
  75. +4 −3 tests/regressiontests/admin_views/tests.py
  76. +3 −2 tests/regressiontests/backends/tests.py
  77. +2 −1  tests/regressiontests/datatypes/tests.py
  78. +4 −3 tests/regressiontests/defaultfilters/tests.py
  79. +2 −1  tests/regressiontests/file_uploads/views.py
  80. +2 −1  tests/regressiontests/fixtures_regress/models.py
  81. +2 −1  tests/regressiontests/forms/tests/models.py
  82. +2 −1  tests/regressiontests/forms/tests/util.py
  83. +4 −3 tests/regressiontests/forms/tests/widgets.py
  84. +4 −3 tests/regressiontests/i18n/contenttypes/tests.py
  85. +5 −4 tests/regressiontests/i18n/tests.py
  86. +3 −2 tests/regressiontests/inline_formsets/tests.py
  87. +5 −4 tests/regressiontests/model_forms_regress/tests.py
  88. +6 −5 tests/regressiontests/model_formsets_regress/tests.py
  89. +3 −2 tests/regressiontests/model_regress/tests.py
  90. +2 −1  tests/regressiontests/queries/models.py
  91. +3 −2 tests/regressiontests/select_related_regress/tests.py
  92. +12 −11 tests/regressiontests/templates/templatetags/custom.py
  93. +2 −1  tests/regressiontests/templates/unicode.py
  94. +4 −3 tests/regressiontests/utils/simplelazyobject.py
  95. +2 −1  tests/regressiontests/wsgi/tests.py
  96. +2 −1  tests/runtests.py
View
2  django/contrib/admin/helpers.py
@@ -189,7 +189,7 @@ def contents(self):
if value is None:
result_repr = EMPTY_CHANGELIST_VALUE
elif isinstance(f.rel, ManyToManyRel):
- result_repr = ", ".join(map(unicode, value.all()))
+ result_repr = ", ".join(map(six.text_type, value.all()))
else:
result_repr = display_for_field(value, f)
return conditional_escape(result_repr)
View
4 django/contrib/admin/util.py
@@ -275,10 +275,10 @@ def label_for_field(name, model, model_admin=None, return_attr=False):
except models.FieldDoesNotExist:
if name == "__unicode__":
label = force_unicode(model._meta.verbose_name)
- attr = unicode
+ attr = six.text_type
elif name == "__str__":
label = smart_str(model._meta.verbose_name)
- attr = str
+ attr = bytes
else:
if callable(name):
attr = name
View
3  django/contrib/admin/widgets.py
@@ -15,6 +15,7 @@
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode
+from django.utils import six
class FilteredSelectMultiple(forms.SelectMultiple):
@@ -121,7 +122,7 @@ def url_params_from_lookup_dict(lookups):
# See django.db.fields.BooleanField.get_prep_lookup
v = ('0', '1')[v]
else:
- v = unicode(v)
+ v = six.text_type(v)
items.append((k, v))
params.update(dict(items))
return params
View
9 django/contrib/auth/models.py
@@ -8,6 +8,7 @@
from django.db.models.manager import EmptyManager
from django.utils.crypto import get_random_string
from django.utils.encoding import smart_str
+from django.utils import six
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
@@ -79,9 +80,9 @@ class Meta:
def __unicode__(self):
return "%s | %s | %s" % (
- unicode(self.content_type.app_label),
- unicode(self.content_type),
- unicode(self.name))
+ six.text_type(self.content_type.app_label),
+ six.text_type(self.content_type),
+ six.text_type(self.name))
def natural_key(self):
return (self.codename,) + self.content_type.natural_key()
@@ -421,7 +422,7 @@ def __unicode__(self):
return 'AnonymousUser'
def __str__(self):
- return unicode(self).encode('utf-8')
+ return six.text_type(self).encode('utf-8')
def __eq__(self, other):
return isinstance(other, self.__class__)
View
5 django/contrib/auth/tokens.py
@@ -2,6 +2,7 @@
from django.conf import settings
from django.utils.http import int_to_base36, base36_to_int
from django.utils.crypto import constant_time_compare, salted_hmac
+from django.utils import six
class PasswordResetTokenGenerator(object):
"""
@@ -56,8 +57,8 @@ def _make_token_with_timestamp(self, user, timestamp):
# Ensure results are consistent across DB backends
login_timestamp = user.last_login.replace(microsecond=0, tzinfo=None)
- value = (unicode(user.id) + user.password +
- unicode(login_timestamp) + unicode(timestamp))
+ value = (six.text_type(user.id) + user.password +
+ six.text_type(login_timestamp) + six.text_type(timestamp))
hash = salted_hmac(key_salt, value).hexdigest()[::2]
return "%s-%s" % (ts_b36, hash)
View
3  django/contrib/contenttypes/tests.py
@@ -9,6 +9,7 @@
from django.http import HttpRequest, Http404
from django.test import TestCase
from django.utils.encoding import smart_str
+from django.utils import six
class ConcreteModel(models.Model):
@@ -271,4 +272,4 @@ def test_missing_model(self):
app_label = 'contenttypes',
model = 'OldModel',
)
- self.assertEqual(unicode(ct), 'Old model')
+ self.assertEqual(six.text_type(ct), 'Old model')
View
5 django/contrib/formtools/wizard/views.py
@@ -7,6 +7,7 @@
from django.views.generic import TemplateView
from django.utils.datastructures import SortedDict
from django.utils.decorators import classonlymethod
+from django.utils import six
from django.contrib.formtools.wizard.storage import get_storage
from django.contrib.formtools.wizard.storage.exceptions import NoFileStorageConfigured
@@ -157,10 +158,10 @@ def get_initkwargs(cls, form_list, initial_dict=None,
if isinstance(form, (list, tuple)):
# if the element is a tuple, add the tuple to the new created
# sorted dictionary.
- init_form_list[unicode(form[0])] = form[1]
+ init_form_list[six.text_type(form[0])] = form[1]
else:
# if not, add the form with a zero based counter as unicode
- init_form_list[unicode(i)] = form
+ init_form_list[six.text_type(i)] = form
# walk through the new created list of forms
for form in init_form_list.itervalues():
View
7 django/contrib/gis/db/backends/base.py
@@ -4,6 +4,7 @@
"""
import re
from django.contrib.gis import gdal
+from django.utils import six
class BaseSpatialOperations(object):
"""
@@ -88,7 +89,7 @@ def convert_geom(self, geom_val, geom_field):
# For quoting column values, rather than columns.
def geo_quote_name(self, name):
- if isinstance(name, unicode):
+ if isinstance(name, six.text_type):
name = name.encode('ascii')
return "'%s'" % name
@@ -330,6 +331,6 @@ def __unicode__(self):
it will be 'pretty' OGC WKT.
"""
try:
- return unicode(self.srs)
+ return six.text_type(self.srs)
except:
- return unicode(self.wkt)
+ return six.text_type(self.wkt)
View
2  django/contrib/gis/db/backends/util.py
@@ -12,7 +12,7 @@ def gqn(val):
backend quotename function).
"""
if isinstance(val, six.string_types):
- if isinstance(val, unicode): val = val.encode('ascii')
+ if isinstance(val, six.text_type): val = val.encode('ascii')
return "'%s'" % val
else:
return str(val)
View
2  django/contrib/gis/gdal/geometries.py
@@ -81,7 +81,7 @@ def __init__(self, geom_input, srs=None):
# Constructing the geometry,
if str_instance:
# Checking if unicode
- if isinstance(geom_input, unicode):
+ if isinstance(geom_input, six.text_type):
# Encoding to ASCII, WKT or HEX doesn't need any more.
geom_input = geom_input.encode('ascii')
View
2  django/contrib/gis/gdal/srs.py
@@ -56,7 +56,7 @@ def __init__(self, srs_input=''):
if isinstance(srs_input, six.string_types):
# Encoding to ASCII if unicode passed in.
- if isinstance(srs_input, unicode):
+ if isinstance(srs_input, six.text_type):
srs_input = srs_input.encode('ascii')
try:
# If SRID is a string, e.g., '4326', then make acceptable
View
2  django/contrib/gis/geos/geometry.py
@@ -55,7 +55,7 @@ def __init__(self, geo_input, srid=None):
(SRID) number for this Geometry. If not set, the SRID will be None.
"""
if isinstance(geo_input, six.string_types):
- if isinstance(geo_input, unicode):
+ if isinstance(geo_input, six.text_type):
# Encoding to ASCII, WKT or HEXEWKB doesn't need any more.
geo_input = geo_input.encode('ascii')
View
2  django/contrib/gis/geos/tests/test_io.py
@@ -13,7 +13,7 @@ def test01_wktreader(self):
# read() should return a GEOSGeometry
ref = GEOSGeometry(wkt)
g1 = wkt_r.read(wkt)
- g2 = wkt_r.read(unicode(wkt))
+ g2 = wkt_r.read(six.text_type(wkt))
for geom in (g1, g2):
self.assertEqual(ref, geom)
View
3  django/contrib/gis/tests/geoapp/tests.py
@@ -11,6 +11,7 @@
no_mysql, no_oracle, no_spatialite,
mysql, oracle, postgis, spatialite)
from django.test import TestCase
+from django.utils import six
from .models import Country, City, PennsylvaniaCity, State, Track
@@ -663,7 +664,7 @@ def test27_snap_to_grid(self):
# Let's try and break snap_to_grid() with bad combinations of arguments.
for bad_args in ((), range(3), range(5)):
self.assertRaises(ValueError, Country.objects.snap_to_grid, *bad_args)
- for bad_args in (('1.0',), (1.0, None), tuple(map(unicode, range(4)))):
+ for bad_args in (('1.0',), (1.0, None), tuple(map(six.text_type, range(4)))):
self.assertRaises(TypeError, Country.objects.snap_to_grid, *bad_args)
# Boundary for San Marino, courtesy of Bjorn Sandvik of thematicmapping.org
View
2  django/contrib/gis/utils/layermapping.py
@@ -330,7 +330,7 @@ def verify_ogr_field(self, ogr_field, model_field):
if self.encoding:
# The encoding for OGR data sources may be specified here
# (e.g., 'cp437' for Census Bureau boundary files).
- val = unicode(ogr_field.value, self.encoding)
+ val = six.text_type(ogr_field.value, self.encoding)
else:
val = ogr_field.value
if model_field.max_length and len(val) > model_field.max_length:
View
5 django/contrib/localflavor/mx/forms.py
@@ -7,6 +7,7 @@
from django.forms import ValidationError
from django.forms.fields import Select, RegexField
+from django.utils import six
from django.utils.translation import ugettext_lazy as _
from django.core.validators import EMPTY_VALUES
from django.contrib.localflavor.mx.mx_states import STATE_CHOICES
@@ -155,7 +156,7 @@ def _checksum(self, rfc):
elif checksum == 11:
return '0'
- return unicode(checksum)
+ return six.text_type(checksum)
def _has_inconvenient_word(self, rfc):
first_four = rfc[:4]
@@ -219,7 +220,7 @@ def _checksum(self, value):
if checksum == 10:
return '0'
- return unicode(checksum)
+ return six.text_type(checksum)
def _has_inconvenient_word(self, curp):
first_four = curp[:4]
View
5 django/contrib/localflavor/se/utils.py
@@ -1,4 +1,5 @@
import datetime
+from django.utils import six
def id_number_checksum(gd):
"""
@@ -65,7 +66,7 @@ def validate_id_birthday(gd, fix_coordination_number_day=True):
def format_personal_id_number(birth_day, gd):
# birth_day.strftime cannot be used, since it does not support dates < 1900
- return unicode(str(birth_day.year) + gd['month'] + gd['day'] + gd['serial'] + gd['checksum'])
+ return six.text_type(str(birth_day.year) + gd['month'] + gd['day'] + gd['serial'] + gd['checksum'])
def format_organisation_number(gd):
if gd['century'] is None:
@@ -73,7 +74,7 @@ def format_organisation_number(gd):
else:
century = gd['century']
- return unicode(century + gd['year'] + gd['month'] + gd['day'] + gd['serial'] + gd['checksum'])
+ return six.text_type(century + gd['year'] + gd['month'] + gd['day'] + gd['serial'] + gd['checksum'])
def valid_organisation(gd):
return gd['century'] in (None, 16) and \
View
3  django/core/management/commands/inspectdb.py
@@ -3,6 +3,7 @@
from django.core.management.base import NoArgsCommand, CommandError
from django.db import connections, DEFAULT_DB_ALIAS
+from django.utils import six
class Command(NoArgsCommand):
help = "Introspects the database tables in the given database and outputs a Django model module."
@@ -115,7 +116,7 @@ def handle_inspection(self, options):
if att_name[0].isdigit():
att_name = 'number_%s' % att_name
- extra_params['db_column'] = unicode(column_name)
+ extra_params['db_column'] = six.text_type(column_name)
comment_notes.append("Field renamed because it wasn't a "
"valid Python identifier.")
View
4 django/core/management/validation.py
@@ -120,7 +120,7 @@ def get_validation_errors(outfile, app=None):
e.add(opts, "'%s' has a relation with model %s, which has either not been installed or is abstract." % (f.name, f.rel.to))
# it is a string and we could not find the model it refers to
# so skip the next section
- if isinstance(f.rel.to, (str, unicode)):
+ if isinstance(f.rel.to, six.string_types):
continue
# Make sure the related field specified by a ForeignKey is unique
@@ -162,7 +162,7 @@ def get_validation_errors(outfile, app=None):
e.add(opts, "'%s' has an m2m relation with model %s, which has either not been installed or is abstract." % (f.name, f.rel.to))
# it is a string and we could not find the model it refers to
# so skip the next section
- if isinstance(f.rel.to, (str, unicode)):
+ if isinstance(f.rel.to, six.string_types):
continue
# Check that the field is not set to unique. ManyToManyFields do not support unique.
View
2  django/core/urlresolvers.py
@@ -169,7 +169,7 @@ def regex(self):
except re.error as e:
raise ImproperlyConfigured(
'"%s" is not a valid regular expression: %s' %
- (regex, unicode(e)))
+ (regex, six.text_type(e)))
self._regex_dict[language_code] = compiled_regex
return self._regex_dict[language_code]
View
7 django/db/backends/__init__.py
@@ -12,6 +12,7 @@
from django.db.transaction import TransactionManagementError
from django.utils.functional import cached_property
from django.utils.importlib import import_module
+from django.utils import six
from django.utils.timezone import is_aware
@@ -808,7 +809,7 @@ def value_to_db_date(self, value):
"""
if value is None:
return None
- return unicode(value)
+ return six.text_type(value)
def value_to_db_datetime(self, value):
"""
@@ -817,7 +818,7 @@ def value_to_db_datetime(self, value):
"""
if value is None:
return None
- return unicode(value)
+ return six.text_type(value)
def value_to_db_time(self, value):
"""
@@ -828,7 +829,7 @@ def value_to_db_time(self, value):
return None
if is_aware(value):
raise ValueError("Django does not support timezone-aware times.")
- return unicode(value)
+ return six.text_type(value)
def value_to_db_decimal(self, value, max_digits, decimal_places):
"""
View
8 django/db/backends/mysql/base.py
@@ -297,7 +297,7 @@ def value_to_db_datetime(self, value):
raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.")
# MySQL doesn't support microseconds
- return unicode(value.replace(microsecond=0))
+ return six.text_type(value.replace(microsecond=0))
def value_to_db_time(self, value):
if value is None:
@@ -308,7 +308,7 @@ def value_to_db_time(self, value):
raise ValueError("MySQL backend does not support timezone-aware times.")
# MySQL doesn't support microseconds
- return unicode(value.replace(microsecond=0))
+ return six.text_type(value.replace(microsecond=0))
def year_lookup_bounds(self, value):
# Again, no microseconds
@@ -399,8 +399,8 @@ def _cursor(self):
kwargs['client_flag'] = CLIENT.FOUND_ROWS
kwargs.update(settings_dict['OPTIONS'])
self.connection = Database.connect(**kwargs)
- self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
- self.connection.encoders[SafeString] = self.connection.encoders[str]
+ self.connection.encoders[SafeUnicode] = self.connection.encoders[six.text_type]
+ self.connection.encoders[SafeString] = self.connection.encoders[bytes]
connection_created.send(sender=self.__class__, connection=self)
cursor = self.connection.cursor()
if new_connection:
View
2  django/db/backends/oracle/base.py
@@ -356,7 +356,7 @@ def value_to_db_datetime(self, value):
else:
raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.")
- return unicode(value)
+ return six.text_type(value)
def value_to_db_time(self, value):
if value is None:
View
4 django/db/backends/sqlite3/base.py
@@ -177,7 +177,7 @@ def value_to_db_datetime(self, value):
else:
raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.")
- return unicode(value)
+ return six.text_type(value)
def value_to_db_time(self, value):
if value is None:
@@ -187,7 +187,7 @@ def value_to_db_time(self, value):
if timezone.is_aware(value):
raise ValueError("SQLite backend does not support timezone-aware times.")
- return unicode(value)
+ return six.text_type(value)
def year_lookup_bounds(self, value):
first = '%s-01-01'
View
14 django/db/models/base.py
@@ -375,7 +375,7 @@ def __init__(self, *args, **kwargs):
def __repr__(self):
try:
- u = unicode(self)
+ u = six.text_type(self)
except (UnicodeEncodeError, UnicodeDecodeError):
u = '[Bad Unicode data]'
return smart_str('<%s: %s>' % (self.__class__.__name__, u))
@@ -790,8 +790,8 @@ def _perform_date_checks(self, date_checks):
def date_error_message(self, lookup_type, field, unique_for):
opts = self._meta
return _("%(field_name)s must be unique for %(date_field)s %(lookup)s.") % {
- 'field_name': unicode(capfirst(opts.get_field(field).verbose_name)),
- 'date_field': unicode(capfirst(opts.get_field(unique_for).verbose_name)),
+ 'field_name': six.text_type(capfirst(opts.get_field(field).verbose_name)),
+ 'date_field': six.text_type(capfirst(opts.get_field(unique_for).verbose_name)),
'lookup': lookup_type,
}
@@ -806,16 +806,16 @@ def unique_error_message(self, model_class, unique_check):
field_label = capfirst(field.verbose_name)
# Insert the error into the error dict, very sneaky
return field.error_messages['unique'] % {
- 'model_name': unicode(model_name),
- 'field_label': unicode(field_label)
+ 'model_name': six.text_type(model_name),
+ 'field_label': six.text_type(field_label)
}
# unique_together
else:
field_labels = map(lambda f: capfirst(opts.get_field(f).verbose_name), unique_check)
field_labels = get_text_list(field_labels, _('and'))
return _("%(model_name)s with this %(field_label)s already exists.") % {
- 'model_name': unicode(model_name),
- 'field_label': unicode(field_labels)
+ 'model_name': six.text_type(model_name),
+ 'field_label': six.text_type(field_labels)
}
def full_clean(self, exclude=None):
View
2  django/db/models/fields/files.py
@@ -265,7 +265,7 @@ def get_prep_value(self, value):
# Need to convert File objects provided via a form to unicode for database insertion
if value is None:
return None
- return unicode(value)
+ return six.text_type(value)
def pre_save(self, model_instance, add):
"Returns field's value just before saving."
View
4 django/forms/fields.py
@@ -331,10 +331,10 @@ def __init__(self, input_formats=None, *args, **kwargs):
def to_python(self, value):
# Try to coerce the value to unicode.
unicode_value = force_unicode(value, strings_only=True)
- if isinstance(unicode_value, unicode):
+ if isinstance(unicode_value, six.text_type):
value = unicode_value.strip()
# If unicode, try to strptime against each input format.
- if isinstance(value, unicode):
+ if isinstance(value, six.text_type):
for format in self.input_formats:
try:
return self.strptime(value, format)
View
4 django/forms/forms.py
@@ -151,7 +151,7 @@ def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_
if bf.is_hidden:
if bf_errors:
top_errors.extend(['(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
- hidden_fields.append(unicode(bf))
+ hidden_fields.append(six.text_type(bf))
else:
# Create a 'class="..."' atribute if the row should have any
# CSS classes applied.
@@ -181,7 +181,7 @@ def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_
output.append(normal_row % {
'errors': force_unicode(bf_errors),
'label': force_unicode(label),
- 'field': unicode(bf),
+ 'field': six.text_type(bf),
'help_text': help_text,
'html_class_attr': html_class_attr
})
View
7 django/forms/formsets.py
@@ -7,6 +7,7 @@
from django.forms.widgets import Media, HiddenInput
from django.utils.encoding import StrAndUnicode
from django.utils.safestring import mark_safe
+from django.utils import six
from django.utils.translation import ugettext as _
@@ -345,17 +346,17 @@ def as_table(self):
# probably should be. It might make sense to render each form as a
# table row with each field as a td.
forms = ' '.join([form.as_table() for form in self])
- return mark_safe('\n'.join([unicode(self.management_form), forms]))
+ return mark_safe('\n'.join([six.text_type(self.management_form), forms]))
def as_p(self):
"Returns this formset rendered as HTML <p>s."
forms = ' '.join([form.as_p() for form in self])
- return mark_safe('\n'.join([unicode(self.management_form), forms]))
+ return mark_safe('\n'.join([six.text_type(self.management_form), forms]))
def as_ul(self):
"Returns this formset rendered as HTML <li>s."
forms = ' '.join([form.as_ul() for form in self])
- return mark_safe('\n'.join([unicode(self.management_form), forms]))
+ return mark_safe('\n'.join([six.text_type(self.management_form), forms]))
def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
can_delete=False, max_num=None):
View
4 django/forms/models.py
@@ -576,7 +576,7 @@ def get_unique_error_message(self, unique_check):
else:
return ugettext("Please correct the duplicate data for %(field)s, "
"which must be unique.") % {
- "field": get_text_list(unique_check, unicode(_("and"))),
+ "field": get_text_list(unique_check, six.text_type(_("and"))),
}
def get_date_error_message(self, date_check):
@@ -584,7 +584,7 @@ def get_date_error_message(self, date_check):
"which must be unique for the %(lookup)s in %(date_field)s.") % {
'field_name': date_check[2],
'date_field': date_check[3],
- 'lookup': unicode(date_check[1]),
+ 'lookup': six.text_type(date_check[1]),
}
def get_form_error(self):
View
16 django/http/__init__.py
@@ -137,10 +137,10 @@ def build_request_repr(request, path_override=None, GET_override=None,
return smart_str('<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
(request.__class__.__name__,
path,
- unicode(get),
- unicode(post),
- unicode(cookies),
- unicode(meta)))
+ six.text_type(get),
+ six.text_type(post),
+ six.text_type(cookies),
+ six.text_type(meta)))
class UnreadablePostError(IOError):
pass
@@ -544,7 +544,7 @@ def __str__(self):
def _convert_to_ascii(self, *values):
"""Converts all values to ascii strings."""
for value in values:
- if isinstance(value, unicode):
+ if isinstance(value, six.text_type):
try:
value = value.encode('us-ascii')
except UnicodeError as e:
@@ -663,7 +663,7 @@ def __iter__(self):
def next(self):
chunk = next(self._iterator)
- if isinstance(chunk, unicode):
+ if isinstance(chunk, six.text_type):
chunk = chunk.encode(self._charset)
return str(chunk)
@@ -740,8 +740,8 @@ def str_to_unicode(s, encoding):
Returns any non-basestring objects without change.
"""
- if isinstance(s, str):
- return unicode(s, encoding, 'replace')
+ if isinstance(s, bytes):
+ return six.text_type(s, encoding, 'replace')
else:
return s
View
3  django/http/multipartparser.py
@@ -11,6 +11,7 @@
from django.core.exceptions import SuspiciousOperation
from django.utils.datastructures import MultiValueDict
from django.utils.encoding import force_unicode
+from django.utils import six
from django.utils.text import unescape_entities
from django.core.files.uploadhandler import StopUpload, SkipFile, StopFutureHandlers
@@ -77,7 +78,7 @@ def __init__(self, META, input_data, upload_handlers, encoding=None):
# This means we shouldn't continue...raise an error.
raise MultiPartParserError("Invalid content length: %r" % content_length)
- if isinstance(boundary, unicode):
+ if isinstance(boundary, six.text_type):
boundary = boundary.encode('ascii')
self._boundary = boundary
self._input_data = input_data
View
2  django/template/base.py
@@ -86,7 +86,7 @@ def __init__(self, msg, params=()):
self.params = params
def __str__(self):
- return unicode(self).encode('utf-8')
+ return six.text_type(self).encode('utf-8')
def __unicode__(self):
return self.msg % tuple([force_unicode(p, errors='replace')
View
9 django/template/defaultfilters.py
@@ -18,6 +18,7 @@
from django.utils.http import urlquote
from django.utils.text import Truncator, wrap, phone2numeric
from django.utils.safestring import mark_safe, SafeData, mark_for_escaping
+from django.utils import six
from django.utils.timesince import timesince, timeuntil
from django.utils.translation import ugettext, ungettext
from django.utils.text import normalize_newlines
@@ -176,7 +177,7 @@ def floatformat(text, arg=-1):
# and `exponent` from `Decimal.as_tuple()` directly.
sign, digits, exponent = d.quantize(exp, ROUND_HALF_UP,
Context(prec=prec)).as_tuple()
- digits = [unicode(digit) for digit in reversed(digits)]
+ digits = [six.text_type(digit) for digit in reversed(digits)]
while len(digits) <= abs(exponent):
digits.append('0')
digits.insert(-exponent, '.')
@@ -200,7 +201,7 @@ def linenumbers(value, autoescape=None):
lines = value.split('\n')
# Find the maximum width of the line count, for use with zero padding
# string format command
- width = unicode(len(unicode(len(lines))))
+ width = six.text_type(len(six.text_type(len(lines))))
if not autoescape or isinstance(value, SafeData):
for i, line in enumerate(lines):
lines[i] = ("%0" + width + "d. %s") % (i + 1, line)
@@ -234,7 +235,7 @@ def slugify(value):
and converts spaces to hyphens.
"""
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
- value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
+ value = six.text_type(re.sub('[^\w\s-]', '', value).strip().lower())
return mark_safe(re.sub('[-\s]+', '-', value))
@register.filter(is_safe=True)
@@ -249,7 +250,7 @@ def stringformat(value, arg):
of Python string formatting
"""
try:
- return ("%" + unicode(arg)) % value
+ return ("%" + six.text_type(arg)) % value
except (ValueError, TypeError):
return ""
View
2  django/test/_doctest.py
@@ -211,7 +211,7 @@ def _normalize_module(module, depth=2):
"""
if inspect.ismodule(module):
return module
- elif isinstance(module, (str, unicode)):
+ elif isinstance(module, six.string_types):
return __import__(module, globals(), locals(), ["*"])
elif module is None:
return sys.modules[sys._getframe(depth).f_globals['__name__']]
View
6 django/test/html.py
@@ -125,14 +125,14 @@ def __unicode__(self):
output += ' %s' % key
if self.children:
output += '>\n'
- output += ''.join(unicode(c) for c in self.children)
+ output += ''.join(six.text_type(c) for c in self.children)
output += '\n</%s>' % self.name
else:
output += ' />'
return output
def __repr__(self):
- return unicode(self)
+ return six.text_type(self)
class RootElement(Element):
@@ -140,7 +140,7 @@ def __init__(self):
super(RootElement, self).__init__(None, ())
def __unicode__(self):
- return ''.join(unicode(c) for c in self.children)
+ return ''.join(six.text_type(c) for c in self.children)
class Parser(HTMLParser):
View
5 django/test/testcases.py
@@ -38,6 +38,7 @@
from django.test.utils import ContextList
from django.utils import unittest as ut2
from django.utils.encoding import smart_str, force_unicode
+from django.utils import six
from django.utils.unittest.util import safe_repr
from django.views.static import serve
@@ -421,8 +422,8 @@ def assertHTMLEqual(self, html1, html2, msg=None):
standardMsg = '%s != %s' % (
safe_repr(dom1, True), safe_repr(dom2, True))
diff = ('\n' + '\n'.join(difflib.ndiff(
- unicode(dom1).splitlines(),
- unicode(dom2).splitlines())))
+ six.text_type(dom1).splitlines(),
+ six.text_type(dom2).splitlines())))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))
View
5 django/utils/dateformat.py
@@ -21,6 +21,7 @@
from django.utils.tzinfo import LocalTimezone
from django.utils.translation import ugettext as _
from django.utils.encoding import force_unicode
+from django.utils import six
from django.utils.timezone import is_aware, is_naive
re_formatchars = re.compile(r'(?<!\\)([aAbBcdDeEfFgGhHiIjlLmMnNoOPrsStTUuwWyYzZ])')
@@ -236,7 +237,7 @@ def T(self):
name = self.timezone and self.timezone.tzname(self.data) or None
if name is None:
name = self.format('O')
- return unicode(name)
+ return six.text_type(name)
def U(self):
"Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
@@ -277,7 +278,7 @@ def W(self):
def y(self):
"Year, 2 digits; e.g. '99'"
- return unicode(self.data.year)[2:]
+ return six.text_type(self.data.year)[2:]
def Y(self):
"Year, 4 digits; e.g. '1999'"
View
37 django/utils/encoding.py
@@ -25,8 +25,12 @@ class StrAndUnicode(object):
Useful as a mix-in.
"""
- def __str__(self):
- return self.__unicode__().encode('utf-8')
+ if six.PY3:
+ def __str__(self):
+ return self.__unicode__()
+ else:
+ def __str__(self):
+ return self.__unicode__().encode('utf-8')
def smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
"""
@@ -59,17 +63,23 @@ def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
# Handle the common case first, saves 30-40% in performance when s
# is an instance of unicode. This function gets called often in that
# setting.
- if isinstance(s, unicode):
+ if isinstance(s, six.text_type):
return s
if strings_only and is_protected_type(s):
return s
try:
- if not isinstance(s, six.string_types,):
+ if not isinstance(s, six.string_types):
if hasattr(s, '__unicode__'):
- s = unicode(s)
+ s = s.__unicode__()
else:
try:
- s = unicode(str(s), encoding, errors)
+ if six.PY3:
+ if isinstance(s, bytes):
+ s = six.text_type(s, encoding, errors)
+ else:
+ s = six.text_type(s)
+ else:
+ s = six.text_type(bytes(s), encoding, errors)
except UnicodeEncodeError:
if not isinstance(s, Exception):
raise
@@ -81,8 +91,8 @@ def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
# output should be.
s = ' '.join([force_unicode(arg, encoding, strings_only,
errors) for arg in s])
- elif not isinstance(s, unicode):
- # Note: We use .decode() here, instead of unicode(s, encoding,
+ elif not isinstance(s, six.text_type):
+ # Note: We use .decode() here, instead of six.text_type(s, encoding,
# errors), so that if s is a SafeString, it ends up being a
# SafeUnicode at the end.
s = s.decode(encoding, errors)
@@ -108,10 +118,13 @@ def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
if strings_only and (s is None or isinstance(s, int)):
return s
if isinstance(s, Promise):
- return unicode(s).encode(encoding, errors)
+ return six.text_type(s).encode(encoding, errors)
elif not isinstance(s, six.string_types):
try:
- return str(s)
+ if six.PY3:
+ return six.text_type(s).encode(encoding)
+ else:
+ return bytes(s)
except UnicodeEncodeError:
if isinstance(s, Exception):
# An Exception subclass containing non-ASCII data that doesn't
@@ -119,8 +132,8 @@ def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
# further exception.
return ' '.join([smart_str(arg, encoding, strings_only,
errors) for arg in s])
- return unicode(s).encode(encoding, errors)
- elif isinstance(s, unicode):
+ 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)
View
4 django/utils/formats.py
@@ -89,7 +89,7 @@ def get_format(format_type, lang=None, use_l10n=None):
_format_cache[cache_key] = None
return getattr(settings, format_type)
-get_format_lazy = lazy(get_format, unicode, list, tuple)
+get_format_lazy = lazy(get_format, six.text_type, list, tuple)
def date_format(value, format=None, use_l10n=None):
"""
@@ -139,7 +139,7 @@ def localize(value, use_l10n=None):
be localized (or not), overriding the value of settings.USE_L10N.
"""
if isinstance(value, bool):
- return mark_safe(unicode(value))
+ return mark_safe(six.text_type(value))
elif isinstance(value, (decimal.Decimal, float) + six.integer_types):
return number_format(value, use_l10n=use_l10n)
elif isinstance(value, datetime.datetime):
View
11 django/utils/functional.py
@@ -3,6 +3,7 @@
from functools import wraps, update_wrapper
import sys
+from django.utils import six
# You can't trivially replace this `functools.partial` because this binds to
# classes and returns bound instances, whereas functools.partial (on CPython)
@@ -92,8 +93,8 @@ def __prepare_class__(cls):
if hasattr(cls, k):
continue
setattr(cls, k, meth)
- cls._delegate_str = str in resultclasses
- cls._delegate_unicode = unicode in resultclasses
+ cls._delegate_str = bytes in resultclasses
+ cls._delegate_unicode = six.text_type in resultclasses
assert not (cls._delegate_str and cls._delegate_unicode), "Cannot call lazy() with both str and unicode return types."
if cls._delegate_unicode:
cls.__unicode__ = cls.__unicode_cast
@@ -147,7 +148,7 @@ def __mod__(self, rhs):
if self._delegate_str:
return str(self) % rhs
elif self._delegate_unicode:
- return unicode(self) % rhs
+ return six.text_type(self) % rhs
else:
raise AssertionError('__mod__ not supported for non-string types')
@@ -255,8 +256,8 @@ def __init__(self, func):
def _setup(self):
self._wrapped = self._setupfunc()
- __str__ = new_method_proxy(str)
- __unicode__ = new_method_proxy(unicode)
+ __str__ = new_method_proxy(bytes)
+ __unicode__ = new_method_proxy(six.text_type)
def __deepcopy__(self, memo):
if self._wrapped is empty:
View
17 django/utils/html.py
@@ -10,6 +10,7 @@
from django.utils.safestring import SafeData, mark_safe
from django.utils.encoding import smart_str, force_unicode
from django.utils.functional import allow_lazy
+from django.utils import six
from django.utils.text import normalize_newlines
# Configuration for urlize() function.
@@ -36,7 +37,7 @@ def escape(text):
Returns the given text with ampersands, quotes and angle brackets encoded for use in HTML.
"""
return mark_safe(force_unicode(text).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;'))
-escape = allow_lazy(escape, unicode)
+escape = allow_lazy(escape, six.text_type)
_base_js_escapes = (
('\\', '\\u005C'),
@@ -61,7 +62,7 @@ def escapejs(value):
for bad, good in _js_escapes:
value = mark_safe(force_unicode(value).replace(bad, good))
return value
-escapejs = allow_lazy(escapejs, unicode)
+escapejs = allow_lazy(escapejs, six.text_type)
def conditional_escape(text):
"""
@@ -112,7 +113,7 @@ def linebreaks(value, autoescape=False):
else:
paras = ['<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
return '\n\n'.join(paras)
-linebreaks = allow_lazy(linebreaks, unicode)
+linebreaks = allow_lazy(linebreaks, six.text_type)
def strip_tags(value):
"""Returns the given HTML with all tags stripped."""
@@ -122,17 +123,17 @@ def strip_tags(value):
def strip_spaces_between_tags(value):
"""Returns the given HTML with spaces between tags removed."""
return re.sub(r'>\s+<', '><', force_unicode(value))
-strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, unicode)
+strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, six.text_type)
def strip_entities(value):
"""Returns the given HTML with all entities (&something;) stripped."""
return re.sub(r'&(?:\w+|#\d+);', '', force_unicode(value))
-strip_entities = allow_lazy(strip_entities, unicode)
+strip_entities = allow_lazy(strip_entities, six.text_type)
def fix_ampersands(value):
"""Returns the given HTML with all unencoded ampersands encoded correctly."""
return unencoded_ampersands_re.sub('&amp;', force_unicode(value))
-fix_ampersands = allow_lazy(fix_ampersands, unicode)
+fix_ampersands = allow_lazy(fix_ampersands, six.text_type)
def smart_urlquote(url):
"Quotes a URL if it isn't already quoted."
@@ -226,7 +227,7 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
elif autoescape:
words[i] = escape(word)
return ''.join(words)
-urlize = allow_lazy(urlize, unicode)
+urlize = allow_lazy(urlize, six.text_type)
def clean_html(text):
"""
@@ -260,4 +261,4 @@ def replace_p_tags(match):
# of the text.
text = trailing_empty_content_re.sub('', text)
return text
-clean_html = allow_lazy(clean_html, unicode)
+clean_html = allow_lazy(clean_html, six.text_type)
View
9 django/utils/http.py
@@ -9,6 +9,7 @@
from django.utils.datastructures import MultiValueDict
from django.utils.encoding import smart_str, force_unicode
from django.utils.functional import allow_lazy
+from django.utils import six
ETAG_MATCH = re.compile(r'(?:W/)?"((?:\\.|[^"])*)"')
@@ -31,7 +32,7 @@ def urlquote(url, safe='/'):
without double-quoting occurring.
"""
return force_unicode(urllib.quote(smart_str(url), smart_str(safe)))
-urlquote = allow_lazy(urlquote, unicode)
+urlquote = allow_lazy(urlquote, six.text_type)
def urlquote_plus(url, safe=''):
"""
@@ -41,7 +42,7 @@ def urlquote_plus(url, safe=''):
iri_to_uri() call without double-quoting occurring.
"""
return force_unicode(urllib.quote_plus(smart_str(url), smart_str(safe)))
-urlquote_plus = allow_lazy(urlquote_plus, unicode)
+urlquote_plus = allow_lazy(urlquote_plus, six.text_type)
def urlunquote(quoted_url):
"""
@@ -49,7 +50,7 @@ def urlunquote(quoted_url):
the result of django.utils.http.urlquote().
"""
return force_unicode(urllib.unquote(smart_str(quoted_url)))
-urlunquote = allow_lazy(urlunquote, unicode)
+urlunquote = allow_lazy(urlunquote, six.text_type)
def urlunquote_plus(quoted_url):
"""
@@ -57,7 +58,7 @@ def urlunquote_plus(quoted_url):
the result of django.utils.http.urlquote_plus().
"""
return force_unicode(urllib.unquote_plus(smart_str(quoted_url)))
-urlunquote_plus = allow_lazy(urlunquote_plus, unicode)
+urlunquote_plus = allow_lazy(urlunquote_plus, six.text_type)
def urlencode(query, doseq=0):
"""
View
5 django/utils/numberformat.py
@@ -1,5 +1,6 @@
from django.conf import settings
from django.utils.safestring import mark_safe
+from django.utils import six
def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
@@ -18,13 +19,13 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
use_grouping = use_grouping and grouping > 0
# Make the common case fast
if isinstance(number, int) and not use_grouping and not decimal_pos:
- return mark_safe(unicode(number))
+ return mark_safe(six.text_type(number))
# sign
if float(number) < 0:
sign = '-'
else:
sign = ''
- str_number = unicode(number)
+ str_number = six.text_type(number)
if str_number[0] == '-':
str_number = str_number[1:]
# decimal part
View
33 django/utils/safestring.py
@@ -5,17 +5,18 @@
be interpreted by the HTML engine (e.g. '<') into the appropriate entities.
"""
from django.utils.functional import curry, Promise
+from django.utils import six
class EscapeData(object):
pass
-class EscapeString(str, EscapeData):
+class EscapeString(bytes, EscapeData):
"""
A string that should be HTML-escaped when output.
"""
pass
-class EscapeUnicode(unicode, EscapeData):
+class EscapeUnicode(six.text_type, EscapeData):
"""
A unicode object that should be HTML-escaped when output.
"""
@@ -24,7 +25,7 @@ class EscapeUnicode(unicode, EscapeData):
class SafeData(object):
pass
-class SafeString(str, SafeData):
+class SafeString(bytes, SafeData):
"""
A string subclass that has been specifically marked as "safe" (requires no
further escaping) for HTML output purposes.
@@ -40,7 +41,7 @@ def __add__(self, rhs):
elif isinstance(rhs, SafeString):
return SafeString(t)
return t
-
+
def _proxy_method(self, *args, **kwargs):
"""
Wrap a call to a normal unicode method up so that we return safe
@@ -49,14 +50,14 @@ def _proxy_method(self, *args, **kwargs):
"""
method = kwargs.pop('method')
data = method(self, *args, **kwargs)
- if isinstance(data, str):
+ if isinstance(data, bytes):
return SafeString(data)
else:
return SafeUnicode(data)
- decode = curry(_proxy_method, method = str.decode)
+ decode = curry(_proxy_method, method=bytes.decode)
-class SafeUnicode(unicode, SafeData):
+class SafeUnicode(six.text_type, SafeData):
"""
A unicode subclass that has been specifically marked as "safe" for HTML
output purposes.
@@ -70,7 +71,7 @@ def __add__(self, rhs):
if isinstance(rhs, SafeData):
return SafeUnicode(t)
return t
-
+
def _proxy_method(self, *args, **kwargs):
"""
Wrap a call to a normal unicode method up so that we return safe
@@ -79,12 +80,12 @@ def _proxy_method(self, *args, **kwargs):
"""
method = kwargs.pop('method')
data = method(self, *args, **kwargs)
- if isinstance(data, str):
+ if isinstance(data, bytes):
return SafeString(data)
else:
return SafeUnicode(data)
- encode = curry(_proxy_method, method = unicode.encode)
+ encode = curry(_proxy_method, method=six.text_type.encode)
def mark_safe(s):
"""
@@ -95,11 +96,11 @@ def mark_safe(s):
"""
if isinstance(s, SafeData):
return s
- if isinstance(s, str) or (isinstance(s, Promise) and s._delegate_str):
+ if isinstance(s, bytes) or (isinstance(s, Promise) and s._delegate_str):
return SafeString(s)
- if isinstance(s, (unicode, Promise)):
+ if isinstance(s, (six.text_type, Promise)):
return SafeUnicode(s)
- return SafeString(str(s))
+ return SafeString(bytes(s))
def mark_for_escaping(s):
"""
@@ -111,9 +112,9 @@ def mark_for_escaping(s):
"""
if isinstance(s, (SafeData, EscapeData)):
return s
- if isinstance(s, str) or (isinstance(s, Promise) and s._delegate_str):
+ if isinstance(s, bytes) or (isinstance(s, Promise) and s._delegate_str):
return EscapeString(s)
- if isinstance(s, (unicode, Promise)):
+ if isinstance(s, (six.text_type, Promise)):
return EscapeUnicode(s)
- return EscapeString(str(s))
+ return EscapeString(bytes(s))
View
25 django/utils/text.py
@@ -9,11 +9,12 @@
from django.utils.encoding import force_unicode
from django.utils.functional import allow_lazy, SimpleLazyObject
+from django.utils import six
from django.utils.translation import ugettext_lazy, ugettext as _, pgettext
# Capitalizes the first letter of a string.
capfirst = lambda x: x and force_unicode(x)[0].upper() + force_unicode(x)[1:]
-capfirst = allow_lazy(capfirst, unicode)
+capfirst = allow_lazy(capfirst, six.text_type)
# Set up regular expressions
re_words = re.compile(r'&.*?;|<.*?>|(\w[\w-]*)', re.U|re.S)
@@ -46,7 +47,7 @@ def _generator():
pos = len(lines[-1])
yield word
return ''.join(_generator())
-wrap = allow_lazy(wrap, unicode)
+wrap = allow_lazy(wrap, six.text_type)
class Truncator(SimpleLazyObject):
@@ -207,14 +208,14 @@ def truncate_words(s, num, end_text='...'):
'in django.utils.text instead.', category=DeprecationWarning)
truncate = end_text and ' %s' % end_text or ''
return Truncator(s).words(num, truncate=truncate)
-truncate_words = allow_lazy(truncate_words, unicode)
+truncate_words = allow_lazy(truncate_words, six.text_type)
def truncate_html_words(s, num, end_text='...'):
warnings.warn('This function has been deprecated. Use the Truncator class '
'in django.utils.text instead.', category=DeprecationWarning)
truncate = end_text and ' %s' % end_text or ''
return Truncator(s).words(num, truncate=truncate, html=True)
-truncate_html_words = allow_lazy(truncate_html_words, unicode)
+truncate_html_words = allow_lazy(truncate_html_words, six.text_type)
def get_valid_filename(s):
"""
@@ -227,7 +228,7 @@ def get_valid_filename(s):
"""
s = force_unicode(s).strip().replace(' ', '_')
return re.sub(r'(?u)[^-\w.]', '', s)
-get_valid_filename = allow_lazy(get_valid_filename, unicode)
+get_valid_filename = allow_lazy(get_valid_filename, six.text_type)
def get_text_list(list_, last_word=ugettext_lazy('or')):
"""
@@ -248,11 +249,11 @@ def get_text_list(list_, last_word=ugettext_lazy('or')):
# Translators: This string is used as a separator between list elements
_(', ').join([force_unicode(i) for i in list_][:-1]),
force_unicode(last_word), force_unicode(list_[-1]))
-get_text_list = allow_lazy(get_text_list, unicode)
+get_text_list = allow_lazy(get_text_list, six.text_type)
def normalize_newlines(text):
return force_unicode(re.sub(r'\r\n|\r|\n', '\n', text))
-normalize_newlines = allow_lazy(normalize_newlines, unicode)
+normalize_newlines = allow_lazy(normalize_newlines, six.text_type)
def recapitalize(text):
"Recapitalizes text, placing caps after end-of-sentence punctuation."
@@ -288,9 +289,9 @@ def javascript_quote(s, quote_double_quotes=False):
def fix(match):
return b"\u%04x" % ord(match.group(1))
- if type(s) == str:
+ if type(s) == bytes:
s = s.decode('utf-8')
- elif type(s) != unicode:
+ elif type(s) != six.text_type:
raise TypeError(s)
s = s.replace('\\', '\\\\')
s = s.replace('\r', '\\r')
@@ -300,7 +301,7 @@ def fix(match):
if quote_double_quotes:
s = s.replace('"', '&quot;')
return str(ustring_re.sub(fix, s))
-javascript_quote = allow_lazy(javascript_quote, unicode)
+javascript_quote = allow_lazy(javascript_quote, six.text_type)
# Expression to match some_token and some_token="with spaces" (and similarly
# for single-quoted strings).
@@ -332,7 +333,7 @@ def smart_split(text):
text = force_unicode(text)
for bit in smart_split_re.finditer(text):
yield bit.group(0)
-smart_split = allow_lazy(smart_split, unicode)
+smart_split = allow_lazy(smart_split, six.text_type)
def _replace_entity(match):
text = match.group(1)
@@ -356,7 +357,7 @@ def _replace_entity(match):
def unescape_entities(text):
return _entity_re.sub(_replace_entity, text)
-unescape_entities = allow_lazy(unescape_entities, unicode)
+unescape_entities = allow_lazy(unescape_entities, six.text_type)
def unescape_string_literal(s):
r"""
View
15 django/utils/translation/__init__.py
@@ -5,6 +5,7 @@
from django.utils.encoding import force_unicode
from django.utils.functional import lazy
+from django.utils import six
__all__ = [
@@ -78,12 +79,12 @@ def pgettext(context, message):
def npgettext(context, singular, plural, number):
return _trans.npgettext(context, singular, plural, number)
-ngettext_lazy = lazy(ngettext, str)
-gettext_lazy = lazy(gettext, str)
-ungettext_lazy = lazy(ungettext, unicode)
-ugettext_lazy = lazy(ugettext, unicode)
-pgettext_lazy = lazy(pgettext, unicode)
-npgettext_lazy = lazy(npgettext, unicode)
+ngettext_lazy = lazy(ngettext, bytes)
+gettext_lazy = lazy(gettext, bytes)
+ungettext_lazy = lazy(ungettext, six.text_type)
+ugettext_lazy = lazy(ugettext, six.text_type)
+pgettext_lazy = lazy(pgettext, six.text_type)
+npgettext_lazy = lazy(npgettext, six.text_type)
def activate(language):
return _trans.activate(language)
@@ -139,7 +140,7 @@ def _string_concat(*strings):
constructed from multiple parts.
"""
return ''.join([force_unicode(s) for s in strings])
-string_concat = lazy(_string_concat, unicode)
+string_concat = lazy(_string_concat, six.text_type)
def get_language_info(lang_code):
from django.conf.locale import LANG_INFO
View
2  django/views/debug.py
@@ -362,7 +362,7 @@ def _get_lines_from_file(self, filename, lineno, context_lines, loader=None, mod
if match:
encoding = match.group(1)
break
- source = [unicode(sline, encoding, 'replace') for sline in source]
+ source = [six.text_type(sline, encoding, 'replace') for sline in source]
lower_bound = max(0, lineno - context_lines)
upper_bound = lineno + context_lines
View
9 tests/modeltests/custom_columns/tests.py
@@ -2,6 +2,7 @@
from django.core.exceptions import FieldError
from django.test import TestCase
+from django.utils import six
from .models import Author, Article
@@ -22,13 +23,13 @@ def test_db_column(self):
Author.objects.all(), [
"Peter Jones", "John Smith",
],
- unicode
+ six.text_type
)
self.assertQuerysetEqual(
Author.objects.filter(first_name__exact="John"), [
"John Smith",
],
- unicode
+ six.text_type
)
self.assertEqual(
Author.objects.get(first_name__exact="John"),
@@ -55,7 +56,7 @@ def test_db_column(self):
"Peter Jones",
"John Smith",
],
- unicode
+ six.text_type
)
# Get the articles for an author
self.assertQuerysetEqual(
@@ -69,5 +70,5 @@ def test_db_column(self):
art.authors.filter(last_name='Jones'), [
"Peter Jones"
],
- unicode
+ six.text_type
)
View
3  tests/modeltests/custom_managers/tests.py
@@ -1,6 +1,7 @@
from __future__ import absolute_import
from django.test import TestCase
+from django.utils import six
from .models import Person, Book, Car, PersonManager, PublishedBookManager
@@ -14,7 +15,7 @@ def test_manager(self):
Person.objects.get_fun_people(), [
"Bugs Bunny"
],
- unicode
+ six.text_type
)
# The RelatedManager used on the 'books' descriptor extends the default
# manager
View
5 tests/modeltests/custom_pk/fields.py
@@ -2,6 +2,7 @@
import string
from django.db import models
+from django.utils import six
class MyWrapper(object):
@@ -44,12 +45,12 @@ def get_db_prep_save(self, value, connection):
if not value:
return
if isinstance(value, MyWrapper):
- return unicode(value)
+ return six.text_type(value)
return value
def get_db_prep_value(self, value, connection, prepared=False):
if not value:
return
if isinstance(value, MyWrapper):
- return unicode(value)
+ return six.text_type(value)
return value
View
15 tests/modeltests/custom_pk/tests.py
@@ -3,6 +3,7 @@
from django.db import transaction, IntegrityError
from django.test import TestCase, skipIfDBFeature
+from django.utils import six
from .models import Employee, Business, Bar, Foo
@@ -16,7 +17,7 @@ def test_custom_pk(self):
Employee.objects.all(), [
"Dan Jones",
],
- unicode
+ six.text_type
)
fran = Employee.objects.create(
@@ -27,7 +28,7 @@ def test_custom_pk(self):
"Fran Bones",
"Dan Jones",
],
- unicode
+ six.text_type
)
self.assertEqual(Employee.objects.get(pk=123), dan)
@@ -45,7 +46,7 @@ def test_custom_pk(self):
"Fran Bones",
"Dan Jones",
],
- unicode
+ six.text_type
)
# The primary key can be accessed via the pk property on the model.
e = Employee.objects.get(pk=123)
@@ -63,7 +64,7 @@ def test_custom_pk(self):
"Dan Jones",
"Fran Jones",
],
- unicode
+ six.text_type
)
emps = Employee.objects.in_bulk([123, 456])
@@ -76,7 +77,7 @@ def test_custom_pk(self):
"Dan Jones",
"Fran Jones",
],
- unicode
+ six.text_type
)
self.assertQuerysetEqual(
fran.business_set.all(), [
@@ -108,14 +109,14 @@ def test_custom_pk(self):
"Dan Jones",
"Fran Jones",
],
- unicode,
+ six.text_type,
)
self.assertQuerysetEqual(
Employee.objects.filter(business__pk="Sears"), [
"Dan Jones",
"Fran Jones",
],
- unicode,
+ six.text_type,
)
self.assertQuerysetEqual(
View
3  tests/modeltests/expressions/tests.py
@@ -3,6 +3,7 @@
from django.core.exceptions import FieldError
from django.db.models import F
from django.test import TestCase
+from django.utils import six
from .models import Company, Employee
@@ -156,7 +157,7 @@ def test_filter(self):
"Frank Meyer",
"Max Mustermann",
],
- lambda c: unicode(c.point_of_contact),
+ lambda c: six.text_type(c.point_of_contact),
)
c = Company.objects.all()[0]
View
4 tests/modeltests/field_subclassing/fields.py
@@ -19,7 +19,7 @@ def __unicode__(self):
return '%s%s' % (force_unicode(self.first), force_unicode(self.second))
def __str__(self):
- return unicode(self).encode('utf-8')
+ return six.text_type(self).encode('utf-8')
class SmallField(models.Field):
"""
@@ -42,7 +42,7 @@ def to_python(self, value):
return Small(value[0], value[1])
def get_db_prep_save(self, value, connection):
- return unicode(value)
+ return six.text_type(value)
def get_prep_lookup(self, lookup_type, value):
if lookup_type == 'exact':
View
3  tests/modeltests/lookup/models.py
@@ -7,6 +7,7 @@
from __future__ import unicode_literals
from django.db import models
+from django.utils import six
class Author(models.Model):
@@ -35,7 +36,7 @@ class Season(models.Model):
gt = models.IntegerField(null=True, blank=True)
def __unicode__(self):
- return unicode(self.year)
+ return six.text_type(self.year)
class Game(models.Model):
season = models.ForeignKey(Season, related_name='games')
View
3  tests/modeltests/m2m_and_m2o/models.py
@@ -6,6 +6,7 @@
from __future__ import unicode_literals
from django.db import models
+from django.utils import six
class User(models.Model):
@@ -17,7 +18,7 @@ class Issue(models.Model):
client = models.ForeignKey(User, related_name='test_issue_client')
def __unicode__(self):
- return unicode(self.num)
+ return six.text_type(self.num)
class Meta:
ordering = ('num',)
View
5 tests/modeltests/m2m_intermediary/tests.py
@@ -3,6 +3,7 @@
from datetime import datetime
from django.test import TestCase
+from django.utils import six
from .models import Reporter, Article, Writer
@@ -24,7 +25,7 @@ def test_intermeiary(self):
("John Smith", "Main writer"),
("Jane Doe", "Contributor"),
],
- lambda w: (unicode(w.reporter), w.position)
+ lambda w: (six.text_type(w.reporter), w.position)
)
self.assertEqual(w1.reporter, r1)
self.assertEqual(w2.reporter, r2)
@@ -36,5 +37,5 @@ def test_intermeiary(self):
r1.writer_set.all(), [
("John Smith", "Main writer")
],
- lambda w: (unicode(w.reporter), w.position)
+ lambda w: (six.text_type(w.reporter), w.position)
)
View
3  tests/modeltests/many_to_one/tests.py
@@ -5,6 +5,7 @@
from django.core.exceptions import MultipleObjectsReturned, FieldError
from django.test import TestCase
+from django.utils import six
from django.utils.translation import ugettext_lazy
from .models import Article, Reporter
@@ -421,7 +422,7 @@ def test_create_relation_with_ugettext_lazy(self):
lazy = ugettext_lazy('test')
reporter.article_set.create(headline=lazy,
pub_date=datetime(2011, 6, 10))
- notlazy = unicode(lazy)
+ notlazy = six.text_type(lazy)
article = reporter.article_set.get()
self.assertEqual(article.headline, notlazy)
View
3  tests/modeltests/model_forms/models.py
@@ -13,6 +13,7 @@
from django.core.files.storage import FileSystemStorage
from django.db import models
+from django.utils import six
temp_storage_dir = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
@@ -226,7 +227,7 @@ class BigInt(models.Model):
biggie = models.BigIntegerField()
def __unicode__(self):
- return unicode(self.biggie)
+ return six.text_type(self.biggie)
class MarkupField(models.CharField):
def __init__(self, *args, **kwargs):
View
33 tests/modeltests/model_forms/tests.py
@@ -11,6 +11,7 @@
from django.forms.models import model_to_dict
from django.utils.unittest import skipUnless
from django.test import TestCase
+from django.utils import six
from .models import (Article, ArticleStatus, BetterWriter, BigInt, Book,
Category, CommaSeparatedInteger, CustomFieldForExclusionModel, DerivedBook,
@@ -653,7 +654,7 @@ def test_with_data(self):
# ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any
# fields with the 'choices' attribute are represented by a ChoiceField.
f = ArticleForm(auto_id=False)
- self.assertHTMLEqual(unicode(f), '''<tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr>
+ self.assertHTMLEqual(six.text_type(f), '''<tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr>
<tr><th>Slug:</th><td><input type="text" name="slug" maxlength="50" /></td></tr>
<tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr>
<tr><th>Writer:</th><td><select name="writer">
@@ -681,14 +682,14 @@ def test_with_data(self):
# a value of None. If a field isn't specified on a form, the object created
# from the form can't provide a value for that field!
f = PartialArticleForm(auto_id=False)
- self.assertHTMLEqual(unicode(f), '''<tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr>
+ self.assertHTMLEqual(six.text_type(f), '''<tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr>
<tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr>''')
# When the ModelForm is passed an instance, that instance's current values are
# inserted as 'initial' data in each Field.
w = Writer.objects.get(name='Mike Royko')
f = RoykoForm(auto_id=False, instance=w)
- self.assertHTMLEqual(unicode(f), '''<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br /><span class="helptext">Use both first and last names.</span></td></tr>''')
+ self.assertHTMLEqual(six.text_type(f), '''<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br /><span class="helptext">Use both first and last names.</span></td></tr>''')
art = Article(
headline='Test article',
@@ -725,7 +726,7 @@ def test_with_data(self):
'headline': 'Test headline',
'slug': 'test-headline',