summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2021-05-14 22:05:03 +1000
committerDamien George <damien@micropython.org>2021-05-16 11:06:46 +1000
commit47e6c52f0c2bf058c5d099dd2993192e0978e172 (patch)
treec2399be1df2dbd0c57fbc707dcfb0c96c0529f2b
parent605b74f390e1ce9acdbca32d0b3215d37b96852e (diff)
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 <damien@micropython.org>
-rw-r--r--tests/cpydiff/core_function_moduleattr.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/tests/cpydiff/core_function_moduleattr.py b/tests/cpydiff/core_function_moduleattr.py
new file mode 100644
index 000000000..71747c1f4
--- /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__)