Message1939

Author pekka.klarck
Recipients
Date 2007-09-30.22:21:49
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
I totally agree that fixing this issue with a hack that just seems to solve the problems is not the right thing to do. My patch was just an example showing that somehow modifying StdoutWrapper might be a part of the solution. Unfortunately I don't understand Jython (nor CPython) internals well enough to be able to figure out a real fix. =/

Thanks for mentioning org.python.core.codecs.setDefaultEncoding. I played with it a little and it seems that we could even have a workaround for the problem in our system. I changed my original example slightly and was able to get "print <unicode>" working. There are still some differences between different Jython versions and CPython but we should be able to handle them.

Here's the new code:

- - - - - - - - - -
import sys
import os
from StringIO import StringIO
if os.name == 'java':
    from org.python.core import codecs
    codecs.setDefaultEncoding('utf-8')

    print 'Jython', sys.version
else:
    print 'Python', sys.version

sys.stdout = StringIO()
msg = u'Circle is 360\u00B0'
print msg

out = sys.stdout.getvalue()
sys.stdout = sys.__stdout__
print out, type(out)
print msg, type(msg)
assert out == msg + '\n'
- - - - - - - - - -

And here are outputs using few different interpreters:

- - - - - - - - - -
Jython 2.2rc3
Circle is 360°
<type 'str'>
Circle is 360° <type 'unicode'>
- - - - - - - - - -
Jython 2.2.1rc1
Circle is 360°
<type 'str'>
Circle is 360° <type 'unicode'>
Traceback (innermost last):
  File "unictest.py", line 21, in ?
AssertionError: 
- - - - - - - - - -
Python 2.5.1 (r251:54863, May  2 2007, 16:56:35) 
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)]
Circle is 360°
<type 'unicode'>
Circle is 360° <type 'unicode'>
History
Date User Action Args
2008-02-20 17:18:03adminlinkissue1802339 messages
2008-02-20 17:18:03admincreate