Skip to content

Commit 22a0865

Browse files
committed
py: Improve exception bases, reduces ROM usage.
Thanks to @pfalcon for the tip!
1 parent 8725f8f commit 22a0865

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

py/objexcept.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,34 @@ const mp_obj_type_t mp_type_BaseException = {
6666
.make_new = mp_obj_exception_make_new,
6767
};
6868

69-
#define MP_DEFINE_EXCEPTION(exc_name) \
70-
STATIC const mp_obj_tuple_t mp_type_ ## exc_name ## _bases_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_BaseException}};\
69+
#define MP_DEFINE_EXCEPTION_BASE(base_name) \
70+
STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
71+
72+
#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
7173
const mp_obj_type_t mp_type_ ## exc_name = { \
7274
{ &mp_type_type }, \
7375
.name = MP_QSTR_ ## exc_name, \
7476
.print = mp_obj_exception_print, \
7577
.make_new = mp_obj_exception_make_new, \
76-
.bases_tuple = (mp_obj_t)&mp_type_ ## exc_name ## _bases_tuple, \
78+
.bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \
7779
};
7880

79-
MP_DEFINE_EXCEPTION(AssertionError)
80-
MP_DEFINE_EXCEPTION(AttributeError)
81-
MP_DEFINE_EXCEPTION(ImportError)
82-
MP_DEFINE_EXCEPTION(IndentationError)
83-
MP_DEFINE_EXCEPTION(IndexError)
84-
MP_DEFINE_EXCEPTION(KeyError)
85-
MP_DEFINE_EXCEPTION(NameError)
86-
MP_DEFINE_EXCEPTION(SyntaxError)
87-
MP_DEFINE_EXCEPTION(TypeError)
88-
MP_DEFINE_EXCEPTION(ValueError)
89-
MP_DEFINE_EXCEPTION(OverflowError)
90-
MP_DEFINE_EXCEPTION(OSError)
91-
MP_DEFINE_EXCEPTION(NotImplementedError)
92-
MP_DEFINE_EXCEPTION(StopIteration)
81+
MP_DEFINE_EXCEPTION_BASE(BaseException)
82+
83+
MP_DEFINE_EXCEPTION(AssertionError, BaseException)
84+
MP_DEFINE_EXCEPTION(AttributeError, BaseException)
85+
MP_DEFINE_EXCEPTION(ImportError, BaseException)
86+
MP_DEFINE_EXCEPTION(IndentationError, BaseException)
87+
MP_DEFINE_EXCEPTION(IndexError, BaseException)
88+
MP_DEFINE_EXCEPTION(KeyError, BaseException)
89+
MP_DEFINE_EXCEPTION(NameError, BaseException)
90+
MP_DEFINE_EXCEPTION(SyntaxError, BaseException)
91+
MP_DEFINE_EXCEPTION(TypeError, BaseException)
92+
MP_DEFINE_EXCEPTION(ValueError, BaseException)
93+
MP_DEFINE_EXCEPTION(OverflowError, BaseException)
94+
MP_DEFINE_EXCEPTION(OSError, BaseException)
95+
MP_DEFINE_EXCEPTION(NotImplementedError, BaseException)
96+
MP_DEFINE_EXCEPTION(StopIteration, BaseException)
9397

9498
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
9599
return mp_obj_new_exception_msg_varg(exc_type, NULL);

py/runtime.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,8 @@ mp_obj_t rt_make_raise_obj(mp_obj_t o) {
10031003
if (mp_obj_is_exception_type(o)) {
10041004
// o is an exception type (it is derived from BaseException (or is BaseException))
10051005
// create and return a new exception instance by calling o
1006+
// TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
1007+
// could have const instances in ROM which we return here instead
10061008
return rt_call_function_n_kw(o, 0, 0, NULL);
10071009
} else if (mp_obj_is_exception_instance(o)) {
10081010
// o is an instance of an exception, so use it as the exception

0 commit comments

Comments
 (0)