Skip to content

Commit 35a1fea

Browse files
committed
all: Raise exceptions via mp_raise_XXX
- Changed: ValueError, TypeError, NotImplementedError - OSError invocations unchanged, because the corresponding utility function takes ints, not strings like the long form invocation. - OverflowError, IndexError and RuntimeError etc. not changed for now until we decide whether to add new utility functions.
1 parent b6a3289 commit 35a1fea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+86
-95
lines changed

drivers/dht/dht.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
4040
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
4141

4242
if (bufinfo.len < 5) {
43-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "buffer too small"));
43+
mp_raise_ValueError("buffer too small");
4444
}
4545

4646
// issue start command

esp8266/machine_hspi.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,13 @@ STATIC void machine_hspi_init(mp_obj_base_t *self_in, size_t n_args, const mp_ob
122122
spi_init_gpio(HSPI, SPI_CLK_80MHZ_NODIV);
123123
spi_clock(HSPI, 0, 0);
124124
} else if (self->baudrate > 40000000L) {
125-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
126-
"impossible baudrate"));
125+
mp_raise_ValueError("impossible baudrate");
127126
} else {
128127
uint32_t divider = 40000000L / self->baudrate;
129128
uint16_t prediv = MIN(divider, SPI_CLKDIV_PRE + 1);
130129
uint16_t cntdiv = (divider / prediv) * 2; // cntdiv has to be even
131130
if (cntdiv > SPI_CLKCNT_N + 1 || cntdiv == 0 || prediv == 0) {
132-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
133-
"impossible baudrate"));
131+
mp_raise_ValueError("impossible baudrate");
134132
}
135133
self->baudrate = 80000000L / (prediv * cntdiv);
136134
spi_init_gpio(HSPI, SPI_CLK_USE_DIV);

esp8266/machine_pin.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void pin_intr_handler(uint32_t status) {
125125

126126
pyb_pin_obj_t *mp_obj_get_pin_obj(mp_obj_t pin_in) {
127127
if (mp_obj_get_type(pin_in) != &pyb_pin_type) {
128-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "expecting a pin"));
128+
mp_raise_ValueError("expecting a pin");
129129
}
130130
pyb_pin_obj_t *self = pin_in;
131131
return self;
@@ -280,7 +280,7 @@ STATIC mp_obj_t pyb_pin_obj_init_helper(pyb_pin_obj_t *self, mp_uint_t n_args, c
280280
// only pull-down seems to be supported by the hardware, and
281281
// we only expose pull-up behaviour in software
282282
if (pull != GPIO_PULL_NONE) {
283-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Pin(16) doesn't support pull"));
283+
mp_raise_ValueError("Pin(16) doesn't support pull");
284284
}
285285
} else {
286286
PIN_FUNC_SELECT(self->periph, self->func);
@@ -319,7 +319,7 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
319319
pin = (pyb_pin_obj_t*)&pyb_pin_obj[wanted_pin];
320320
}
321321
if (pin == NULL || pin->base.type == NULL) {
322-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid pin"));
322+
mp_raise_ValueError("invalid pin");
323323
}
324324

325325
if (n_args > 1 || n_kw > 0) {

esp8266/machine_rtc.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ STATIC mp_obj_t pyb_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) {
183183
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
184184

185185
if (bufinfo.len > MEM_USER_MAXLEN) {
186-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
187-
"buffer too long"));
186+
mp_raise_ValueError("buffer too long");
188187
}
189188

190189
len = bufinfo.len;
@@ -208,7 +207,7 @@ STATIC mp_obj_t pyb_rtc_alarm(mp_obj_t self_in, mp_obj_t alarm_id, mp_obj_t time
208207

209208
// check we want alarm0
210209
if (mp_obj_get_int(alarm_id) != 0) {
211-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid alarm"));
210+
mp_raise_ValueError("invalid alarm");
212211
}
213212

214213
// set expiry time (in microseconds)
@@ -245,7 +244,7 @@ STATIC mp_obj_t pyb_rtc_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
245244

246245
// check we want alarm0
247246
if (args[ARG_trigger].u_int != 0) {
248-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid alarm"));
247+
mp_raise_ValueError("invalid alarm");
249248
}
250249

251250
// set the wake value

esp8266/modesp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ STATIC mp_obj_t esp_flash_write(mp_obj_t offset_in, const mp_obj_t buf_in) {
118118
mp_buffer_info_t bufinfo;
119119
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
120120
if (bufinfo.len & 0x3) {
121-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "len must be multiple of 4"));
121+
mp_raise_ValueError("len must be multiple of 4");
122122
}
123123
SpiFlashOpResult res = spi_flash_write(offset, bufinfo.buf, bufinfo.len);
124124
if (res == SPI_FLASH_RESULT_OK) {

esp8266/modmachine.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) {
5959
// set
6060
mp_int_t freq = mp_obj_get_int(args[0]) / 1000000;
6161
if (freq != 80 && freq != 160) {
62-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
63-
"frequency can only be either 80Mhz or 160MHz"));
62+
mp_raise_ValueError("frequency can only be either 80Mhz or 160MHz");
6463
}
6564
system_update_cpu_freq(freq);
6665
return mp_const_none;

