summaryrefslogtreecommitdiff
path: root/tests/micropython/native_fun_attrs_code.py
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2025-01-20 22:23:48 +1100
committerDamien George <damien@micropython.org>2025-02-11 16:51:50 +1100
commitceb8ba60b4fea0c32e4977d0e45d5c0203b27b34 (patch)
tree9c100e265c53509822ecc08155545701a370db6f /tests/micropython/native_fun_attrs_code.py
parent62e821ccb82fd8362a8198ad59ccb51b8a5c441e (diff)
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 <damien@micropython.org>
Diffstat (limited to 'tests/micropython/native_fun_attrs_code.py')
-rw-r--r--tests/micropython/native_fun_attrs_code.py21
1 files changed, 21 insertions, 0 deletions
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")