Skip to content

Commit 4004782

Browse files
committed
py/modsys: Append MicroPython git version and build date to sys.version.
This commit adds the git hash and build date to sys.version. This is allowed according to CPython docs, and is what PyPy does. The docs state: A string containing the version number of the Python interpreter plus additional information on the build number and compiler used. Eg on CPython: Python 3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.version '3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0]' and PyPy: Python 2.7.12 (5.6.0+dfsg-4, Nov 20 2016, 10:43:30) [PyPy 5.6.0 with GCC 6.2.0 20161109] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>> import sys >>>> sys.version '2.7.12 (5.6.0+dfsg-4, Nov 20 2016, 10:43:30)\n[PyPy 5.6.0 with GCC ... With this commit on MicroPython we now have: MicroPython v1.18-371-g9d08eb024 on 2022-04-28; linux [GCC 11.2.0] v... Use Ctrl-D to exit, Ctrl-E for paste mode >>> import sys >>> sys.version '3.4.0; MicroPython v1.18-371-g9d08eb024 on 2022-04-28' Note that the start of the banner is the same as the end of sys.version. This helps to keep code size under control because the string can be reused by the compiler. Signed-off-by: Damien George <[email protected]>
1 parent 9d08eb0 commit 4004782

4 files changed

Lines changed: 13 additions & 5 deletions

File tree

ports/unix/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
178178
#endif
179179

180180
STATIC int do_repl(void) {
181-
mp_hal_stdout_tx_str("MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; "
182-
MICROPY_PY_SYS_PLATFORM " [" MICROPY_PLATFORM_COMPILER "] version\n"
181+
mp_hal_stdout_tx_str(MICROPY_BANNER_NAME_AND_VERSION);
182+
mp_hal_stdout_tx_str("; " MICROPY_PY_SYS_PLATFORM " [" MICROPY_PLATFORM_COMPILER "] version\n"
183183
"Use Ctrl-D to exit, Ctrl-E for paste mode\n");
184184

185185
#if MICROPY_USE_READLINE == 1

py/modsys.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "py/smallint.h"
3737
#include "py/runtime.h"
3838
#include "py/persistentcode.h"
39+
#include "genhdr/mpversion.h"
3940

4041
#if MICROPY_PY_SYS_SETTRACE
4142
#include "py/objmodule.h"
@@ -54,7 +55,7 @@ const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adap
5455
#endif
5556

5657
// version - Python language version that this implementation conforms to, as a string
57-
STATIC const MP_DEFINE_STR_OBJ(mp_sys_version_obj, "3.4.0");
58+
STATIC const MP_DEFINE_STR_OBJ(mp_sys_version_obj, "3.4.0; " MICROPY_BANNER_NAME_AND_VERSION);
5859

5960
// version_info - Python language version that this implementation conforms to, as a tuple of ints
6061
#define I(n) MP_OBJ_NEW_SMALL_INT(n)

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,11 @@ typedef double mp_float_t;
17141714
#define MICROPY_OBJ_BASE_ALIGNMENT
17151715
#endif
17161716

1717+
// String used for the banner, and sys.version additional information
1718+
#ifndef MICROPY_BANNER_NAME_AND_VERSION
1719+
#define MICROPY_BANNER_NAME_AND_VERSION "MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE
1720+
#endif
1721+
17171722
// On embedded platforms, these will typically enable/disable irqs.
17181723
#ifndef MICROPY_BEGIN_ATOMIC_SECTION
17191724
#define MICROPY_BEGIN_ATOMIC_SECTION() (0)

shared/runtime/pyexec.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ STATIC int pyexec_friendly_repl_process_char(int c) {
401401
} else if (ret == CHAR_CTRL_B) {
402402
// reset friendly REPL
403403
mp_hal_stdout_tx_str("\r\n");
404-
mp_hal_stdout_tx_str("MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n");
404+
mp_hal_stdout_tx_str(MICROPY_BANNER_NAME_AND_VERSION);
405+
mp_hal_stdout_tx_str("; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n");
405406
#if MICROPY_PY_BUILTINS_HELP
406407
mp_hal_stdout_tx_str("Type \"help()\" for more information.\r\n");
407408
#endif
@@ -552,7 +553,8 @@ int pyexec_friendly_repl(void) {
552553
vstr_init(&line, 32);
553554

554555
friendly_repl_reset:
555-
mp_hal_stdout_tx_str("MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n");
556+
mp_hal_stdout_tx_str(MICROPY_BANNER_NAME_AND_VERSION);
557+
mp_hal_stdout_tx_str("; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n");
556558
#if MICROPY_PY_BUILTINS_HELP
557559
mp_hal_stdout_tx_str("Type \"help()\" for more information.\r\n");
558560
#endif

0 commit comments

Comments
 (0)