esp8266/modnetwork.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig)
275275

276276
STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
277277
if (n_args != 1 && kwargs->used != 0) {
278-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
279-
"either pos or kw args are allowed"));
278+
mp_raise_TypeError("either pos or kw args are allowed");
280279
}
281280

282281
wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
@@ -303,8 +302,7 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
303302
mp_buffer_info_t bufinfo;
304303
mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ);
305304
if (bufinfo.len != 6) {
306-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
307-
"invalid buffer length"));
305+
mp_raise_ValueError("invalid buffer length");
308306
}
309307
wifi_set_macaddr(self->if_id, bufinfo.buf);
310308
break;
@@ -374,8 +372,7 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
374372
// Get config
375373

376374
if (n_args != 2) {
377-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
378-
"can query only one param"));
375+
mp_raise_TypeError("can query only one param");
379376
}
380377

381378
mp_obj_t val;
@@ -422,8 +419,7 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
422419
return val;
423420

424421
unknown:
425-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
426-
"unknown config param"));
422+
mp_raise_ValueError("unknown config param");
427423
}
428424
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp_config_obj, 1, esp_config);
429425

extmod/modlwip.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ STATIC mp_obj_t lwip_slip_make_new(mp_obj_t type_in, size_t n_args, size_t n_kw,
129129

130130
ip_addr_t iplocal, ipremote;
131131
if (!ipaddr_aton(mp_obj_str_get_str(args[1]), &iplocal)) {
132-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "not a valid local IP"));
132+
mp_raise_ValueError("not a valid local IP");
133133
}
134134
if (!ipaddr_aton(mp_obj_str_get_str(args[2]), &ipremote)) {
135-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "not a valid remote IP"));
135+
mp_raise_ValueError("not a valid remote IP");
136136
}
137137

138138
struct netif *n = &lwip_slip_obj.lwip_netif;
139139
if (netif_add(n, &iplocal, IP_ADDR_BROADCAST, &ipremote, NULL, slipif_init, ip_input) == NULL) {
140-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "out of memory"));
140+
mp_raise_ValueError("out of memory");
141141
}
142142
netif_set_up(n);
143143
netif_set_default(n);
@@ -1033,7 +1033,7 @@ STATIC mp_obj_t lwip_socket_sendall(mp_obj_t self_in, mp_obj_t buf_in) {
10331033
break;
10341034
}
10351035
case MOD_NETWORK_SOCK_DGRAM:
1036-
mp_not_implemented("");
1036+
mp_raise_NotImplementedError("");
10371037
break;
10381038
}
10391039

extmod/modubinascii.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ mp_obj_t mod_binascii_a2b_base64(mp_obj_t data) {
109109
mp_buffer_info_t bufinfo;
110110
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
111111
if (bufinfo.len % 4 != 0) {
112-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "incorrect padding"));
112+
mp_raise_ValueError("incorrect padding");
113113
}
114114

115115
vstr_t vstr;
@@ -136,11 +136,11 @@ mp_obj_t mod_binascii_a2b_base64(mp_obj_t data) {
136136
hold[j] = 63;
137137
} else if (in[j] == '=') {
138138
if (j < 2 || i > 4) {
139-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "incorrect padding"));
139+
mp_raise_ValueError("incorrect padding");
140140
}
141141
hold[j] = 64;
142142
} else {
143-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid character"));
143+
mp_raise_ValueError("invalid character");
144144
}
145145
}
146146
in += 4;

