Skip to content

Commit 3cc1757

Browse files
committed
add test for creating bytes from foreign array
1 parent 8e36f2c commit 3cc1757

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/interop/JavaInteropTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ public void javaArraySet() {
182182
assertPrints("42\n", source);
183183
}
184184

185+
@Test
186+
public void javaArrayBytes() {
187+
String source = "import java\n" +
188+
"array = java.type(\"short[]\")(4)\n" +
189+
"array[0] = 1\n" +
190+
"array[1] = 2\n" +
191+
"array[2] = 3\n" +
192+
"array[3] = 4\n" +
193+
"print(bytes(array))\n\n";
194+
assertPrints("b'\\x01\\x02\\x03\\x04'\n", source);
195+
}
196+
185197
@Test
186198
public void testPassingFloats() throws UnsupportedEncodingException {
187199
String source = "import polyglot\n" +

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/CastToByteNode.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -87,6 +87,20 @@ protected byte doByte(byte value) {
8787
return value;
8888
}
8989

90+
@Specialization(rewriteOn = ArithmeticException.class)
91+
protected byte doShort(short value) {
92+
return PInt.byteValueExact(value);
93+
}
94+
95+
@Specialization(replaces = "doShort")
96+
protected byte doShortOvf(short value) {
97+
try {
98+
return PInt.byteValueExact(value);
99+
} catch (ArithmeticException e) {
100+
return handleRangeError(value);
101+
}
102+
}
103+
90104
@Specialization(rewriteOn = ArithmeticException.class)
91105
protected byte doInt(int value) {
92106
return PInt.byteValueExact(value);

0 commit comments

Comments
 (0)