Files
micropython/tests/basics/fun_code.py
Damien George 878f8004fd py/objfun: Support __code__ on functions with children.
If a function has children then the code object returned from __code__ must
contain an `mp_raw_code_t` (actually `mp_raw_code_truncated_t` is enough)
that points to the child table.

Signed-off-by: Damien George <damien@micropython.org>
2026-01-27 01:20:36 +11:00

42 lines
801 B
Python

# Test function.__code__ attribute.
try:
(lambda: 0).__code__
except AttributeError:
print("SKIP")
raise SystemExit
def f():
return a
ftype = type(f)
# Test __code__ access and function constructor.
code = f.__code__
print(type(ftype(code, {})) is ftype)
# Test instantiating multiple code's with different globals dicts.
code = f.__code__
f1 = ftype(code, {"a": 1})
f2 = ftype(code, {"a": 2})
print(f1(), f2())
# Test bad first argument type.
try:
ftype(None, {})
except TypeError:
print("TypeError")
# Test bad second argument type.
try:
ftype(f.__code__, None)
except TypeError:
print("TypeError")
# Test __code__ on functions with children functions.
code = (lambda: (lambda: a)).__code__
print(ftype(code, {"a": 1})()())
print(ftype(code, {"a": 2})()())