extmod/moductypes.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ typedef struct _mp_obj_uctypes_struct_t {
118118
} mp_obj_uctypes_struct_t;
119119

120120
STATIC NORETURN void syntax_error(void) {
121-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "syntax error in uctypes descriptor"));
121+
mp_raise_TypeError("syntax error in uctypes descriptor");
122122
}
123123

124124
STATIC mp_obj_t uctypes_struct_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
@@ -215,7 +215,7 @@ STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, int layout_type, mp_uint_
215215
// but scalar structure field is lowered into native Python int, so all
216216
// type info is lost. So, we cannot say if it's scalar type description,
217217
// or such lowered scalar.
218-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Cannot unambiguously get sizeof scalar"));
218+
mp_raise_TypeError("Cannot unambiguously get sizeof scalar");
219219
}
220220
syntax_error();
221221
}
@@ -393,7 +393,7 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set
393393

394394
// TODO: Support at least OrderedDict in addition
395395
if (!MP_OBJ_IS_TYPE(self->desc, &mp_type_dict)) {
396-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "struct: no fields"));
396+
mp_raise_TypeError("struct: no fields");
397397
}
398398

399399
mp_obj_t deref = mp_obj_dict_get(self->desc, MP_OBJ_NEW_QSTR(attr));
@@ -526,7 +526,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
526526
} else {
527527
// load / store
528528
if (!MP_OBJ_IS_TYPE(self->desc, &mp_type_tuple)) {
529-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "struct: cannot index"));
529+
mp_raise_TypeError("struct: cannot index");
530530
}
531531

532532
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->desc);

extmod/moduheapq.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
STATIC mp_obj_list_t *get_heap(mp_obj_t heap_in) {
3737
if (!MP_OBJ_IS_TYPE(heap_in, &mp_type_list)) {
38-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "heap must be a list"));
38+
mp_raise_TypeError("heap must be a list");
3939
}
4040
return MP_OBJ_TO_PTR(heap_in);
4141
}

extmod/modujson.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ STATIC mp_obj_t mod_ujson_load(mp_obj_t stream_obj) {
269269
return stack_top;
270270

271271
fail:
272-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "syntax error in JSON"));
272+
mp_raise_ValueError("syntax error in JSON");
273273
}
274274
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_ujson_load_obj, mod_ujson_load);
275275

extmod/modure.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ STATIC mp_obj_t re_split(size_t n_args, const mp_obj_t *args) {
156156
mp_obj_t s = mp_obj_new_str_of_type(str_type, (const byte*)subj.begin, caps[0] - subj.begin);
157157
mp_obj_list_append(retval, s);
158158
if (self->re.sub > 0) {
159-
mp_not_implemented("Splitting with sub-captures");
159+
mp_raise_NotImplementedError("Splitting with sub-captures");
160160
}
161161
subj.begin = caps[1];
162162
if (maxsplit > 0 && --maxsplit == 0) {
@@ -200,7 +200,7 @@ STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args) {
200200
int error = re1_5_compilecode(&o->re, re_str);
201201
if (error != 0) {
202202
error:
203-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Error in regex"));
203+
mp_raise_ValueError("Error in regex");
204204
}
205205
if (flags & FLAG_DEBUG) {
206206
re1_5_dumpcode(&o->re);

extmod/modussl_axtls.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {
153153
// Currently supports only blocking mode
154154
(void)self_in;
155155
if (!mp_obj_is_true(flag_in)) {
156-
mp_not_implemented("");
156+
mp_raise_NotImplementedError("");
157157
}
158158
return mp_const_none;
159159
}

extmod/moduzlib.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ STATIC mp_obj_t decompio_make_new(const mp_obj_type_t *type, size_t n_args, size
9292
dict_opt = uzlib_zlib_parse_header(&o->decomp);
9393
if (dict_opt < 0) {
9494
header_error:
95-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "compression header"));
95+
mp_raise_ValueError("compression header");
9696
}
9797
dict_sz = 1 << dict_opt;
9898
} else {

lib/netutils/netutils.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "py/obj.h"
3333
#include "py/nlr.h"
34+
#include "py/runtime.h"
3435
#include "lib/netutils/netutils.h"
3536

3637
// Takes an array with a raw IPv4 address and returns something like '192.168.0.1'.
@@ -80,7 +81,7 @@ void netutils_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip, netutils_endian
8081
} else if (i > 0 && s < s_top && *s == '.') {
8182
s++;
8283
} else {
83-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid arguments"));
84+
mp_raise_ValueError("invalid arguments");
8485
}
8586
}
8687
}

