printable filter should replace non-ascii bytes

In Python 2, when repr() returns bytes, replace any non-ascii bytes
with the unicode ? character to ensure that the result is printable.

Fixes #66
This commit is contained in:
Matt Good
2014-02-02 14:44:20 -08:00
parent 70488fc14a
commit 82295aa4aa
2 changed files with 34 additions and 1 deletions

View File

@@ -30,7 +30,10 @@ def replace_insensitive(string, target, replacement):
def _printable(value):
try:
return repr(value)
value = repr(value)
if isinstance(value, bytes):
value = value.decode('ascii', 'replace')
return value
except Exception as e:
return '<repr(%s) raised %s: %s>' % (
object.__repr__(value), type(e).__name__, e)