Skip to content

Commit 2b33034

Browse files
committed
fix ints.to_bytes, byteorder positional argument is mandatory
1 parent 12cb0b2 commit 2b33034

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints

‎graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,20 +1769,26 @@ Object doGeneric(@SuppressWarnings("unused") Object self) {
17691769
}
17701770
}
17711771

1772-
@Builtin(name = "to_bytes", minNumOfArguments = 1, maxNumOfArguments = 2)
1772+
@Builtin(name = "to_bytes", fixedNumOfArguments = 3, takesVariableArguments = true)
17731773
@GenerateNodeFactory
17741774
abstract static class ToBytesNode extends PythonBuiltinNode {
17751775
@Specialization
1776-
PBytes doInt(int self, int length) {
1776+
PBytes doInt(int self, int length, String byteorder) {
17771777
byte[] bytes = new byte[length];
1778-
for (int i = 0 ; i < length; i++) {
1779-
bytes[i] = (byte) ((self >> i) & 0xFF);
1778+
if (byteorder.equals("big")) {
1779+
for (int i = 0; i < length; i++) {
1780+
bytes[i] = (byte) ((self >> i) & 0xFF);
1781+
}
1782+
} else {
1783+
for (int i = 0; i < length; i++) {
1784+
bytes[i] = (byte) ((self << i) & 0xFF);
1785+
}
17801786
}
17811787
return factory().createBytes(bytes);
17821788
}
17831789

17841790
@Specialization
1785-
PBytes doLong(long self, int length) {
1791+
PBytes doLong(long self, int length, String byteorder) {
17861792
byte[] bytes = new byte[length];
17871793
for (int i = 0 ; i < length; i++) {
17881794
bytes[i] = (byte) ((self >> i) & 0xFF);

0 commit comments

Comments
 (0)