Skip to content

Commit c3a18d7

Browse files
committed
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>
1 parent a11ba77 commit c3a18d7

File tree

9 files changed

+283
-6
lines changed

9 files changed

+283
-6
lines changed

extmod/extmod.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ set(MICROPY_SOURCE_EXTMOD
2424
${MICROPY_EXTMOD_DIR}/modframebuf.c
2525
${MICROPY_EXTMOD_DIR}/modlwip.c
2626
${MICROPY_EXTMOD_DIR}/modmachine.c
27+
${MICROPY_EXTMOD_DIR}/modmarshal.c
2728
${MICROPY_EXTMOD_DIR}/modnetwork.c
2829
${MICROPY_EXTMOD_DIR}/modonewire.c
2930
${MICROPY_EXTMOD_DIR}/modasyncio.c

extmod/extmod.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ SRC_EXTMOD_C += \
2929
extmod/modjson.c \
3030
extmod/modlwip.c \
3131
extmod/modmachine.c \
32+
extmod/modmarshal.c \
3233
extmod/modnetwork.c \
3334
extmod/modonewire.c \
3435
extmod/modopenamp.c \

extmod/modmarshal.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2025 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
#include "py/objcode.h"
29+
#include "py/objfun.h"
30+
#include "py/persistentcode.h"
31+
#include "py/runtime.h"
32+
33+
#if MICROPY_PY_MARSHAL
34+
35+
static mp_obj_t marshal_dumps(mp_obj_t value_in) {
36+
if (mp_obj_is_type(value_in, &mp_type_code)) {
37+
mp_obj_code_t *code = MP_OBJ_TO_PTR(value_in);
38+
const void *proto_fun = mp_code_get_proto_fun(code);
39+
const uint8_t *bytecode;
40+
if (mp_proto_fun_is_bytecode(proto_fun)) {
41+
bytecode = proto_fun;
42+
} else {
43+
const mp_raw_code_t *rc = proto_fun;
44+
if (!(rc->kind == MP_CODE_BYTECODE && rc->children == NULL)) {
45+
mp_raise_ValueError(MP_ERROR_TEXT("function must be bytecode with no children"));
46+
}
47+
bytecode = rc->fun_data;
48+
}
49+
return mp_raw_code_save_fun_to_bytes(mp_code_get_constants(code), bytecode);
50+
} else {
51+
mp_raise_ValueError(MP_ERROR_TEXT("unmarshallable object"));
52+
}
53+
}
54+
static MP_DEFINE_CONST_FUN_OBJ_1(marshal_dumps_obj, marshal_dumps);
55+
56+
static mp_obj_t marshal_loads(mp_obj_t data_in) {
57+
mp_buffer_info_t bufinfo;
58+
mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
59+
mp_module_context_t ctx;
60+
ctx.module.globals = mp_globals_get();
61+
mp_compiled_module_t cm = { .context = &ctx };
62+
mp_raw_code_load_mem(bufinfo.buf, bufinfo.len, &cm);
63+
#if MICROPY_PY_BUILTINS_CODE <= MICROPY_PY_BUILTINS_CODE_BASIC
64+
return mp_obj_new_code(ctx.constants, cm.rc);
65+
#else
66+
mp_module_context_t *ctx_ptr = m_new_obj(mp_module_context_t);
67+
*ctx_ptr = ctx;
68+
return mp_obj_new_code(ctx_ptr, cm.rc, true);
69+
#endif
70+
}
71+
static MP_DEFINE_CONST_FUN_OBJ_1(marshal_loads_obj, marshal_loads);
72+
73+
static const mp_rom_map_elem_t mod_marshal_globals_table[] = {
74+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_marshal) },
75+
{ MP_ROM_QSTR(MP_QSTR_dumps), MP_ROM_PTR(&marshal_dumps_obj) },
76+
{ MP_ROM_QSTR(MP_QSTR_loads), MP_ROM_PTR(&marshal_loads_obj) },
77+
};
78+
79+
static MP_DEFINE_CONST_DICT(mod_marshal_globals, mod_marshal_globals_table);
80+
81+
const mp_obj_module_t mp_module_marshal = {
82+
.base = { &mp_type_module },
83+
.globals = (mp_obj_dict_t *)&mod_marshal_globals,
84+
};
85+
86+
MP_REGISTER_MODULE(MP_QSTR_marshal, mp_module_marshal);
87+
88+
#endif // MICROPY_PY_MARSHAL

