mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
unicode: Added some bullet-proofing to the output encoding path. Passing bady
encoded data to template rendering shouldn't crash the framework. git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5199 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
a8e5db536e
commit
7c45aad673
@ -724,6 +724,11 @@ class Node(object):
|
|||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
class NodeList(list):
|
class NodeList(list):
|
||||||
|
# How invalid encoding sequences are handled. The default 'strict' is not
|
||||||
|
# appropriate, because the framework is not in control of all the string
|
||||||
|
# data.
|
||||||
|
codec_errors = 'replace'
|
||||||
|
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
bits = []
|
bits = []
|
||||||
for node in self:
|
for node in self:
|
||||||
@ -731,7 +736,8 @@ class NodeList(list):
|
|||||||
bits.append(self.render_node(node, context))
|
bits.append(self.render_node(node, context))
|
||||||
else:
|
else:
|
||||||
bits.append(node)
|
bits.append(node)
|
||||||
return ''.join([smart_str(b, settings.DEFAULT_CHARSET) for b in bits])
|
encoding = settings.DEFAULT_CHARSET
|
||||||
|
return ''.join([smart_str(b, encoding, errors=self.codec_errors) for b in bits])
|
||||||
|
|
||||||
def get_nodes_by_type(self, nodetype):
|
def get_nodes_by_type(self, nodetype):
|
||||||
"Return a list of all nodes of the given type"
|
"Return a list of all nodes of the given type"
|
||||||
|
@ -2,7 +2,7 @@ import types
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.functional import Promise
|
from django.utils.functional import Promise
|
||||||
|
|
||||||
def smart_unicode(s, encoding='utf-8'):
|
def smart_unicode(s, encoding='utf-8', errors='strict'):
|
||||||
"""
|
"""
|
||||||
Returns a unicode object representing 's'. Treats bytestrings using the
|
Returns a unicode object representing 's'. Treats bytestrings using the
|
||||||
'encoding' codec.
|
'encoding' codec.
|
||||||
@ -20,12 +20,12 @@ def smart_unicode(s, encoding='utf-8'):
|
|||||||
if hasattr(s, '__unicode__'):
|
if hasattr(s, '__unicode__'):
|
||||||
s = unicode(s)
|
s = unicode(s)
|
||||||
else:
|
else:
|
||||||
s = unicode(str(s), encoding)
|
s = unicode(str(s), encoding, errors)
|
||||||
elif not isinstance(s, unicode):
|
elif not isinstance(s, unicode):
|
||||||
s = unicode(s, encoding)
|
s = unicode(s, encoding, errors)
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def smart_str(s, encoding='utf-8', strings_only=False):
|
def smart_str(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'.
|
||||||
|
|
||||||
@ -37,11 +37,11 @@ def smart_str(s, encoding='utf-8', strings_only=False):
|
|||||||
try:
|
try:
|
||||||
return str(s)
|
return str(s)
|
||||||
except UnicodeEncodeError:
|
except UnicodeEncodeError:
|
||||||
return unicode(s).encode(encoding)
|
return unicode(s).encode(encoding, errors)
|
||||||
elif isinstance(s, unicode):
|
elif isinstance(s, unicode):
|
||||||
return s.encode(encoding)
|
return s.encode(encoding, errors)
|
||||||
elif s and encoding != 'utf-8':
|
elif s and encoding != 'utf-8':
|
||||||
return s.decode('utf-8').encode(encoding)
|
return s.decode('utf-8', errors).encode(encoding, errors)
|
||||||
else:
|
else:
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user