Skip to content

Commit 715e82d

Browse files
committed
support creating bytes from list of integer, long, and pint
1 parent e8dd14f commit 715e82d

File tree

1 file changed

+18
-2
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes

1 file changed

+18
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesUtils.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.ArrayList;
3535
import java.util.List;
3636

37+
import com.oracle.graal.python.builtins.objects.ints.PInt;
3738
import com.oracle.graal.python.builtins.objects.list.PList;
3839
import com.oracle.graal.python.runtime.PythonCore;
3940
import com.oracle.graal.python.runtime.sequence.PSequence;
@@ -74,12 +75,27 @@ public static byte[] fromList(PythonCore core, PList list) {
7475
Integer integer = (Integer) item;
7576
if (integer >= 0 && integer < 256) {
7677
bytes[i] = integer.byteValue();
77-
} else {
78-
throw core.raise(ValueError, "byte must be in range(0, 256)");
78+
continue;
7979
}
80+
} else if (item instanceof Long) {
81+
Long integer = (Long) item;
82+
if (integer >= 0 && integer < 256) {
83+
bytes[i] = integer.byteValue();
84+
continue;
85+
}
86+
} else if (item instanceof PInt) {
87+
try {
88+
long integer = ((PInt) item).intValueExact();
89+
if (integer >= 0 && integer < 256) {
90+
bytes[i] = (byte) integer;
91+
}
92+
} catch (ArithmeticException e) {
93+
}
94+
continue;
8095
} else {
8196
throw core.raise(TypeError, "'%s' object cannot be interpreted as an integer", core.lookupType(item.getClass()));
8297
}
98+
throw core.raise(ValueError, "byte must be in range(0, 256)");
8399
}
84100
return bytes;
85101
}

0 commit comments

Comments
 (0)