← index #6332PR #1063
Related · medium · value 4.133
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 · PULL REQUEST

py: Convert float to correct int type

closedby doogleopened 2015-01-13updated 2015-01-24

A redo of pull request #1047, to address issue #1042 (currently worked around by ad2307c92c15f0aa90dbd0741fd2538719d0b5e1).

The approach I used in the end allowed for the full range of small ints to be used. Exceptions are now raised in the event of an overflow as well.

As discussed, fpclassify() is used to test for inf and NaN, and the new mp_classify_fp_as_int() function is used for all other values.

The code size could be reduced slightly by not caring so much about ensuring that the full range of small ints are used, but (in the case of MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_MPZ), this would also result in exceptions being raised when the number could be converted to an int (which I would count as a bug).

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