Message10968

Author yan12125
Recipients yan12125
Date 2016-10-20.13:12:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1476969172.79.0.172212034968.issue2527@psf.upfronthosting.co.za>
In-reply-to
Content
$ ./dist/bin/jython   
Jython 2.7.1b3 (default:a07c595b410f, Oct 20 2016, 21:07:16) 
[OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_111
Type "help", "copyright", "credits" or "license" for more information.
>>> import cStringIO
>>> s = cStringIO.StringIO(u'中文')
>>> s.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
        at org.python.core.PyString.<init>(PyString.java:64)
        at org.python.core.PyString.<init>(PyString.java:70)
        at org.python.modules.cStringIO$StringIO.read(cStringIO.java:225)
        at org.python.modules.cStringIO$StringIO.read(cStringIO.java:198)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

From https://docs.python.org/2/library/stringio.html:
Unlike the StringIO module, this module is not able to accept Unicode strings that cannot be encoded as plain ASCII strings.

Python 2 handles it well:

$ python2
Python 2.7.12 (default, Jun 28 2016, 08:31:05) 
[GCC 6.1.1 20160602] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cStringIO
>>> s = cStringIO.StringIO(u'中文')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

Jython should throw a Python exception instead of a Java one in this case.
History
Date User Action Args
2016-10-20 13:12:52yan12125setrecipients: + yan12125
2016-10-20 13:12:52yan12125setmessageid: <1476969172.79.0.172212034968.issue2527@psf.upfronthosting.co.za>
2016-10-20 13:12:52yan12125linkissue2527 messages
2016-10-20 13:12:51yan12125create