summaryrefslogtreecommitdiff
path: root/tests/basics/fun_code_micropython.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/basics/fun_code_micropython.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/basics/fun_code_micropython.py')
-rw-r--r--tests/basics/fun_code_micropython.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/basics/fun_code_micropython.py b/tests/basics/fun_code_micropython.py
new file mode 100644
index 000000000..2c319a2db
--- /dev/null
+++ b/tests/basics/fun_code_micropython.py
@@ -0,0 +1,19 @@
+# Test MicroPython-specific restrictions of function.__code__ attribute.
+
+try:
+ (lambda: 0).__code__
+except AttributeError:
+ print("SKIP")
+ raise SystemExit
+
+
+def f_with_children():
+ def g():
+ pass
+
+
+# Can't access __code__ when function has children.
+try:
+ f_with_children.__code__
+except AttributeError:
+ print("AttributeError")