Skip to content

Commit be6d8be

Browse files
committed
py: Rename mp_obj_int_get to mp_obj_int_get_truncated; fix struct.pack.
mp_obj_int_get_truncated is used as a "fast path" int accessor that doesn't check for overflow and returns the int truncated to the machine word size, ie mp_int_t. Use mp_obj_int_get_truncated to fix struct.pack when packing maximum word sized values. Addresses issues adafruit#779 and adafruit#998.
1 parent 451a087 commit be6d8be

13 files changed

Lines changed: 37 additions & 20 deletions

File tree

extmod/moductypes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_addressof_obj, uctypes_struct_addressof
571571
/// captured by reference (and thus memory pointed by bytearray may change
572572
/// or become invalid at later time). Use bytes_at() to capture by value.
573573
mp_obj_t uctypes_struct_bytearray_at(mp_obj_t ptr, mp_obj_t size) {
574-
return mp_obj_new_bytearray_by_ref(mp_obj_int_get(size), (void*)mp_obj_int_get(ptr));
574+
return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void*)mp_obj_int_get_truncated(ptr));
575575
}
576576
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytearray_at);
577577

@@ -580,7 +580,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytear
580580
/// captured by value, i.e. copied. Use bytearray_at() to capture by reference
581581
/// ("zero copy").
582582
mp_obj_t uctypes_struct_bytes_at(mp_obj_t ptr, mp_obj_t size) {
583-
return mp_obj_new_bytes((void*)mp_obj_int_get(ptr), mp_obj_int_get(size));
583+
return mp_obj_new_bytes((void*)mp_obj_int_get_truncated(ptr), mp_obj_int_get_truncated(size));
584584
}
585585
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytes_at_obj, uctypes_struct_bytes_at);
586586

extmod/modure.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ STATIC void match_print(void (*print)(void *env, const char *fmt, ...), void *en
6262

6363
STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) {
6464
mp_obj_match_t *self = self_in;
65-
mp_int_t no = mp_obj_int_get(no_in);
65+
mp_int_t no = mp_obj_int_get_truncated(no_in);
6666
if (no < 0 || no >= self->num_matches / 2) {
6767
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, no_in));
6868
}
@@ -130,7 +130,7 @@ STATIC mp_obj_t re_split(uint n_args, const mp_obj_t *args) {
130130

131131
int maxsplit = 0;
132132
if (n_args > 2) {
133-
maxsplit = mp_obj_int_get(args[2]);
133+
maxsplit = mp_obj_int_get_truncated(args[2]);
134134
}
135135

136136
mp_obj_t retval = mp_obj_new_list(0, NULL);

py/binary.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,12 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
252252
val = (mp_uint_t)val_in;
253253
break;
254254
default:
255-
val = mp_obj_get_int(val_in);
255+
// we handle large ints here by calling the truncated accessor
256+
if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
257+
val = mp_obj_int_get_truncated(val_in);
258+
} else {
259+
val = mp_obj_get_int(val_in);
260+
}
256261
}
257262

258263
mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, val);

py/obj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ mp_int_t mp_obj_hash(mp_obj_t o_in) {
179179
if (hash_method[0] != MP_OBJ_NULL) {
180180
mp_obj_t hash_val = mp_call_method_n_kw(0, 0, hash_method);
181181
if (MP_OBJ_IS_INT(hash_val)) {
182-
return mp_obj_int_get(hash_val);
182+
return mp_obj_int_get_truncated(hash_val);
183183
}
184184
}
185185
}

py/obj.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,12 +460,12 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
460460

461461
// int
462462
// For long int, returns value truncated to mp_int_t
463-
mp_int_t mp_obj_int_get(mp_const_obj_t self_in);
463+
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
464+
// Will raise exception if value doesn't fit into mp_int_t
465+
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
464466
#if MICROPY_PY_BUILTINS_FLOAT
465467
mp_float_t mp_obj_int_as_float(mp_obj_t self_in);
466468
#endif
467-
// Will raise exception if value doesn't fit into mp_int_t
468-
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
469469

470470
// exception
471471
#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)

py/objint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ mp_obj_t mp_obj_new_int(mp_int_t value) {
270270
return mp_const_none;
271271
}
272272

273-
mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
273+
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
274274
return MP_OBJ_SMALL_INT_VALUE(self_in);
275275
}
276276

py/objint_longlong.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg,
198198
return o;
199199
}
200200

201-
mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
201+
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
202202
if (MP_OBJ_IS_SMALL_INT(self_in)) {
203203
return MP_OBJ_SMALL_INT_VALUE(self_in);
204204
} else {
@@ -209,7 +209,7 @@ mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
209209

210210
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
211211
// TODO: Check overflow
212-
return mp_obj_int_get(self_in);
212+
return mp_obj_int_get_truncated(self_in);
213213
}
214214

215215
#if MICROPY_PY_BUILTINS_FLOAT

py/objint_mpz.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,12 @@ mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg,
305305
return o;
306306
}
307307

308-
mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
308+
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
309309
if (MP_OBJ_IS_SMALL_INT(self_in)) {
310310
return MP_OBJ_SMALL_INT_VALUE(self_in);
311311
} else {
312312
const mp_obj_int_t *self = self_in;
313-
// TODO this is a hack until we remove mp_obj_int_get function entirely
313+
// hash returns actual int value if it fits in mp_int_t
314314
return mpz_hash(&self->mpz);
315315
}
316316
}

py/stream.c

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

stmhal/modusocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) {
288288
const void *optval;
289289
mp_uint_t optlen;
290290
if (mp_obj_is_integer(args[3])) {
291-
int val = mp_obj_int_get(args[3]);
291+
mp_int_t val = mp_obj_int_get_truncated(args[3]);
292292
optval = &val;
293293
optlen = sizeof(val);
294294
} else {

0 commit comments

Comments
 (0)