py/modsys: Report .mpy version in sys.implementation.
This commit adds a sys.implementation.mpy entry when the system supports importing .mpy files. This entry is a 16-bit integer which encodes two bytes of information from the header of .mpy files that are supported by the system being run: the second and third bytes, .mpy version, and flags and native architecture. This allows determining the supported .mpy file dynamically by code, and also for the user to find it out by inspecting this value. It's further possible to dynamically detect if the system supports importing .mpy files by `hasattr(sys.implementation, 'mpy')`.
This commit is contained in:
parent
f4601af10a
commit
80df377e95
29
py/modsys.c
29
py/modsys.c
|
@ -34,6 +34,7 @@
|
|||
#include "py/stream.h"
|
||||
#include "py/smallint.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/persistentcode.h"
|
||||
|
||||
#if MICROPY_PY_SYS_SETTRACE
|
||||
#include "py/objmodule.h"
|
||||
|
@ -66,22 +67,36 @@ STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
|
|||
3,
|
||||
{ I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) }
|
||||
};
|
||||
#if MICROPY_PERSISTENT_CODE_LOAD
|
||||
#define SYS_IMPLEMENTATION_ELEMS \
|
||||
MP_ROM_QSTR(MP_QSTR_micropython), \
|
||||
MP_ROM_PTR(&mp_sys_implementation_version_info_obj), \
|
||||
MP_ROM_INT(MPY_FILE_HEADER_INT)
|
||||
#else
|
||||
#define SYS_IMPLEMENTATION_ELEMS \
|
||||
MP_ROM_QSTR(MP_QSTR_micropython), \
|
||||
MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
|
||||
#endif
|
||||
#if MICROPY_PY_ATTRTUPLE
|
||||
STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
|
||||
STATIC const qstr impl_fields[] = {
|
||||
MP_QSTR_name,
|
||||
MP_QSTR_version,
|
||||
#if MICROPY_PERSISTENT_CODE_LOAD
|
||||
MP_QSTR_mpy,
|
||||
#endif
|
||||
};
|
||||
STATIC MP_DEFINE_ATTRTUPLE(
|
||||
mp_sys_implementation_obj,
|
||||
impl_fields,
|
||||
2,
|
||||
MP_ROM_QSTR(MP_QSTR_micropython),
|
||||
MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
|
||||
2 + MICROPY_PERSISTENT_CODE_LOAD,
|
||||
SYS_IMPLEMENTATION_ELEMS
|
||||
);
|
||||
#else
|
||||
STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
|
||||
{&mp_type_tuple},
|
||||
2,
|
||||
2 + MICROPY_PERSISTENT_CODE_LOAD,
|
||||
{
|
||||
MP_ROM_QSTR(MP_QSTR_micropython),
|
||||
MP_ROM_PTR(&mp_sys_implementation_version_info_obj),
|
||||
SYS_IMPLEMENTATION_ELEMS
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
|
|
@ -70,6 +70,10 @@
|
|||
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_NONE)
|
||||
#endif
|
||||
|
||||
// 16-bit little-endian integer with the second and third bytes of supported .mpy files
|
||||
#define MPY_FILE_HEADER_INT (MPY_VERSION \
|
||||
| (MPY_FEATURE_ENCODE_FLAGS(MPY_FEATURE_FLAGS) | MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH)) << 8)
|
||||
|
||||
enum {
|
||||
MP_NATIVE_ARCH_NONE = 0,
|
||||
MP_NATIVE_ARCH_X86,
|
||||
|
|
|
@ -18,3 +18,9 @@ try:
|
|||
except AttributeError:
|
||||
# Effectively skip subtests
|
||||
print(True)
|
||||
|
||||
if hasattr(sys.implementation, 'mpy'):
|
||||
print(type(sys.implementation.mpy))
|
||||
else:
|
||||
# Effectively skip subtests
|
||||
print(int)
|
||||
|
|
Loading…
Reference in New Issue