summaryrefslogtreecommitdiffstatshomepage
path: root/py/builtinimport.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-03-14 11:16:31 +1100
committerDamien George <damien.p.george@gmail.com>2017-03-14 11:52:05 +1100
commit1831034be13fef5344583c557ff089df31788251 (patch)
tree391459a654f8b48a5d21af14dc7b9c677ed9af51 /py/builtinimport.c
parent9773506ab131422433c830e18ab044f0c7d3e0b0 (diff)
downloadmicropython-1831034be13fef5344583c557ff089df31788251.tar.gz
micropython-1831034be13fef5344583c557ff089df31788251.zip
py: Allow lexer to raise exceptions during construction.
This patch refactors the error handling in the lexer, to simplify it (ie reduce code size). A long time ago, when the lexer/parser/compiler were first written, the lexer and parser were designed so they didn't use exceptions (ie nlr) to report errors but rather returned an error code. Over time that has gradually changed, the parser in particular has more and more ways of raising exceptions. Also, the lexer never really handled all errors without raising, eg there were some memory errors which could raise an exception (and in these rare cases one would get a fatal nlr-not-handled fault). This patch accepts the fact that the lexer can raise exceptions in some cases and allows it to raise exceptions to handle all its errors, which are for the most part just out-of-memory errors during construction of the lexer. This makes the lexer a bit simpler, and also the persistent code stuff is simplified. What this means for users of the lexer is that calls to it must be wrapped in a nlr handler. But all uses of the lexer already have such an nlr handler for the parser (and compiler) so that doesn't put any extra burden on the callers.
Diffstat (limited to 'py/builtinimport.c')
-rw-r--r--py/builtinimport.c17
1 files changed, 3 insertions, 14 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 0a917c6f83..96846b9636 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -131,18 +131,7 @@ STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *d
}
#if MICROPY_ENABLE_COMPILER
-STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char *fname) {
-
- if (lex == NULL) {
- // we verified the file exists using stat, but lexer could still fail
- if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
- mp_raise_msg(&mp_type_ImportError, "module not found");
- } else {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
- "no module named '%s'", fname));
- }
- }
-
+STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) {
#if MICROPY_PY___FILE__
qstr source_name = lex->source_name;
mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
@@ -207,7 +196,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
// found the filename in the list of frozen files, then load and execute it.
#if MICROPY_MODULE_FROZEN_STR
if (frozen_type == MP_FROZEN_STR) {
- do_load_from_lexer(module_obj, modref, file_str);
+ do_load_from_lexer(module_obj, modref);
return;
}
#endif
@@ -235,7 +224,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
#if MICROPY_ENABLE_COMPILER
{
mp_lexer_t *lex = mp_lexer_new_from_file(file_str);
- do_load_from_lexer(module_obj, lex, file_str);
+ do_load_from_lexer(module_obj, lex);
return;
}
#endif