8000 py/objtype: Add __dict__ attribute for class objects. · micropython/micropython@28370c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28370c0

Browse files
pi-anldpgeorge
authored andcommitted
py/objtype: Add __dict__ attribute for class objects.
The behavior mirrors the instance object dict attribute where a copy of the local attributes are provided (unless the dict is read-only, then that dict itself is returned, as an optimisation). MicroPython does not support modifying this dict because the changes will not be reflected in the class. The feature is only enabled if MICROPY_CPYTHON_COMPAT is set, the same as the instance version.
1 parent 29e2586 commit 28370c0

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ size_t mp_obj_dict_len(mp_obj_t self_in);
895895
mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
896896
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
897897
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
898+
mp_obj_t mp_obj_dict_copy(mp_obj_t self_in);
898899
static inline mp_map_t *mp_obj_dict_get_map(mp_obj_t dict) {
899900
return &((mp_obj_dict_t *)MP_OBJ_TO_PTR(dict))->map;
900901
}

py/objdict.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
227227
}
228228
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
229229

230-
STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
230+
mp_obj_t mp_obj_dict_copy(mp_obj_t self_in) {
231231
mp_check_self(mp_obj_is_dict_type(self_in));
232232
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
233233
mp_obj_t other_out = mp_obj_new_dict(self->map.alloc);
@@ -240,7 +240,7 @@ STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
240240
memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
241241
return other_out;
242242
}
243-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
243+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, mp_obj_dict_copy);
244244

245245
#if MICROPY_PY_BUILTINS_DICT_FROMKEYS
246246
// this is a classmethod

py/objtype.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,21 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
10131013
dest[0] = MP_OBJ_NEW_QSTR(self->name);
10141014
return;
10151015
}
1016+
#if MICROPY_CPYTHON_COMPAT
1017+
if (attr == MP_QSTR___dict__) {
1018+
// Returns a read-only dict of the class attributes.
1019+
// If the internal locals is not fixed, a copy will be created.
1020+
mp_obj_dict_t *dict = self->locals_dict;
1021+
if (dict->map.is_fixed) {
1022+
dest[0] = MP_OBJ_FROM_PTR(dict);
1023+
} else {
1024+
dest[0] = mp_obj_dict_copy(MP_OBJ_FROM_PTR(dict));
1025+
dict = MP_OBJ_TO_PTR(dest[0]);
1026+
dict->map.is_fixed = 1;
1027+
}
1028+
return;
1029+
}
1030+
#endif
10161031
if (attr == MP_QSTR___bases__) {
10171032
if (self == &mp_type_object) {
10181033
dest[0] = mp_const_empty_tuple;

tests/basics/class_dict.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# test __dict__ attribute of a class
2+
3+
if not hasattr(int, "__dict__"):
4+
print("SKIP")
5+
raise SystemExit
6+
7+
8+
# dict of a built-in type
9+
print("from_bytes" in int.__dict__)
10+
11+
12+
# dict of a user class
13+
class Foo:
14+
a = 1
15+
b = "bar"
16+
17+
18+
d = Foo.__dict__
19+
print(d["a"], d["b"])

0 commit comments

Comments
 (0)
0