← index #6332Issue #2868
Duplicate · high · value 3.793
QUERY · ISSUE

Overflow converting -2147483648 to int when using MPZ

openby markstzopened 2020-08-12updated 2020-08-12
py-core

When MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ, calling mp_obj_get_int() for a Python variable that contains the value -2147483648 results in an overflow converting long int to machine word error.

If MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG instead, the conversion works OK.

Is the MPZ overflow check incorrect?

CANDIDATE · ISSUE

Can't convert -0x80000000 to int32 with mp_obj_int_get_checked

closedby ghsebopened 2017-02-13updated 2019-07-17

Hello,

correct me if im wrong, but

mp_int_t in = -0x80000000;
mp_obj_t i = mp_obj_new_int_from_ll(in);
mp_int_t out = mp_obj_int_get_checked(i);

throws

OverflowError: overflow converting long int to machine word

I would expect this conversion to be valid since -0x80000000 is in the range of a int32.

This works:

mp_int_t in = -0x7FFFFFFF;
mp_obj_t i = mp_obj_new_int_from_ll(in);
mp_int_t out = mp_obj_int_get_checked(i);

Sebastian

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied