Skip to content

Commit c06fc8e

Browse files
committed
Introduce, use mp_raise_arg1
This raises an exception with a given object value. Saves a bit of code size.
1 parent d5f6748 commit c06fc8e

File tree

10 files changed

+21
-16
lines changed

10 files changed

+21
-16
lines changed

extmod/modure.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) {
4343
mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in);
4444
mp_int_t no = mp_obj_get_int(no_in);
4545
if (no < 0 || no >= self->num_matches) {
46-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, no_in));
46+
mp_raise_arg1(&mp_type_IndexError, no_in);
4747
}
4848

4949
const char *start = self->caps[no * 2];
@@ -82,7 +82,7 @@ STATIC void match_span_helper(size_t n_args, const mp_obj_t *args, mp_obj_t span
8282
if (n_args == 2) {
8383
no = mp_obj_get_int(args[1]);
8484
if (no < 0 || no >= self->num_matches) {
85-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, args[1]));
85+
mp_raise_arg1(&mp_type_IndexError, args[1]);
8686
}
8787
}
8888

@@ -326,7 +326,7 @@ STATIC mp_obj_t re_sub_helper(mp_obj_t self_in, size_t n_args, const mp_obj_t *a
326326
}
327327

328328
if (match_no >= (unsigned int)match->num_matches) {
329-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, MP_OBJ_NEW_SMALL_INT(match_no)));
329+
mp_raise_arg1(&mp_type_IndexError, MP_OBJ_NEW_SMALL_INT(match_no));
330330
}
331331

332332
const char *start_match = match->caps[match_no * 2];

extmod/moduzlib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ STATIC mp_obj_t mod_uzlib_decompress(size_t n_args, const mp_obj_t *args) {
179179
return res;
180180

181181
error:
182-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_ValueError, MP_OBJ_NEW_SMALL_INT(st)));
182+
mp_raise_arg1(&mp_type_ValueError, MP_OBJ_NEW_SMALL_INT(st));
183183
}
184184
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_uzlib_decompress_obj, 1, 3, mod_uzlib_decompress);
185185

ports/unix/modjni.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ STATIC void check_exception(void) {
102102
mp_obj_t py_e = new_jobject(exc);
103103
JJ1(ExceptionClear);
104104
if (JJ(IsInstanceOf, exc, IndexException_class)) {
105-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, py_e));
105+
mp_raise_arg1(&mp_type_IndexError, py_e);
106106
}
107-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_Exception, py_e));
107+
mp_raise_arg1(&mp_type_Exception, py_e);
108108
}
109109
}
110110

py/modsys.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
9696
if (n_args == 0) {
9797
exc = mp_obj_new_exception(&mp_type_SystemExit);
9898
} else {
99-
exc = mp_obj_new_exception_arg1(&mp_type_SystemExit, args[0]);
99+
mp_raise_arg1(&mp_type_SystemExit, args[0]);
100100
}
101101
nlr_raise(exc);
102102
}

py/objdict.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
169169
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
170170
mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
171171
if (elem == NULL) {
172-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, index));
172+
mp_raise_arg1(&mp_type_KeyError, index);
173173
} else {
174174
return elem->value;
175175
}
@@ -185,7 +185,7 @@ STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
185185
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
186186
mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
187187
if (elem == NULL) {
188-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, index));
188+
mp_raise_arg1(&mp_type_KeyError, index);
189189
} else {
190190
return elem->value;
191191
}
@@ -272,7 +272,7 @@ STATIC mp_obj_t dict_get_helper(size_t n_args, const mp_obj_t *args, mp_map_look
272272
if (elem == NULL || elem->value == MP_OBJ_NULL) {
273273
if (n_args == 2) {
274274
if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
275-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, args[1]));
275+
mp_raise_arg1(&mp_type_KeyError, args[1]);
276276
} else {
277277
value = mp_const_none;
278278
}

py/objset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
378378
check_set(self_in);
379379
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
380380
if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
381-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, item));
381+
mp_raise_arg1(&mp_type_KeyError, item);
382382
}
383383
return mp_const_none;
384384
}

py/objstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
10871087
field_name = lookup;
10881088
mp_map_elem_t *key_elem = mp_map_lookup(kwargs, field_q, MP_MAP_LOOKUP);
10891089
if (key_elem == NULL) {
1090-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, field_q));
1090+
mp_raise_arg1(&mp_type_KeyError, field_q);
10911091
}
10921092
arg = key_elem->value;
10931093
}

py/pystack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ void *mp_pystack_alloc(size_t n_bytes) {
4343
#endif
4444
if (MP_STATE_THREAD(pystack_cur) + n_bytes > MP_STATE_THREAD(pystack_end)) {
4545
// out of memory in the pystack
46-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_RuntimeError,
47-
MP_OBJ_NEW_QSTR(MP_QSTR_pystack_space_exhausted)));
46+
mp_raise_arg1(&mp_type_RuntimeError,
47+
MP_OBJ_NEW_QSTR(MP_QSTR_pystack_space_exhausted));
4848
}
4949
void *ptr = MP_STATE_THREAD(pystack_cur);
5050
MP_STATE_THREAD(pystack_cur) += n_bytes;

py/runtime.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,10 @@ NORETURN void m_malloc_fail(size_t num_bytes) {
15141514
translate("memory allocation failed, allocating %u bytes"), (uint)num_bytes);
15151515
}
15161516

1517+
NORETURN void mp_raise_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
1518+
nlr_raise(mp_obj_new_exception_arg1(exc_type, arg));
1519+
}
1520+
15171521
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const compressed_string_t *msg) {
15181522
if (msg == NULL) {
15191523
nlr_raise(mp_obj_new_exception(exc_type));
@@ -1580,7 +1584,7 @@ NORETURN void mp_raise_TypeError_varg(const compressed_string_t *fmt, ...) {
15801584
}
15811585

15821586
NORETURN void mp_raise_OSError(int errno_) {
1583-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_)));
1587+
mp_raise_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_));
15841588
}
15851589

15861590
NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg) {
@@ -1607,7 +1611,7 @@ NORETURN void mp_raise_ConnectionError(const compressed_string_t *msg) {
16071611
}
16081612

16091613
NORETURN void mp_raise_BrokenPipeError(void) {
1610-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_BrokenPipeError, MP_OBJ_NEW_SMALL_INT(MP_EPIPE)));
1614+
mp_raise_arg1(&mp_type_BrokenPipeError, MP_OBJ_NEW_SMALL_INT(MP_EPIPE));
16111615
}
16121616

16131617
NORETURN void mp_raise_NotImplementedError(const compressed_string_t *msg) {

py/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
150150
mp_obj_t mp_import_from(mp_obj_t module, qstr name);
151151
void mp_import_all(mp_obj_t module);
152152

153+
NORETURN void mp_raise_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
153154
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const compressed_string_t *msg);
154155
NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, const compressed_string_t *fmt, ...);
155156
NORETURN void mp_raise_msg_vlist(const mp_obj_type_t *exc_type, const compressed_string_t *fmt, va_list argptr);

0 commit comments

Comments
 (0)