10000 py: Add mp_raise_msg_varg helper and use it where appropriate. · jeremyherbert/micropython@ad7213d · GitHub
[go: up one dir, main page]

Skip to content

Commit ad7213d

Browse files
committed
py: Add mp_raise_msg_varg helper and use it where appropriate.
This commit adds mp_raise_msg_varg(type, fmt, ...) as a helper for nlr_raise(mp_obj_new_exception_msg_varg(type, fmt, ...)). It makes the C-level API for raising exceptions more consistent, and reduces code size on most ports: bare-arm: +28 +0.042% minimal x86: +100 +0.067% unix x64: -56 -0.011% unix nanbox: -300 -0.068% stm32: -204 -0.054% PYBV10 cc3200: +0 +0.000% esp8266: -64 -0.010% GENERIC esp32: -104 -0.007% GENERIC nrf: -136 -0.094% pca10040 samd: +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
1 parent 97eca38 commit ad7213d

Some content is hidden

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

53 files changed

+234
-242
lines changed

extmod/machine_mem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
STATIC uintptr_t machine_mem_get_addr(mp_obj_t addr_o, uint align) {
4343
uintptr_t addr = mp_obj_int_get_truncated(addr_o);
4444
if ((addr & (align - 1)) != 0) {
45-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "address %08x is not aligned to %d bytes", addr, align));
45+
mp_raise_msg_varg(&mp_type_ValueError, "address %08x is not aligned to %d bytes", addr, align);
4646
}
4747
return addr;
4848
}

ports/esp32/machine_i2c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_
130130
// Get I2C bus
131131
mp_int_t i2c_id = mp_obj_get_int(args[ARG_id].u_obj);
132132
if (!(I2C_NUM_0 <= i2c_id && i2c_id < I2C_NUM_MAX)) {
133-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "I2C(%d) doesn't exist", i2c_id));
133+
mp_raise_msg_varg(&mp_type_ValueError, "I2C(%d) doesn't exist", i2c_id);
134134
}
135135

136136
// Get static peripheral object

ports/esp32/machine_pwm.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ STATIC void esp32_pwm_init_helper(esp32_pwm_obj_t *self,
163163
.timer_sel = PWTIMER,
164164
};
165165
if (ledc_channel_config(&cfg) != ESP_OK) {
166-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
167-
"PWM not supported on pin %d", self->pin));
166+
mp_raise_msg_varg(&mp_type_ValueError, "PWM not supported on pin %d", self->pin);
168167
}
169168
chan_gpio[channel] = self->pin;
170169
}
@@ -174,8 +173,7 @@ STATIC void esp32_pwm_init_helper(esp32_pwm_obj_t *self,
174173
if (tval != -1) {
175174
if (tval != timer_cfg.freq_hz) {
176175
if (!set_freq(tval)) {
177-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
178-
"Bad frequency %d", tval));
176+
mp_raise_msg_varg(&mp_type_ValueError, "Bad frequency %d", tval);
179177
}
180178
}
181179
}
@@ -249,8 +247,7 @@ STATIC mp_obj_t esp32_pwm_freq(size_t n_args, const mp_obj_t *args) {
249247
// set
250248
int tval = mp_obj_get_int(args[1]);
251249
if (!set_freq(tval)) {
252-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
253-
"Bad frequency %d", tval));
250+
mp_raise_msg_varg(&mp_type_ValueError, "Bad frequency %d", tval);
254251
}
255252
return mp_const_none;
256253
}

ports/esp32/machine_uart.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
251251
// get uart id
252252
mp_int_t uart_num = mp_obj_get_int(args[0]);
253253
if (uart_num < 0 || uart_num >= UART_NUM_MAX) {
254-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%d) does not exist", uart_num));
254+
mp_raise_msg_varg(&mp_type_ValueError, "UART(%d) does not exist", uart_num);
255255
}
256256

257257
// Attempts to use UART0 from Python has resulted in all sorts of fun errors.
258258
// FIXME: UART0 is disabled for now.
259259
if (uart_num == UART_NUM_0) {
260-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%d) is disabled (dedicated to REPL)", uart_num));
260+
mp_raise_msg_varg(&mp_type_ValueError, "UART(%d) is disabled (dedicated to REPL)", uart_num);
261261
}
262262

263263
// Defaults

ports/esp32/modnetwork.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ NORETURN void _esp_exceptions(esp_err_t e) {
9898
case ESP_ERR_TCPIP_ADAPTER_NO_MEM:
9999
mp_raise_OSError(MP_ENOMEM);
100100
default:
101-
nlr_raise(mp_obj_new_exception_msg_varg(
102-
&mp_type_RuntimeError, "Wifi Unknown Error 0x%04x", e
103-
));
101+
mp_raise_msg_varg( &mp_type_RuntimeError, "Wifi Unknown Error 0x%04x", e);
104102
}
105103
}
106104

ports/esp32/modutime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
6666

6767
// localtime generates a tuple of len 8. CPython uses 9, so we accept both.
6868
if (len < 8 || len > 9) {
69-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "mktime needs a tuple of length 8 or 9 (%d given)", len));
69+
mp_raise_msg_varg(&mp_type_TypeError, "mktime needs a tuple of length 8 or 9 (%d given)", len);
7070
}
7171

7272
return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),

ports/esp8266/machine_adc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mp_obj_t machine_adc_make_new(const mp_obj_type_t *type_in, size_t n_args, size_
6464
case 1:
6565
return &machine_adc_vdd3;
6666
default:
67-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ADC(%d) doesn't exist", chn));
67+
mp_raise_msg_varg(&mp_type_ValueError, "ADC(%d) doesn't exist", chn);
6868
}
6969
}
7070

ports/esp8266/machine_pwm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ STATIC void pyb_pwm_init_helper(pyb_pwm_obj_t *self, size_t n_args, const mp_obj
6565

6666
int channel = pwm_add(self->pin->phys_port, self->pin->periph, self->pin->func);
6767
if (channel == -1) {
68-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
69-
"PWM not supported on pin %d", self->pin->phys_port));
68+
mp_raise_msg_varg(&mp_type_ValueError, "PWM not supported on pin %d", self->pin->phys_port);
7069
}
7170

7271
self->channel = channel;

ports/esp8266/machine_uart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size
198198
// get uart id
199199
mp_int_t uart_id = mp_obj_get_int(args[0]);
200200
if (uart_id != 0 && uart_id != 1) {
201-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%d) does not exist", uart_id));
201+
mp_raise_msg_varg(&mp_type_ValueError, "UART(%d) does not exist", uart_id);
202202
}
203203

204204
// create instance

ports/esp8266/modesp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ void *esp_native_code_commit(void *buf, size_t len, void *reloc) {
288288

289289
len = (len + 3) & ~3;
290290
if (esp_native_code_cur + len > esp_native_code_end) {
291-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError,
292-
"memory allocation failed, allocating %u bytes for native code", (uint)len));
291+
mp_raise_msg_varg(&mp_type_MemoryError,
292+
"memory allocation failed, allocating %u bytes for native code", (uint)len);
293293
}
294294

295295
void *dest;

0 commit comments

Comments
 (0)
0