From 625d08a93ead4b43573bd9ad7c99af18472fe67b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 5 Feb 2014 00:59:54 +0200 Subject: unix: Initialize sys.path from MICROPYPATH environment variable. If it's not available, "~/.micropython/lib:/usr/lib/micropython" is used as a fallback. --- unix/main.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'unix') diff --git a/unix/main.c b/unix/main.c index cc942163f..c9d7b4f79 100644 --- a/unix/main.c +++ b/unix/main.c @@ -233,7 +233,41 @@ int main(int argc, char **argv) { qstr_init(); rt_init(); + char *home = getenv("HOME"); + char *path = getenv("MICROPYPATH"); + if (path == NULL) { + path = "~/.micropython/lib:/usr/lib/micropython"; + } + uint path_num = 0; + for (char *p = path; p != NULL; p = strchr(p, ':')) { + path_num++; + if (p != NULL) { + p++; + } + } + sys_path = mp_obj_new_list(path_num, NULL); + mp_obj_t *items; + mp_obj_list_get(sys_path, &path_num, &items); + char *p = path; + for (int i = 0; i < path_num; i++) { + char *p1 = strchr(p, ':'); + if (p1 == NULL) { + p1 = p + strlen(p); + } + if (p[0] == '~' && p[1] == '/' && home != NULL) { + // Expand standalone ~ to $HOME + CHECKBUF(buf, PATH_MAX); + CHECKBUF_APPEND(buf, home, strlen(home)); + CHECKBUF_APPEND(buf, p + 1, p1 - p - 1); + items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf))); + } else { + items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p)); + } + p = p1 + 1; + } + mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys); + rt_store_attr(m_sys, MP_QSTR_path, sys_path); mp_obj_t py_argv = mp_obj_new_list(0, NULL); rt_store_attr(m_sys, MP_QSTR_argv, py_argv); -- cgit v1.2.3 From 630d85120fd2b745f314e6810e41144e026ff2f3 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 5 Feb 2014 01:53:44 +0200 Subject: unix: Be sure to add current/base dir of a script to sys.path. This mirrors CPython behavior and makes possible to run scripts which import other modules not from script's directory. --- unix/main.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'unix') diff --git a/unix/main.c b/unix/main.c index c9d7b4f79..f73c98ed6 100644 --- a/unix/main.c +++ b/unix/main.c @@ -238,7 +238,7 @@ int main(int argc, char **argv) { if (path == NULL) { path = "~/.micropython/lib:/usr/lib/micropython"; } - uint path_num = 0; + uint path_num = 1; // [0] is for current dir (or base dir of the script) for (char *p = path; p != NULL; p = strchr(p, ':')) { path_num++; if (p != NULL) { @@ -246,10 +246,11 @@ int main(int argc, char **argv) { } } sys_path = mp_obj_new_list(path_num, NULL); - mp_obj_t *items; - mp_obj_list_get(sys_path, &path_num, &items); + mp_obj_t *path_items; + mp_obj_list_get(sys_path, &path_num, &path_items); + path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_); char *p = path; - for (int i = 0; i < path_num; i++) { + for (int i = 1; i < path_num; i++) { char *p1 = strchr(p, ':'); if (p1 == NULL) { p1 = p + strlen(p); @@ -259,9 +260,9 @@ int main(int argc, char **argv) { CHECKBUF(buf, PATH_MAX); CHECKBUF_APPEND(buf, home, strlen(home)); CHECKBUF_APPEND(buf, p + 1, p1 - p - 1); - items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf))); + path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf))); } else { - items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p)); + path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p)); } p = p1 + 1; } @@ -318,6 +319,13 @@ int main(int argc, char **argv) { return usage(); } } else { + // Set base dir of the script as first entry in sys.path + char *basedir = realpath(argv[a], NULL); + if (basedir != NULL) { + char *p = strrchr(basedir, '/'); + path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir)); + free(basedir); + } for (int i = a; i < argc; i++) { rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i]))); } -- cgit v1.2.3 From 911089606376e60bd9451a85eb9558a23cde9039 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 5 Feb 2014 02:03:23 +0200 Subject: Remove older import helpers, no longer used. --- unix/main.c | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'unix') diff --git a/unix/main.c b/unix/main.c index f73c98ed6..d66595cd6 100644 --- a/unix/main.c +++ b/unix/main.c @@ -147,18 +147,6 @@ static void do_repl(void) { } static void do_file(const char *file) { - // hack: set dir for import based on where this file is - { - const char * s = strrchr(file, '/'); - if (s != NULL) { - int len = s - file; - char *dir = m_new(char, len + 1); - memcpy(dir, file, len); - dir[len] = '\0'; - mp_import_set_directory(dir); - } - } - mp_lexer_t *lex = mp_lexer_new_from_file(file); execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false); } -- cgit v1.2.3