Skip to content

py/objtype: Add __dict__ attribute for class objects. #5324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions py/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ size_t mp_obj_dict_len(mp_obj_t self_in);
mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
mp_obj_t mp_obj_dict_copy(mp_obj_t self_in);
static inline mp_map_t *mp_obj_dict_get_map(mp_obj_t dict) {
return &((mp_obj_dict_t *)MP_OBJ_TO_PTR(dict))->map;
}
Expand Down
4 changes: 2 additions & 2 deletions py/objdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);

STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
mp_obj_t mp_obj_dict_copy(mp_obj_t self_in) {
mp_check_self(mp_obj_is_dict_type(self_in));
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_t other_out = mp_obj_new_dict(self->map.alloc);
Expand All @@ -240,7 +240,7 @@ STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
return other_out;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, mp_obj_dict_copy);

#if MICROPY_PY_BUILTINS_DICT_FROMKEYS
// this is a classmethod
Expand Down
32 changes: 23 additions & 9 deletions py/objtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,17 +587,16 @@ STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des
}
#if MICROPY_CPYTHON_COMPAT
if (attr == MP_QSTR___dict__) {
// Create a new dict with a copy of the instance's map items.
// Returns a read-only dict of the instance members.
// If the internal locals is not fixed, a copy will be created.
// This creates, unlike CPython, a 'read-only' __dict__: modifying
// it will not result in modifications to the actual instance members.
mp_map_t *map = &self->members;
mp_obj_t attr_dict = mp_obj_new_dict(map->used);
for (size_t i = 0; i < map->alloc; ++i) {
if (mp_map_slot_is_filled(map, i)) {
mp_obj_dict_store(attr_dict, map->table[i].key, map->table[i].value);
}
}
dest[0] = attr_dict;
mp_obj_dict_t dict;
dict.map = self->members;
dict.base.type = &mp_type_dict;
dest[0] = mp_obj_dict_copy(MP_OBJ_FROM_PTR(&dict));
mp_obj_dict_t *dest_dict = MP_OBJ_TO_PTR(dest[0]);
dest_dict->map.is_fixed = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please put these changes to the instance __dict__ lookup into a separate commit?

@stinos as the original author of this code, what are your thoughts on this change? In particular do you think it's a good idea to set is_fixed=1 to prevent modification of the returned dict (and so prevent subtle errors when the user tries to change the dict and expects those changes to be mirrored in the instance, which they are not)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be my preference to reduce chances of things being misused yes. And looking at the original discussion in #1757, I actually wanted it to be like that but that wasn't possible back then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so let's keep the is_fixed=1 part, to make the returned dict R/O. It will be a breaking change though, if users are relying on the dict being modifiable (eg because they know it's not reflected in the instance but they want anyway to reuse the dict for something else). But it'll fail pretty quickly now if someone is doing this and they can fix their code by simply doing d = dict(my_type.__dict__)

return;
}
#endif
Expand Down Expand Up @@ -1013,6 +1012,21 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
dest[0] = MP_OBJ_NEW_QSTR(self->name);
return;
}
#if MICROPY_CPYTHON_COMPAT
if (attr == MP_QSTR___dict__) {
// Returns a read-only dict of the class attributes.
// If the internal locals is not fixed, a copy will be created.
mp_obj_dict_t *dict = self->locals_dict;
if (dict->map.is_fixed) {
dest[0] = MP_OBJ_FROM_PTR(dict);
} else {
dest[0] = mp_obj_dict_copy(MP_OBJ_FROM_PTR(dict));
dict = MP_OBJ_TO_PTR(dest[0]);
dict->map.is_fixed = 1;
}
return;
}
#endif
if (attr == MP_QSTR___bases__) {
if (self == &mp_type_object) {
dest[0] = mp_const_empty_tuple;
Expand Down
10 changes: 10 additions & 0 deletions tests/basics/class_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

class Foo:
a = 1
b = "bar"

if not hasattr(Foo, "__dict__"):
print("SKIP")
raise SystemExit

print(Foo.__dict__ == {'a': 1, 'b': 'bar'})
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