|
From: Chris C. <Chr...@ac...> - 2016-04-09 03:40:25
|
TL;DR I need a way to convert str type into a Java array of bytes type, jarray used to be the way to do this with all previous versions of Jython before 2.7.
I'm seeing a change in behavior in Jython 2.7.0 compared with Jython 2.5.3 related to jarray and bytes.
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_67
Type "help", "copyright", "credits" or "license" for more information.
>>> import jarray
>>> sval = 'a string'
>>> print type(sval)
<type 'str'>
>>> x = jarray.array(sval, 'b')
>>>
Above works fine.
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_67
Type "help", "copyright", "credits" or "license" for more information.
>>> import jarray
>>> sval = 'a string'
>>> print type(sval)
<type 'str'>
>>> x = jarray.array(sval, 'b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Type not compatible with array type
Above fails :-(
I can't see anything in the docs about this. There is the new bytearray support in Jython 2.7.0 which appears to work for the above example:
>>> x = jarray.array(bytearray(sval), 'b')
But this doesn't really work once things get more complicated.
>>> sval = ''.join([chr(z) for z in range(0xff + 1)])
>>> x = jarray.array(bytearray(sval), 'b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: value too large for byte
I don't even know what too large might mean, it seems to be a problem with values above 128.
This is preventing blob/binary support from working in jyjdbc (https://bitbucket.org/clach04/jyjdbc/) with Jython 2.7.0, binary works fine in Jython 2.2 and 2.5.3.
Thanks,
Chris
|