From ceb8ba60b4fea0c32e4977d0e45d5c0203b27b34 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 20 Jan 2025 22:23:48 +1100 Subject: py/objfun: Implement function.__code__ and function constructor. This allows retrieving the code object of a function using `function.__code__`, and then reconstructing a function from a code object using `FunctionType(code_object)`. This feature is controlled by `MICROPY_PY_FUNCTION_ATTRS_CODE` and is enabled at the full-features level. Signed-off-by: Damien George --- tests/micropython/native_fun_attrs_code.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/micropython/native_fun_attrs_code.py (limited to 'tests/micropython/native_fun_attrs_code.py') diff --git a/tests/micropython/native_fun_attrs_code.py b/tests/micropython/native_fun_attrs_code.py new file mode 100644 index 000000000..d277a7b9b --- /dev/null +++ b/tests/micropython/native_fun_attrs_code.py @@ -0,0 +1,21 @@ +# Test MicroPython-specific restrictions of function.__code__ attribute. + +try: + (lambda: 0).__code__ +except AttributeError: + print("SKIP") + raise SystemExit + +import micropython + + +@micropython.native +def f_native(): + pass + + +# Can't access __code__ when function is native code. +try: + f_native.__code__ +except AttributeError: + print("AttributeError") -- cgit v1.2.3