8000 unix: Enable sys.executable. · micropython/micropython@8e912a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e912a5

Browse files
jimmodpgeorge
authored andcommitted
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>
1 parent 0e8dfaf commit 8e912a5

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

ports/unix/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "py/builtin.h"
4444
#include "py/repl.h"
4545
#include "py/gc.h"
46+
#include "py/objstr.h"
4647
#include "py/stackctrl.h"
4748
#include "py/mphal.h"
4849
#include "py/mpthread.h"
@@ -435,6 +436,17 @@ STATIC void set_sys_argv(char *argv[], int argc, int start_arg) {
435436
}
436437
}
437438

439+
#if MICROPY_PY_SYS_EXECUTABLE
440+
extern mp_obj_str_t mp_sys_executable_obj;
441+
STATIC char executable_path[MICROPY_ALLOC_PATH_MAX];
442+
443+
STATIC void sys_set_excecutable(char *argv0) {
444+
if (realpath(argv0, executable_path)) {
445+
mp_obj_str_set_data(&mp_sys_executable_obj, (byte *)executable_path, strlen(executable_path));
446+
}
447+
}
448+
#endif
449+
438450
#ifdef _WIN32
439451
#define PATHLIST_SEP_CHAR ';'
440452
#else
@@ -598,6 +610,10 @@ MP_NOINLINE int main_(int argc, char **argv) {
598610
printf(" peak %d\n", m_get_peak_bytes_allocated());
599611
*/
600612

613+
#if MICROPY_PY_SYS_EXECUTABLE
614+
sys_set_excecutable(argv[0]);
615+
#endif
616+
601617
const int NOTHING_EXECUTED = -2;
602618
int ret = NOTHING_EXECUTED;
603619
bool inspect = false;

ports/unix/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ typedef long mp_off_t;
154154
// Don't default sys.argv because we do that in main.
155155
#define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (0)
156156

157+
// Enable sys.executable.
158+
#define MICROPY_PY_SYS_EXECUTABLE (1)
159+
157160
#define MICROPY_PY_USOCKET_LISTEN_BACKLOG_DEFAULT (SOMAXCONN < 128 ? SOMAXCONN : 128)
158161

159162
// Bare-metal ports don't have stderr. Printing debug to stderr may give tests

tests/unix/extra_coverage.py.exp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ ime
6262
utime utimeq
6363

6464
argv atexit byteorder exc_info
65-
exit getsizeof implementation maxsize
66-
modules path platform print_exception
67-
ps1 ps2 stderr stdin
68-
stdout tracebacklimit version version_info
65+
executable exit getsizeof implementation
66+
maxsize modules path platform
67+
print_exception ps1 ps2
68+
stderr stdin stdout tracebacklimit
69+
version version_info
6970
ementation
7071
# attrtuple
7172
(start=1, stop=2, step=3)

0 commit comments

Comments
 (0)
0