Skip to content

Commit 9344e87

Browse files
committed
py/objtype: Allow mp_instance_cast_to_native_base to take native obj.
And rename it to mp_obj_cast_to_native_base() to indicate this. This allows users of this function to easily support native and native-subclass objects in the same way (by just passing the object through this function).
1 parent d3b2c6e commit 9344e87

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

py/obj.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items);
728728
const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
729729
const char *mp_obj_get_type_str(mp_const_obj_t o_in);
730730
bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
731-
mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type);
731+
mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type);
732732

733733
void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
734734
void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);

py/objtuple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ STATIC mp_obj_t tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t anothe
109109
mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
110110
if (another_type->getiter != mp_obj_tuple_getiter) {
111111
// Slow path for user subclasses
112-
another_in = mp_instance_cast_to_native_base(another_in, MP_OBJ_FROM_PTR(&mp_type_tuple));
112+
another_in = mp_obj_cast_to_native_base(another_in, MP_OBJ_FROM_PTR(&mp_type_tuple));
113113
if (another_in == MP_OBJ_NULL) {
114114
return MP_OBJ_NULL;
115115
}

py/objtype.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,13 +1389,17 @@ STATIC mp_obj_t mp_builtin_isinstance(mp_obj_t object, mp_obj_t classinfo) {
13891389

13901390
MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_isinstance_obj, mp_builtin_isinstance);
13911391

1392-
mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type) {
1392+
mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type) {
13931393
const mp_obj_type_t *self_type = mp_obj_get_type(self_in);
1394-
if (!mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(self_type), native_type)) {
1394+
1395+
if (MP_OBJ_FROM_PTR(self_type) == native_type) {
1396+
return self_in;
1397+
} else if (!mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(self_type), native_type)) {
13951398
return MP_OBJ_NULL;
1399+
} else {
1400+
mp_obj_instance_t *self = (mp_obj_instance_t*)MP_OBJ_TO_PTR(self_in);
1401+
return self->subobj[0];
13961402
}
1397-
mp_obj_instance_t *self = (mp_obj_instance_t*)MP_OBJ_TO_PTR(self_in);
1398-
return self->subobj[0];
13991403
}
14001404

14011405
/******************************************************************************/

0 commit comments

Comments
 (0)