Skip to content

Commit f9448dd

Browse files
committed
minimal: Add example of frozen persistent bytecode (.mpy file).
frozentest.py is frozen into the binary as frozen bytecode. The .mpy file is included so that there is no dependency on the cross compiler.
1 parent 0a2e965 commit f9448dd

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

minimal/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ SRC_C = \
5050
lib/utils/pyexec.c \
5151
lib/libc/string0.c \
5252
lib/mp-readline/readline.c \
53+
$(BUILD)/_frozen_mpy.c \
5354

5455
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
5556

@@ -59,6 +60,10 @@ else
5960
all: $(BUILD)/firmware.elf
6061
endif
6162

63+
$(BUILD)/_frozen_mpy.c: frozentest.mpy
64+
$(ECHO) "MISC freezing bytecode"
65+
$(Q)../tools/mpy-tool.py -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h -mlongint-impl=none $^ > $@
66+
6267
$(BUILD)/firmware.elf: $(OBJ)
6368
$(ECHO) "LINK $@"
6469
$(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)

minimal/frozentest.mpy

255 Bytes
Binary file not shown.

minimal/frozentest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
print('uPy')
2+
print('a long string that is not interned')
3+
print('a string that has unicode αβγ chars')
4+
print(b'bytes 1234\x01')
5+
print(123456789)
6+
for i in range(4):
7+
print(i)

minimal/mpconfigport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// options to control how Micro Python is built
44

55
#define MICROPY_QSTR_BYTES_IN_HASH (1)
6+
#define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool
67
#define MICROPY_ALLOC_PATH_MAX (256)
78
#define MICROPY_ALLOC_PARSE_CHUNK_INIT (16)
89
#define MICROPY_EMIT_X64 (0)
@@ -43,7 +44,7 @@
4344
#define MICROPY_PY_IO (0)
4445
#define MICROPY_PY_STRUCT (0)
4546
#define MICROPY_PY_SYS (0)
46-
#define MICROPY_MODULE_FROZEN (0)
47+
#define MICROPY_MODULE_FROZEN_MPY (1)
4748
#define MICROPY_CPYTHON_COMPAT (0)
4849
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
4950
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)

0 commit comments

Comments
 (0)