diff options
author | Michael Bentley <mikebentley15@gmail.com> | 2021-10-03 00:05:27 -0600 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2022-06-24 23:46:59 +1000 |
commit | d68532558d0f60737ccd5a38ae33f719333520a4 (patch) | |
tree | c66fd2830c2750c745d061b86c40c5cfa6fa91b4 | |
parent | 432b65f17849fdc94a96df4fd7db490257bd0a2c (diff) | |
download | micropython-d68532558d0f60737ccd5a38ae33f719333520a4.tar.gz micropython-d68532558d0f60737ccd5a38ae33f719333520a4.zip |
py/objclosure: Forward function attributes for closures.
Add .attr attribute which forwards to self->fun.
A closure is intended to wrap around a function object, so forward any
requested attributes to the wrapped function object.
Signed-off-by: Michael Bentley <mikebentley15@gmail.com>
-rw-r--r-- | py/objclosure.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/py/objclosure.c b/py/objclosure.c index 9dc3e54532..5b9923a44b 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -78,6 +78,14 @@ STATIC void closure_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_ } #endif +#if MICROPY_PY_FUNCTION_ATTRS +STATIC void mp_obj_closure_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { + // forward to self_in->fun + mp_obj_closure_t *o = MP_OBJ_TO_PTR(self_in); + mp_load_method_maybe(o->fun, attr, dest); +} +#endif + const mp_obj_type_t mp_type_closure = { { &mp_type_type }, .flags = MP_TYPE_FLAG_BINDS_SELF, @@ -86,6 +94,9 @@ const mp_obj_type_t mp_type_closure = { .print = closure_print, #endif .call = closure_call, + #if MICROPY_PY_FUNCTION_ATTRS + .attr = mp_obj_closure_attr, + #endif }; mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed_over, const mp_obj_t *closed) { |