Skip to content

Commit

Permalink
unix: Enable sys.executable.
Browse files Browse the repository at this point in the history
Gives the absolute path to the unix micropython binary.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
jimmo authored and dpgeorge committed Oct 11, 2022
1 parent 0e8dfaf commit 8e912a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 16 additions & 0 deletions ports/unix/main.c
Expand Up @@ -43,6 +43,7 @@
#include "py/builtin.h"
#include "py/repl.h"
#include "py/gc.h"
#include "py/objstr.h"
#include "py/stackctrl.h"
#include "py/mphal.h"
#include "py/mpthread.h"
Expand Down Expand Up @@ -435,6 +436,17 @@ STATIC void set_sys_argv(char *argv[], int argc, int start_arg) {
}
}

#if MICROPY_PY_SYS_EXECUTABLE
extern mp_obj_str_t mp_sys_executable_obj;
STATIC char executable_path[MICROPY_ALLOC_PATH_MAX];

STATIC void sys_set_excecutable(char *argv0) {
if (realpath(argv0, executable_path)) {
mp_obj_str_set_data(&mp_sys_executable_obj, (byte *)executable_path, strlen(executable_path));
}
}
#endif

#ifdef _WIN32
#define PATHLIST_SEP_CHAR ';'
#else
Expand Down Expand Up @@ -598,6 +610,10 @@ MP_NOINLINE int main_(int argc, char **argv) {
printf(" peak %d\n", m_get_peak_bytes_allocated());
*/

#if MICROPY_PY_SYS_EXECUTABLE
sys_set_excecutable(argv[0]);
#endif

const int NOTHING_EXECUTED = -2;
int ret = NOTHING_EXECUTED;
bool inspect = false;
Expand Down
3 changes: 3 additions & 0 deletions ports/unix/mpconfigport.h
Expand Up @@ -154,6 +154,9 @@ typedef long mp_off_t;
// Don't default sys.argv because we do that in main.
#define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (0)

// Enable sys.executable.
#define MICROPY_PY_SYS_EXECUTABLE (1)

#define MICROPY_PY_USOCKET_LISTEN_BACKLOG_DEFAULT (SOMAXCONN < 128 ? SOMAXCONN : 128)

// Bare-metal ports don't have stderr. Printing debug to stderr may give tests
Expand Down
9 changes: 5 additions & 4 deletions tests/unix/extra_coverage.py.exp
Expand Up @@ -62,10 +62,11 @@ ime
utime utimeq

argv atexit byteorder exc_info
exit getsizeof implementation maxsize
modules path platform print_exception
ps1 ps2 stderr stdin
stdout tracebacklimit version version_info
executable exit getsizeof implementation
maxsize modules path platform
print_exception ps1 ps2
stderr stdin stdout tracebacklimit
version version_info
ementation
# attrtuple
(start=1, stop=2, step=3)
Expand Down

0 comments on commit 8e912a5

Please sign in to comment.