From c3184aea63b6eaf0a0037f691927028ca4be84c2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 21 Apr 2015 14:45:04 +0000 Subject: [PATCH] py: Add sys.implementation, containing uPy name and version number. Uses attrtuple if it's enabled, otherwise just a normal tuple. --- py/modsys.c | 31 +++++++++++++++++++++++++++++++ py/py-version.sh | 19 +++++++++++++++++++ py/qstrdefs.h | 4 ++++ 3 files changed, 54 insertions(+) diff --git a/py/modsys.c b/py/modsys.c index 537befd07e..182ae13ecc 100644 --- a/py/modsys.c +++ b/py/modsys.c @@ -35,6 +35,8 @@ #if MICROPY_PY_SYS +#include "genhdr/py-version.h" + /// \module sys - system specific functions // defined per port; type of these is irrelevant, just need pointer @@ -53,6 +55,34 @@ STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0"); #define I(n) MP_OBJ_NEW_SMALL_INT(n) // TODO: CPython is now at 5-element array, but save 2 els so far... STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}}; + +// sys.implementation object +// this holds the MicroPython version +STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = { + {&mp_type_tuple}, + 3, + { I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) } +}; +#if MICROPY_PY_ATTRTUPLE +STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version }; +STATIC MP_DEFINE_ATTRTUPLE( + mp_sys_implementation_obj, + impl_fields, + 2, + MP_OBJ_NEW_QSTR(MP_QSTR_micropython), + (mp_obj_t)&mp_sys_implementation_version_info_obj +); +#else +STATIC const mp_obj_tuple_t mp_sys_implementation_obj = { + {&mp_type_tuple}, + 2, + { + MP_OBJ_NEW_QSTR(MP_QSTR_micropython), + (mp_obj_t)&mp_sys_implementation_version_info_obj, + } +}; +#endif + #undef I #ifdef MICROPY_PY_SYS_PLATFORM @@ -98,6 +128,7 @@ STATIC const mp_map_elem_t mp_module_sys_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&MP_STATE_VM(mp_sys_argv_obj) }, { MP_OBJ_NEW_QSTR(MP_QSTR_version), (mp_obj_t)&version_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_version_info), (mp_obj_t)&mp_sys_version_info_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_implementation), (mp_obj_t)&mp_sys_implementation_obj }, #ifdef MICROPY_PY_SYS_PLATFORM { MP_OBJ_NEW_QSTR(MP_QSTR_platform), (mp_obj_t)&platform_obj }, #endif diff --git a/py/py-version.sh b/py/py-version.sh index 88c3ebb986..7db84adda9 100755 --- a/py/py-version.sh +++ b/py/py-version.sh @@ -11,9 +11,28 @@ git diff-index --cached --quiet HEAD -- 2> /dev/null || git_files_are_clean=0 if [ "${git_files_are_clean}" != "1" ]; then git_hash="${git_hash}-dirty" fi + +# Try to extract MicroPython version +if echo ${git_tag} | grep -q '^v[0-9]'; then + ver=$(echo ${git_tag} | cut -b 2- | cut -d - -f 1) + ver_major=$(echo ${ver} | cut -d . -f 1) + ver_minor=$(echo ${ver} | cut -d . -f 2) + ver_micro=$(echo ${ver} | cut -d . -f 3) + if [ -z ${ver_micro} ]; then + ver_micro="0" + fi +else + ver_major="0" + ver_minor="0" + ver_micro="1" +fi + cat <