summaryrefslogtreecommitdiffstatshomepage
path: root/py/objint_mpz.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-03-19 23:15:25 +0000
committerDamien George <damien.p.george@gmail.com>2014-03-19 23:15:25 +0000
commitcd8b2baf4344cb4ace3a5d85a13047e77ef3d743 (patch)
tree8396a8921c7bca528b56722cebd574af4a7ab187 /py/objint_mpz.c
parent494600bc996ff46ae9081148590eb749ea6123af (diff)
downloadmicropython-cd8b2baf4344cb4ace3a5d85a13047e77ef3d743.tar.gz
micropython-cd8b2baf4344cb4ace3a5d85a13047e77ef3d743.zip
py: Fix bug in mpz int, where small int is on lhs, mpz on rhs.
Diffstat (limited to 'py/objint_mpz.c')
-rw-r--r--py/objint_mpz.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index 8b7ed78a2c..342e8ec276 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -44,11 +44,22 @@ mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
}
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
- mpz_t *zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
+ const mpz_t *zlhs;
const mpz_t *zrhs;
mpz_t z_int;
mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
+ // lhs could be a small int (eg small-int + mpz)
+ if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
+ mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
+ zlhs = &z_int;
+ } else if (MP_OBJ_IS_TYPE(lhs_in, &int_type)) {
+ zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
+ } else {
+ return MP_OBJ_NULL;
+ }
+
+ // if rhs is small int, then lhs was not (otherwise rt_binary_op handles it)
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
zrhs = &z_int;