Skip to content

Commit 43ed928

Browse files
committed
py/objtype: Implement __set_name__.
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
1 parent 0fb11d3 commit 43ed928

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

py/objtype.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,8 @@ static void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des
661661
// try __getattr__
662662
if (attr != MP_QSTR___getattr__) {
663663
#if MICROPY_PY_DESCRIPTORS
664-
// With descriptors enabled, don't delegate lookups of __get__/__set__/__delete__.
665-
if (attr == MP_QSTR___get__ || attr == MP_QSTR___set__ || attr == MP_QSTR___delete__) {
664+
// With descriptors enabled, don't delegate lookups of __get__/__set__/__delete__/__set_name__.
665+
if (attr == MP_QSTR___get__ || attr == MP_QSTR___set__ || attr == MP_QSTR___delete__ || attr == MP_QSTR___set_name__) {
666666
return;
667667
}
668668
#endif
@@ -960,7 +960,7 @@ static bool check_for_special_accessors(mp_obj_t key, mp_obj_t value) {
960960
#endif
961961
#if MICROPY_PY_DESCRIPTORS
962962
static const uint8_t to_check[] = {
963-
MP_QSTR___get__, MP_QSTR___set__, MP_QSTR___delete__,
963+
MP_QSTR___get__, MP_QSTR___set__, MP_QSTR___delete__, // not needed for MP_QSTR___set_name__ tho
964964
};
965965
for (size_t i = 0; i < MP_ARRAY_SIZE(to_check); ++i) {
966966
mp_obj_t dest_temp[2];
@@ -1241,6 +1241,23 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
12411241
}
12421242
}
12431243

1244+
#if MICROPY_PY_DESCRIPTORS
1245+
// call __set_name__ on all entries (especially descriptors)
1246+
for (size_t i = 0; i < locals_map->alloc; i++) {
1247+
if (mp_map_slot_is_filled(locals_map, i)) {
1248+
elem = &(locals_map->table[i]);
1249+
1250+
mp_obj_t set_name_method[4];
1251+
mp_load_method_maybe(elem->value, MP_QSTR___set_name__, set_name_method);
1252+
if (set_name_method[1] != MP_OBJ_NULL) {
1253+
set_name_method[2] = MP_OBJ_FROM_PTR(o);
1254+
set_name_method[3] = elem->key;
1255+
mp_call_method_n_kw(2, 0, set_name_method);
1256+
}
1257+
}
1258+
}
1259+
#endif
1260+
12441261
return MP_OBJ_FROM_PTR(o);
12451262
}
12461263

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy