8000 py/dynruntime: Add mp_obj_exception_init function to create C exception. · micropython/micropython@482292c · GitHub
[go: up one dir, main page]

Skip to content

Commit 482292c

Browse files
committed
py/dynruntime: Add mp_obj_exception_init function to create C exception.
Signed-off-by: Damien George <damien@micropython.org>
1 parent a919ce2 commit 482292c

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

py/dynruntime.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) {
9090
#define mp_type_bytes (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_bytes)))
9191
#define mp_type_tuple (*((mp_obj_base_t *)mp_const_empty_tuple)->type)
9292
#define mp_type_list (*mp_fun_table.type_list)
93+
#define mp_type_Exception (*mp_fun_table.type_Exception)
9394
#define mp_type_EOFError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_EOFError)))
9495
#define mp_type_IndexError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_IndexError)))
9596
#define mp_type_KeyError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_KeyError)))
@@ -236,6 +237,10 @@ static inline void *mp_obj_malloc_helper_dyn(size_t num_bytes, const mp_obj_type
236237
/******************************************************************************/
237238
// Exceptions
238239

240+
#define mp_obj_exception_make_new (MP_OBJ_TYPE_GET_SLOT(&mp_type_Exception, make_new))
241+
#define mp_obj_exception_print (MP_OBJ_TYPE_GET_SLOT(&mp_type_Exception, print))
242+
#define mp_obj_exception_attr (MP_OBJ_TYPE_GET_SLOT(&mp_type_Exception, attr))
243+
239244
#define mp_obj_new_exception(o) ((mp_obj_t)(o)) // Assumes returned object will be raised, will create instance then
240245
#define mp_obj_new_exception_arg1(e_type, arg) (mp_obj_new_exception_arg1_dyn((e_type), (arg)))
241246

@@ -263,6 +268,16 @@ static inline void mp_raise_OSError_dyn(int er) {
263268
nlr_raise(mp_call_function_n_kw(mp_load_global(MP_QSTR_OSError), 1, 0, &args[0]));
264269
}
265270

271+
static inline void mp_obj_exception_init(mp_obj_full_type_t *exc, qstr name, const mp_obj_type_t *base) {
272+
exc->base.type = &mp_type_type;
273+
exc->flags = MP_TYPE_FLAG_NONE;
274+
exc->name = name;
275+
MP_OBJ_TYPE_SET_SLOT(exc, make_new, mp_obj_exception_make_new, 0);
276+
MP_OBJ_TYPE_SET_SLOT(exc, print, mp_obj_exception_print, 1);
277+
MP_OBJ_TYPE_SET_SLOT(exc, attr, mp_obj_exception_attr, 2);
278+
MP_OBJ_TYPE_SET_SLOT(exc, parent, base, 3);
279+
}
280+
266281
/******************************************************************************/
267282
// Floating point
268283

py/nativeglue.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ const mp_fun_table_t mp_fun_table = {
344344
&mp_type_fun_builtin_2,
345345
&mp_type_fun_builtin_3,
346346
&mp_type_fun_builtin_var,
347+
&mp_type_Exception,
347348
&mp_stream_read_obj,
348349
&mp_stream_readinto_obj,
349350
&mp_stream_unbuffered_readline_obj,

py/nativeglue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ typedef struct _mp_fun_table_t {
171171
const mp_obj_type_t *type_fun_builtin_2;
172172
const mp_obj_type_t *type_fun_builtin_3;
173173
const mp_obj_type_t *type_fun_builtin_var;
174+
const mp_obj_type_t *type_Exception;
174175
const mp_obj_fun_builtin_var_t *stream_read_obj;
175176
const mp_obj_fun_builtin_var_t *stream_readinto_obj;
176177
const mp_obj_fun_builtin_var_t *stream_unbuffered_readline_obj;

tools/mpy_ld.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ def link_objects(env, native_qstr_vals_len):
767767
"mp_type_fun_builtin_2",
768768
"mp_type_fun_builtin_3",
769769
"mp_type_fun_builtin_var",
770+
"mp_type_Exception",
770771
"mp_stream_read_obj",
771772
"mp_stream_readinto_obj",
772773
"mp_stream_unbuffered_readline_obj",

0 commit comments

Comments
 (0)
0