Skip to content

Commit 97eca38

Browse files
committed
py: Add mp_raise_type helper macro and use it where appropriate.
This provides a more consistent C-level API to raise exceptions, ie moving away from nlr_raise towards mp_raise_XXX. It also reduces code size by a small amount on some ports.
1 parent 7679e3b commit 97eca38

12 files changed

Lines changed: 17 additions & 16 deletions

File tree

extmod/modbtree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
256256
key.data = (void*)mp_obj_str_get_data(index, &key.size);
257257
int res = __bt_delete(self->db, &key, 0);
258258
if (res == RET_SPECIAL) {
259-
nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
259+
mp_raise_type(&mp_type_KeyError);
260260
}
261261
CHECK_ERROR(res);
262262
return mp_const_none;
@@ -266,7 +266,7 @@ STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
266266
key.data = (void*)mp_obj_str_get_data(index, &key.size);
267267
int res = __bt_get(self->db, &key, &val, 0);
268268
if (res == RET_SPECIAL) {
269-
nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
269+
mp_raise_type(&mp_type_KeyError);
270270
}
271271
CHECK_ERROR(res);
272272
return mp_obj_new_bytes(val.data, val.size);

extmod/modurandom.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ STATIC mp_obj_t mod_urandom_choice(mp_obj_t seq) {
154154
if (len > 0) {
155155
return mp_obj_subscr(seq, mp_obj_new_int(yasmarang_randbelow(len)), MP_OBJ_SENTINEL);
156156
} else {
157-
nlr_raise(mp_obj_new_exception(&mp_type_IndexError));
157+
mp_raise_type(&mp_type_IndexError);
158158
}
159159
}
160160
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_choice_obj, mod_urandom_choice);

extmod/moduzlib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ STATIC int read_src_stream(TINF_DATA *data) {
6161
mp_raise_OSError(err);
6262
}
6363
if (out_sz == 0) {
64-
nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
64+
mp_raise_type(&mp_type_EOFError);
6565
}
6666
return c;
6767
}

ports/cc3200/mods/pybsleep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ void pyb_sleep_suspend_exit (void) {
477477
MAP_IntPendSet(INT_PRCM);
478478

479479
// force an exception to go back to the point where suspend mode was entered
480-
nlr_raise(mp_obj_new_exception(&mp_type_SystemExit));
480+
mp_raise_type(&mp_type_SystemExit);
481481
}
482482

483483
STATIC void PRCMInterruptHandler (void) {

ports/esp32/modmachine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
196196

197197
STATIC mp_obj_t machine_soft_reset(void) {
198198
pyexec_system_exit = PYEXEC_FORCED_EXIT;
199-
nlr_raise(mp_obj_new_exception(&mp_type_SystemExit));
199+
mp_raise_type(&mp_type_SystemExit);
200200
}
201201
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
202202

ports/nrf/modules/machine/modmachine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
147147

148148
STATIC mp_obj_t machine_soft_reset(void) {
149149
pyexec_system_exit = PYEXEC_FORCED_EXIT;
150-
nlr_raise(mp_obj_new_exception(&mp_type_SystemExit));
150+
mp_raise_type(&mp_type_SystemExit);
151151
}
152152
MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
153153

ports/nrf/modules/random/modrandom.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ STATIC mp_obj_t mod_random_choice(mp_obj_t seq) {
155155
if (len > 0) {
156156
return mp_obj_subscr(seq, mp_obj_new_int(randbelow(len)), MP_OBJ_SENTINEL);
157157
} else {
158-
nlr_raise(mp_obj_new_exception(&mp_type_IndexError));
158+
mp_raise_type(&mp_type_IndexError);
159159
}
160160
}
161161
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_random_choice_obj, mod_random_choice);

ports/stm32/modmachine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
249249

250250
STATIC mp_obj_t machine_soft_reset(void) {
251251
pyexec_system_exit = PYEXEC_FORCED_EXIT;
252-
nlr_raise(mp_obj_new_exception(&mp_type_SystemExit));
252+
mp_raise_type(&mp_type_SystemExit);
253253
}
254254
MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
255255

py/modbuiltins.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@ STATIC mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) {
244244
vstr_init(&line, 16);
245245
int ret = mp_hal_readline(&line, "");
246246
if (ret == CHAR_CTRL_C) {
247-
nlr_raise(mp_obj_new_exception(&mp_type_KeyboardInterrupt));
247+
mp_raise_type(&mp_type_KeyboardInterrupt);
248248
}
249249
if (line.len == 0 && ret == CHAR_CTRL_D) {
250-
nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
250+
mp_raise_type(&mp_type_EOFError);
251251
}
252252
return mp_obj_new_str_from_vstr(&mp_type_str, &line);
253253
}
@@ -321,7 +321,7 @@ STATIC mp_obj_t mp_builtin_next(size_t n_args, const mp_obj_t *args) {
321321
if (n_args == 1) {
322322
mp_obj_t ret = mp_iternext_allow_raise(args[0]);
323323
if (ret == MP_OBJ_STOP_ITERATION) {
324-
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
324+
mp_raise_type(&mp_type_StopIteration);
325325
} else {
326326
return ret;
327327
}
@@ -335,7 +335,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_next_obj, 1, 2, mp_builtin_next);
335335
STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
336336
mp_obj_t ret = mp_iternext_allow_raise(o);
337337
if (ret == MP_OBJ_STOP_ITERATION) {
338-
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
338+
mp_raise_type(&mp_type_StopIteration);
339339
} else {
340340
return ret;
341341
}

py/modthread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args)
271271
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_start_new_thread_obj, 2, 3, mod_thread_start_new_thread);
272272

273273
STATIC mp_obj_t mod_thread_exit(void) {
274-
nlr_raise(mp_obj_new_exception(&mp_type_SystemExit));
274+
mp_raise_type(&mp_type_SystemExit);
275275
}
276276
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_exit_obj, mod_thread_exit);
277277

0 commit comments

Comments
 (0)