From: <pj...@us...> - 2009-07-11 23:22:26
|
Revision: 6529 http://jython.svn.sourceforge.net/jython/?rev=6529&view=rev Author: pjenvey Date: 2009-07-11 23:22:13 +0000 (Sat, 11 Jul 2009) Log Message: ----------- don't encode unicode when printing to an intercepted stream thanks Pekka Klarck fixes #1802339 Modified Paths: -------------- trunk/jython/Lib/test/test_unicode_jy.py trunk/jython/NEWS trunk/jython/src/org/python/core/StdoutWrapper.java Modified: trunk/jython/Lib/test/test_unicode_jy.py =================================================================== --- trunk/jython/Lib/test/test_unicode_jy.py 2009-07-11 22:38:33 UTC (rev 6528) +++ trunk/jython/Lib/test/test_unicode_jy.py 2009-07-11 23:22:13 UTC (rev 6529) @@ -6,6 +6,7 @@ import re import sys import unittest +from StringIO import StringIO from test import test_support class UnicodeTestCase(unittest.TestCase): @@ -156,9 +157,25 @@ self.assertEquals(u"\u00e7%s" % "foo", u"\u00e7foo") +class UnicodeStdIOTestCase(unittest.TestCase): + + def setUp(self): + self.stdout = sys.stdout + + def tearDown(self): + sys.stdout = self.stdout + + def test_intercepted_stdout(self): + msg = u'Circle is 360\u00B0' + sys.stdout = StringIO() + print msg, + self.assertEqual(sys.stdout.getvalue(), msg) + + def test_main(): test_support.run_unittest(UnicodeTestCase, - UnicodeFormatTestCase) + UnicodeFormatTestCase, + UnicodeStdIOTestCase) if __name__ == "__main__": Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2009-07-11 22:38:33 UTC (rev 6528) +++ trunk/jython/NEWS 2009-07-11 23:22:13 UTC (rev 6529) @@ -10,6 +10,7 @@ - [ 1377 ] Event names shadowed by a field name on Java types leads to a NPE - [ 1381 ] Redundant declarations of interface implementation hides overriden methods - [ 1189 ] MD5 hash is incorrectly calculated when string contains non-latin chars and using python md5 lib + - [ 1802339 ] Problem printing unicode when stdout intercepted Jython 2.5.0 The same as rc4. Modified: trunk/jython/src/org/python/core/StdoutWrapper.java =================================================================== --- trunk/jython/src/org/python/core/StdoutWrapper.java 2009-07-11 22:38:33 UTC (rev 6528) +++ trunk/jython/src/org/python/core/StdoutWrapper.java 2009-07-11 23:22:13 UTC (rev 6529) @@ -41,6 +41,7 @@ return obj; } + @Override public void flush() { PyObject obj = myFile(); if (obj instanceof PyFile) { @@ -64,10 +65,12 @@ } } + @Override public void write(int i) { write(new String(new char[] { (char) i })); } + @Override public void write(byte[] data, int off, int len) { write(StringUtil.fromBytes(data, off, len)); } @@ -112,10 +115,10 @@ } else { s = o.__str__().toString(); } + file.write(s); - int len = s.length(); - file.write(s); if (o instanceof PyString) { + int len = s.length(); if (len == 0 || !Character.isWhitespace(s.charAt(len - 1)) || s.charAt(len - 1) == ' ') { file.softspace = space; @@ -123,6 +126,7 @@ } else { file.softspace = space; } + if (newline) { file.write("\n"); file.softspace = false; @@ -143,9 +147,10 @@ } else { s = o.toString(); } - int len = s.length(); file.write(s); + if (o instanceof PyString) { + int len = s.length(); if (len == 0 || !Character.isWhitespace(s.charAt(len - 1)) || s.charAt(len - 1) == ' ') { file.softspace = space; @@ -153,6 +158,7 @@ } else { file.softspace = space; } + if (newline) { file.write("\n"); file.softspace = false; @@ -164,11 +170,15 @@ obj.invoke("write", Py.Space); obj.__setattr__("softspace", Py.Zero); } - PyString string = o.__str__(); - String s = o.toString(); - int len = s.length(); - obj.invoke("write", string); + + if (!(o instanceof PyUnicode)) { + o = o.__str__(); + } + obj.invoke("write", o); + if (o instanceof PyString) { + String s = o.toString(); + int len = s.length(); if (len == 0 || !Character.isWhitespace(s.charAt(len - 1)) || s.charAt(len - 1) == ' ') { obj.__setattr__("softspace", space ? Py.One : Py.Zero); @@ -176,6 +186,7 @@ } else { obj.__setattr__("softspace", space ? Py.One : Py.Zero); } + if (newline) { obj.invoke("write", Py.Newline); obj.__setattr__("softspace", Py.Zero); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |