diff --git a/py/objtype.c b/py/objtype.c index c20b0693e522f..93c45203bfacb 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -828,6 +828,14 @@ 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 (attr == MP_QSTR___dict__) { + if (self->locals_dict == NULL) { + dest[0] = mp_obj_new_dict(0); + } else { + dest[0] = self->locals_dict; + } + return; + } #endif struct class_lookup_data lookup = { .obj = (mp_obj_instance_t*)self, diff --git a/tests/basics/class_dict.py b/tests/basics/class_dict.py new file mode 100644 index 0000000000000..f80f45d87c53e --- /dev/null +++ b/tests/basics/class_dict.py @@ -0,0 +1,14 @@ +import sys + +class Foo: + a = 1 + def b(): + pass + +if not hasattr(Foo, "__dict__"): + print("SKIP") + sys.exit() + +print(Foo.__dict__ == {'a': 1, 'b': Foo.b, '__qualname__': 'Foo', '__module__': '__main__'}) +# Also test C implemented class without `locals_attr` +print(type.__dict__ == {}) diff --git a/tests/basics/class_dict.py.exp b/tests/basics/class_dict.py.exp new file mode 100644 index 0000000000000..dbde422651c9a --- /dev/null +++ b/tests/basics/class_dict.py.exp @@ -0,0 +1,2 @@ +True +True 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