Skip to content

Commit 6a11048

Browse files
committed
py/persistentcode: Bump .mpy version due to change in bytecode.
1 parent c264414 commit 6a11048

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

py/persistentcode.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838

3939
#include "py/smallint.h"
4040

41+
// The current version of .mpy files
42+
#define MPY_VERSION (1)
43+
4144
// The feature flags byte encodes the compile-time config options that
4245
// affect the generate bytecode.
4346
#define MPY_FEATURE_FLAGS ( \
@@ -209,10 +212,10 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
209212
mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) {
210213
byte header[4];
211214
read_bytes(reader, header, sizeof(header));
212-
if (strncmp((char*)header, "M\x00", 2) != 0) {
213-
mp_raise_ValueError("invalid .mpy file");
214-
}
215-
if (header[2] != MPY_FEATURE_FLAGS || header[3] > mp_small_int_bits()) {
215+
if (header[0] != 'M'
216+
|| header[1] != MPY_VERSION
217+
|| header[2] != MPY_FEATURE_FLAGS
218+
|| header[3] > mp_small_int_bits()) {
216219
mp_raise_ValueError("incompatible .mpy file");
217220
}
218221
mp_raw_code_t *rc = load_raw_code(reader);
@@ -359,7 +362,7 @@ void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) {
359362
// byte version
360363
// byte feature flags
361364
// byte number of bits in a small int
362-
byte header[4] = {'M', 0, MPY_FEATURE_FLAGS_DYNAMIC,
365+
byte header[4] = {'M', MPY_VERSION, MPY_FEATURE_FLAGS_DYNAMIC,
363366
#if MICROPY_DYNAMIC_COMPILER
364367
mp_dynamic_compiler.small_int_bits,
365368
#else

tools/mpy-tool.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __str__(self):
5757
return 'error while freezing %s: %s' % (self.rawcode.source_file, self.msg)
5858

5959
class Config:
60+
MPY_VERSION = 1
6061
MICROPY_LONGINT_IMPL_NONE = 0
6162
MICROPY_LONGINT_IMPL_LONGLONG = 1
6263
MICROPY_LONGINT_IMPL_MPZ = 2
@@ -438,8 +439,8 @@ def read_mpy(filename):
438439
header = bytes_cons(f.read(4))
439440
if header[0] != ord('M'):
440441
raise Exception('not a valid .mpy file')
441-
if header[1] != 0:
442-
raise Exception('incompatible version')
442+
if header[1] != config.MPY_VERSION:
443+
raise Exception('incompatible .mpy version')
443444
feature_flags = header[2]
444445
config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE = (feature_flags & 1) != 0
445446
config.MICROPY_PY_BUILTINS_STR_UNICODE = (feature_flags & 2) != 0

0 commit comments

Comments
 (0)