From 47e6c52f0c2bf058c5d099dd2993192e0978e172 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 14 May 2021 22:05:03 +1000 Subject: [PATCH] tests/cpydiff: Add test and workaround for function.__module__ attr. MicroPython does not store any reference from a function object to the module it was defined in, but there is a way to use function.__globals__ to indirectly get the module. See issue #7259. Signed-off-by: Damien George --- tests/cpydiff/core_function_moduleattr.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/cpydiff/core_function_moduleattr.py diff --git a/tests/cpydiff/core_function_moduleattr.py b/tests/cpydiff/core_function_moduleattr.py new file mode 100644 index 0000000000..71747c1f40 --- /dev/null +++ b/tests/cpydiff/core_function_moduleattr.py @@ -0,0 +1,13 @@ +""" +categories: Core,Functions +description: Function objects do not have the ``__module__`` attribute +cause: MicroPython is optimized for reduced code size and RAM usage. +workaround: Use ``sys.modules[function.__globals__['__name__']]`` for non-builtin modules. +""" + + +def f(): + pass + + +print(f.__module__)