Skip to content

Commit 0e8dfaf

Browse files
jimmodpgeorge
authored andcommitted
py/modsys: Add support for sys.executable.
Only intended to be used on Unix and other "OS" ports. Matches CPython. This should give the absolute path to the executing binary. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <[email protected]> Signed-off-by: Damien George <[email protected]>
1 parent c44b392 commit 0e8dfaf

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

py/modsys.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
115115
STATIC const MP_DEFINE_STR_OBJ(mp_sys_platform_obj, MICROPY_PY_SYS_PLATFORM);
116116
#endif
117117

118+
#ifdef MICROPY_PY_SYS_EXECUTABLE
119+
// executable - the path to the micropython binary
120+
// This object is non-const and is populated at startup in main()
121+
MP_DEFINE_STR_OBJ(mp_sys_executable_obj, "");
122+
#endif
123+
118124
// exit([retval]): raise SystemExit, with optional argument given to the exception
119125
STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
120126
if (n_args == 0) {
@@ -262,6 +268,10 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
262268
{ MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
263269
#endif
264270

271+
#if MICROPY_PY_SYS_EXECUTABLE
272+
{ MP_ROM_QSTR(MP_QSTR_executable), MP_ROM_PTR(&mp_sys_executable_obj) },
273+
#endif
274+
265275
/*
266276
* Extensions to CPython
267277
*/

py/mpconfig.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,13 @@ typedef double mp_float_t;
13721372
#define MICROPY_PY_SYS_EXC_INFO (0)
13731373
#endif
13741374

1375+
// Whether to provide "sys.executable", which is the absolute path to the
1376+
// micropython binary
1377+
// Intended for use on the "OS" ports (e.g. Unix)
1378+
#ifndef MICROPY_PY_SYS_EXECUTABLE
1379+
#define MICROPY_PY_SYS_EXECUTABLE (0)
1380+
#endif
1381+
13751382
// Whether to provide "sys.exit" function
13761383
#ifndef MICROPY_PY_SYS_EXIT
13771384
#define MICROPY_PY_SYS_EXIT (1)

0 commit comments

Comments
 (0)