diff options
author | Damien George <damien.p.george@gmail.com> | 2015-01-28 23:43:01 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-01-28 23:43:01 +0000 |
commit | 0d3cb6726ddc1bab9fdd11a0aaa259fb436da4b2 (patch) | |
tree | 7285c3f452efdfce8c0ecb302bbd0e2efcca0c15 /py/builtinimport.c | |
parent | 57aebe171459fd599f8d430c1ea1660ed307360c (diff) | |
download | micropython-0d3cb6726ddc1bab9fdd11a0aaa259fb436da4b2.tar.gz micropython-0d3cb6726ddc1bab9fdd11a0aaa259fb436da4b2.zip |
py: Change vstr so that it doesn't null terminate buffer by default.
This cleans up vstr so that it's a pure "variable buffer", and the user
can decide whether they need to add a terminating null byte. In most
places where vstr is used, the vstr did not need to be null terminated
and so this patch saves code size, a tiny bit of RAM, and makes vstr
usage more efficient. When null termination is needed it must be
done explicitly using vstr_null_terminate.
Diffstat (limited to 'py/builtinimport.c')
-rw-r--r-- | py/builtinimport.c | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index 73bf0b4722..1717c500d9 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -61,12 +61,14 @@ bool mp_obj_is_package(mp_obj_t module) { } STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) { + vstr_null_terminate(path); //printf("stat %s\n", vstr_str(path)); mp_import_stat_t stat = mp_import_stat(vstr_str(path)); if (stat == MP_IMPORT_STAT_DIR) { return stat; } vstr_add_str(path, ".py"); + vstr_null_terminate(path); stat = mp_import_stat(vstr_str(path)); if (stat == MP_IMPORT_STAT_FILE) { return stat; @@ -134,6 +136,7 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { // create the lexer + vstr_null_terminate(file); mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file)); do_load_from_lexer(module_obj, lex, vstr_str(file)); } @@ -263,7 +266,7 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) { // create a qstr for the module name up to this depth qstr mod_name = qstr_from_strn(mod_str, i); DEBUG_printf("Processing module: %s\n", qstr_str(mod_name)); - DEBUG_printf("Previous path: %s\n", vstr_str(&path)); + DEBUG_printf("Previous path: %.*s\n", vstr_len(&path), vstr_str(&path)); // find the file corresponding to the module name mp_import_stat_t stat; @@ -276,7 +279,7 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) { vstr_add_strn(&path, mod_str + last, i - last); stat = stat_dir_or_file(&path); } - DEBUG_printf("Current path: %s\n", vstr_str(&path)); + DEBUG_printf("Current path: %.*s\n", vstr_len(&path), vstr_str(&path)); if (stat == MP_IMPORT_STAT_NO_EXIST) { #if MICROPY_MODULE_WEAK_LINKS @@ -320,12 +323,13 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) { } if (stat == MP_IMPORT_STAT_DIR) { - DEBUG_printf("%s is dir\n", vstr_str(&path)); + DEBUG_printf("%.*s is dir\n", vstr_len(&path), vstr_str(&path)); // https://docs.python.org/3/reference/import.html // "Specifically, any module that contains a __path__ attribute is considered a package." mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path), false)); vstr_add_char(&path, PATH_SEP_CHAR); vstr_add_str(&path, "__init__.py"); + vstr_null_terminate(&path); if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) { vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py mp_warning("%s is imported as namespace package", vstr_str(&path)); |