Skip to content

Commit

Permalink
Merge 13eac94 into 9d373d8
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Sep 4, 2017
2 parents 9d373d8 + 13eac94 commit 29aafa1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
23 changes: 15 additions & 8 deletions _pytest/compat.py
Expand Up @@ -7,6 +7,7 @@
import types
import re
import functools
import codecs

import py

Expand Down Expand Up @@ -121,12 +122,24 @@ def isclass(object):


if _PY3:
import codecs
imap = map
izip = zip
STRING_TYPES = bytes, 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):
"""If val is pure ascii, returns it as a str(). Otherwise, escapes
bytes objects into a sequence of escaped bytes:
Expand All @@ -146,13 +159,7 @@ def _ascii_escaped(val):
"""
if isinstance(val, bytes):
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 ''
return _bytes_to_ascii(val)
else:
return val.encode('unicode_escape').decode('ascii')
else:
Expand Down
1 change: 1 addition & 0 deletions changelog/2734.trivial
@@ -0,0 +1 @@
- simplify ascii string escaping by using the backslashreplace error handler

0 comments on commit 29aafa1

Please sign in to comment.