From c9aa58e6381018cca2513e3468363af0b5442d1f Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 31 Jul 2014 13:41:43 +0000 Subject: py: Improve handling of long-int overflow. This removes mpz_as_int, since that was a terrible function (it implemented saturating conversion). Use mpz_as_int_checked and mpz_as_uint_checked. These now work correctly (they previously had wrong overflow checking, eg print(chr(10000000000000)) on 32-bit machine would incorrectly convert this large number to a small int). --- py/objint_mpz.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'py/objint_mpz.c') diff --git a/py/objint_mpz.c b/py/objint_mpz.c index c60e5c2b8..8233773b8 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -225,8 +225,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { case MP_BINARY_OP_INPLACE_LSHIFT: case MP_BINARY_OP_RSHIFT: case MP_BINARY_OP_INPLACE_RSHIFT: { - // TODO check conversion overflow - mp_int_t irhs = mpz_as_int(zrhs); + mp_int_t irhs = mp_obj_int_get_checked(rhs_in); if (irhs < 0) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count")); } @@ -303,7 +302,8 @@ mp_int_t mp_obj_int_get(mp_const_obj_t self_in) { return MP_OBJ_SMALL_INT_VALUE(self_in); } else { const mp_obj_int_t *self = self_in; - return mpz_as_int(&self->mpz); + // TODO this is a hack until we remove mp_obj_int_get function entirely + return mpz_hash(&self->mpz); } } -- cgit v1.2.3