Skip to content

Commit 660aef6

Browse files
committed
py: Allow multiple of str/list/tuple on left by an integer.
1 parent 4881566 commit 660aef6

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

py/objint.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <stdlib.h>
12
#include <stdint.h>
23
#include <assert.h>
34

@@ -9,6 +10,8 @@
910
#include "parsenum.h"
1011
#include "mpz.h"
1112
#include "objint.h"
13+
#include "runtime0.h"
14+
#include "runtime.h"
1215

1316
#if MICROPY_ENABLE_FLOAT
1417
#include <math.h>
@@ -59,16 +62,20 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
5962
}
6063
}
6164

62-
// This is called only for non-SMALL_INT
65+
// This is called for operations on SMALL_INT that are not handled by mp_unary_op
6366
mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
64-
assert(0);
65-
return mp_const_none;
67+
return MP_OBJ_NULL;
6668
}
6769

68-
// This is called only for non-SMALL_INT
70+
// This is called for operations on SMALL_INT that are not handled by mp_binary_op
6971
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
70-
assert(0);
71-
return mp_const_none;
72+
if (op == MP_BINARY_OP_MULTIPLY) {
73+
if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) {
74+
// multiply is commutative for these types, so delegate to them
75+
return mp_binary_op(op, rhs_in, lhs_in);
76+
}
77+
}
78+
return MP_OBJ_NULL;
7279
}
7380

7481
// This is called only with strings whose value doesn't fit in SMALL_INT

py/objint_longlong.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "mpz.h"
1010
#include "objint.h"
1111
#include "runtime0.h"
12+
#include "runtime.h"
1213

1314
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
1415

@@ -57,6 +58,13 @@ mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
5758
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
5859
rhs_val = ((mp_obj_int_t*)rhs_in)->val;
5960
} else {
61+
if (op == MP_BINARY_OP_MULTIPLY) {
62+
if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) {
63+
// multiply is commutative for these types, so delegate to them
64+
return mp_binary_op(op, rhs_in, lhs_in);
65+
}
66+
}
67+
// unsupported operation/type
6068
return MP_OBJ_NULL;
6169
}
6270

py/objint_mpz.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "mpz.h"
1212
#include "objint.h"
1313
#include "runtime0.h"
14+
#include "runtime.h"
1415

1516
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
1617

@@ -74,7 +75,13 @@ mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
7475
return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
7576
#endif
7677
} else {
77-
// unsupported type
78+
if (op == MP_BINARY_OP_MULTIPLY) {
79+
if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) {
80+
// multiply is commutative for these types, so delegate to them
81+
return mp_binary_op(op, rhs_in, lhs_in);
82+
}
83+
}
84+
// unsupported operation/type
7885
return MP_OBJ_NULL;
7986
}
8087

0 commit comments

Comments
 (0)