[pypy-svn] pypy default: (alex, armin) handle `print unicode` encoding correctly, fixes a failure in test_sys

alex_gaynor commits-noreply at bitbucket.org
Sun Mar 6 02:09:34 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r42437:4c541f936faf
Date: 2011-03-05 17:06 -0800
http://bitbucket.org/pypy/pypy/changeset/4c541f936faf/

Log:	(alex, armin) handle `print unicode` encoding correctly, fixes a
	failure in test_sys

diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -1408,6 +1408,8 @@
     def print_item_to(x, stream):
         if file_softspace(stream, False):
            stream.write(" ")
+        if isinstance(x, unicode) and getattr(stream, "encoding", None) is not None:
+            x = x.encode(stream.encoding, getattr(stream, "errors", None) or "strict")
         stream.write(str(x))
 
         # add a softspace unless we just printed a string which ends in a '\t'

diff --git a/pypy/interpreter/test/test_interpreter.py b/pypy/interpreter/test/test_interpreter.py
--- a/pypy/interpreter/test/test_interpreter.py
+++ b/pypy/interpreter/test/test_interpreter.py
@@ -270,6 +270,25 @@
         finally:
             sys.stdout = save
 
+    def test_print_unicode(self):
+        import sys
+
+        save = sys.stdout
+        class Out(object):
+            def __init__(self):
+                self.data = []
+
+            def write(self, x):
+                self.data.append(x)
+        sys.stdout = out = Out()
+        try:
+            raises(UnicodeError, "print unichr(0xa2)")
+            out.encoding = "cp424"
+            print unichr(0xa2)
+            assert out.data == [unichr(0xa2).encode("cp424"), "\n"]
+        finally:
+            sys.stdout = save
+
     def test_identity(self):
         def f(x): return x
         assert f(666) == 666


More information about the Pypy-commit mailing list