Skip to content

Commit c3184ae

Browse files
committed
py: Add sys.implementation, containing uPy name and version number.
Uses attrtuple if it's enabled, otherwise just a normal tuple.
1 parent 5aa311d commit c3184ae

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

py/modsys.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
#if MICROPY_PY_SYS
3737

38+
#include "genhdr/py-version.h"
39+
3840
/// \module sys - system specific functions
3941

4042
// 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");
5355
#define I(n) MP_OBJ_NEW_SMALL_INT(n)
5456
// TODO: CPython is now at 5-element array, but save 2 els so far...
5557
STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}};
58+
59+
// sys.implementation object
60+
// this holds the MicroPython version
61+
STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
62+
{&mp_type_tuple},
63+
3,
64+
{ I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) }
65+
};
66+
#if MICROPY_PY_ATTRTUPLE
67+
STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
68+
STATIC MP_DEFINE_ATTRTUPLE(
69+
mp_sys_implementation_obj,
70+
impl_fields,
71+
2,
72+
MP_OBJ_NEW_QSTR(MP_QSTR_micropython),
73+
(mp_obj_t)&mp_sys_implementation_version_info_obj
74+
);
75+
#else
76+
STATIC const mp_obj_tuple_t mp_sys_implementation_obj = {
77+
{&mp_type_tuple},
78+
2,
79+
{
80+
MP_OBJ_NEW_QSTR(MP_QSTR_micropython),
81+
(mp_obj_t)&mp_sys_implementation_version_info_obj,
82+
}
83+
};
84+
#endif
85+
5686
#undef I
5787

5888
#ifdef MICROPY_PY_SYS_PLATFORM
@@ -98,6 +128,7 @@ STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
98128
{ MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&MP_STATE_VM(mp_sys_argv_obj) },
99129
{ MP_OBJ_NEW_QSTR(MP_QSTR_version), (mp_obj_t)&version_obj },
100130
{ MP_OBJ_NEW_QSTR(MP_QSTR_version_info), (mp_obj_t)&mp_sys_version_info_obj },
131+
{ MP_OBJ_NEW_QSTR(MP_QSTR_implementation), (mp_obj_t)&mp_sys_implementation_obj },
101132
#ifdef MICROPY_PY_SYS_PLATFORM
102133
{ MP_OBJ_NEW_QSTR(MP_QSTR_platform), (mp_obj_t)&platform_obj },
103134
#endif

py/py-version.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,28 @@ git diff-index --cached --quiet HEAD -- 2> /dev/null || git_files_are_clean=0
1111
if [ "${git_files_are_clean}" != "1" ]; then
1212
git_hash="${git_hash}-dirty"
1313
fi
14+
15+
# Try to extract MicroPython version
16+
if echo ${git_tag} | grep -q '^v[0-9]'; then
17+
ver=$(echo ${git_tag} | cut -b 2- | cut -d - -f 1)
18+
ver_major=$(echo ${ver} | cut -d . -f 1)
19+
ver_minor=$(echo ${ver} | cut -d . -f 2)
20+
ver_micro=$(echo ${ver} | cut -d . -f 3)
21+
if [ -z ${ver_micro} ]; then
22+
ver_micro="0"
23+
fi
24+
else
25+
ver_major="0"
26+
ver_minor="0"
27+
ver_micro="1"
28+
fi
29+
1430
cat <<EOF
1531
// This file was generated by py/py-version.sh
1632
#define MICROPY_GIT_TAG "${git_tag}"
1733
#define MICROPY_GIT_HASH "${git_hash}"
1834
#define MICROPY_BUILD_DATE "$(date '+%Y-%m-%d')"
35+
#define MICROPY_VERSION_MAJOR (${ver_major})
36+
#define MICROPY_VERSION_MINOR (${ver_minor})
37+
#define MICROPY_VERSION_MICRO (${ver_micro})
1938
EOF

py/qstrdefs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ Q(stdout)
450450
Q(stderr)
451451
Q(version)
452452
Q(version_info)
453+
#if MICROPY_PY_ATTRTUPLE
454+
Q(name)
455+
#endif
456+
Q(implementation)
453457
#if MICROPY_PY_SYS_MAXSIZE
454458
Q(maxsize)
455459
#endif

0 commit comments

Comments
 (0)