mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
[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:
@@ -19,7 +19,7 @@ from django.http import SimpleCookie, HttpRequest, QueryDict
|
||||
from django.template import TemplateDoesNotExist
|
||||
from django.test import signals
|
||||
from django.utils.functional import curry
|
||||
from django.utils.encoding import smart_str
|
||||
from django.utils.encoding import smart_bytes
|
||||
from django.utils.http import urlencode
|
||||
from django.utils.importlib import import_module
|
||||
from django.utils.itercompat import is_iterable
|
||||
@@ -108,7 +108,7 @@ def encode_multipart(boundary, data):
|
||||
as an application/octet-stream; otherwise, str(value) will be sent.
|
||||
"""
|
||||
lines = []
|
||||
to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
|
||||
to_str = lambda s: smart_bytes(s, settings.DEFAULT_CHARSET)
|
||||
|
||||
# Not by any means perfect, but good enough for our purposes.
|
||||
is_file = lambda thing: hasattr(thing, "read") and callable(thing.read)
|
||||
@@ -145,7 +145,7 @@ def encode_multipart(boundary, data):
|
||||
return '\r\n'.join(lines)
|
||||
|
||||
def encode_file(boundary, key, file):
|
||||
to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
|
||||
to_str = lambda s: smart_bytes(s, settings.DEFAULT_CHARSET)
|
||||
content_type = mimetypes.guess_type(file.name)[0]
|
||||
if content_type is None:
|
||||
content_type = 'application/octet-stream'
|
||||
@@ -220,7 +220,7 @@ class RequestFactory(object):
|
||||
charset = match.group(1)
|
||||
else:
|
||||
charset = settings.DEFAULT_CHARSET
|
||||
return smart_str(data, encoding=charset)
|
||||
return smart_bytes(data, encoding=charset)
|
||||
|
||||
def _get_path(self, parsed):
|
||||
# If there are parameters, add them
|
||||
@@ -291,7 +291,7 @@ class RequestFactory(object):
|
||||
def generic(self, method, path,
|
||||
data='', content_type='application/octet-stream', **extra):
|
||||
parsed = urlparse(path)
|
||||
data = smart_str(data, settings.DEFAULT_CHARSET)
|
||||
data = smart_bytes(data, settings.DEFAULT_CHARSET)
|
||||
r = {
|
||||
'PATH_INFO': self._get_path(parsed),
|
||||
'QUERY_STRING': parsed[4],
|
||||
|
||||
@@ -5,7 +5,7 @@ Comparing two html documents.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import re
|
||||
from django.utils.encoding import force_unicode
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.html_parser import HTMLParser, HTMLParseError
|
||||
from django.utils import six
|
||||
|
||||
@@ -25,7 +25,7 @@ class Element(object):
|
||||
|
||||
def append(self, element):
|
||||
if isinstance(element, six.string_types):
|
||||
element = force_unicode(element)
|
||||
element = force_text(element)
|
||||
element = normalize_whitespace(element)
|
||||
if self.children:
|
||||
if isinstance(self.children[-1], six.string_types):
|
||||
|
||||
@@ -41,7 +41,7 @@ from django.test.utils import (get_warnings_state, restore_warnings_state,
|
||||
override_settings)
|
||||
from django.test.utils import ContextList
|
||||
from django.utils import unittest as ut2
|
||||
from django.utils.encoding import smart_str, force_unicode
|
||||
from django.utils.encoding import smart_bytes, force_text
|
||||
from django.utils import six
|
||||
from django.utils.unittest.util import safe_repr
|
||||
from django.views.static import serve
|
||||
@@ -398,7 +398,7 @@ class SimpleTestCase(ut2.TestCase):
|
||||
optional.clean(input)
|
||||
self.assertEqual(context_manager.exception.messages, errors)
|
||||
# test required inputs
|
||||
error_required = [force_unicode(required.error_messages['required'])]
|
||||
error_required = [force_text(required.error_messages['required'])]
|
||||
for e in EMPTY_VALUES:
|
||||
with self.assertRaises(ValidationError) as context_manager:
|
||||
required.clean(e)
|
||||
@@ -647,7 +647,7 @@ class TransactionTestCase(SimpleTestCase):
|
||||
self.assertEqual(response.status_code, status_code,
|
||||
msg_prefix + "Couldn't retrieve content: Response code was %d"
|
||||
" (expected %d)" % (response.status_code, status_code))
|
||||
enc_text = smart_str(text, response._charset)
|
||||
enc_text = smart_bytes(text, response._charset)
|
||||
content = response.content
|
||||
if html:
|
||||
content = assert_and_parse_html(self, content, None,
|
||||
@@ -683,7 +683,7 @@ class TransactionTestCase(SimpleTestCase):
|
||||
self.assertEqual(response.status_code, status_code,
|
||||
msg_prefix + "Couldn't retrieve content: Response code was %d"
|
||||
" (expected %d)" % (response.status_code, status_code))
|
||||
enc_text = smart_str(text, response._charset)
|
||||
enc_text = smart_bytes(text, response._charset)
|
||||
content = response.content
|
||||
if html:
|
||||
content = assert_and_parse_html(self, content, None,
|
||||
|
||||
Reference in New Issue
Block a user