ports/windows/msvc/sources.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<PyExtModSource Include="$(PyBaseDir)extmod\modheapq.c" />
1616
<PyExtModSource Include="$(PyBaseDir)extmod\modjson.c" />
1717
<PyExtModSource Include="$(PyBaseDir)extmod\modmachine.c" />
18+
<PyExtModSource Include="$(PyBaseDir)extmod\modmarshal.c" />
1819
<PyExtModSource Include="$(PyBaseDir)extmod\modos.c" />
1920
<PyExtModSource Include="$(PyBaseDir)extmod\modrandom.c" />
2021
<PyExtModSource Include="$(PyBaseDir)extmod\modre.c" />

py/mpconfig.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@
344344

345345
// Whether to support converting functions to persistent code (bytes)
346346
#ifndef MICROPY_PERSISTENT_CODE_SAVE_FUN
347-
#define MICROPY_PERSISTENT_CODE_SAVE_FUN (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING)
347+
#define MICROPY_PERSISTENT_CODE_SAVE_FUN (MICROPY_PY_MARSHAL)
348348
#endif
349349

350350
// Whether generated code can persist independently of the VM/runtime instance
@@ -1382,6 +1382,11 @@ typedef double mp_float_t;
13821382
#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING)
13831383
#endif
13841384

1385+
// Whether to provide "marshal" module
1386+
#ifndef MICROPY_PY_MARSHAL
1387+
#define MICROPY_PY_MARSHAL (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EVERYTHING)
1388+
#endif
1389+
13851390
// Whether to provide "math" module
13861391
#ifndef MICROPY_PY_MATH
13871392
#define MICROPY_PY_MATH (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_CORE_FEATURES)

tests/extmod/marshal_basic.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Test the marshal module, basic functionality.
2+
3+
try:
4+
import marshal
5+
6+
(lambda: 0).__code__
7+
except (AttributeError, ImportError):
8+
print("SKIP")
9+
raise SystemExit
10+
11+
ftype = type(lambda: 0)
12+
13+
# Test basic dumps and loads.
14+
print(ftype(marshal.loads(marshal.dumps((lambda: a).__code__)), {"a": 4})())
15+
16+
# Test dumps of a result from compile().
17+
ftype(marshal.loads(marshal.dumps(compile("print(a)", "", "exec"))), {"print": print, "a": 5})()
18+
19+
# Test marshalling a function with arguments.
20+
print(ftype(marshal.loads(marshal.dumps((lambda x, y: x + y).__code__)), {})(1, 2))
21+
22+
# Test marshalling a function with default arguments.
23+
print(ftype(marshal.loads(marshal.dumps((lambda x=0: x).__code__)), {})("arg"))
24+
25+
# Test marshalling a function containing constant objects (a tuple).
26+
print(ftype(marshal.loads(marshal.dumps((lambda: (None, ...)).__code__)), {})())
27+
28+
# Test instantiating multiple code's with different globals dicts.
29+
code = marshal.loads(marshal.dumps((lambda: a).__code__))
30+
f1 = ftype(code, {"a": 1})
31+
f2 = ftype(code, {"a": 2})
32+
print(f1(), f2())
33+
34+
# Test unmarshallable object.
35+
try:
36+
marshal.dumps(type)
37+
except ValueError:
38+
print("ValueError")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Test the marshal module, MicroPython-specific functionality.
2+
3+
try:
4+
import marshal
5+
except ImportError:
6+
print("SKIP")
7+
raise SystemExit
8+
9+
import unittest
10+
11+
12+
class Test(unittest.TestCase):
13+
def test_function_with_children(self):
14+
# Can't marshal a function with children (in this case the module has a child function f).
15+
code = compile("def f(): pass", "", "exec")
16+
with self.assertRaises(ValueError):
17+
marshal.dumps(code)
18+
19+
20+
if __name__ == "__main__":
21+
unittest.main()

