def to_utf8(text):
<EOL>if not text:<EOL><INDENT>if isinstance(text, string_types):<EOL><INDENT>text = "<STR_LIT>"<EOL><DEDENT>return text<EOL><DEDENT>try:<EOL><INDENT>return text.encode("<STR_LIT:utf8>")<EOL><DEDENT>except UnicodeDecodeError:<EOL><INDENT>try:<EOL><INDENT>if text.startswith(codecs.BOM_UTF8):<EOL><INDENT>text = text[len(codecs.BOM_UTF8):]<EOL><DEDENT>return text.decode("<STR_LIT:utf8>").encode("<STR_LIT:utf8>")<EOL><DEDENT>except UnicodeDecodeError:<EOL><INDENT>if text.startswith(codecs.BOM_UTF16_LE):<EOL><INDENT>encoding = "<STR_LIT>"<EOL>text = text[len(codecs.BOM_UTF16_LE):]<EOL><DEDENT>elif text.startswith(codecs.BOM_UTF16_BE):<EOL><INDENT>encoding = "<STR_LIT>"<EOL>text = text[len(codecs.BOM_UTF16_BE):]<EOL><DEDENT>else:<EOL><INDENT>encoding = "<STR_LIT>"<EOL><DEDENT>try:<EOL><INDENT>return text.decode(encoding).encode("<STR_LIT:utf8>")<EOL><DEDENT>except UnicodeDecodeError as exc:<EOL><INDENT>for line in text.splitlines():<EOL><INDENT>try:<EOL><INDENT>line.decode(encoding).encode("<STR_LIT:utf8>")<EOL><DEDENT>except UnicodeDecodeError:<EOL><INDENT>log.warn("<STR_LIT>" % (exc, line))<EOL>break<EOL><DEDENT><DEDENT>return text<EOL><DEDENT><DEDENT><DEDENT>
Enforce UTF8 encoding.