unix: Enable sys.executable.

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>
This commit is contained in:
Jim Mussared 2022-10-07 02:13:58 +11:00 committed by Damien George
parent 0e8dfaf538
commit 8e912a501a
3 changed files with 24 additions and 4 deletions

View File

@ -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"
@ -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
@ -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;

View File

@ -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

View File

@ -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)