Skip to content

py/objint: Make byteorder argument optional in byte-conversion methods. #14387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions py/objint.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,15 +390,15 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp
// this is a classmethod
static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
// TODO: Support signed param (assumes signed=False at the moment)
(void)n_args;

// get the buffer info
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);

const byte *buf = (const byte *)bufinfo.buf;
int delta = 1;
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
bool big_endian = n_args < 3 || args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
if (!big_endian) {
buf += bufinfo.len - 1;
delta = -1;
}
Expand All @@ -409,27 +409,26 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
if (value > (MP_SMALL_INT_MAX >> 8)) {
// Result will overflow a small-int so construct a big-int
return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
return mp_obj_int_from_bytes_impl(big_endian, bufinfo.len, bufinfo.buf);
}
#endif
value = (value << 8) | *buf;
}
return mp_obj_new_int_from_uint(value);
}

static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 2, 4, int_from_bytes);
static MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));

static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
// TODO: Support signed (currently behaves as if signed=(val < 0))
(void)n_args;
bool overflow;

mp_int_t dlen = mp_obj_get_int(args[1]);
mp_int_t dlen = n_args < 2 ? 1 : mp_obj_get_int(args[1]);
if (dlen < 0) {
mp_raise_ValueError(NULL);
}
bool big_endian = args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
bool big_endian = n_args < 3 || args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);

vstr_t vstr;
vstr_init_len(&vstr, dlen);
Expand Down Expand Up @@ -469,7 +468,7 @@ static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {

return mp_obj_new_bytes_from_vstr(&vstr);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 3, 4, int_to_bytes);
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 1, 4, int_to_bytes);

static const mp_rom_map_elem_t int_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_from_bytes), MP_ROM_PTR(&int_from_bytes_obj) },
Expand Down
9 changes: 9 additions & 0 deletions tests/basics/int_bytes_optional_args_cp311.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Check optional byteorder argument (CPython 3.11+)
print((10).to_bytes(1))
print((100).to_bytes(10))
print(int.from_bytes(b"\0\0\0\0\0\0\0\0\0\x01"))
print(int.from_bytes(b"\x01\0"))

# Check optional length argument (CPython 3.11+)
print((10).to_bytes())
print((100).to_bytes())
6 changes: 6 additions & 0 deletions tests/basics/int_bytes_optional_args_cp311.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
b'\n'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00d'
1
256
b'\n'
b'd'
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy