Skip to content

Commit 9e1b61d

Browse files
committed
py/runtime: Factor out exception raising helpers.
Introduce mp_raise_msg(), mp_raise_ValueError(), mp_raise_TypeError() instead of previous pattern nlr_raise(mp_obj_new_exception_msg(...)). Save few bytes on each call, which are many.
1 parent af9889f commit 9e1b61d

File tree

3 files changed

+53
-46
lines changed

3 files changed

+53
-46
lines changed

py/objstr.c

+36-45
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size
231231
mp_int_t val = mp_obj_get_int(item);
232232
#if MICROPY_CPYTHON_COMPAT
233233
if (val < 0 || val > 255) {
234-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bytes value out of range"));
234+
mp_raise_ValueError("bytes value out of range");
235235
}
236236
#endif
237237
vstr_add_byte(&vstr, val);
@@ -240,7 +240,7 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size
240240
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
241241

242242
wrong_args:
243-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "wrong number of arguments"));
243+
mp_raise_TypeError("wrong number of arguments");
244244
}
245245

246246
// like strstr but with specified length and allows \0 bytes
@@ -436,8 +436,8 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
436436
mp_uint_t required_len = 0;
437437
for (mp_uint_t i = 0; i < seq_len; i++) {
438438
if (mp_obj_get_type(seq_items[i]) != self_type) {
439-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
440-
"join expects a list of str/bytes objects consistent with self object"));
439+
mp_raise_msg(&mp_type_TypeError,
440+
"join expects a list of str/bytes objects consistent with self object");
441441
}
442442
if (i > 0) {
443443
required_len += sep_len;
@@ -511,7 +511,7 @@ mp_obj_t mp_obj_str_split(size_t n_args, const mp_obj_t *args) {
511511
const char *sep_str = mp_obj_str_get_data(sep, &sep_len);
512512

513513
if (sep_len == 0) {
514-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
514+
mp_raise_ValueError("empty separator");
515515
}
516516

517517
for (;;) {
@@ -609,7 +609,7 @@ STATIC mp_obj_t str_rsplit(size_t n_args, const mp_obj_t *args) {
609609
const char *sep_str = mp_obj_str_get_data(sep, &sep_len);
610610

611611
if (sep_len == 0) {
612-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
612+
mp_raise_ValueError("empty separator");
613613
}
614614

615615
const byte *beg = s;
@@ -672,7 +672,7 @@ STATIC mp_obj_t str_finder(mp_uint_t n_args, const mp_obj_t *args, mp_int_t dire
672672
if (p == NULL) {
673673
// not found
674674
if (is_index) {
675-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "substring not found"));
675+
mp_raise_ValueError("substring not found");
676676
} else {
677677
return MP_OBJ_NEW_SMALL_INT(-1);
678678
}
@@ -878,7 +878,7 @@ STATIC mp_obj_t arg_as_int(mp_obj_t arg) {
878878
}
879879

880880
STATIC NORETURN void terse_str_format_value_error(void) {
881-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad format string"));
881+
mp_raise_ValueError("bad format string");
882882
}
883883

884884
STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *arg_i, mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
@@ -896,8 +896,8 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
896896
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
897897
terse_str_format_value_error();
898898
} else {
899-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
900-
"single '}' encountered in format string"));
899+
mp_raise_msg(&mp_type_ValueError,
900+
"single '}' encountered in format string");
901901
}
902902
}
903903
if (*str != '{') {
@@ -936,12 +936,11 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
936936
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
937937
terse_str_format_value_error();
938938
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
939-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
940-
"bad conversion specifier"));
939+
mp_raise_ValueError("bad conversion specifier");
941940
} else {
942941
if (str >= top) {
943-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
944-
"end of format while looking for conversion specifier"));
942+
mp_raise_msg(&mp_type_ValueError,
943+
"end of format while looking for conversion specifier");
945944
} else {
946945
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
947946
"unknown conversion specifier %c", *str));
@@ -975,16 +974,14 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
975974
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
976975
terse_str_format_value_error();
977976
} else {
978-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
979-
"unmatched '{' in format"));
977+
mp_raise_ValueError("unmatched '{' in format");
980978
}
981979
}
982980
if (*str != '}') {
983981
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
984982
terse_str_format_value_error();
985983
} else {
986-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
987-
"expected ':' after format specifier"));
984+
mp_raise_ValueError("expected ':' after format specifier");
988985
}
989986
}
990987

