bytearray: fix bytes -128-127 from java to 0-255 python

This commit is contained in:
Mathieu Virbel 2015-12-10 13:11:25 +01:00
parent f11f88e2df
commit c5863be424
3 changed files with 19 additions and 13 deletions

View File

@ -57,8 +57,12 @@ cdef void populate_args(JNIEnv *j_env, tuple definition_args, jvalue *j_args, ar
j_args[index].l = NULL
elif (isinstance(py_arg, basestring) or (PY_MAJOR_VERSION >=3 and isinstance(py_arg, str))) \
and jstringy_arg(argtype):
py_str = <bytes>py_arg.encode('utf-8')
j_args[index].l = j_env[0].NewStringUTF(j_env, <char *>py_str)
try:
py_str = <bytes>py_arg
j_args[index].l = j_env[0].NewStringUTF(j_env, <char *>py_str)
except UnicodeEncodeError:
py_str = <bytes>py_arg.encode('utf-8')
j_args[index].l = j_env[0].NewStringUTF(j_env, <char *>py_str)
elif isinstance(py_arg, JavaClass):
jc = py_arg
check_assignable_from(j_env, jc, argtype[1:-1])
@ -492,7 +496,7 @@ cdef jobject convert_pyarray_to_java(JNIEnv *j_env, definition, pyarray) except
if isinstance(pyarray, ByteArray):
a_bytes = pyarray
j_env[0].SetByteArrayRegion(j_env,
ret, 0, array_size, a_bytes._buf)
ret, 0, array_size, <const_jbyte *>a_bytes._buf)
else:
for i in range(array_size):
c_tmp = pyarray[i]

View File

@ -17,8 +17,8 @@ cdef python_op(int op, object a, object b):
cdef class ByteArray:
cdef LocalRef _jobject
cdef long _size
cdef jbyte *_buf
cdef jbyte[:] _arr
cdef unsigned char *_buf
cdef unsigned char[:] _arr
def __cinit__(self):
self._size = 0
@ -31,7 +31,8 @@ cdef class ByteArray:
cdef JNIEnv *j_env
if self._buf != NULL:
j_env = get_jnienv()
j_env[0].ReleaseByteArrayElements(j_env, self._jobject.obj, self._buf, 0)
j_env[0].ReleaseByteArrayElements(
j_env, self._jobject.obj, <jbyte *>self._buf, 0)
self._buf = NULL
self._jobject = None
@ -41,8 +42,8 @@ cdef class ByteArray:
self._jobject = LocalRef()
self._jobject.create(env, obj)
self._size = size
self._buf = buf
self._arr = <jbyte[:size]>self._buf
self._buf = <unsigned char *><signed char *>buf
self._arr = <unsigned char[:size]>self._buf
def __str__(self):
return '<ByteArray size={} at 0x{}>'.format(

View File

@ -16,8 +16,8 @@ cdef python_op(int op, object a, object b):
cdef class ByteArray:
cdef LocalRef _jobject
cdef long _size
cdef jbyte *_buf
cdef jbyte[:] _arr
cdef unsigned char *_buf
cdef unsigned char[:] _arr
def __cinit__(self):
self._size = 0
@ -28,7 +28,8 @@ cdef class ByteArray:
cdef JNIEnv *j_env
if self._buf != NULL:
j_env = get_jnienv()
j_env[0].ReleaseByteArrayElements(j_env, self._jobject.obj, self._buf, 0)
j_env[0].ReleaseByteArrayElements(
j_env, self._jobject.obj, <jbyte *>self._buf, 0)
self._buf = NULL
self._jobject = None
@ -38,8 +39,8 @@ cdef class ByteArray:
self._jobject = LocalRef()
self._jobject.create(env, obj)
self._size = size
self._buf = buf
self._arr = <jbyte[:size]>self._buf
self._buf = <unsigned char *><signed char *>buf
self._arr = <unsigned char[:size]>self._buf
def __str__(self):
return '<ByteArray size={} at 0x{}>'.format(