>>> x = r'backslash n, not newline: \n'
>>> x.encode('string_escape').replace(r'\n', '\n')
'backslash n, not newline: \\\n'
>>> '\n' in x
False
>>> '\n' in x.encode('string_escape').replace(r'\n', '\n')
True
Otherwise this seems ok. When UnicodeErrors occur you _safe_str all variable that might be involved, which is perhaps too zealous. But it's undoubtedly better than allowing the UnicodeError to abort selftest.
+ return string_ or_unicode. encode( 'string_ escape' ).replace( r'\n', '\n')
This mangles the output:
>>> x = r'backslash n, not newline: \n' 'string_ escape' ).replace( r'\n', '\n') 'string_ escape' ).replace( r'\n', '\n')
>>> x.encode(
'backslash n, not newline: \\\n'
>>> '\n' in x
False
>>> '\n' in x.encode(
True
Otherwise this seems ok. When UnicodeErrors occur you _safe_str all variable that might be involved, which is perhaps too zealous. But it's undoubtedly better than allowing the UnicodeError to abort selftest.