py/objexcept: Make MP_DEFINE_EXCEPTION public so ports can define excs.

This commit is contained in:
Damien George 2018-03-17 00:31:40 +11:00
parent f6a1f18603
commit 5edce4539b
2 changed files with 16 additions and 13 deletions

View File

@ -93,7 +93,7 @@ mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in) {
// definition module-private so far, have it here. // definition module-private so far, have it here.
const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, 0, 0, NULL, (mp_obj_tuple_t*)&mp_const_empty_tuple_obj}; const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, 0, 0, NULL, (mp_obj_tuple_t*)&mp_const_empty_tuple_obj};
STATIC void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_exception_t *o = MP_OBJ_TO_PTR(o_in); mp_obj_exception_t *o = MP_OBJ_TO_PTR(o_in);
mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS; mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
bool is_subclass = kind & PRINT_EXC_SUBCLASS; bool is_subclass = kind & PRINT_EXC_SUBCLASS;
@ -186,7 +186,7 @@ mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
} }
} }
STATIC void exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
mp_obj_exception_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_exception_t *self = MP_OBJ_TO_PTR(self_in);
if (dest[0] != MP_OBJ_NULL) { if (dest[0] != MP_OBJ_NULL) {
// store/delete attribute // store/delete attribute
@ -214,17 +214,7 @@ const mp_obj_type_t mp_type_BaseException = {
.name = MP_QSTR_BaseException, .name = MP_QSTR_BaseException,
.print = mp_obj_exception_print, .print = mp_obj_exception_print,
.make_new = mp_obj_exception_make_new, .make_new = mp_obj_exception_make_new,
.attr = exception_attr, .attr = mp_obj_exception_attr,
};
#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
const mp_obj_type_t mp_type_ ## exc_name = { \
{ &mp_type_type }, \
.name = MP_QSTR_ ## exc_name, \
.print = mp_obj_exception_print, \
.make_new = mp_obj_exception_make_new, \
.attr = exception_attr, \
.parent = &mp_type_ ## base_name, \
}; };
// List of all exceptions, arranged as in the table at: // List of all exceptions, arranged as in the table at:

View File

@ -37,4 +37,17 @@ typedef struct _mp_obj_exception_t {
mp_obj_tuple_t *args; mp_obj_tuple_t *args;
} mp_obj_exception_t; } mp_obj_exception_t;
void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
const mp_obj_type_t mp_type_ ## exc_name = { \
{ &mp_type_type }, \
.name = MP_QSTR_ ## exc_name, \
.print = mp_obj_exception_print, \
.make_new = mp_obj_exception_make_new, \
.attr = mp_obj_exception_attr, \
.parent = &mp_type_ ## base_name, \
};
#endif // MICROPY_INCLUDED_PY_OBJEXCEPT_H #endif // MICROPY_INCLUDED_PY_OBJEXCEPT_H