simplyfy ascii escaping by using backslashreplace error handling
This commit is contained in:
parent
488bbd2aeb
commit
78a027e128
|
@ -121,7 +121,6 @@ if sys.version_info[:2] == (2, 6):
|
||||||
|
|
||||||
|
|
||||||
if _PY3:
|
if _PY3:
|
||||||
import codecs
|
|
||||||
imap = map
|
imap = map
|
||||||
izip = zip
|
izip = zip
|
||||||
STRING_TYPES = bytes, str
|
STRING_TYPES = bytes, str
|
||||||
|
@ -146,13 +145,7 @@ if _PY3:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if isinstance(val, bytes):
|
if isinstance(val, bytes):
|
||||||
if val:
|
return val.decode('ascii', 'backslashreplace')
|
||||||
# source: http://goo.gl/bGsnwC
|
|
||||||
encoded_bytes, _ = codecs.escape_encode(val)
|
|
||||||
return encoded_bytes.decode('ascii')
|
|
||||||
else:
|
|
||||||
# empty bytes crashes codecs.escape_encode (#1087)
|
|
||||||
return ''
|
|
||||||
else:
|
else:
|
||||||
return val.encode('unicode_escape').decode('ascii')
|
return val.encode('unicode_escape').decode('ascii')
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
- simplify ascii string escaping by using the backslashreplace error handler
|
Loading…
Reference in New Issue