tests/extmod/marshal_stress.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Test the marshal module, stressing edge cases.
2+
3+
try:
4+
import marshal
5+
6+
(lambda: 0).__code__
7+
except (AttributeError, ImportError):
8+
print("SKIP")
9+
raise SystemExit
10+
11+
ftype = type(lambda: 0)
12+
13+
# Test a large function.
14+
15+
16+
def large_function(arg0, arg1, arg2, arg3):
17+
# Arguments.
18+
print(arg0, arg1, arg2, arg3)
19+
20+
# Positive medium-sized integer (still a small-int though).
21+
print(1234)
22+
23+
# Negative small-ish integer.
24+
print(-20)
25+
26+
# More than 64 constant objects.
27+
x = (0,)
28+
x = (1,)
29+
x = (2,)
30+
x = (3,)
31+
x = (4,)
32+
x = (5,)
33+
x = (6,)
34+
x = (7,)
35+
x = (8,)
36+
x = (9,)
37+
x = (10,)
38+
x = (11,)
39+
x = (12,)
40+
x = (13,)
41+
x = (14,)
42+
x = (15,)
43+
x = (16,)
44+
x = (17,)
45+
x = (18,)
46+
x = (19,)
47+
x = (20,)
48+
x = (21,)
49+
x = (22,)
50+
x = (23,)
51+
x = (24,)
52+
x = (25,)
53+
x = (26,)
54+
x = (27,)
55+
x = (28,)
56+
x = (29,)
57+
x = (30,)
58+
x = (31,)
59+
x = (32,)
60+
x = (33,)
61+
x = (34,)
62+
x = (35,)
63+
x = (36,)
64+
x = (37,)
65+
x = (38,)
66+
x = (39,)
67+
x = (40,)
68+
x = (41,)
69+
x = (42,)
70+
x = (43,)
71+
x = (44,)
72+
x = (45,)
73+
x = (46,)
74+
x = (47,)
75+
x = (48,)
76+
x = (49,)
77+
x = (50,)
78+
x = (51,)
79+
x = (52,)
80+
x = (53,)
81+
x = (54,)
82+
x = (55,)
83+
x = (56,)
84+
x = (57,)
85+
x = (58,)
86+
x = (59,)
87+
x = (60,)
88+
x = (61,)
89+
x = (62,)
90+
x = (63,)
91+
x = (64,)
92+
93+
# Small jump.
94+
x = 0
95+
while x < 2:
96+
print("loop", x)
97+
x += 1
98+
99+
# Large jump.
100+
x = 0
101+
while x < 2:
102+
try:
103+
try:
104+
try:
105+
print
106+
except Exception as e:
107+
print
108+
finally:
109+
print
110+
except Exception as e:
111+
print
112+
finally:
113+
print
114+
except Exception as e:
115+
print
116+
finally:
117+
print("loop", x)
118+
x += 1
119+
120+
121+
code = marshal.dumps(large_function.__code__)
122+
ftype(marshal.loads(code), {"print": print})(0, 1, 2, 3)

tests/ports/unix/extra_coverage.py.exp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ cmath collections cppexample cryptolib
5656
deflate errno example_package
5757
ffi framebuf gc hashlib
5858
heapq io json machine
59-
math os platform random
60-
re select socket struct
61-
sys termios time tls
62-
uctypes vfs websocket
59+
marshal math os platform
60+
random re select socket
61+
struct sys termios time
62+
tls uctypes vfs websocket
6363
me
6464

65-
micropython machine math
65+
micropython machine marshal math
6666

6767
argv atexit byteorder exc_info
6868
executable exit getsizeof implementation

0 commit comments

Comments
 (0)