Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r61707:34a601ca6d23
Date: 2013-02-23 12:55 -0800
http://bitbucket.org/pypy/pypy/changeset/34a601ca6d23/

Log:    handle unencodable strings in displayhook

diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -1477,7 +1477,20 @@
         # (and let it deals itself with unicode handling)
         if not isinstance(x, str):
             x = str(x)
-        stream.write(x)
+        try:
+            stream.write(x)
+        except UnicodeEncodeError:
+            print_unencodable_to(x, stream)
+
+    def print_unencodable_to(x, stream):
+        encoding = stream.encoding
+        encoded = x.encode(encoding, 'backslashreplace')
+        buffer = getattr(stream, 'buffer', None)
+        if buffer is not None:
+             buffer.write(encoded)
+        else:
+            escaped = encoded.decode(encoding, 'strict')
+            stream.write(escaped)
 
     def print_item(x):
         print_item_to(x, sys_stdout())
diff --git a/pypy/module/sys/test/test_sysmodule.py 
b/pypy/module/sys/test/test_sysmodule.py
--- a/pypy/module/sys/test/test_sysmodule.py
+++ b/pypy/module/sys/test/test_sysmodule.py
@@ -170,6 +170,18 @@
 
         sys.stdout = savestdout
 
+    def test_original_displayhook_unencodable(self):
+        import sys, _io
+        out = _io.BytesIO()
+        savestdout = sys.stdout
+        sys.stdout = _io.TextIOWrapper(out, encoding='ascii')
+
+        sys.__displayhook__("a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF")
+        assert (out.getvalue() ==
+                b"'a=\\xe9 b=\\udc80 c=\\U00010000 d=\\U0010ffff'")
+
+        sys.stdout = savestdout
+
     def test_lost_displayhook(self):
         import sys
         olddisplayhook = sys.displayhook
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to