From e22b7fb4afbf62a9ca3162539c2d0236aefd8945 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 25 Jun 2022 00:05:39 +1000 Subject: [PATCH] py/objfun: Support function attributes on native functions. Native functions can just reuse the bytecode function attribute code. Signed-off-by: Damien George --- py/objfun.c | 6 ++++++ tests/micropython/native_fun_attrs.py | 25 +++++++++++++++++++++++ tests/micropython/native_fun_attrs.py.exp | 4 ++++ tests/run-tests.py | 1 + 4 files changed, 36 insertions(+) create mode 100644 tests/micropython/native_fun_attrs.py create mode 100644 tests/micropython/native_fun_attrs.py.exp diff --git a/py/objfun.c b/py/objfun.c index 8f0c3eb6d4..37c1eaa549 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -421,8 +421,14 @@ STATIC const mp_obj_type_t mp_type_fun_native = { { &mp_type_type }, .flags = MP_TYPE_FLAG_BINDS_SELF, .name = MP_QSTR_function, + #if MICROPY_CPYTHON_COMPAT + .print = fun_bc_print, + #endif .call = fun_native_call, .unary_op = mp_generic_unary_op, + #if MICROPY_PY_FUNCTION_ATTRS + .attr = mp_obj_fun_bc_attr, + #endif }; mp_obj_t mp_obj_new_fun_native(const mp_obj_t *def_args, const void *fun_data, const mp_module_context_t *mc, struct _mp_raw_code_t *const *child_table) { diff --git a/tests/micropython/native_fun_attrs.py b/tests/micropython/native_fun_attrs.py new file mode 100644 index 0000000000..e61c869755 --- /dev/null +++ b/tests/micropython/native_fun_attrs.py @@ -0,0 +1,25 @@ +# test native function attributes + + +def f(): + pass + + +if not hasattr(f, "__name__"): + print("SKIP") + raise SystemExit + + +@micropython.native +def native_f(): + pass + + +print(type(native_f.__name__)) +print(type(native_f.__globals__)) +print(native_f.__globals__ is globals()) + +try: + native_f.__name__ = None +except AttributeError: + print("AttributeError") diff --git a/tests/micropython/native_fun_attrs.py.exp b/tests/micropython/native_fun_attrs.py.exp new file mode 100644 index 0000000000..8be35e2f69 --- /dev/null +++ b/tests/micropython/native_fun_attrs.py.exp @@ -0,0 +1,4 @@ + + +True +AttributeError diff --git a/tests/run-tests.py b/tests/run-tests.py index 7377e8ae2c..2d6dbaaf1c 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -603,6 +603,7 @@ def run_tests(pyb, tests, args, result_dir, num_threads=1): skip_tests.add("basics/del_deref.py") # requires checking for unbound local skip_tests.add("basics/del_local.py") # requires checking for unbound local skip_tests.add("basics/exception_chain.py") # raise from is not supported + skip_tests.add("basics/fun_name.py") # requires proper names for native functions skip_tests.add("basics/scope_implicit.py") # requires checking for unbound local skip_tests.add("basics/sys_tracebacklimit.py") # requires traceback info skip_tests.add("basics/try_finally_return2.py") # requires raise_varargs