Merge pull request #2734 from RonnyPfannschmidt/simplify-string-safening
simplyfy ascii escaping by using backslashreplace error handling
This commit is contained in:
commit
e1f2254fc2
|
@ -7,6 +7,7 @@ import inspect
|
||||||
import types
|
import types
|
||||||
import re
|
import re
|
||||||
import functools
|
import functools
|
||||||
|
import codecs
|
||||||
|
|
||||||
import py
|
import py
|
||||||
|
|
||||||
|
@ -122,12 +123,24 @@ 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
|
||||||
UNICODE_TYPES = str,
|
UNICODE_TYPES = str,
|
||||||
|
|
||||||
|
if PY35:
|
||||||
|
def _bytes_to_ascii(val):
|
||||||
|
return val.decode('ascii', 'backslashreplace')
|
||||||
|
else:
|
||||||
|
def _bytes_to_ascii(val):
|
||||||
|
if val:
|
||||||
|
# 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 ''
|
||||||
|
|
||||||
def _ascii_escaped(val):
|
def _ascii_escaped(val):
|
||||||
"""If val is pure ascii, returns it as a str(). Otherwise, escapes
|
"""If val is pure ascii, returns it as a str(). Otherwise, escapes
|
||||||
bytes objects into a sequence of escaped bytes:
|
bytes objects into a sequence of escaped bytes:
|
||||||
|
@ -147,13 +160,7 @@ if _PY3:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if isinstance(val, bytes):
|
if isinstance(val, bytes):
|
||||||
if val:
|
return _bytes_to_ascii(val)
|
||||||
# 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 @@
|
||||||
|
Internal refactor: simplify ascii string escaping by using the backslashreplace error handler in newer Python 3 versions.
|
Loading…
Reference in New Issue