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/builtin.h"
#include "py/repl.h" #include "py/repl.h"
#include "py/gc.h" #include "py/gc.h"
#include "py/objstr.h"
#include "py/stackctrl.h" #include "py/stackctrl.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "py/mpthread.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 #ifdef _WIN32
#define PATHLIST_SEP_CHAR ';' #define PATHLIST_SEP_CHAR ';'
#else #else
@@ -598,6 +610,10 @@ MP_NOINLINE int main_(int argc, char **argv) {
printf(" peak %d\n", m_get_peak_bytes_allocated()); 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; const int NOTHING_EXECUTED = -2;
int ret = NOTHING_EXECUTED; int ret = NOTHING_EXECUTED;
bool inspect = false; 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. // Don't default sys.argv because we do that in main.
#define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (0) #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) #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 // Bare-metal ports don't have stderr. Printing debug to stderr may give tests

View File

@@ -62,10 +62,11 @@ ime
utime utimeq utime utimeq
argv atexit byteorder exc_info argv atexit byteorder exc_info
exit getsizeof implementation maxsize executable exit getsizeof implementation
modules path platform print_exception maxsize modules path platform
ps1 ps2 stderr stdin print_exception ps1 ps2
stdout tracebacklimit version version_info stderr stdin stdout tracebacklimit
version version_info
ementation ementation
# attrtuple # attrtuple
(start=1, stop=2, step=3) (start=1, stop=2, step=3)