@@ -997,13 +994,13 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
997994
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
998995
terse_str_format_value_error();
999996
} else {
1000-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
1001-
"can't switch from automatic field numbering to manual field specification"));
997+
mp_raise_msg(&mp_type_ValueError,
998+
"can't switch from automatic field numbering to manual field specification");
1002999
}
10031000
}
10041001
field_name = str_to_int(field_name, field_name_top, &index);
10051002
if ((uint)index >= n_args - 1) {
1006-
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "tuple index out of range"));
1003+
mp_raise_msg(&mp_type_IndexError, "tuple index out of range");
10071004
}
10081005
arg = args[index + 1];
10091006
*arg_i = -1;
@@ -1026,12 +1023,12 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
10261023
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
10271024
terse_str_format_value_error();
10281025
} else {
1029-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
1030-
"can't switch from manual field specification to automatic field numbering"));
1026+
mp_raise_msg(&mp_type_ValueError,
1027+
"can't switch from manual field specification to automatic field numbering");
10311028
}
10321029
}
10331030
if ((uint)*arg_i >= n_args - 1) {
1034-
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "tuple index out of range"));
1031+
mp_raise_msg(&mp_type_IndexError, "tuple index out of range");
10351032
}
10361033
arg = args[(*arg_i) + 1];
10371034
(*arg_i)++;
@@ -1120,8 +1117,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
11201117
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
11211118
terse_str_format_value_error();
11221119
} else {
1123-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
1124-
"invalid format specifier"));
1120+
mp_raise_ValueError("invalid format specifier");
11251121
}
11261122
}
11271123
vstr_clear(&format_spec_vstr);
@@ -1142,16 +1138,16 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
11421138
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
11431139
terse_str_format_value_error();
11441140
} else {
1145-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
1146-
"sign not allowed in string format specifier"));
1141+
mp_raise_msg(&mp_type_ValueError,
1142+
"sign not allowed in string format specifier");
11471143
}
11481144
}
11491145
if (type == 'c') {
11501146
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
11511147
terse_str_format_value_error();
11521148
} else {
1153-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
1154-
"sign not allowed with integer format specifier 'c'"));
1149+
mp_raise_msg(&mp_type_ValueError,
1150+
"sign not allowed with integer format specifier 'c'");
11551151
}
11561152
}
11571153
} else {
@@ -1295,8 +1291,8 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
12951291
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
12961292
terse_str_format_value_error();
12971293
} else {
1298-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
1299-
"'=' alignment not allowed in string format specifier"));
1294+
mp_raise_msg(&mp_type_ValueError,
1295+
"'=' alignment not allowed in string format specifier");
13001296
}
13011297
}
13021298

@@ -1372,8 +1368,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
13721368
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
13731369
terse_str_format_value_error();
13741370
} else {
1375-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
1376-
"incomplete format key"));
1371+
mp_raise_ValueError("incomplete format key");
13771372
}
13781373
}
13791374
++str;
@@ -1431,16 +1426,15 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
14311426
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
14321427
terse_str_format_value_error();
14331428
} else {
1434-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
1435-
"incomplete format"));
1429+
mp_raise_ValueError("incomplete format");
14361430
}
14371431
}
14381432

14391433
// Tuple value lookup
14401434
if (arg == MP_OBJ_NULL) {
14411435
if ((uint)arg_i >= n_args) {
14421436
not_enough_args:
1443-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "not enough arguments for format string"));
1437+
mp_raise_TypeError("not enough arguments for format string");
14441438
}
14451439
arg = args[arg_i++];
14461440
}
@@ -1450,16 +1444,14 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
14501444
mp_uint_t slen;
14511445
const char *s = mp_obj_str_get_data(arg, &slen);
14521446
if (slen != 1) {
1453-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
1454-
"%%c requires int or char"));
1447+
mp_raise_TypeError("%%c requires int or char");
14551448
}
14561449
mp_print_strn(&print, s, 1, flags, ' ', width);
14571450
} else if (arg_looks_integer(arg)) {
14581451
char ch = mp_obj_get_int(arg);
14591452
mp_print_strn(&print, &ch, 1, flags, ' ', width);
14601453
} else {
1461-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
1462-
"integer required"));
1454+
mp_raise_TypeError("integer required");
14631455
}
14641456
break;
14651457

@@ -1529,7 +1521,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
15291521
}
15301522

15311523
if ((uint)arg_i != n_args) {
1532-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "not all arguments converted during string formatting"));
1524+
mp_raise_TypeError("not all arguments converted during string formatting");
15331525
}
15341526

15351527
return mp_obj_new_str_from_vstr(is_bytes ? &mp_type_bytes : &mp_type_str, &vstr);
@@ -1695,7 +1687,7 @@ STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, mp_int_t directi
16951687
GET_STR_DATA_LEN(arg, sep, sep_len);
16961688

16971689
if (sep_len == 0) {
1698-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
1690+
mp_raise_ValueError("empty separator");
16991691
}
17001692

17011693
mp_obj_t result[3];
@@ -2061,8 +2053,7 @@ bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
20612053

20622054
STATIC void bad_implicit_conversion(mp_obj_t self_in) {
20632055
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
2064-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
2065-
"can't convert to str implicitly"));
2056+
mp_raise_TypeError("can't convert to str implicitly");
20662057
} else {
20672058
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
20682059
"can't convert '%s' object to str implicitly",

py/runtime.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,18 @@ void *m_malloc_fail(size_t num_bytes) {
13901390
}
13911391
}
13921392

1393+
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg) {
1394+
nlr_raise(mp_obj_new_exception_msg(exc_type, msg));
1395+
}
1396+
1397+
NORETURN void mp_raise_ValueError(const char *msg) {
1398+
mp_raise_msg(&mp_type_ValueError, msg);
1399+
}
1400+
1401+
NORETURN void mp_raise_TypeError(const char *msg) {
1402+
mp_raise_msg(&mp_type_TypeError, msg);
1403+
}
1404+
13931405
NORETURN void mp_not_implemented(const char *msg) {
1394-
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, msg));
1406+
mp_raise_msg(&mp_type_NotImplementedError, msg);
13951407
}

py/runtime.h

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ mp_obj_t mp_import_from(mp_obj_t module, qstr name);
134134
void mp_import_all(mp_obj_t module);
135135

136136
// Raise NotImplementedError with given message
137+
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg);
138+
//NORETURN void nlr_raise_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...);
139+
NORETURN void mp_raise_ValueError(const char *msg);
140+
NORETURN void mp_raise_TypeError(const char *msg);
137141
NORETURN void mp_not_implemented(const char *msg);
138142
NORETURN void mp_exc_recursion_depth(void);
139143

0 commit comments

Comments
 (0)