diff options
author | Damien George <damien.p.george@gmail.com> | 2017-03-14 11:16:31 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-03-14 11:52:05 +1100 |
commit | 1831034be13fef5344583c557ff089df31788251 (patch) | |
tree | 391459a654f8b48a5d21af14dc7b9c677ed9af51 /py/persistentcode.c | |
parent | 9773506ab131422433c830e18ab044f0c7d3e0b0 (diff) | |
download | micropython-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/persistentcode.c')
-rw-r--r-- | py/persistentcode.c | 9 |
1 files changed, 2 insertions, 7 deletions
diff --git a/py/persistentcode.c b/py/persistentcode.c index 5cb5117093..2a9a5b7cc0 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -225,18 +225,13 @@ mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) { mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len) { mp_reader_t reader; - if (!mp_reader_new_mem(&reader, buf, len, 0)) { - m_malloc_fail(BYTES_PER_WORD); // we need to raise a MemoryError - } + mp_reader_new_mem(&reader, buf, len, 0); return mp_raw_code_load(&reader); } mp_raw_code_t *mp_raw_code_load_file(const char *filename) { mp_reader_t reader; - int ret = mp_reader_new_file(&reader, filename); - if (ret != 0) { - mp_raise_OSError(ret); - } + mp_reader_new_file(&reader, filename); return mp_raw_code_load(&reader); } |