Python4j: bytes conversion fix + test (#497)

* bytes fix+test

* bytes fix+test
master
Fariz Rahman 2020-06-29 10:48:25 +04:00 committed by GitHub
parent 4190c9ee0f
commit 99b85c5006
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

View File

@ -258,7 +258,7 @@ public class PythonTypes {
return ret; return ret;
}else if (javaObject instanceof byte[]){ }else if (javaObject instanceof byte[]){
byte[] arr = (byte[]) javaObject; byte[] arr = (byte[]) javaObject;
for (int x : arr) ret.add(x); for (int x : arr) ret.add(x & 0xff);
return ret; return ret;
} else if (javaObject instanceof long[]) { } else if (javaObject instanceof long[]) {
long[] arr = (long[]) javaObject; long[] arr = (long[]) javaObject;

View File

@ -81,13 +81,28 @@ public class PythonPrimitiveTypesTest {
} }
@Test @Test
public void testBytes() { public void testBytes() {
byte[] bytes = new byte[256];
for (int i = 0; i < 256; i++) {
bytes[i] = (byte) i;
}
List<PythonVariable> inputs = new ArrayList<>();
inputs.add(new PythonVariable<>("b1", PythonTypes.BYTES, bytes));
List<PythonVariable> outputs = new ArrayList<>();
outputs.add(new PythonVariable<>("b2", PythonTypes.BYTES));
String code = "b2=b1";
PythonExecutioner.exec(code, inputs, outputs);
Assert.assertArrayEquals(bytes, (byte[]) outputs.get(0).getValue());
}
@Test
public void testBytes2() {
byte[] bytes = new byte[]{97, 98, 99}; byte[] bytes = new byte[]{97, 98, 99};
List<PythonVariable> inputs = new ArrayList<>(); List<PythonVariable> inputs = new ArrayList<>();
inputs.add(new PythonVariable<>("buff", PythonTypes.BYTES, bytes)); inputs.add(new PythonVariable<>("b1", PythonTypes.BYTES, bytes));
List<PythonVariable> outputs = new ArrayList<>(); List<PythonVariable> outputs = new ArrayList<>();
outputs.add(new PythonVariable<>("s1", PythonTypes.STR)); outputs.add(new PythonVariable<>("s1", PythonTypes.STR));
outputs.add(new PythonVariable<>("buff2", PythonTypes.BYTES)); outputs.add(new PythonVariable<>("b2", PythonTypes.BYTES));
String code = "s1 = ''.join(chr(c) for c in buff)\nbuff2=b'def'"; String code = "s1 = ''.join(chr(c) for c in b1)\nb2=b'def'";
PythonExecutioner.exec(code, inputs, outputs); PythonExecutioner.exec(code, inputs, outputs);
Assert.assertEquals("abc", outputs.get(0).getValue()); Assert.assertEquals("abc", outputs.get(0).getValue());
Assert.assertArrayEquals(new byte[]{100, 101, 102}, (byte[]) outputs.get(1).getValue()); Assert.assertArrayEquals(new byte[]{100, 101, 102}, (byte[]) outputs.get(1).getValue());