def _apply(self, value):
decoded = super(ByteString, self)._apply(value) # type: Text
#
# No need to catch UnicodeEncodeErrors here; UTF-8 can handle
# any unicode value.
#
# Technically, we could get this error if we encounter a code
# point beyond U+10FFFF (the highest valid code point in the
# Unicode standard).
#
# However, it's not possible to create a `unicode` object with
# an invalid code point, so we wouldn't even be able to get
# this far if the incoming value contained a character that
# can't be represented using UTF-8.
#
# Note that in some versions of Python, it is possible (albeit
# really difficult) to trick Python into creating unicode
# objects with invalid code points, but it generally requires
# using specific codecs that aren't UTF-8.
#
# Example of exploit and release notes from the Python release
# (2.7.6) that fixes the issue:
#
# - https://gist.github.com/rspeer/7559750
# - https://hg.python.org/cpython/raw-file/99d03261c1ba/Misc/NEWS
#
# Normally we return ``None`` if we get any errors, but in this
# case, we'll let the superclass method decide.
return decoded if self._has_errors else decoded.encode('utf-8')
评论列表
文章目录