Commit 22a0865d authored by Damien George's avatar Damien George
Browse files

py: Improve exception bases, reduces ROM usage.

Thanks to @pfalcon for the tip!
parent 8725f8f7
...@@ -66,30 +66,34 @@ const mp_obj_type_t mp_type_BaseException = { ...@@ -66,30 +66,34 @@ const mp_obj_type_t mp_type_BaseException = {
.make_new = mp_obj_exception_make_new, .make_new = mp_obj_exception_make_new,
}; };
#define MP_DEFINE_EXCEPTION(exc_name) \ #define MP_DEFINE_EXCEPTION_BASE(base_name) \
STATIC const mp_obj_tuple_t mp_type_ ## exc_name ## _bases_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_BaseException}};\ STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
const mp_obj_type_t mp_type_ ## exc_name = { \ const mp_obj_type_t mp_type_ ## exc_name = { \
{ &mp_type_type }, \ { &mp_type_type }, \
.name = MP_QSTR_ ## exc_name, \ .name = MP_QSTR_ ## exc_name, \
.print = mp_obj_exception_print, \ .print = mp_obj_exception_print, \
.make_new = mp_obj_exception_make_new, \ .make_new = mp_obj_exception_make_new, \
.bases_tuple = (mp_obj_t)&mp_type_ ## exc_name ## _bases_tuple, \ .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
}; };
MP_DEFINE_EXCEPTION(AssertionError) MP_DEFINE_EXCEPTION_BASE(BaseException)
MP_DEFINE_EXCEPTION(AttributeError)
MP_DEFINE_EXCEPTION(ImportError) MP_DEFINE_EXCEPTION(AssertionError, BaseException)
MP_DEFINE_EXCEPTION(IndentationError) MP_DEFINE_EXCEPTION(AttributeError, BaseException)
MP_DEFINE_EXCEPTION(IndexError) MP_DEFINE_EXCEPTION(ImportError, BaseException)
MP_DEFINE_EXCEPTION(KeyError) MP_DEFINE_EXCEPTION(IndentationError, BaseException)
MP_DEFINE_EXCEPTION(NameError) MP_DEFINE_EXCEPTION(IndexError, BaseException)
MP_DEFINE_EXCEPTION(SyntaxError) MP_DEFINE_EXCEPTION(KeyError, BaseException)
MP_DEFINE_EXCEPTION(TypeError) MP_DEFINE_EXCEPTION(NameError, BaseException)
MP_DEFINE_EXCEPTION(ValueError) MP_DEFINE_EXCEPTION(SyntaxError, BaseException)
MP_DEFINE_EXCEPTION(OverflowError) MP_DEFINE_EXCEPTION(TypeError, BaseException)
MP_DEFINE_EXCEPTION(OSError) MP_DEFINE_EXCEPTION(ValueError, BaseException)
MP_DEFINE_EXCEPTION(NotImplementedError) MP_DEFINE_EXCEPTION(OverflowError, BaseException)
MP_DEFINE_EXCEPTION(StopIteration) MP_DEFINE_EXCEPTION(OSError, BaseException)
MP_DEFINE_EXCEPTION(NotImplementedError, BaseException)
MP_DEFINE_EXCEPTION(StopIteration, BaseException)
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) { mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
return mp_obj_new_exception_msg_varg(exc_type, NULL); return mp_obj_new_exception_msg_varg(exc_type, NULL);
......
...@@ -1003,6 +1003,8 @@ mp_obj_t rt_make_raise_obj(mp_obj_t o) { ...@@ -1003,6 +1003,8 @@ mp_obj_t rt_make_raise_obj(mp_obj_t o) {
if (mp_obj_is_exception_type(o)) { if (mp_obj_is_exception_type(o)) {
// o is an exception type (it is derived from BaseException (or is BaseException)) // o is an exception type (it is derived from BaseException (or is BaseException))
// create and return a new exception instance by calling o // create and return a new exception instance by calling o
// TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
// could have const instances in ROM which we return here instead
return rt_call_function_n_kw(o, 0, 0, NULL); return rt_call_function_n_kw(o, 0, 0, NULL);
} else if (mp_obj_is_exception_instance(o)) { } else if (mp_obj_is_exception_instance(o)) {
// o is an instance of an exception, so use it as the exception // o is an instance of an exception, so use it as the exception
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment