summaryrefslogtreecommitdiff
path: root/tests/extmod/marshal_basic.py
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2025-01-20 22:24:10 +1100
committerDamien George <damien@micropython.org>2025-02-11 16:54:20 +1100
commitc3a18d74ebebe1c68955c3dce3c782af949aa4c7 (patch)
tree1e76917c9e98b1ee1b4196e4533cb429e5ec8d94 /tests/extmod/marshal_basic.py
parenta11ba7775e600b45c0e93443ca05dffb09a49389 (diff)
extmod/modmarshal: Add new marshal module.
This commit implements a small subset of the CPython `marshal` module. It implements `marshal.dumps()` and `marshal.loads()`, but only supports (un)marshalling code objects at this stage. The semantics match CPython, except that the actual marshalled bytes is not compatible with CPython's marshalled bytes. The module is enabled at the everything level (only on the unix coverage build at this stage). Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'tests/extmod/marshal_basic.py')
-rw-r--r--tests/extmod/marshal_basic.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/extmod/marshal_basic.py b/tests/extmod/marshal_basic.py
new file mode 100644
index 000000000..9e7b70be4
--- /dev/null
+++ b/tests/extmod/marshal_basic.py
@@ -0,0 +1,38 @@
+# Test the marshal module, basic functionality.
+
+try:
+ import marshal
+
+ (lambda: 0).__code__
+except (AttributeError, ImportError):
+ print("SKIP")
+ raise SystemExit
+
+ftype = type(lambda: 0)
+
+# Test basic dumps and loads.
+print(ftype(marshal.loads(marshal.dumps((lambda: a).__code__)), {"a": 4})())
+
+# Test dumps of a result from compile().
+ftype(marshal.loads(marshal.dumps(compile("print(a)", "", "exec"))), {"print": print, "a": 5})()
+
+# Test marshalling a function with arguments.
+print(ftype(marshal.loads(marshal.dumps((lambda x, y: x + y).__code__)), {})(1, 2))
+
+# Test marshalling a function with default arguments.
+print(ftype(marshal.loads(marshal.dumps((lambda x=0: x).__code__)), {})("arg"))
+
+# Test marshalling a function containing constant objects (a tuple).
+print(ftype(marshal.loads(marshal.dumps((lambda: (None, ...)).__code__)), {})())
+
+# Test instantiating multiple code's with different globals dicts.
+code = marshal.loads(marshal.dumps((lambda: a).__code__))
+f1 = ftype(code, {"a": 1})
+f2 = ftype(code, {"a": 2})
+print(f1(), f2())
+
+# Test unmarshallable object.
+try:
+ marshal.dumps(type)
+except ValueError:
+ print("ValueError")