diff options
author | Damien George <damien.p.george@gmail.com> | 2018-05-10 23:10:46 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-05-10 23:14:23 +1000 |
commit | 3678a6bdc6a925f2bce6423c41f911229836f946 (patch) | |
tree | 954bd10aefdaf95a238a7a8b6d11cf46dfee542d /py/modbuiltins.c | |
parent | 29d28c2574d6c0b93fd3fc869b389a61dee12102 (diff) |
py/modbuiltins: Make built-in dir support the __dir__ special method.
If MICROPY_PY_ALL_SPECIAL_METHODS is enabled then dir() will now delegate
to the special method __dir__ if the object it is listing has this method.
Diffstat (limited to 'py/modbuiltins.c')
-rw-r--r-- | py/modbuiltins.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/py/modbuiltins.c b/py/modbuiltins.c index bc99f8261..0d511338b 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -190,6 +190,13 @@ STATIC mp_obj_t mp_builtin_dir(size_t n_args, const mp_obj_t *args) { mp_obj_t dest[2]; mp_load_method_protected(args[0], i, dest, false); if (dest[0] != MP_OBJ_NULL) { + #if MICROPY_PY_ALL_SPECIAL_METHODS + // Support for __dir__: see if we can dispatch to this special method + // This relies on MP_QSTR__dir__ being first after MP_QSTR_ + if (i == MP_QSTR___dir__ && dest[1] != MP_OBJ_NULL) { + return mp_call_method_n_kw(0, 0, dest); + } + #endif mp_obj_list_append(dir, MP_OBJ_NEW_QSTR(i)); } } |