py/argcheck.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,6 @@ NORETURN void mp_arg_error_terse_mismatch(void) {
142142

143143
#if MICROPY_CPYTHON_COMPAT
144144
NORETURN void mp_arg_error_unimpl_kw(void) {
145-
mp_not_implemented("keyword argument(s) not yet implemented - use normal args instead");
145+
mp_raise_NotImplementedError("keyword argument(s) not yet implemented - use normal args instead");
146146
}
147147
#endif

py/emitnative.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_de
824824
break;
825825
default:
826826
// not handled
827-
mp_not_implemented("conversion to object");
827+
mp_raise_NotImplementedError("conversion to object");
828828
}
829829
}
830830

@@ -2158,7 +2158,7 @@ STATIC void emit_native_call_function(emit_t *emit, mp_uint_t n_positional, mp_u
21582158
break;
21592159
default:
21602160
// this can happen when casting a cast: int(int)
2161-
mp_not_implemented("casting");
2161+
mp_raise_NotImplementedError("casting");
21622162
}
21632163
} else {
21642164
assert(vtype_fun == VTYPE_PYOBJ);
@@ -2232,12 +2232,12 @@ STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
22322232
STATIC void emit_native_yield_value(emit_t *emit) {
22332233
// not supported (for now)
22342234
(void)emit;
2235-
mp_not_implemented("native yield");
2235+
mp_raise_NotImplementedError("native yield");
22362236
}
22372237
STATIC void emit_native_yield_from(emit_t *emit) {
22382238
// not supported (for now)
22392239
(void)emit;
2240-
mp_not_implemented("native yield from");
2240+
mp_raise_NotImplementedError("native yield from");
22412241
}
22422242

22432243
STATIC void emit_native_start_except_handler(emit_t *emit) {

py/lexer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ STATIC void parse_string_literal(mp_lexer_t *lex, bool is_raw) {
341341
// 3MB of text; even gzip-compressed and with minimal structure, it'll take
342342
// roughly half a meg of storage. This form of Unicode escape may be added
343343
// later on, but it's definitely not a priority right now. -- CJA 20140607
344-
mp_not_implemented("unicode name escapes");
344+
mp_raise_NotImplementedError("unicode name escapes");
345345
break;
346346
default:
347347
if (c >= '0' && c <= '7') {

py/objarray.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ STATIC mp_obj_t array_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in)
288288

289289
// Otherwise, can only look for a scalar numeric value in an array
290290
if (MP_OBJ_IS_INT(rhs_in) || mp_obj_is_float(rhs_in)) {
291-
mp_not_implemented("");
291+
mp_raise_NotImplementedError("");
292292
}
293293

294294
return mp_const_false;
@@ -378,7 +378,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
378378
} else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
379379
mp_bound_slice_t slice;
380380
if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
381-
mp_not_implemented("only slices with step=1 (aka None) are supported");
381+
mp_raise_NotImplementedError("only slices with step=1 (aka None) are supported");
382382
}
383383
if (value != MP_OBJ_SENTINEL) {
384384
#if MICROPY_PY_ARRAY_SLICE_ASSIGN
@@ -409,7 +409,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
409409
src_len = bufinfo.len;
410410
src_items = bufinfo.buf;
411411
} else {
412-
mp_not_implemented("array/bytes required on right side");
412+
mp_raise_NotImplementedError("array/bytes required on right side");
413413
}
414414

415415
// TODO: check src/dst compat

py/objlist.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
162162
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
163163
mp_bound_slice_t slice;
164164
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
165-
mp_not_implemented("");
165+
mp_raise_NotImplementedError("");
166166
}
167167

168168
mp_int_t len_adj = slice.start - slice.stop;
@@ -202,7 +202,7 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
202202
mp_obj_get_array(value, &value_len, &value_items);
203203
mp_bound_slice_t slice_out;
204204
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice_out)) {
205-
mp_not_implemented("");
205+
mp_raise_NotImplementedError("");
206206
}
207207
mp_int_t len_adj = value_len - (slice_out.stop - slice_out.start);
208208
//printf("Len adj: %d\n", len_adj);

0 commit comments

Comments
 (0)