Skip to content

Commit c7687ad

Browse files
committed
py: Rename mp_builtin_id to mp_obj_id and make it public.
1 parent a2f55fe commit c7687ad

3 files changed

Lines changed: 20 additions & 19 deletions

File tree

py/builtin.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -458,25 +458,6 @@ STATIC mp_obj_t mp_builtin_sorted(mp_uint_t n_args, const mp_obj_t *args, mp_map
458458
}
459459
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
460460

461-
STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) {
462-
mp_int_t id = (mp_int_t)o_in;
463-
if (!MP_OBJ_IS_OBJ(o_in)) {
464-
return mp_obj_new_int(id);
465-
} else if (id >= 0) {
466-
// Many OSes and CPUs have affinity for putting "user" memories
467-
// into low half of address space, and "system" into upper half.
468-
// We're going to take advantage of that and return small int
469-
// (signed) for such "user" addresses.
470-
return MP_OBJ_NEW_SMALL_INT(id);
471-
} else {
472-
// If that didn't work, well, let's return long int, just as
473-
// a (big) positve value, so it will never clash with the range
474-
// of small int returned in previous case.
475-
return mp_obj_new_int_from_uint((mp_uint_t)id);
476-
}
477-
}
478-
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_builtin_id);
479-
480461
// See mp_load_attr() if making any changes
481462
STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
482463
mp_obj_t dest[2];
@@ -523,6 +504,7 @@ STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
523504
MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
524505

525506
// These are defined in terms of MicroPython API functions right away
507+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_obj_id);
526508
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len);
527509
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
528510
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);

py/obj.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,24 @@ mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index,
352352
return i;
353353
}
354354

355+
mp_obj_t mp_obj_id(mp_obj_t o_in) {
356+
mp_int_t id = (mp_int_t)o_in;
357+
if (!MP_OBJ_IS_OBJ(o_in)) {
358+
return mp_obj_new_int(id);
359+
} else if (id >= 0) {
360+
// Many OSes and CPUs have affinity for putting "user" memories
361+
// into low half of address space, and "system" into upper half.
362+
// We're going to take advantage of that and return small int
363+
// (signed) for such "user" addresses.
364+
return MP_OBJ_NEW_SMALL_INT(id);
365+
} else {
366+
// If that didn't work, well, let's return long int, just as
367+
// a (big) positve value, so it will never clash with the range
368+
// of small int returned in previous case.
369+
return mp_obj_new_int_from_uint((mp_uint_t)id);
370+
}
371+
}
372+
355373
// will raise a TypeError if object has no length
356374
mp_obj_t mp_obj_len(mp_obj_t o_in) {
357375
mp_obj_t len = mp_obj_len_maybe(o_in);

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
430430
void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items);
431431
void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items);
432432
mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice);
433+
mp_obj_t mp_obj_id(mp_obj_t o_in);
433434
mp_obj_t mp_obj_len(mp_obj_t o_in);
434435
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return MP_OBJ_NULL */
435436
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);

0 commit comments

Comments
 (0)