10000 py/objfun: Support function attributes on native functions. · DucRP/micropython@e22b7fb · GitHub
[go: up one dir, main page]

Skip to content

Commit e22b7fb

Browse files
committed
py/objfun: Support function attributes on native functions.
Native functions can just reuse the bytecode function attribute code. Signed-off-by: Damien George <damien@micropython.org>
1 parent 268ec1e commit e22b7fb

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

py/objfun.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,14 @@ STATIC const mp_obj_type_t mp_type_fun_native = {
421421
{ &mp_type_type },
422422
.flags = MP_TYPE_FLAG_BINDS_SELF,
423423
.name = MP_QSTR_function,
424+
#if MICROPY_CPYTHON_COMPAT
425+
.print = fun_bc_print,
426+
#endif
424427
.call = fun_native_call,
425428
.unary_op = mp_generic_unary_op,
429+
#if MICROPY_PY_FUNCTION_ATTRS
430+
.attr = mp_obj_fun_bc_attr,
431+
#endif
426432
};
427433

428434
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) {

tests/micropython/native_fun_attrs.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# test native function attributes
2+
3+
4+
def f():
5+
pass
6+
7+
8+
if not hasattr(f, "__name__"):
9+
print("SKIP")
10+
raise SystemExit
11+
12+
13+
@micropython.native
14+
def native_f():
15+
pass
16+
17+
18+
print(type(native_f.__name__))
19+
print(type(native_f.__globals__))
20+
print(native_f.__globals__ is globals())
21+
22+
try:
23+
native_f.__name__ = None
24+
except AttributeError:
25+
print("AttributeError")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<class 'str'>
2+
<class 'dict'>
3+
True
4+
AttributeError

tests/run-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ def run_tests(pyb, tests, args, result_dir, num_threads=1):
603603
skip_tests.add("basics/del_deref.py") # requires checking for unbound local
604604
skip_tests.add("basics/del_local.py") # requires checking for unbound local
605605
skip_tests.add("basics/exception_chain.py") # raise from is not supported
606+
skip_tests.add("basics/fun_name.py") # requires proper names for native functions
606607
skip_tests.add("basics/scope_implicit.py") # requires checking for unbound local
607608
skip_tests.add("basics/sys_tracebacklimit.py") # requires traceback info
608609
skip_tests.add("basics/try_finally_return2.py") # requires raise_varargs

0 commit comments

Comments
 (0)
0