Skip to content

Commit c50772d

Browse files
committed
py: Add mp_obj_get_int_truncated and use it where appropriate.
mp_obj_get_int_truncated will raise a TypeError if the argument is not an integral type. Use mp_obj_int_get_truncated only when you know the argument is a small or big int.
1 parent c2a4e4e commit c50772d

File tree

7 files changed

+15
-6
lines changed

7 files changed

+15
-6
lines changed

cc3200/mods/modusocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) {
285285
mp_uint_t optlen;
286286
mp_int_t val;
287287
if (mp_obj_is_integer(args[3])) {
288-
val = mp_obj_int_get_truncated(args[3]);
288+
val = mp_obj_get_int_truncated(args[3]);
289289
optval = &val;
290290
optlen = sizeof(val);
291291
} else {

extmod/modure.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ STATIC void match_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind
5959

6060
STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) {
6161
mp_obj_match_t *self = self_in;
62-
mp_int_t no = mp_obj_int_get_truncated(no_in);
62+
mp_int_t no = mp_obj_get_int(no_in);
6363
if (no < 0 || no >= self->num_matches) {
6464
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, no_in));
6565
}
@@ -135,7 +135,7 @@ STATIC mp_obj_t re_split(uint n_args, const mp_obj_t *args) {
135135

136136
int maxsplit = 0;
137137
if (n_args > 2) {
138-
maxsplit = mp_obj_int_get_truncated(args[2]);
138+
maxsplit = mp_obj_get_int(args[2]);
139139
}
140140

141141
mp_obj_t retval = mp_obj_new_list(0, NULL);

py/obj.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
231231
}
232232
}
233233

234+
mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg) {
235+
if (MP_OBJ_IS_INT(arg)) {
236+
return mp_obj_int_get_truncated(arg);
237+
} else {
238+
return mp_obj_get_int(arg);
239+
}
240+
}
241+
234242
// returns false if arg is not of integral type
235243
// returns true and sets *value if it is of integral type
236244
// can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ bool mp_obj_is_callable(mp_obj_t o_in);
511511
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
512512

513513
mp_int_t mp_obj_get_int(mp_const_obj_t arg);
514+
mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
514515
bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
515516
#if MICROPY_PY_BUILTINS_FLOAT
516517
mp_float_t mp_obj_get_float(mp_obj_t self_in);

py/objtype.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ STATIC mp_obj_t instance_unary_op(mp_uint_t op, mp_obj_t self_in) {
359359
mp_obj_t val = mp_call_function_1(member[0], self_in);
360360
// __hash__ must return a small int
361361
if (op == MP_UNARY_OP_HASH) {
362-
val = MP_OBJ_NEW_SMALL_INT(mp_obj_int_get_truncated(val));
362+
val = MP_OBJ_NEW_SMALL_INT(mp_obj_get_int_truncated(val));
363363
}
364364
return val;
365365
} else {

py/stream.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ STATIC mp_obj_t stream_readinto(mp_uint_t n_args, const mp_obj_t *args) {
225225
// https://docs.python.org/3/library/socket.html#socket.socket.recv_into
226226
mp_uint_t len = bufinfo.len;
227227
if (n_args > 2) {
228-
len = mp_obj_int_get_truncated(args[2]);
228+
len = mp_obj_get_int(args[2]);
229229
if (len > bufinfo.len) {
230230
len = bufinfo.len;
231231
}

stmhal/modusocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) {
287287
mp_uint_t optlen;
288288
mp_int_t val;
289289
if (mp_obj_is_integer(args[3])) {
290-
val = mp_obj_int_get_truncated(args[3]);
290+
val = mp_obj_get_int_truncated(args[3]);
291291
optval = &val;
292292
optlen = sizeof(val);
293293
} else {

0 commit comments

Comments
 (0)