Skip to content

Commit 03b37bd

Browse files
committed
py/objtype: Added alternate no-mutate __set_name__ for descriptor use.
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
1 parent 4ebde41 commit 03b37bd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

py/objtype.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,23 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
13081308

13091309
#if MICROPY_PY_METACLASSES_LITE
13101310
setname_consume_call_all(setname_list, MP_OBJ_FROM_PTR(o));
1311+
#elif MICROPY_PY_DESCRIPTORS
1312+
locals_map->is_fixed = 1; // just block modify-while-write of __set_name__
1313+
// call __set_name__ on all entries (especially descriptors)
1314+
for (size_t i = 0; i < locals_map->alloc; i++) {
1315+
if (mp_map_slot_is_filled(locals_map, i)) {
1316+
elem = &(locals_map->table[i]);
1317+
1318+
mp_obj_t set_name_method[4];
1319+
mp_load_method_maybe(elem->value, MP_QSTR___set_name__, set_name_method);
1320+
if (set_name_method[1] != MP_OBJ_NULL) {
1321+
set_name_method[2] = MP_OBJ_FROM_PTR(o);
1322+
set_name_method[3] = elem->key;
1323+
mp_call_method_n_kw(2, 0, set_name_method);
1324+
}
1325+
}
1326+
}
1327+
locals_map->is_fixed = 0; // would be finalizer, but class isn't returned anyway
13111328
#endif
13121329

13131330
return MP_OBJ_FROM_PTR(o);

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