Skip to content

Commit 8c5632a

Browse files
committed
py/objint: Support "big" byte-order in int.to_bytes().
1 parent 2bf5a94 commit 8c5632a

5 files changed

Lines changed: 12 additions & 12 deletions

File tree

py/objint.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_fro
432432
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
433433

434434
STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
435-
// TODO: Support byteorder param
436435
// TODO: Support signed param (assumes signed=False)
437436
(void)n_args;
438437

439-
if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
440-
mp_not_implemented("");
441-
}
442-
443438
mp_uint_t len = MP_OBJ_SMALL_INT_VALUE(args[1]);
439+
bool big_endian = args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
444440

445441
vstr_t vstr;
446442
vstr_init_len(&vstr, len);
@@ -449,12 +445,13 @@ STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
449445

450446
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
451447
if (!MP_OBJ_IS_SMALL_INT(args[0])) {
452-
mp_obj_int_to_bytes_impl(args[0], false, len, data);
448+
mp_obj_int_to_bytes_impl(args[0], big_endian, len, data);
453449
} else
454450
#endif
455451
{
456452
mp_int_t val = MP_OBJ_SMALL_INT_VALUE(args[0]);
457-
mp_binary_set_int(MIN((size_t)len, sizeof(val)), false, data, val);
453+
size_t l = MIN((size_t)len, sizeof(val));
454+
mp_binary_set_int(l, big_endian, data + (big_endian ? (len - l) : 0), val);
458455
}
459456

460457
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);

tests/basics/int_bytes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
# check that extra zero bytes don't change the internal int value
99
print(int.from_bytes(bytes(20), "little") == 0)
1010
print(int.from_bytes(b"\x01" + bytes(20), "little") == 1)
11+
12+
# big-endian conversion
13+
print((10).to_bytes(1, "big"))
14+
print((100).to_bytes(10, "big"))
15+
print(int.from_bytes(b"\0\0\0\0\0\0\0\0\0\x01", "big"))
16+
print(int.from_bytes(b"\x01\0", "big"))

tests/basics/int_bytes_intbig.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
print((2**64).to_bytes(9, "little"))
2+
print((2**64).to_bytes(9, "big"))
23

34
b = bytes(range(20))
45

@@ -7,6 +8,7 @@
78
print(il)
89
print(ib)
910
print(il.to_bytes(20, "little"))
11+
print(ib.to_bytes(20, "big"))
1012

1113
# check that extra zero bytes don't change the internal int value
1214
print(int.from_bytes(b + bytes(10), "little") == int.from_bytes(b, "little"))

tests/basics/int_bytes_notimpl.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/basics/int_bytes_notimpl.py.exp

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)