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>
This commit is contained in:
Damien George
2025-01-20 22:24:10 +11:00
parent a11ba7775e
commit c3a18d74eb
9 changed files with 283 additions and 6 deletions

View File

@@ -0,0 +1,21 @@
# Test the marshal module, MicroPython-specific functionality.
try:
import marshal
except ImportError:
print("SKIP")
raise SystemExit
import unittest
class Test(unittest.TestCase):
def test_function_with_children(self):
# Can't marshal a function with children (in this case the module has a child function f).
code = compile("def f(): pass", "", "exec")
with self.assertRaises(ValueError):
marshal.dumps(code)
if __name__ == "__main__":
unittest.main()