Source

py-patches / decode-errors

Full commit
# HG changeset patch
# Parent b418f117d16a960b5c276f06b51b4fe2d4ff3e80
support passing error handler names to py.builtin._totext

diff --git a/py/_builtin.py b/py/_builtin.py
--- a/py/_builtin.py
+++ b/py/_builtin.py
@@ -113,9 +113,9 @@ if sys.version_info >= (3, 0):
 
     # some backward compatibility helpers
     _basestring = str
-    def _totext(obj, encoding=None):
+    def _totext(obj, encoding=None, errors=None):
         if isinstance(obj, bytes):
-            obj = obj.decode(encoding)
+            obj = obj.decode(encoding, errors)
         elif not isinstance(obj, str):
             obj = str(obj)
         return obj
diff --git a/testing/root/test_builtin.py b/testing/root/test_builtin.py
--- a/testing/root/test_builtin.py
+++ b/testing/root/test_builtin.py
@@ -133,6 +133,15 @@ def test_callable():
 def test_totext():
     py.builtin._totext("hello", "UTF-8")
 
+def test_totext_badutf8():
+    # this was in printouts within the pytest testsuite
+    # totext would fail
+    if sys.version_info >= (3,):
+        errors = 'surrogateescape'
+    else: # old python has crappy error handlers
+        errors = 'replace'
+    py.builtin._totext("\xa6", "UTF-8", errors)
+
 def test_reraise():
     from py.builtin import _reraise
     try: